fix(app): news digests come the day they are turned on, and nothing is posted when the state cannot be saved

Turning a daily target on only marks its current items as seen, so that
day's digest still goes out at its hour. Each round saves its state before
posting and posts nothing when the save fails, so the same items are not
posted again every minute.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-14 17:26:48 +09:00
parent 324fd2ea0a
commit 7e51ebd0ba
2 changed files with 51 additions and 18 deletions

View File

@@ -207,10 +207,12 @@ channel, titles and links only (no LLM, nothing Jarvis writes about them).
announced) and `collect.digestHour` (0-23, default 9, local time). Nothing is announced with
`features.collect` off.
- While the services run, every minute: a target whose notifications just began only marks what it
has as seen (nothing old is announced); a `now` target posts what is new since its last post; a
`daily` target posts once a day in the digest hour what came since its last digest (nothing when
nothing came). The state is `config/collect-notify.json` (per target the newest item announced and
the day of its last digest).
has as seen (nothing old is announced; its first digest still comes that day); a `now` target
posts what is new since its last post; a `daily` target posts once a day in the digest hour what
came since its last digest (nothing when nothing came). The state is `config/collect-notify.json`
(per target the newest item announced and the day of its last digest), saved before anything is
posted: a round whose state cannot be saved posts nothing (else it would post the same items every
minute) and the next round tries again.
- A post is `수집 소식: <target> 새 글 N건` (`수집 소식 하루 요약: ...` for the digest), then at most 10
items as `- <title> <link>` (titles at most 200 characters; the link in `<>` so Discord shows no
preview), then `외 N건`. Posts ping nobody. Each is recorded in the activity (`collect.notify`,

View File

@@ -68,8 +68,9 @@ fn message(target: &str, new: &[Value], daily: bool) -> Message {
}
/// 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; `now` targets announce what is
/// new since then; `daily` ones once a day at `digest_hour`, what came since their last digest.
/// 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<Local>, state: &mut State) -> Vec<Message> {
if !s.features.collect || s.collect.notify_channel.is_none() {
return vec![];
@@ -80,10 +81,8 @@ pub fn due(data: &Path, s: &Settings, now: DateTime<Local>, state: &mut State) -
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)));
if t.notify == "daily" {
state.digest_day.insert(t.name.clone(), today.clone());
}
continue;
};
if t.notify == "daily" && (now.hour() != s.collect.digest_hour || state.digest_day.get(&t.name) == Some(&today)) {
@@ -105,15 +104,25 @@ pub fn due(data: &Path, s: &Settings, now: DateTime<Local>, state: &mut State) -
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<Local>) -> Option<Vec<Message>> {
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<dyn crate::schedule::Deliver>,
activity: std::sync::Arc<crate::activity::Activity>) {
loop {
let mut state = load(&data);
let msgs = due(&data, &settings, Local::now(), &mut state);
if let Err(e) = save(&data, &state) {
crate::warn_log!("conversation", "Warning: could not save the news notification state: {e}");
}
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;
@@ -174,17 +183,39 @@ mod tests {
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::<Vec<_>>());
assert!(due(&data, &s, at(8, 59), &mut st).is_empty(), "not before its hour");
// The first day's digest was marked when notifications began; the next day's goes out.
let next = Local.with_ymd_and_hms(2026, 11, 15, 9, 0, 0).unwrap();
let m = due(&data, &s, next, &mut st);
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, next + chrono::Duration::minutes(30), &mut st).is_empty(), "once a day");
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]