Cotton code
72 lines · 3.4 KB · RustRaw
//! The Stories table's shared pieces — what every story writer needs (the//! WhatsApp statuses ingest and the Instagram sources sink), outside the//! feature-gated `ingest` module so the phone build has them too.use rusqlite::types::Value as SqlValue;use cotton_store::outbox::LocalChange;use cotton_store::Driver;use serde_json::{json, Map};pub const STORY_TABLE: &str = "cotton_stories";/// A story lives a day (WhatsApp and Instagram alike).pub const STATUS_LIFETIME_MS: i64 = 24 * 60 * 60 * 1000;/// The folder a status's picture is kept under — `{file}/messages/statuses/…`/// — so the message media limits, signing and storage rules apply unchanged./// No thread id is `statuses`. The store's own constant: the keep rules read/// the same folder to say a status's bytes are never on a phone.pub const STATUS_MEDIA_FOLDER: &str = cotton_store::keep::STATUS_MEDIA_FOLDER;/// The pictures whose day is over and that nobody asked to keep: a story by/// someone on no People record, not kept, past `expires_at`. The row stays;/// setting `media_dropped_at` lets its bytes go (a media-key release)./// Instagram expiry changes presentation only; its imported originals stay.pub fn story_media_drops(db: &Driver, workspace_id: &str, now: &str) -> Result<Vec<LocalChange>, String> {    // A person on no People record: none named, or one since removed.    let sql = format!(        "SELECT s.id FROM cotton_stories s WHERE s.workspace_id = ? AND s.media_key IS NOT NULL \         AND s.source != 'instagram' AND s.media_dropped_at IS NULL AND s.kept = 0 AND {} AND s.expires_at <= ?",        cotton_store::soft_refs::soft_absent("s.person_id", "cotton_people"),    );    let rows = db        .all(&sql, &[SqlValue::Text(workspace_id.to_string()), SqlValue::Text(now.to_string())])        .map_err(|error| error.to_string())?;    Ok(drops(rows.iter().filter_map(|row| row.text("id")), now))}/// The one-time release of a WhatsApp number's status backlog, asked for by/// the person (the connection's Release status videos act), never by a timer:/// every status of `account` whose day is over and that nobody kept lets its/// picture go, whoever posted it — the People condition of/// [`story_media_drops`] is lifted for this act only. Rows and captions stay.pub fn status_media_releases(    db: &Driver,    workspace_id: &str,    account: &str,    now: &str,) -> Result<Vec<LocalChange>, String> {    let rows = db        .all(            "SELECT id FROM cotton_stories WHERE workspace_id = ? AND source = 'whatsapp' AND account = ? \             AND media_key IS NOT NULL AND media_dropped_at IS NULL AND kept = 0 AND expires_at <= ?",            &[                SqlValue::Text(workspace_id.to_string()),                SqlValue::Text(account.to_string()),                SqlValue::Text(now.to_string()),            ],        )        .map_err(|error| error.to_string())?;    Ok(drops(rows.iter().filter_map(|row| row.text("id")), now))}/// Each story's picture let go at `now`: `media_dropped_at` set, a media-key/// release, so the bytes leave through the ordinary unlink path.fn drops<'a>(ids: impl Iterator<Item = &'a str>, now: &str) -> Vec<LocalChange> {    ids.map(|id| {        let mut patch = Map::new();        patch.insert("id".into(), json!(id));        patch.insert("media_dropped_at".into(), json!(now));        LocalChange::upsert(STORY_TABLE, id, patch)    })    .collect()}