//! Which channels know a handle (lane channels, 21 Oct 2026; the record-page //! proposal's open question). A channel knows a handle when this database //! holds a thread, call or mail with it on that channel — evidence a source //! finds and hands in as [`HandleActivity`] — and every handle kind is also //! reached by its own channels: a number is called and texted, an address //! mailed, a username opened on its network. No network and no lookup //! outside the database: the answer is what the file already shows. //! //! Every label is a definition's own word — the message table's channel //! choice, the call or email table's name, the handle table's kind — never a //! sentence written here. use serde::Serialize; use crate::definitions::TableDefinition; /// The kit's shared tables whose words name a channel. pub const MESSAGE_TABLE: &str = "message"; pub const MESSAGE_CHANNEL_COLUMN: &str = "channel"; pub const CALL_TABLE: &str = "call"; pub const EMAIL_TABLE: &str = "email"; /// The channel ids that are not a message channel. pub const CALL: &str = "call"; pub const MAIL: &str = "mail"; /// The message channel a number is always texted on. pub const TEXT: &str = "sms"; /// One channel the database holds activity with a handle on, as a source /// found it: `channel` is a message channel's choice (`whatsapp`, /// `imessage`, `sms`, `instagram`, …), [`CALL`] or [`MAIL`]; `at` the /// newest activity, an ISO stamp. #[derive(Debug, Clone, PartialEq, Eq)] pub struct HandleActivity { pub channel: String, pub at: String, } /// A channel a handle reaches by. #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct Channel { pub id: String, pub label: String, /// What the phone does on it: `call`, `message`, `mail` or `open`. pub act: String, /// The newest activity with the handle on it; absent for a channel the /// kind offers without evidence. #[serde(rename = "lastActivity", skip_serializing_if = "Option::is_none")] pub last_activity: Option, } /// The channels `kind`'s handle reaches by: those `evidence` holds, newest /// first, then the kind's own not already listed. One rule per kind — a /// phone is called and texted, an email mailed, a network handle opened on /// its network; a kind the rules do not name offers only its evidence. /// `handles` is the table that holds the handle (its kind column /// `kind_column` names a network), `tables` every installed definition. pub fn channels_for_handle( kind: &str, evidence: &[HandleActivity], handles: &TableDefinition, kind_column: &str, tables: &[TableDefinition], ) -> Vec { let words = Words { tables, handles, kind_column }; let mut found: Vec<&HandleActivity> = evidence.iter().filter(|seen| !seen.channel.trim().is_empty()).collect(); found.sort_by(|a, b| b.at.cmp(&a.at)); let mut channels: Vec = Vec::new(); for seen in found { let mut channel = words.seen(kind, &seen.channel); if channels.iter().any(|held| held.id == channel.id && held.act == channel.act) { continue; } channel.last_activity = Some(seen.at.clone()); channels.push(channel); } let own: Vec = match kind { "phone" => vec![words.seen(kind, CALL), words.seen(kind, TEXT)], "email" => vec![words.seen(kind, MAIL)], _ if words.is_network(kind) => vec![Channel { id: kind.to_string(), label: words.kind(kind), act: "open".into(), last_activity: None }], _ => Vec::new(), }; for channel in own { if !channels.iter().any(|held| held.id == channel.id && held.act == channel.act) { channels.push(channel); } } channels } struct Words<'a> { tables: &'a [TableDefinition], handles: &'a TableDefinition, kind_column: &'a str, } impl Words<'_> { fn table(&self, id: &str) -> Option<&TableDefinition> { self.tables.iter().find(|table| table.id == id) } /// A handle kind's own word ("Phone", "Instagram"). fn kind(&self, kind: &str) -> String { self.handles.column(self.kind_column).map_or_else(|| kind.to_string(), |column| column.choice_label(kind)) } /// A network kind: one the handle table names that is neither a number /// nor an address. fn is_network(&self, kind: &str) -> bool { kind != "phone" && kind != "email" && self.handles.column(self.kind_column).is_some_and(|column| column.choices.iter().flatten().any(|choice| choice == kind)) } /// The message table's word for a channel, where it names one. fn message_channel(&self, id: &str) -> Option { let column = self.table(MESSAGE_TABLE)?.column(MESSAGE_CHANNEL_COLUMN)?; column.choices.iter().flatten().any(|choice| choice == id).then(|| column.choice_label(id)) } /// A channel the handle was seen on: a call is called, mail mailed, a /// message channel written to; anything else opened where it lives. fn seen(&self, kind: &str, id: &str) -> Channel { let (label, act) = match id { CALL => (self.table(CALL_TABLE).map_or_else(|| self.kind(kind), |table| table.name.clone()), CALL), MAIL => (self.table(EMAIL_TABLE).map_or_else(|| self.kind(kind), |table| table.name.clone()), MAIL), _ => match self.message_channel(id) { Some(label) => (label, "message"), None => (self.kind(id), "open"), }, }; Channel { id: id.to_string(), label, act: act.to_string(), last_activity: None } } }