//! 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;pubusesuper::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.pubfn channel_for_service(service: Option<&str>) -> Option<MessageChannel> {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.pubconst CHAT_STYLE_DM: i64 = 45;pubconst CHAT_STYLE_GROUP: i64 = 43;#[derive(Debug, Clone, Copy, PartialEq, Eq)]pubenumThreadShape {Dm,Group,}implThreadShape {pubconstfn as_str(self) -> &'static str {matchself {ThreadShape::Dm => "dm",ThreadShape::Group => "group", } }}pubfn thread_kind_for_style(style: Option<i64>) -> Option<ThreadShape> {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("❤️"), // loved2001 => Some("👍"), // liked2002 => Some("👎"), // disliked2003 => Some("😂"), // laughed2004 => Some("‼️"), // emphasised2005 => 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)]pubstructTapback {/// 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/<guid>` names one/// attachment of a multi-part message, `bp:` a whole one — and the reaction/// belongs to the message either way.pubfn target_guid_of(raw: Option<&str>) -> Option<String> {let value = raw?.trim();if value.is_empty() {returnNone; }if value.starts_with("p:") || value.starts_with("bp:") {ifletSome(slash) = value.find('/') {let tail = &value[slash + 1..];returnif tail.is_empty() {None } else {Some(tail.to_string()) }; } }ifletSome(tail) = value.strip_prefix("bp:") {returnif tail.is_empty() {None } else {Some(tail.to_string()) }; }Some(value.to_string())}pubfn read_tapback( associated_message_type: Option<i64>, associated_message_guid: Option<&str>, associated_message_emoji: Option<&str>,) -> Option<Tapback> {let kind = associated_message_type.unwrap_or(0);if !(2000..=3007).contains(&kind) || (2007 < kind && kind < 3000) {returnNone; }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.pubfn group_title(display_name: Option<&str>, member_labels: &[String]) -> String {ifletSome(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.pubfn is_message_row(item_type: Option<i64>) -> bool { matches!(item_type, None | Some(0))}#[cfg(test)]#[path = "messages_schema_tests.rs"]mod tests;