//! A contact, found or made, from one way of reaching somebody: the rule lives//! in cotton-rules (`cotton_rules::messages::resolve_contact`, re-exported//! here whole); the three statements the phone's [`ContactDriver`] runs stay//! here, beside the file they read.pubuse cotton_rules::messages::resolve_contact::*;/// The handles people carry, per kind (`cotton_person_handles`, v90): the/// kind comes from [`ContactHandleKind::as_str`] — never from a caller, because/// it is interpolated into SQL. The value read is the NORMALISED form. The/// rows are who(handle)'s (`cotton_store::protocol::who`, PEOPLE BUILD 1b):/// only a living person holds a handle (neither in the bin nor archived), and/// a contact keeping a handle as shared (`shared_handle`, a household/// landline) is a claim too, so a shared handle is two claims and names/// nobody. One parameter: the workspace. `shared`: whether this copy holds/// the kit's `shared_handle` (`cotton_store::protocol::who::shared_held`).pubfn people_handles_sql(shared: bool, kind: ContactHandleKind) -> String {let own = format!("SELECT cotton_people.id, cotton_people.full_name, h.normalised AS value\n FROM cotton_person_handles h JOIN cotton_people ON cotton_people.id = h.person_id\n WHERE h.workspace_id = ?1 AND h.kind = '{kind}' AND h.trashed_at IS NULL AND cotton_people.trashed_at IS NULL AND cotton_people.archived_at IS NULL", kind = kind.as_str(), );if !shared {return own; } format!("{own}\n UNION ALL SELECT cotton_people.id, cotton_people.full_name, s.normalised AS value\n FROM {table} s JOIN cotton_people ON cotton_people.id = s.person\n WHERE s.workspace_id = ?1 AND s.kind = '{kind}' AND s.deleted_at IS NULL AND cotton_people.trashed_at IS NULL AND cotton_people.archived_at IS NULL", kind = kind.as_str(), table = cotton_store::protocol::who::SHARED_TABLE, )}/// The handle row's own join, its contact's person DERIVED (who(handle),/// `cotton_store::protocol::who::contact_person_sql`), never the link stored/// when it was made. Parameters: workspace, kind, value.pubfn contact_by_handle_sql(shared: bool) -> String { format!("SELECT c.id, c.display_name, {} AS person_id\n FROM cotton_msg_contact_handles h JOIN cotton_msg_contacts c ON c.id = h.contact_id\n WHERE h.workspace_id = ? AND h.kind = ? AND h.value = ?", cotton_store::protocol::who::contact_person_sql(shared, "c") )}/// The primary-phone fallback, its person derived the same way. Parameters:/// workspace, phone.pubfn contact_by_phone_sql(shared: bool) -> String { format!("SELECT c.id, c.display_name, {} AS person_id FROM cotton_msg_contacts c\n WHERE c.workspace_id = ? AND c.phone_e164 = ?", cotton_store::protocol::who::contact_person_sql(shared, "c") )}#[cfg(test)]mod tests {usesuper::*;// ── the statements ───────────────────────────────────────────────────────────/// The SQL is declared here so a server issues the TypeScript's own statements/// rather than a paraphrase, and the two visibility filters — the workspace and/// the archived person — are the reason: a second spelling is where one of them/// goes missing. #[test]fn the_three_statements_are_the_typescripts_own() {let phones = people_handles_sql(true, ContactHandleKind::Phone);// v90: a person's handles are rows, matched on their normalised form. assert!(phones.contains("FROM cotton_person_handles h"), "{phones}"); assert!(phones.contains("h.kind = 'phone'"), "{phones}"); assert!(phones.contains("h.trashed_at IS NULL AND cotton_people.trashed_at IS NULL AND cotton_people.archived_at IS NULL"), "{phones}"); assert!(phones.contains("FROM cotton_shared_handle s"), "a shared handle is a claim too: {phones}"); assert!(phones.contains("workspace_id = ?1"), "{phones}"); assert!(people_handles_sql(true, ContactHandleKind::Email).contains("h.kind = 'email'")); assert!(!people_handles_sql(false, ContactHandleKind::Phone).contains("shared_handle"), "a copy without the table reads none"); assert!(contact_by_handle_sql(true).contains("h.workspace_id = ? AND h.kind = ? AND h.value = ?")); assert!(contact_by_phone_sql(true).contains("c.workspace_id = ? AND c.phone_e164 = ?")); assert!(!contact_by_phone_sql(true).contains("c.person_id AS"), "the person is derived"); }}