//! A sender's page (SENDERS.md "DECIDED: Senders 3", "A sender's page"; owner, 24 Nov 2026): reached from the Senders//! tab and from any mail by its sender.//!//! `chat.sender.page { key, after? }`: `key` is a mail address, a domain (`@domain`), a number or an SMS short code's//! name, as the Senders tab names it, with the senders merged into it by hand. Answers://! - `name`, `handle`, `line` (how much, whether he replied, the channels), `group`;//! - `acts`: what can be done to the whole sender (`chat.sender.ask` / `chat.sender.act`, as the tab's hold);//! - `chats`: their SMS and WhatsApp chats, each `{ thread, label }`, opened as the chat itself (its own select);//! - `mail`: `{ rows, next, select }`, their mail newest first, 60 a page, each `{ email, thread, subject, line,//! when, unread }`; `select` is the select mode's words and acts, each run on the chosen mails as//! `chat.mail.act { emails, act }`.usesuper::*;use cotton_core::messages::app::senders::{selfas senders_rule, KeyKind};use cotton_core::rules::mail_predicate;/// Mails a page of the sender's page holds.const PAGE: usize = 60;pub(super) fn page(client: &ClientController, request: &Value) -> Result<Value, MethodError> {let key = cotton_chat::text(request, "key")?.trim().to_lowercase();// The (sent_at, id) cursor of the last mail the page drew, as `next` handed it; none: the newest first.let after: Option<(String, String)> = request.get("after").and_then(Value::as_str).and_then(|a| a.split_once('|')).map(|(at, id)| (at.to_string(), id.to_string()));let lines = list::lines();let clock = Clock::of(client);let now = cotton_chat::now_ms(client);// His mailbox's own count of their mail, asked of the Mac when it has none fresh, for Delete everything's ask// (DELETE FAST).crate::phone::chat_app::senders::wide::want_counts(client, &key, false)?;crate::phone::chat_app::senders::wide::want_counts(client, &key, true)?;let merges = crate::phone::chat_app::senders::merges(client);let members: Vec<String> = merges.iter().filter(|(_, into)| *into == key).map(|(m, _)| m.clone()).collect();// A sender merged into another opens as that one.ifletSome((_, into)) = merges.iter().find(|(m, _)| *m == key) {return page(client, &json!({ "key": into, "after": request.get("after") })); } read(client, "chat.sender.page", |ctx| {let blocked = crate::phone::chat_app::senders::Blocked::read(ctx)?;let states = Default::default();let only: Vec<String> = std::iter::once(key.clone()).chain(members.iter().cloned()).collect();let (all, groups, merged) = crate::phone::chat_app::senders::tab_in(ctx, &blocked, &states, &merges, Some(&only))?;let domain = key.strip_prefix('@').map(str::to_string);// The sender as the tab counts them: one sender, or every sender at the domain.let mine: Vec<&senders_rule::Sender> = all.iter().filter(|s| s.key.key.to_lowercase() == key || domain.as_ref().is_some_and(|d| senders_rule::domain_of(&s.key).as_ref() == Some(d))).collect();let first = mine.first().copied();let states = crate::phone::chat_app::senders::wide::mail_states(ctx)?;// Gone from Comms, and the Mac done too: still the name he knew them by, from their newest mail act.let remembered = states.get(&key).and_then(|act| act.name.clone());let name = match (&domain, first) { (Some(d), _) => d.clone(), (None, Some(s)) => senders_rule::name_of(s), (None, None) => remembered.clone().unwrap_or_else(|| key.clone()), };let sent: u32 = mine.iter().map(|s| s.sent).sum();let replied = mine.iter().any(|s| s.replied);letmut channels: Vec<String> = Vec::new();for s in &mine {for c in &s.channels {if !channels.contains(c) { channels.push(c.clone()); } } }// Nothing of theirs left here (after Delete everything, while the Mac moves their mail): said so, under the name// he knew them by, never a raw address over "0 messages" (ACTS TRUTH, owner's Comms 31, 24 Nov 2026).let line = if sent == 0 { say(&lines, "app.senders.page.nothing_left", &[]) } else { [ senders_rule::sent_line(&lines, sent), say(&lines, if replied { "app.senders.replied" } else { "app.senders.never" }, &[]), senders_rule::channels_line(&lines, &channels), ] .into_iter() .filter(|l| !l.is_empty()) .collect::<Vec<_>>() .join(" · ") };// The Mac's half of their newest mail act: waiting, done, or refused with Try again (`chat.sender.act`// `retry`), as their row in the tab says it.let state_keys: Vec<String> = match &domain {Some(d) => states.keys().filter(|a| a.ends_with(&format!("@{d}"))).cloned().collect(),None => only.clone(), };let source = crate::phone::chat_app::senders::wide::newest_of(&states, state_keys).map(|act| crate::phone::chat_app::senders::source_json(&lines, act, now));let handle = match (&domain, first) { (Some(_), _) => Some(say(&lines, "app.senders.addresses", &[("n", &mine.len().to_string())])), (None, Some(s)) if senders_rule::name_of(s) != s.key.key => Some(s.key.key.clone()), (None, None) if remembered.is_some() => Some(key.clone()), _ => None, };let group = groups.get(&key).copied().flatten().map(|g| g.as_str());// The whole sender's acts, as the tab's hold has them.letmut acts: Vec<Value> = senders_rule::sender_acts(&lines, channels.iter().any(|c| c == "mail"), domain.is_some()).into_iter().map(|a| json!({ "id": a.id, "label": a.label })).collect();// Nothing of theirs left: only Block stands (the rest would only say there is nothing), beside Try again.if sent == 0 { acts.retain(|a| a["id"] == "block"); } elseif domain.is_none() { acts.push(json!({ "id": "merge", "label": say(&lines, "app.senders.hold.merge", &[]) }));if merged.get(&key).is_some_and(|m| !m.is_empty()) { acts.push(json!({ "id": "part", "label": say(&lines, "app.senders.hold.part", &[]) })); } }// Their SMS and WhatsApp chats: the chat itself is their page there.let keys: Vec<String> = std::iter::once(key.clone()).chain(members.iter().cloned()).collect();let chats: Vec<Value> = crate::phone::chat_app::senders::chat_shares(ctx)? .into_iter() .filter(|c| keys.iter().any(|k| c.key.key.eq_ignore_ascii_case(k)) && c.key.kind != KeyKind::Address) .map(|c| json!({ "thread": c.thread, "channel": c.channel, "label": say(&lines, &format!("app.senders.page.open_{}", c.channel), &[]) })) .collect();// Their mail, newest first.let (clause, mut params) = crate::phone::chat_app::senders::mail_where(Some(&only));letmut sql_params = vec![ws(ctx)]; sql_params.append(&mut params);// A window the screen grows by the (sent_at, id) cursor, never a page by number.let past = match &after {Some((at, id)) => { sql_params.push(text(at.clone())); sql_params.push(text(id.clone()));" AND (coalesce(sent_at, ''), id) < (?, ?)" }None => "", };let found = rows( ctx, &format!("SELECT id, thread_id, subject, body, snippet, sent_at, {unread_col} AS unread FROM {EMAILS} WHERE workspace_id = ? AND {live_mail} AND archived_at IS NULL AND {clause}{past} ORDER BY coalesce(sent_at, '') DESC, id DESC LIMIT {}", PAGE + 1, live_mail = mail_predicate::live(""), unread_col = mail_predicate::unread("")), &sql_params, )?;let more = found.len() > PAGE;let mail: Vec<Value> = found .iter() .take(PAGE) .map(|m| { json!({"email": s(m, "id"),"thread": s(m, "thread_id"),"subject": subject_or(&lines, s(m, "subject")),"line": line_of(m),"when": clock.recent(&s(m, "sent_at").unwrap_or_default()),"unread": truthy(m, "unread"), }) }) .collect();let select = json!({"label": say(&lines, "app.senders.page.select", &[]),"all": say(&lines, "app.senders.page.select_all", &[]),"done": say(&lines, "app.senders.page.done", &[]),"chosen": say(&lines, "app.senders.page.chosen", &[]),"acts": [ { "id": "read", "label": say(&lines, "app.mail.acts.read", &[]) }, { "id": "unread", "label": say(&lines, "app.mail.acts.unread", &[]) }, { "id": "archive", "label": say(&lines, "app.mail.acts.archive", &[]) }, ], });// Said once: with nothing left, `line` already says so (look-ios, 24 Nov 2026).letmut empty = (sent > 0 && mail.is_empty() && chats.is_empty()).then(|| say(&lines, "app.senders.page.empty", &[]));// What they sent is archived, not gone: the page says where it is, never "Nothing from them is here." under// their count (look-ios walking ACTS TRUTH, 24 Nov 2026).if empty.is_some() && after.is_none() {let (clause, mut params) = crate::phone::chat_app::senders::mail_where(Some(&only));letmut all = vec![ws(ctx)]; all.append(&mut params);let archived = rows(ctx, &format!("SELECT COUNT(*) AS n FROM {EMAILS} WHERE workspace_id = ? AND {live_mail} AND archived_at IS NOT NULL AND {clause}", live_mail = mail_predicate::live("")), &all)? .first() .and_then(|r| r.get("n").and_then(Value::as_i64)) .unwrap_or(0);if archived == 1 { empty = Some(say(&lines, "app.senders.page.one_archived", &[])); } elseif archived > 1 { empty = Some(say(&lines, "app.senders.page.all_archived", &[("n", &archived.to_string())])); } }Ok(json!({"key": key,"kind": if domain.is_some() { "domain" } else { first.map_or("address", |s| s.key.kind.id()) },"name": name,"handle": handle,"line": line,"source": source,"group": group,"acts": acts,"chats": chats,"mail": { "title": say(&lines, "app.senders.page.mail", &[]), "rows": mail, "next": more.then(|| found.get(PAGE - 1).map(|m| format!("{}|{}", s(m, "sent_at").unwrap_or_default(), s(m, "id").unwrap_or_default()))).flatten(), "select": select },"empty": empty, })) })}