//! News notifications (`app/src/admin.spec.md`, "News notifications"; `docs/v2/proactive.md` 제안 2): //! new items of the background collection announced in a chat channel, at once or once a day, as //! titles and links only (no LLM, nothing written by Jarvis about them). use std::collections::HashMap; use std::path::{Path, PathBuf}; use std::time::Duration; use chrono::{DateTime, Local, Timelike}; use serde::{Deserialize, Serialize}; use serde_json::Value; use crate::settings::Settings; /// Items listed per target in one message; the rest are counted. pub const LISTED: usize = 10; const TITLE_CHARS: usize = 200; pub const TICK: Duration = Duration::from_secs(60); #[derive(Debug, Default, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct State { /// Per target: the `at` of the newest item announced (or seen when its notifications began). #[serde(default)] seen: HashMap, /// Per daily target: the day its digest last went out (YYYY-MM-DD). #[serde(default)] digest_day: HashMap, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct Message { pub target: String, pub count: usize, pub text: String, } fn state_file(data: &Path) -> PathBuf { data.join("config").join("collect-notify.json") } pub fn load(data: &Path) -> State { std::fs::read_to_string(state_file(data)).ok().and_then(|t| serde_json::from_str(&t).ok()).unwrap_or_default() } pub fn save(data: &Path, s: &State) -> std::io::Result<()> { crate::datadir::write_atomic(&state_file(data), serde_json::to_string_pretty(s).unwrap().as_bytes()) } fn line(item: &Value) -> String { let title: String = item["title"].as_str().unwrap_or("").split_whitespace().collect::>().join(" ") .chars().take(TITLE_CHARS).collect(); let url = item["url"].as_str().unwrap_or(""); let title = if title.is_empty() { "(제목 없음)".to_owned() } else { title }; // keeps Discord from showing a preview for every link. if url.starts_with("http://") || url.starts_with("https://") { format!("- {title} <{url}>") } else { format!("- {title}") } } fn message(target: &str, new: &[Value], daily: bool) -> Message { let head = if daily { format!("수집 소식 하루 요약: {target} 새 글 {}건", new.len()) } else { format!("수집 소식: {target} 새 글 {}건", new.len()) }; let mut lines = vec![head]; lines.extend(new.iter().take(LISTED).map(line)); if new.len() > LISTED { lines.push(format!("외 {}건", new.len() - LISTED)); } Message { target: target.to_owned(), count: new.len(), text: lines.join("\n") } } /// The messages due at `now`, updating `state`. Nothing without the feature or a channel. A target /// whose notifications just began only marks what it has as seen (its first digest still comes /// that day); `now` targets announce what is new since then; `daily` ones once a day at /// `digest_hour`, what came since their last digest. pub fn due(data: &Path, s: &Settings, now: DateTime, state: &mut State) -> Vec { if !s.features.collect || s.collect.notify_channel.is_none() { return vec![]; } let today = now.date_naive().to_string(); let mut out = vec![]; for t in s.collect.targets.iter().filter(|t| t.enabled && t.notify != "off") { let items = crate::collect::results(data, Some(&t.name), 500).unwrap_or_default(); let newest = items.first().and_then(|i| i["at"].as_str()).map(str::to_owned); let Some(last) = state.seen.get(&t.name).cloned() else { // Only what is there now is marked seen; that day's digest still goes out at its hour. state.seen.insert(t.name.clone(), newest.unwrap_or_else(|| now.to_rfc3339_opts(chrono::SecondsFormat::Secs, false))); continue; }; if t.notify == "daily" && (now.hour() != s.collect.digest_hour || state.digest_day.get(&t.name) == Some(&today)) { continue; } // `at` is the local time with its offset in one format, so text order is time order. let new: Vec = items.into_iter().filter(|i| i["at"].as_str().is_some_and(|a| a > last.as_str())).collect(); if t.notify == "daily" { state.digest_day.insert(t.name.clone(), today.clone()); } if new.is_empty() { continue; } if let Some(n) = new[0]["at"].as_str() { state.seen.insert(t.name.clone(), n.to_owned()); } out.push(message(&t.name, &new, t.notify == "daily")); } out } /// One round: the messages due now, or None when the updated state could not be saved (they would /// then be sent again every minute; the next round tries again). pub fn prepare(data: &Path, s: &Settings, now: DateTime) -> Option> { let mut state = load(data); let msgs = due(data, s, now, &mut state); match save(data, &state) { Ok(()) => Some(msgs), Err(e) => { crate::warn_log!("conversation", "Warning: could not save the news notification state, nothing sent: {e}"); None } } } /// Announce due news every minute until aborted; each post is recorded in the activity log. pub async fn run(data: PathBuf, settings: Settings, d: std::sync::Arc, activity: std::sync::Arc) { loop { let msgs = prepare(&data, &settings, Local::now()).unwrap_or_default(); if let Some(channel) = settings.collect.notify_channel { for m in msgs { let posted = d.post(channel, &m.text).await; crate::info_log!("conversation", "news {} items={} posted={}", m.target, m.count, posted.is_ok()); activity.record("collect.notify", &format!("수집 {} → 채널 {channel}", m.target), None, posted.is_ok(), posted.err().map(|_| "채팅 채널에 보내지 못함".to_owned())); } } tokio::time::sleep(TICK).await; } } #[cfg(test)] mod tests { use super::*; use chrono::TimeZone; use serde_json::json; fn settings(notify: &str) -> Settings { crate::settings::parse_settings(&json!({ "chatChannels": [{"id": "123456789012345678"}], "collect": {"notifyChannel": "123456789012345678", "digestHour": 9, "targets": [ {"name": "뉴스", "kind": "rss", "value": "https://feed.example/rss", "notify": notify}]}})).unwrap() } fn write(data: &Path, day: &str, items: &[(&str, &str)]) { let dir = data.join("collect").join("뉴스"); std::fs::create_dir_all(&dir).unwrap(); let mut text = std::fs::read_to_string(dir.join(format!("{day}.jsonl"))).unwrap_or_default(); for (at, title) in items { text.push_str(&json!({"at": at, "title": title, "url": format!("https://e.example/{title}")}).to_string()); text.push('\n'); } std::fs::write(dir.join(format!("{day}.jsonl")), text).unwrap(); } fn at(h: u32, m: u32) -> DateTime { Local.with_ymd_and_hms(2026, 11, 14, h, m, 0).unwrap() } #[test] fn at_once_announces_only_what_is_new_since_notifications_began() { let data = crate::datadir::temp_dir("news-now"); let s = settings("now"); write(&data, "2026-11-14", &[("2026-11-14T08:00:00+09:00", "old")]); let mut st = State::default(); assert!(due(&data, &s, at(8, 30), &mut st).is_empty(), "what was there before is not announced"); write(&data, "2026-11-14", &[("2026-11-14T08:40:00+09:00", "new one")]); let m = due(&data, &s, at(8, 41), &mut st); assert_eq!(m.len(), 1); assert_eq!(m[0].count, 1); assert!(m[0].text.contains("new one ") && !m[0].text.contains("old"), "{}", m[0].text); assert!(due(&data, &s, at(8, 42), &mut st).is_empty(), "each item once"); } #[test] fn a_daily_digest_goes_out_once_at_its_hour_with_everything_since_the_last() { let data = crate::datadir::temp_dir("news-daily"); let s = settings("daily"); let mut st = State::default(); // Turned on at 7: what is there is seen; what comes later goes in that same day's digest. assert!(due(&data, &s, at(7, 0), &mut st).is_empty()); let many: Vec<(String, String)> = (0..12).map(|i| (format!("2026-11-14T08:{i:02}:00+09:00"), format!("t{i}"))).collect(); write(&data, "2026-11-14", &many.iter().map(|(a, t)| (a.as_str(), t.as_str())).collect::>()); assert!(due(&data, &s, at(8, 59), &mut st).is_empty(), "not before its hour"); let m = due(&data, &s, at(9, 0), &mut st); assert_eq!(m.len(), 1); assert_eq!(m[0].count, 12); assert!(m[0].text.contains("외 2건"), "{}", m[0].text); assert!(due(&data, &s, at(9, 30), &mut st).is_empty(), "once a day"); // The next day only what came after. write(&data, "2026-11-15", &[("2026-11-15T06:00:00+09:00", "next day")]); let next = Local.with_ymd_and_hms(2026, 11, 15, 9, 0, 0).unwrap(); let m = due(&data, &s, next, &mut st); assert_eq!(m.len(), 1); assert_eq!(m[0].count, 1); } #[cfg(unix)] #[test] fn a_round_whose_state_cannot_be_saved_sends_nothing() { use std::os::unix::fs::PermissionsExt; let data = crate::datadir::temp_dir("news-readonly"); let s = settings("now"); write(&data, "2026-11-14", &[("2026-11-14T08:00:00+09:00", "old")]); assert_eq!(prepare(&data, &s, at(8, 0)), Some(vec![])); write(&data, "2026-11-14", &[("2026-11-14T08:30:00+09:00", "new")]); let config = data.join("config"); std::fs::set_permissions(&config, std::fs::Permissions::from_mode(0o555)).unwrap(); let blocked = prepare(&data, &s, at(8, 31)); std::fs::set_permissions(&config, std::fs::Permissions::from_mode(0o755)).unwrap(); assert_eq!(blocked, None, "not sent: it would be sent again every minute"); assert_eq!(prepare(&data, &s, at(8, 32)).map(|m| m.len()), Some(1), "sent once the state can be saved"); } #[test] fn nothing_is_announced_without_a_channel_the_feature_or_with_notify_off() { let data = crate::datadir::temp_dir("news-off"); write(&data, "2026-11-14", &[("2026-11-14T08:00:00+09:00", "x")]); let mut off = settings("now"); off.collect.notify_channel = None; let mut nofeature = settings("now"); nofeature.features.collect = false; for s in [off, nofeature, settings("off")] { let mut st = State::default(); due(&data, &s, at(8, 0), &mut st); write(&data, "2026-11-14", &[("2026-11-14T08:30:00+09:00", "y")]); assert!(due(&data, &s, at(8, 31), &mut st).is_empty()); } } }