//! Done for you — what Cotton did by itself, newest first, each with the one //! action that puts it right. Home draws it under "Asked of you". //! //! The design's other half (Cotton note d0e27772): everything that is not a //! question Cotton does by itself, SHOWS what it did, and lets the owner //! correct it. Four kinds of act show here — a thought the router filed as a //! record, an entry Cotton remembered, a document it could not read, and a //! pass that filled people's cities from their time zones — and //! nothing on this list waits on the owner: there is no count, no badge and no //! "to review". A row is a receipt with an undo, never a question. //! //! The action is the kind's, said once here so Home and a test read the same //! answer: a filing is UNDONE (the thought is kept as a thought and the //! untouched record goes), a remembered entry is UNDONE (archived — the owner //! drops it the moment he notices it is wrong), a city fill is UNDONE (every //! city of that pass that still stands is cleared), and a document that could not //! be read is CHANGED on its own page, since there is nothing Cotton did to it //! that could be taken back. //! //! Pure: the caller hands in what it read, `now` comes through the seam, and //! the list is only ever the recent past — a receipt from last month is not //! something anyone looks for on Home. use super::shell_lines::{number, resolve, LineRow}; use crate::constants::RouteTargetKind; use cotton_store::protocol::js::parse_date_ms; /// How far back Home looks. A receipt is for noticing; after two weeks the /// record's own screen is where it is found. pub const DONE_FOR_YOU_WINDOW_DAYS: i64 = 14; /// How many rows Home shows. Enough to notice a pattern, few enough that the /// list stays a glance. pub const DONE_FOR_YOU_LIMIT: usize = 8; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum DoneKind { /// A thought the router filed as a todo, event, note or other record. Filed, /// A memory entry an assistant wrote. Remembered, /// A document whose reading failed. CouldNotRead, /// One pass of `people.fillCities`: the people whose city Cotton filled /// from their time zone at one instant, and still holding that city. CityFilled, /// One day of the junk classifier: the senders it filed into Junk that /// day and still stands behind (the owner has not kept them since). JunkFiled, /// This device was removed from a shared database: its copy went to the /// Bin on this device. Nothing to take back — only a new admission brings /// the database back — so the row opens the Bin (architect, 16 Oct 2026). RemovedFrom, } impl DoneKind { pub const fn as_str(self) -> &'static str { match self { DoneKind::Filed => "filed", DoneKind::Remembered => "remembered", DoneKind::CouldNotRead => "couldNotRead", DoneKind::CityFilled => "cityFilled", DoneKind::JunkFiled => "junkFiled", DoneKind::RemovedFrom => "removedFrom", } } } /// The one action a row carries. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum DoneAction { /// Take the act back — `keep_as_thought`, `memory_archive` or /// `people.unfillCities`. Undo, /// Open the record to put it right by hand. Change, /// Nothing to put right: the row only opens where it went. Open, } impl DoneAction { pub const fn as_str(self) -> &'static str { match self { DoneAction::Undo => "undo", DoneAction::Change => "change", DoneAction::Open => "open", } } } /// One act, as the caller read it. #[derive(Debug, Clone, PartialEq, Eq)] pub struct DoneAct { pub kind: DoneKind, /// The thought, memory entry or document the row is about — for a city /// fill, the pass's instant, which is what its undo names. pub id: String, /// The owner's words where there are any — the thought's text, the /// entry's sentence, the document's title. pub text: String, /// For a filing, the route target kind it became (`todo`, `event`, …). pub filed_as: Option, /// When it happened. pub at: String, /// How many records the act touched — a city fill's people; 1 otherwise. pub count: usize, /// The one record a row opens when that is not `id` — a city fill of one /// person opens that person. pub opens: Option, /// Who did it, when it was a person and not Cotton — who removed this /// device from a database. pub by: Option, /// What a city fill or a junk day names, in order: each person with the /// city filled in, each sender with none. Said by [`done_text`]. pub listed: Vec, } /// One thing a row names: a person and the city Cotton filled in, or a /// sender. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Listed { pub name: String, pub city: Option, } /// The line set Home's words live in. pub const SET: &str = "home"; /// One person a city fill still stands for, as the caller read them. #[derive(Debug, Clone, PartialEq, Eq)] pub struct CityFill { pub person_id: String, pub name: String, pub city: String, /// The pass's instant (`cotton_people.city_filled_at`). pub at: String, } /// How many people a city fill's row names before it counts the rest. pub const CITY_FILL_NAMED: usize = 2; /// The city fills as acts: one per pass, its people named in the order given /// ("Maren Holt — Oslo, Cassius Brandt — London and 3 more"). A pass of one /// person opens that person; a larger one opens People. pub fn city_fill_acts(fills: &[CityFill]) -> Vec { let mut passes: Vec<(&str, Vec<&CityFill>)> = Vec::new(); for fill in fills { match passes.iter_mut().find(|(at, _)| *at == fill.at) { Some((_, people)) => people.push(fill), None => passes.push((&fill.at, vec![fill])), } } passes .into_iter() .map(|(at, people)| { DoneAct { kind: DoneKind::CityFilled, id: at.to_string(), text: String::new(), filed_as: None, at: at.to_string(), count: people.len(), opens: (people.len() == 1).then(|| people[0].person_id.clone()), by: None, listed: people .iter() .map(|fill| Listed { name: fill.name.trim().to_string(), city: Some(fill.city.clone()) }) .collect(), } }) .collect() } /// How many senders a junk day's row names before it counts the rest. pub const JUNK_DAY_NAMED: usize = 2; /// One day of the junk classifier as an act: `day` is its date (the row's id), /// `at` its newest decision, `addresses` the senders newest first. pub fn junk_day_act(day: &str, at: &str, addresses: &[String]) -> DoneAct { DoneAct { kind: DoneKind::JunkFiled, id: day.to_string(), text: String::new(), filed_as: None, at: at.to_string(), count: addresses.len(), opens: None, by: None, listed: addresses.iter().map(|address| Listed { name: address.clone(), city: None }).collect(), } } pub const fn done_action(kind: DoneKind) -> DoneAction { match kind { DoneKind::Filed | DoneKind::Remembered | DoneKind::CityFilled => DoneAction::Undo, // Taken back one sender at a time, by Keep on the Senders surface — // a day's filing has no single undo. DoneKind::CouldNotRead | DoneKind::JunkFiled => DoneAction::Change, DoneKind::RemovedFrom => DoneAction::Open, } } /// A removal of this device from the database `database` (its id) named /// `name`, at `at`, by `by` when known — Home's receipt for it. pub fn removed_from_act(database: &str, name: &str, by: Option<&str>, at: &str) -> DoneAct { DoneAct { kind: DoneKind::RemovedFrom, id: database.to_string(), text: name.to_string(), filed_as: None, at: at.to_string(), count: 1, opens: None, by: by.map(str::to_string), listed: Vec::new(), } } /// The acts inside the window, newest first, at most `limit`. /// /// Parsed, never lexical — an offset-bearing instant sorts wrong as a string. /// An act whose instant does not parse is dropped rather than guessed at: /// placing it would be inventing when it happened. Ties break on kind then id, /// so two devices draw the same list. pub fn done_rows(acts: &[DoneAct], now: &str, limit: usize) -> Vec { let Some(now_ms) = parse_date_ms(now) else { return Vec::new(); }; let since = now_ms - DONE_FOR_YOU_WINDOW_DAYS * 24 * 60 * 60 * 1000; let mut dated: Vec<(i64, &DoneAct)> = acts .iter() .filter_map(|act| parse_date_ms(&act.at).map(|at| (at, act))) .filter(|(at, _)| *at >= since) .collect(); dated.sort_by(|(a_at, a), (b_at, b)| { b_at.cmp(a_at) .then_with(|| a.kind.as_str().cmp(b.kind.as_str())) .then_with(|| a.id.cmp(&b.id)) }); dated .into_iter() .take(limit) .map(|(_, act)| act.clone()) .collect() } /// Where a row opens. pub fn done_path(act: &DoneAct) -> String { match act.kind { DoneKind::Filed => format!("/thoughts/thought/{}", act.id), DoneKind::Remembered => format!("/memory/{}", act.id), DoneKind::CouldNotRead => format!("/documents/document/{}", act.id), DoneKind::CityFilled => match &act.opens { Some(person) => format!("/people/person/{person}"), None => "/people".to_string(), }, DoneKind::JunkFiled => "/email".to_string(), DoneKind::RemovedFrom => "/bin".to_string(), } } /// What Cotton did, in a few words — the line above the owner's own text — /// in the database's words (set `home`, `done.*`). pub fn done_label(act: &DoneAct, rows: &[LineRow]) -> String { let say = |line: &str, fills: &[(&str, &str)]| resolve(SET, line, rows, fills).unwrap_or_default(); let n = number(act.count as i64); match act.kind { DoneKind::Filed => { // The route target kinds, each its own line; any other is a record. let kind = act.filed_as.as_deref().and_then(RouteTargetKind::parse).map_or("record", |kind| kind.as_str()); say(&format!("done.filed.{kind}"), &[]) } DoneKind::Remembered => say("done.remembered", &[]), DoneKind::CouldNotRead => say("done.could_not_read", &[]), DoneKind::CityFilled => say("done.city_filled", &[("n", &n)]), DoneKind::JunkFiled => say("done.junk_filed", &[("n", &n)]), DoneKind::RemovedFrom => { let name = act.text.trim(); let unnamed = say("done.removed_from.a_shared_database", &[]); let name = if name.is_empty() { unnamed.as_str() } else { name }; match act.by.as_deref().map(str::trim).filter(|by| !by.is_empty()) { Some(by) => say("done.removed_from.by", &[("name", name), ("by", by)]), None => say("done.removed_from.alone", &[("name", name)]), } } } } /// The row's own text: the owner's words where the act carries them, else /// what it names — the first few (a city fill names [`CITY_FILL_NAMED`], a /// junk day [`JUNK_DAY_NAMED`]) and how many more. pub fn done_text(act: &DoneAct, rows: &[LineRow]) -> String { if act.listed.is_empty() { return act.text.clone(); } let say = |line: &str, fills: &[(&str, &str)]| resolve(SET, line, rows, fills).unwrap_or_default(); let shown = match act.kind { DoneKind::JunkFiled => JUNK_DAY_NAMED, _ => CITY_FILL_NAMED, }; let named: Vec = act .listed .iter() .take(shown) .map(|listed| match &listed.city { Some(city) => say("done.person_city", &[("name", &listed.name), ("city", city)]), None => listed.name.clone(), }) .collect(); let list = named.join(&say("done.separator", &[])); let rest = act.listed.len().saturating_sub(shown); if rest == 0 { list } else { say("done.and_more", &[("list", &list), ("n", &number(rest as i64))]) } } #[cfg(test)] #[path = "done_for_you_tests.rs"] mod tests;