Cotton code
141 lines · 8.3 KB · RustRaw
//! `chat.you {notifications?}` — COMMS' YOU, GROUPED (you-grouped.html, the//! owner's review of 22 Nov 2026): who the person is, then seven rows, each//! saying in a few words how it stands, each opening its own page.//! `chat.you.now {notifications?}` is the same answer from this phone alone,//! for You's first frame (owner, build 24).//!//! ```json//! { "rows": [{ "id": "export|sites|sync|claude|privacy|notifications|storage|help|about", "label", "line"?, "later"? }],//!   "sync": { "title", "last"?, "trouble", "stuck"? },//!   "muted": [{ "thread", "name", "picture", "until" }],//!   "profile": { "name"?, "number"?, "picture"? } }//! ```//!//! - `notifications` is whether the phone lets Comms notify at all, which only//!   the phone knows; without it the row says only how many chats are muted.//! - A row whose figures cannot be read now (signed out, Cotton out of reach//!   for the blocked people) says what it can, never a guess.//! - `sync` is the Sync page: the state line, when it last synced while the//!   line says something else, and whether Cotton is out of reach.//! - `muted` is Notifications' Muted: every chat muted now, in the list's//!   order, with until when.//! - `profile` is the card as this phone knows it, with no network: the//!   name others see (`chat.profile`'s, else the person's own in the copy),//!   the number, and the picture's file; `chat.profile` answers the same//!   from the network.//! - **`chat.you`** asks the server for the blocked people and walks the//!   app's folder for Storage, and keeps both figures//!   (`shell_settings::Later`). **`chat.you.now`** does neither: the blocked//!   count and Storage are as the last `chat.you` found them, across//!   launches; never found yet, Storage stands with `later: true` and no//!   line, and Privacy says only what this phone holds (Read receipts) with//!   `later: true`, both filled in place by the whole answer.use serde_json::{json, Value};use cotton_core::messages::app::list::{self, say};use crate::handle::CottonHandle;use crate::methods::shell_settings::{keep_later, kept, later, size_said, Kept};use crate::methods::MethodError;/// `chat.you`: the whole answer, the server and the folder asked now.pub fn call(handle: &CottonHandle, request: &Value) -> Result<Value, MethodError> {    let client = handle.client();    // The blocked people are the server's (`chat.blocked`): out of reach, the row says only what this phone holds.    let blocked = crate::methods::dispatch(handle, "chat.blocked", &json!({})).ok().and_then(|b| b["people"].as_array().map(Vec::len));    let kept = kept(&client);    keep_later(&client, |later| {        later.kept = Some(kept);        if blocked.is_some() {            later.blocked = blocked;        }    });    answered(handle, request, blocked, Some(kept), false)}/// `chat.you.now`: the same, from this phone alone (no network, no folder walk).pub fn now(handle: &CottonHandle, request: &Value) -> Result<Value, MethodError> {    let later = later(&handle.client());    answered(handle, request, later.blocked, later.kept, later.blocked.is_none())}fn answered(handle: &CottonHandle, request: &Value, blocked: Option<usize>, kept: Option<Kept>, blocked_later: bool) -> Result<Value, MethodError> {    let client = handle.client();    let lines = list::lines();    let row = |id: &str, line: Option<String>, later: bool| {        let mut row = json!({ "id": id, "label": say(&lines, &format!("app.you.rows.{id}"), &[]) });        if let Some(line) = line.filter(|l| !l.is_empty()) {            row["line"] = json!(line);        }        if later {            row["later"] = json!(true);        }        row    };    let sync = crate::methods::shell_account::sync_said(&client);    let claude = super::assistants::call_on(&client, "assistant.screen", &json!({})).ok().and_then(|screen| screen["reading"]["row"].as_str().map(str::to_string));    let receipts = crate::methods::dispatch(handle, "chat.settings", &json!({})).ok().and_then(|s| s["readReceipts"].as_bool()).unwrap_or(false);    let receipts = say(&lines, if receipts { "app.you.glance.on" } else { "app.you.glance.off" }, &[]);    let privacy = match blocked {        Some(n) if n > 0 => say(&lines, "app.you.glance.privacy_blocked", &[("n", &n.to_string()), ("receipts", &receipts)]),        _ => say(&lines, "app.you.glance.privacy", &[("receipts", &receipts)]),    };    let muted = super::chat_app::muted(&client).unwrap_or_default();    let notifications = match request.get("notifications").and_then(Value::as_bool) {        Some(false) => say(&lines, "app.you.glance.notifications_off", &[]),        _ if !muted.is_empty() => say(&lines, "app.you.glance.muted", &[("n", &muted.len().to_string())]),        Some(true) => say(&lines, "app.you.glance.notifications_on", &[]),        None => String::new(),    };    let storage = kept.map(|kept| say(&lines, "app.you.glance.storage", &[("size", &size_said(kept.total()))]));    let mut rows = vec![        // Comms' own row first, after the card (shared/DECISIONS.md "Settings"): Export, every chat (CONTRACT.md "Export").        row("export", Some(say(&lines, "app.export.hint", &[])), false),        // Changes that cannot be sent are said first, on the row that opens the Sync page (UNSENT HEAL, 26 Nov 2026).        row("sync", sync["stuck"]["title"].as_str().or(sync["title"].as_str()).map(str::to_string), false),        row("claude", claude, false),        row("privacy", Some(privacy), blocked_later),        row("notifications", Some(notifications), false),    ];    // Blocked, the place a blocked sender's texts wait 30 days (SENDERS.md), stands under You now that Senders is    // Cotton Post's (COMMS WITHOUT MAIL): only while it holds someone.    let senders_blocked = super::chat_app::senders::blocked_count(&client).filter(|n| *n > 0);    rows.extend(senders_blocked.map(|n| row("blocked", Some(say(&lines, "app.you.glance.blocked", &[("n", &n.to_string())])), false)));    // Mail is Cotton Post's now (COMMS WITHOUT MAIL; mail/DECISIONS.md step 9): one line pointing to Post, right after    // Export, on a phone that says whether Post is on it (as Texts below: a phone that cannot open Post draws no row).    if let Some(here) = request.get("post").and_then(Value::as_bool) {        let said = say(&lines, if here { "app.you.glance.post" } else { "app.you.glance.post_absent" }, &[]);        rows.insert(1, post_row(row("post", Some(said), false), say(&lines, "app.you.glance.post_absent", &[])));    }    // Texts (SMS): only on an Android phone, which says whether Comms holds the SMS role (DECISIONS "SMS on Android").    let sms = request.get("sms").and_then(Value::as_bool).map(|held| row("sms", Some(say(&lines, if held { "app.you.glance.sms_on" } else { "app.you.glance.sms_off" }, &[])), false));    let rows: Vec<Value> = rows.into_iter().chain(sms).chain([        row("storage", storage, kept.is_none()),        row("help", Some(say(&lines, "app.you.glance.help", &[])), false),        row("about", Some(say(&lines, "app.you.glance.about", &[("version", handle.app_version())])), false),    ]).collect();    // Sites is Cotton Sites' now (COMMS WITHOUT MAIL; sites CONTRACT row 8): no row under You. A list Comms still    // holds stays its own place until it has moved to Sites (`sites::is_place`, `sites.move.*`).    Ok(json!({ "rows": rows, "sync": sync, "muted": muted, "profile": super::chat_people::profile_here(handle) }))}/// Cotton Post's App Store id, filled in at listing time (the coordinator, 27 Nov 2026: Post is on TestFlight only)./// While it is `None`, You's Mail row offers nothing to tap on a phone without Post.pub const POST_APP_STORE_ID: Option<&str> = None;/// You's Mail row with Post's own address on each phone (`from=comms`, as Comms hands links to Sites: `chat.link`),/// what it says when Post is not on the phone, and Post's store page once it is listed.fn post_row(mut row: Value, absent: String) -> Value {    row["post"] = json!({        "ios": { "live": "cottonmail://open?from=comms", "dev": "cottonmaildev://open?from=comms" },        "android": { "package": "com.cottondb.mail", "dev_package": "com.cottondb.mail.dev", "extra": { "from": "comms" } },        "absent": absent,        "store": POST_APP_STORE_ID.map(|id| format!("https://apps.apple.com/app/id{id}")),    });    row}#[cfg(test)]#[path = "chat_you_tests.rs"]mod tests;