//! Everything the Messages reader DECIDES, as pure functions over one row. //! //! Split from the reader so the decisions can be tested without a database and //! so there is exactly one place that answers each of them. The reader runs //! over the Mac's live `chat.db`; Apple's dates and handles are shared with the //! call history and Photos readers in [`super::apple_schema`]. use cotton_core::constants::MessageChannel; pub use super::apple_schema::{apple_date_to_iso, read_handle, HandleKind, MessageHandle, APPLE_EPOCH_MS, DEFAULT_COUNTRY_CODE}; /// The channel a service label belongs to. /// /// FOUR channels exist and the exact label is not one of them: `iMessage` is /// `imessage`, and `SMS`, `MMS` and `RCS` are all `sms` — a carrier can print /// any number of labels and the app badges a transport, not a spelling. The /// verbatim label rides `meta.source.service` instead. pub fn channel_for_service(service: Option<&str>) -> Option { let label = service?.trim().to_ascii_uppercase(); match label.as_str() { "IMESSAGE" => Some(MessageChannel::Imessage), "SMS" | "MMS" | "RCS" => Some(MessageChannel::Sms), _ => None, } } /// `chat.style`: 45 is a one-to-one conversation, 43 is a group. pub const CHAT_STYLE_DM: i64 = 45; pub const CHAT_STYLE_GROUP: i64 = 43; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ThreadShape { Dm, Group, } impl ThreadShape { pub const fn as_str(self) -> &'static str { match self { ThreadShape::Dm => "dm", ThreadShape::Group => "group", } } } pub fn thread_kind_for_style(style: Option) -> Option { match style { Some(CHAT_STYLE_DM) => Some(ThreadShape::Dm), Some(CHAT_STYLE_GROUP) => Some(ThreadShape::Group), _ => None, } } /// The tapback emoji each 2000-series type stands for. /// /// 2006 IS the custom-emoji tapback and 2007 the sticker one; both carry /// whatever the person actually picked in `associated_message_emoji`, so /// neither is in this table. fn tapback_emoji(base: i64) -> Option<&'static str> { match base { 2000 => Some("❤️"), // loved 2001 => Some("👍"), // liked 2002 => Some("👎"), // disliked 2003 => Some("😂"), // laughed 2004 => Some("‼️"), // emphasised 2005 => Some("❓"), // questioned _ => None, } } /// The tapback a row carries, if it is one. /// /// 2000–2007 ADD a reaction to another message; 3000–3007 REMOVE the one they /// mirror. Both are rows in `message` and NEITHER is a message: filing one as a /// message puts `Liked "see you at 8"` in somebody's conversation as if they /// had typed it. /// /// A REMOVAL is read and then dropped, deliberately and with a written reason: /// the import engine's reaction rows are upserts keyed on (message, actor), and /// it exposes no delete — so this reader can say "there was a removal" in its /// counts and must not pretend to have applied one. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Tapback { /// The guid of the message being reacted to. pub target_guid: String, pub emoji: String, /// True for the 3000-series rows, which take a reaction back. pub removal: bool, } /// The message a tapback points at. /// /// The column holds the guid behind a part prefix — `p:0/` names one /// attachment of a multi-part message, `bp:` a whole one — and the reaction /// belongs to the message either way. pub fn target_guid_of(raw: Option<&str>) -> Option { let value = raw?.trim(); if value.is_empty() { return None; } if value.starts_with("p:") || value.starts_with("bp:") { if let Some(slash) = value.find('/') { let tail = &value[slash + 1..]; return if tail.is_empty() { None } else { Some(tail.to_string()) }; } } if let Some(tail) = value.strip_prefix("bp:") { return if tail.is_empty() { None } else { Some(tail.to_string()) }; } Some(value.to_string()) } pub fn read_tapback( associated_message_type: Option, associated_message_guid: Option<&str>, associated_message_emoji: Option<&str>, ) -> Option { let kind = associated_message_type.unwrap_or(0); if !(2000..=3007).contains(&kind) || (2007 < kind && kind < 3000) { return None; } let target_guid = target_guid_of(associated_message_guid)?; let removal = kind >= 3000; let base = if removal { kind - 1000 } else { kind }; let custom = associated_message_emoji .map(str::trim) .filter(|value| !value.is_empty()); // A 2006/2007 row with an empty emoji column has told us nothing to store. let emoji = tapback_emoji(base) .map(str::to_string) .or_else(|| custom.map(str::to_string))?; Some(Tapback { target_guid, emoji, removal, }) } /// The group's title. /// /// `display_name` when the group has been named, and otherwise the members — /// which is what Messages itself shows, and what makes an unnamed group /// recognisable in a list. pub fn group_title(display_name: Option<&str>, member_labels: &[String]) -> String { if let Some(name) = display_name.map(str::trim).filter(|name| !name.is_empty()) { return name.to_string(); } if member_labels.is_empty() { return "Group".to_string(); } member_labels.join(", ") } /// Is this row a message at all? /// /// `item_type` 0 is a message; everything else is an EVENT the chat recorded — /// somebody named the group (1/2), joined or left (3), started sharing their /// location (4) or a screen (5). The chat's structured event rows are written /// by the relay for CottonDB's own groups and the import engine has no path /// that writes one, so an imported iMessage group event is DROPPED and counted /// rather than turned into a message that says something nobody typed. pub fn is_message_row(item_type: Option) -> bool { matches!(item_type, None | Some(0)) } #[cfg(test)] #[path = "messages_schema_tests.rs"] mod tests;