//! `people.add|accept|refuse|block|remove` and the Your people part of the //! Account card (14 Nov 2026): the one place a person's connections live, //! in every Cotton app that deals Account, so someone who uses only Books //! can connect with someone. The object keeps the one relationship (chat's //! `chat_peer` and `chat_ask`, `apps/business/src/chat/chat.ts`); Messages //! shows the same people. //! //! ```text //! people.add { number } → { said, number } //! people.accept { number, name? } → { said } //! people.refuse { number, name? } → { said } //! people.block { number, name? } → { said } //! people.remove { number, name? } → { said } //! people.unblock { number, name? } → { said } //! people.pick {} → { title, heading, rows: [{ number, words, detail?, initials }], //! add: { words, hint }, empty?, unreachable? } //! ``` //! //! **New chat's page** (`people.pick`, owner, 15 Nov 2026): a new chat is //! started by picking one of Your people, the same list Account reads, in //! the same order: the connected, then those still waiting (their line says //! so); a person asking is answered on Account, not picked. Someone not yet //! among them is reached by adding their number (`add`, `people.add`), which //! answers the same whether or not the number is on Cotton. //! //! **The part** (`shell.account`'s `people`, only while signed in): //! //! ```json //! { "title", "add": { "words", "hint", "ask": { "kind": "people_add" } }, //! "sections": [{ "id": "asking|people|waiting|blocked", "title", //! "rows": [{ "number", "words", "detail"?, "initials", //! "acts": [{ "id", "words", "red"?, "primary"?, "call": { "method", "request" } }] }] }], //! "empty"?, "unreachable"? } //! ``` //! //! Sections in that order, an empty one left out. Asking: " wants to //! connect", the number under it, Accept, Refuse, Block. Connected: the //! name, Remove and Block. Waiting: "They have not added you yet", Remove. //! Blocked (the object's `blocked`, 15 Nov 2026): the name, "They cannot //! reach you", Unblock, which returns them where they were before the block //! and lets their chat on this phone take words again; never on New chat. //! Adding any valid number answers the same whether or not it is on Cotton; //! only the owner's own number is refused, in the object's words. use serde_json::{json, Value}; use super::cotton_chat::{ask, e164, person_of, refused, text}; use crate::handle::CottonHandle; use crate::methods::MethodError; use crate::shell::client::ClientController; use cotton_core::rules::shell_lines::resolve; /// Every method this family answers. pub const METHODS: [&str; 7] = ["people.accept", "people.add", "people.block", "people.pick", "people.refuse", "people.remove", "people.unblock"]; /// The input kind Add a person asks its number in (`shell.input {op: ask, kind}`). pub const ADD_KIND: &str = "people_add"; fn say(line: &str, fills: &[(&str, &str)]) -> String { resolve("boot", &format!("account.people.{line}"), &[], fills).unwrap_or_default() } pub fn call(handle: &CottonHandle, method: &str, request: &Value) -> Result { over(&handle.client(), method, request) } /// The same, over a named controller (the suite's phone). pub fn over(client: &ClientController, method: &str, request: &Value) -> Result { match method { "people.add" => add(client, request), "people.accept" => answer(client, request, "accept", "accepted"), "people.refuse" => answer(client, request, "refuse", "refused"), "people.block" => answer(client, request, "block", "blocked"), "people.remove" => answer(client, request, "remove", "removed"), "people.unblock" => answer(client, request, "unblock", "unblocked"), "people.pick" => Ok(pick(client)), other => Err(MethodError::new("unknown_method", format!("no method {other}"))), } } /// One of this person's people by their number: the object keeps it as /// sign-in does and answers the same for any valid number; they wait until /// they add this person back. pub fn add(client: &ClientController, request: &Value) -> Result { let typed = text(request, "number")?.trim().to_string(); ask(client, "POST", "/v1/chat/accept", Some(&json!({ "number": typed }))).map_err(refused)?; let number = e164(&typed).unwrap_or(typed); Ok(json!({ "said": say("added", &[("number", &number)]), "number": number })) } /// The owner's answer to one person, through the object's one relationship. fn answer(client: &ClientController, request: &Value, what: &str, said: &str) -> Result { let number = text(request, "number")?.to_string(); ask(client, "POST", &format!("/v1/chat/{what}"), Some(&json!({ "number": number }))).map_err(refused)?; if what == "block" { // Their chat on this phone is blocked too: it says so, with Unblock (`chat.unblock`). super::cotton_chat::mark_blocked(client, &number, true)?; } if what == "unblock" { // Their chat on this phone takes words again. super::cotton_chat::mark_blocked(client, &number, false)?; } let name = request["name"].as_str().filter(|n| !n.trim().is_empty()).unwrap_or(&number).to_string(); Ok(json!({ "said": say(&format!("said.{said}"), &[("name", &name)]) })) } /// `shell.input {op: ask, kind: people_add}`: the line a number is typed in, on the phone keyboard. pub fn add_ask() -> Value { json!({ "editor": "line", "title": say("add", &[]), "words": "", "hint": say("hint", &[]), "keyboard": "phone", "keeps_words": false, "writes": { "kind": ADD_KIND } }) } /// Add a person as a row says it: its words, the number's hint and the ask its tap opens. pub fn add_row() -> Value { json!({ "words": say("add", &[]), "hint": say("hint", &[]), "ask": { "kind": ADD_KIND } }) } /// How a number is named on every screen that shows one of his people (PEOPLE BUILD 1, C7; 1b, review M3): the name /// his own contacts give it (who(handle), `cotton_store::protocol::who`; every holder's name for a number several of /// them share); else the NUMBER is the title, and a name they chose for themselves is only ever the second line /// ("calls themselves …"), never the title and never what a share is addressed by. #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) struct Named { /// The name from his contacts; None for a number they do not hold (a self-chosen name is not one). pub name: Option, /// What the row's title says: the name from his contacts, else the number. pub shown: String, /// The number is one of his contacts'. pub in_contacts: bool, /// The name they gave themselves, when the number is not in his contacts. pub calls_themselves: Option, } impl Named { /// The row's quiet second line: the number under a name from his contacts; otherwise the name they gave /// themselves with the plain marker, or the marker alone. pub fn detail(&self, number: &str) -> String { if self.in_contacts { return number.to_string(); } match &self.calls_themselves { Some(name) => say("calls_themselves", &[("name", name)]), None => say("not_in_contacts", &[]), } } } /// [`Named`] for `number`, with `given` the name they chose for themselves (an ask's, the server's). pub(crate) fn named(client: &ClientController, number: &str, given: Option<&str>) -> Named { if let (_, Some(name)) = person_of(client, number) { return Named { shown: name.clone(), name: Some(name), in_contacts: true, calls_themselves: None }; } let given = given.map(str::trim).filter(|n| !n.is_empty()).map(str::to_string); Named { shown: number.to_string(), name: None, in_contacts: false, calls_themselves: given } } /// The Your people part of the Account card, read from the object now. pub fn part(client: &ClientController) -> Value { let mut part = json!({ "title": say("title", &[]), "add": add_row(), "sections": [] }); let Ok(mut listed) = ask(client, "GET", "/v1/chat/people", None) else { part["unreachable"] = json!(say("unreachable", &[])); return part; }; super::circle::open_sealed(client, &mut listed); let mut asking = Vec::new(); for asker in listed["asking"].as_array().into_iter().flatten() { let Some(number) = asker["number"].as_str() else { continue }; let named = named(client, number, asker["name"].as_str()); asking.push(json!({ "number": number, "words": say("asks", &[("name", &named.shown)]), "detail": named.detail(number), "inContacts": named.in_contacts, "initials": initials(&named.shown), "acts": [act("accept", number, named.name.as_deref(), false, true), act("refuse", number, named.name.as_deref(), false, false), act("block", number, named.name.as_deref(), true, false)], })); } let (mut connected, mut waiting) = (Vec::new(), Vec::new()); for person in listed["people"].as_array().into_iter().flatten() { let Some(number) = person["number"].as_str() else { continue }; let named = named(client, number, person["name"].as_str()); if person["waiting"] == json!(true) { waiting.push(json!({ "number": number, "words": named.shown, "detail": say("waiting", &[]), "inContacts": named.in_contacts, "initials": initials(&named.shown), "acts": [act("remove", number, named.name.as_deref(), true, false)] })); } else { connected.push(json!({ "number": number, "words": named.shown, "detail": named.detail(number), "inContacts": named.in_contacts, "initials": initials(&named.shown), "acts": [act("remove", number, named.name.as_deref(), true, false), act("block", number, named.name.as_deref(), true, false)] })); } } // Blocked, after the others (15 Nov 2026): a block taken back from here returns the person where they were. let mut blocked = Vec::new(); for person in listed["blocked"].as_array().into_iter().flatten() { let Some(number) = person["number"].as_str() else { continue }; let named = named(client, number, person["name"].as_str()); blocked.push(json!({ "number": number, "words": named.shown, "detail": say("blocked", &[]), "inContacts": named.in_contacts, "initials": initials(&named.shown), "acts": [act("unblock", number, named.name.as_deref(), false, false)] })); } let sections: Vec = [("asking", asking), ("people", connected), ("waiting", waiting), ("blocked", blocked)] .into_iter() .filter(|(_, rows)| !rows.is_empty()) .map(|(id, rows)| json!({ "id": id, "title": say(&format!("sections.{id}"), &[]), "rows": rows })) .collect(); if sections.is_empty() { part["empty"] = json!(say("empty", &[])); } part["sections"] = json!(sections); part } /// New chat's page: Your people to pick from (`part`'s connected and /// waiting rows, without their acts), under the page's title, and the add /// row for someone not yet among them. pub fn pick(client: &ClientController) -> Value { let part = part(client); let rows: Vec = part["sections"] .as_array() .into_iter() .flatten() .filter(|section| section["id"] != "asking" && section["id"] != "blocked") .flat_map(|section| section["rows"].as_array().cloned().unwrap_or_default()) .map(|mut row| { if let Some(row) = row.as_object_mut() { row.remove("acts"); } row }) .collect(); let mut page = json!({ "title": resolve("chat", "stream.new", &[], &[]).unwrap_or_default(), "heading": part["title"], "add": { "words": part["add"]["words"], "hint": part["add"]["hint"] }, }); if let Some(unreachable) = part.get("unreachable") { page["unreachable"] = unreachable.clone(); } else if rows.is_empty() { page["empty"] = json!(say("pick_empty", &[])); } page["rows"] = json!(rows); page } /// One act on a person's row, carrying the method and request it runs. fn act(id: &str, number: &str, name: Option<&str>, red: bool, primary: bool) -> Value { let mut request = json!({ "number": number }); if let Some(name) = name { request["name"] = json!(name); } let mut act = json!({ "id": id, "words": say(&format!("acts.{id}"), &[]), "call": { "method": format!("people.{id}"), "request": request } }); if red { act["red"] = json!(true); } if primary { act["primary"] = json!(true); } act } /// A person's letters: the one rule for people (`lay::initials`), so a number shows `#`, not a digit or `+`. fn initials(name: &str) -> String { cotton_core::messages::app::lay::initials(name) } #[cfg(test)] #[path = "your_people_tests.rs"] mod tests;