Cotton code
77 lines · 3.1 KB · RustRaw
//! A message that carried media and no words.//!//! Its body stays empty: a row is data, and words a person reads are kit lines//! resolved at read. `meta.mediaOnly` is the typed mark that says what it//! carried (`image`, `video`, `voice`, `audio`, `sticker`, `document`, …).//! Every reader that needs a label asks [`media_only_words`]; no caller//! formats its own, and no importer writes one into a body.use serde_json::{Map, Value};use crate::messages::MessageMeta;/// `meta.mediaOnly`: the kind a media-only message carried, as the source/// names it.pub const MEDIA_ONLY_META: &str = "mediaOnly";/// The kit line (in the `conversation` set) a media-only message of `kind`/// is read as.pub fn media_only_line(kind: &str) -> &'static str {    match kind {        "image" | "photo" => "preview.photo",        "video" | "gif" => "preview.video",        "voice" | "ptt" => "preview.voice",        "audio" => "preview.audio",        "sticker" => "preview.sticker",        "document" => "preview.document",        "contact" | "contacts" => "preview.contact",        _ => "preview.attachment",    }}/// The words a media-only message of `kind` reads as: its kit line.pub fn media_only_words(kind: &str) -> String {    crate::lines::resolve("conversation", media_only_line(kind), &[])}/// The words importers once wrote into a media-only message's body (before/// 7 Nov 2026). Rows never move, so a reader treats a marked row whose body is/// one of these as empty: an old row reads as a new one does.pub const LEFT_BEHIND_LABELS: [&str; 7] = ["Photo", "Video", "Voice message", "Audio", "Sticker", "Document", "Attachment"];/// Whether `body` is the token a media-only message whose bytes did not come/// held from 7 Nov 2026 until schema 145 dropped the message table's CHECK:/// its kit line's full key (`conversation.preview.photo`). Such a message now/// keeps an empty body; rows never move, so a reader treats an old marked row/// holding the token as empty.pub fn is_placeholder(body: &str) -> bool {    crate::js::js_trim(body)        .strip_prefix("conversation.preview.")        .is_some_and(|kind| ["photo", "video", "voice", "audio", "sticker", "document", "contact", "attachment"].contains(&kind))}/// Whether `body` is one of the [`LEFT_BEHIND_LABELS`].pub fn is_left_behind_label(body: &str) -> bool {    LEFT_BEHIND_LABELS.contains(&crate::js::js_trim(body))}/// The mark on a row's raw meta.pub fn media_only_kind(meta: &Map<String, Value>) -> Option<&str> {    meta.get(MEDIA_ONLY_META).and_then(Value::as_str)}/// The mark on a row's typed meta.pub fn media_only_kind_of(meta: Option<&MessageMeta>) -> Option<&str> {    meta.and_then(|meta| media_only_kind(&meta.extra))}/// Whether a body says nothing anybody wrote: empty, or, on a marked row, the/// old token or one of the old labels. The one rule for every reader.pub fn body_is_wordless(body: &str, marked: bool) -> bool {    let body = crate::js::js_trim(body);    body.is_empty() || (marked && (is_placeholder(body) || is_left_behind_label(body)))}#[cfg(test)]#[path = "media_only_tests.rs"]mod tests;