//! A chat's disappearing timer (chat-search, 21 Nov 2026): its choices, its//! words, and when a message sent under it goes.//!//! RULE: a timer belongs to the chat, and both sides see it. Either person//! sets it; the line that says so lands on both sides (and the person's other//! phones) and is the chat's timer from then on. A message written while it is//! on carries it, sealed, and disappears from every phone that holds it that//! long after it was written, by the writer's clock. The words never promise//! more than that: disappearing cannot stop a screenshot, a photo of the//! screen, or an older app that does not know the timer.usecrate::rules::shell_lines::LineSet;usesuper::list::say;usesuper::shapes::{TimerCard, TimerChoice};/// Off, a day, a week, ninety days — the only lengths a timer takes.pubconst CHOICES: [u32; 4] = [0, 86_400, 604_800, 7_776_000];fn length_key(seconds: u32) -> &'static str {match seconds {86_400 => "app.timer.h24",604_800 => "app.timer.d7",7_776_000 => "app.timer.d90", _ => "app.timer.off", }}/// A length in words: "24 hours", "7 days", "Off".pubfn length(lines: &LineSet, seconds: u32) -> String { say(lines, length_key(seconds), &[])}/// When a message written at `written_ms` under `seconds` goes, ms; none when the timer is off.pubfn expires_at(written_ms: i64, seconds: u32) -> Option<i64> { (seconds > 0).then(|| written_ms + i64::from(seconds) * 1000)}/// The line a timer change leaves in the chat: "You turned on …" for the/// person who set it, "{name} turned on …" for everyone else.pubfn event_line(lines: &LineSet, seconds: u32, by: Option<&str>) -> String {let length = length(lines, seconds);match (seconds > 0, by) { (true, None) => say(lines, "app.timer.you_on", &[("length", &length)]), (true, Some(name)) => say(lines, "app.timer.they_on", &[("name", name), ("length", &length)]), (false, None) => say(lines, "app.timer.you_off", &[]), (false, Some(name)) => say(lines, "app.timer.they_off", &[("name", name)]), }}/// What a conversation offers about its timer: where it stands, the honest/// note, and the choices (only where the person can write in the chat).pubfn card(lines: &LineSet, seconds: u32, can_set: bool) -> TimerCard {let length = length(lines, seconds);TimerCard { seconds, title: say(lines, "app.timer.title", &[]), label: if seconds > 0 { Some(say(lines, "app.timer.on", &[("length", &length)])) } else { None }, note: if seconds > 0 { say(lines, "app.timer.note", &[("length", &length)]) } else { say(lines, "app.timer.note_off", &[]) }, choices: if can_set { CHOICES.iter().map(|&s| TimerChoice { seconds: s, label: self::length(lines, s), on: s == seconds }).collect() } else { Vec::new() }, }}/// The same card on a chat that came from WhatsApp (FEEDBACK-2 B.2, 23 Nov/// 2026): the timer erases the person's own copy in Cotton only, so its note/// says exactly that, on or off. Nothing is sent to WhatsApp or to the other/// person, so the person can set it wherever they read the chat.pubfn own_card(lines: &LineSet, seconds: u32) -> TimerCard {TimerCard { note: say(lines, "app.timer.own", &[]), ..card(lines, seconds, true) }}/// RULE: on a WhatsApp chat a message goes by the timer standing when it was/// sent: the newest timer line at or before its `sent_at`, never one turned on/// after it. `lines` are the chat's timer lines, `(sent_at ms, seconds)`/// oldest first. Answers the spans of `sent_at` due at `now_ms`, each/// `[from, until)` in ms: sent under a timer that is on, and that long ago.////// The newest line's span has no end this copy holds: a later "off" line may not have come yet (a pull still going,/// a row parked). It erases nothing unless `open_end` says how far this copy knows: the moment just after the newest/// message it holds in the chat, given only once the copy is caught up with nothing of the chat parked (a chat is/// imported in order, so an "off" line sent before that message would be here). Never to `now` on absence (ABSENCE 2,/// 26 Nov 2026).pubfn due_spans(lines: &[(i64, u32)], now_ms: i64, open_end: Option<i64>) -> Vec<(i64, i64)> {letmut spans = Vec::new();for (i, &(from, seconds)) in lines.iter().enumerate() {letSome(gone) = expires_at(from, seconds) else { continue };if gone > now_ms {continue; }// Sent at or before `now - seconds` has passed its age.let past = now_ms - i64::from(seconds) * 1000 + 1;let until = match lines.get(i + 1) {Some(&(next, _)) => next.min(past),None => match open_end {Some(end) => end.min(past),None => continue, }, };if from < until { spans.push((from, until)); } } spans}/// RULE: when a message sent at `sent_ms` on a WhatsApp chat goes: by the newest timer line at or before it (never/// one turned on after it); none when that line is off or there is none.pubfn due_at(lines: &[(i64, u32)], sent_ms: i64) -> Option<i64> {let &(_, seconds) = lines.iter().rev().find(|(from, _)| *from <= sent_ms)?; expires_at(sent_ms, seconds)}/// RULE: the spans of `sent_at` whose messages are still held but will go, each `[from, until)` with the seconds/// they go under: sent under a timer that is on, less than that long before `now_ms`. The earliest message of a span/// is the next of it to go, so a phone asks only for that one (`next_due`).pubfn waiting_spans(lines: &[(i64, u32)], now_ms: i64) -> Vec<(i64, i64, u32)> {letmut spans = Vec::new();for (i, &(from, seconds)) in lines.iter().enumerate() {if seconds == 0 {continue; }let from = from.max(now_ms - i64::from(seconds) * 1000 + 1);let until = lines.get(i + 1).map_or(i64::MAX, |&(next, _)| next);if from < until { spans.push((from, until, seconds)); } } spans}/// RULE: the next moment a message of `sent` goes, after `now_ms` (the moment the phone asks the chat again, so a/// message never stays on screen past its time); none when nothing is set to go.pubfn next_due(lines: &[(i64, u32)], sent: &[i64], now_ms: i64) -> Option<i64> { sent.iter().filter_map(|&at| due_at(lines, at)).filter(|&due| due > now_ms).min()}/// The wait the phone keeps until `due`, in ms from `now_ms` (at least a tick, so a moment just passed is asked at once).pubfn wait_until(due: i64, now_ms: i64) -> i64 { (due - now_ms).max(1)}#[cfg(test)]#[path = "timer_tests.rs"]mod tests;