//! Deleted in WhatsApp means deleted in Cotton (the owner, FEEDBACK-3, 23 Nov 2026; CONTRACT.md "WhatsApp through the //! Mac", "Deleted for everyone in WhatsApp"). //! //! The Mac's link reports a delete for everyone, by the sender or by the owner from WhatsApp itself, and the import's //! changes pass lands it as it always has: the message's words go and `deleted_at` is set (cotton-rules //! `plan_message_changes`). That kept the row, its media and every earlier copy of its words in history. Now the //! phone's sweep ERASES each such WhatsApp message, as the timer's erase does (reason `revoked`): the row, its media, //! receipts, history, reactions and search entry, here and, through the object, on every phone of his. In the same act //! a marker stands in its place, under the same (thread, `whatsapp`, WhatsApp id), holding only what the chat needs to //! draw "This message was deleted" where it was: who sent it (its direction, and a group's sender) and when, and when //! it was deleted. It holds no words, media, quote or reaction, and no later import brings the message back. //! //! Every message already deleted for everyone before this (FEEDBACK-2 B.4: each kept as a blanked row) goes on the //! first sweep, so the rule holds for the past too. use serde_json::{json, Map, Value}; use cotton_tools::mcp::handlers::common::insert_row; use super::chat_timer::{newest_of, unword_lists}; use super::cotton_chat::{exists, read, rows, write, ws, MESSAGES}; use crate::methods::MethodError; use crate::shell::client::ClientController; /// The erase's reason, and the marker's `meta.cotton.erased`. pub(crate) const REVOKED: &str = "revoked"; /// At most this many messages go in one sweep; the next sweep takes the rest. const SWEEP_MAX: i64 = 500; /// One WhatsApp message deleted for everyone, as the sweep finds it. struct Revoked { id: String, thread: String, external: String, direction: Value, sender: Value, sent_at: String, deleted_at: String, } /// Erase every WhatsApp message deleted for everyone that is still kept as a row, each with its marker. Answers how /// many went. pub(crate) fn sweep_revoked(client: &ClientController) -> Result { let found = read(client, "chat.revoked", |ctx| { let found = rows( ctx, &format!( "SELECT id, thread_id, external_id, direction, sender_participant_id, sent_at, deleted_at FROM {MESSAGES} \ WHERE workspace_id = ? AND channel = 'whatsapp' AND deleted_at IS NOT NULL AND deleted_at <> '' \ AND external_id IS NOT NULL AND external_id <> '' \ AND json_extract(meta, '$.cotton.erased') IS NULL AND json_extract(meta, '$.cotton.timer') IS NULL LIMIT {SWEEP_MAX}" ), &[ws(ctx)], )?; Ok(Value::Array(found.into_iter().map(Value::Object).collect())) })?; let text = |row: &Value, key: &str| row.get(key).and_then(Value::as_str).unwrap_or_default().to_string(); let due: Vec = found .as_array() .into_iter() .flatten() .map(|row| Revoked { id: text(row, "id"), thread: text(row, "thread_id"), external: text(row, "external_id"), direction: row.get("direction").cloned().unwrap_or(Value::Null), sender: row.get("sender_participant_id").cloned().unwrap_or(Value::Null), sent_at: text(row, "sent_at"), deleted_at: text(row, "deleted_at"), }) .filter(|r| !r.id.is_empty() && !r.thread.is_empty()) .collect(); if due.is_empty() { return Ok(0); } let count = due.len(); write(client, "chat.revoked", move |ctx| { // Erased already (another of the person's phones went first, and its erase folded here). let mut live = Vec::new(); for r in &due { if exists(ctx, MESSAGES, &r.id)? { live.push(r); } } // The list lines first, as the timer's erase writes them: a revoked newest message's line already reads // "This message was deleted", and stays so; every earlier line that held its words is superseded. unword_lists(ctx, newest_of(&live.iter().map(|r| (r.thread.clone(), r.sent_at.clone())).collect::>()), false)?; for r in live { cotton_tools::mcp::handlers::tables::forget_changes(ctx, &r.id).map_err(|f| f.to_string())?; ctx.writer().map_err(|f| f.to_string())?.erase(MESSAGES, &r.id, REVOKED); let marker = cotton_core::chat::ids::erased_marker_id(&r.thread, "whatsapp", &r.external); if !exists(ctx, MESSAGES, &marker)? { insert_row(ctx, MESSAGES, &marker, revoked_columns(r)).map_err(|f| f.to_string())?; } } Ok(Value::Null) })?; Ok(count) } /// The marker in place of a message deleted for everyone: drawn as "This message was deleted" where the message /// was, so it keeps who sent it and when, and when it was deleted; nothing it said. fn revoked_columns(r: &Revoked) -> Map { let mut columns = Map::new(); columns.insert("thread_id".into(), json!(r.thread)); columns.insert("direction".into(), r.direction.clone()); columns.insert("sender_participant_id".into(), r.sender.clone()); columns.insert("channel".into(), json!("whatsapp")); columns.insert("origin".into(), json!("imported")); columns.insert("body".into(), json!("")); columns.insert("external_id".into(), json!(r.external)); columns.insert("sent_at".into(), json!(r.sent_at)); columns.insert("deleted_at".into(), json!(r.deleted_at)); columns.insert("meta".into(), json!({ "cotton": { "erased": REVOKED } })); columns }