//! `developer.inventSenders` — COTTON DEV ONLY: invented WhatsApp senders, as the Mac's link lands them, for a walk of //! People (SENDERS.md) on a phone with no Mac. SMS lands through `chat.sms.land` and mail through //! `developer.inventMail`, their own paths. The real Cotton refuses it by name. //! //! ```text //! developer.inventSenders {} → { threads: […], messages: } //! ``` //! //! Every name and number invented: //! - "Promo Deals" (+1 555 010 7301), nine messages, never answered; //! - Asha, three messages and two answers of his. //! //! The WhatsApp chats read as linked (a message's `meta.source` names the linked device). Run again, it writes no row //! twice (each row's id is fixed). use serde_json::{json, Map, Value}; use super::cotton_chat::write; use crate::handle::CottonHandle; use crate::methods::MethodError; use cotton_tools::mcp::handlers::common::insert_row; const LINK_ACCOUNT: &str = "15550100900@s.whatsapp.net"; pub fn invent(handle: &CottonHandle, _request: &Value) -> Result { if handle.flavor() != cotton_core::flavor::Flavor::Dev { return Err(MethodError::new("not_dev", "developer.inventSenders answers only in Cotton Dev")); } let client = handle.client(); if client.object_door().is_none() { return Err(MethodError::new("sign_in_first", "invented senders are a signed-in phone's")); } let now = chrono::Utc::now(); let at = move |minutes_ago: i64| super::cotton_chat::iso((now - chrono::Duration::minutes(minutes_ago)).timestamp_millis()); // (thread, channel, contact (id, number, name) or a title, [(id suffix, out, words, minutes ago)]) type Said = (&'static str, bool, &'static str, i64); let promo: Vec = (1..=9).map(|i| (["p1", "p2", "p3", "p4", "p5", "p6", "p7", "p8", "p9"][i - 1], false, "Big sale today only, 70% off", 600 - i as i64 * 50)).collect(); let chats: Vec<(&str, &str, Option<(&str, &str, &str)>, &str, Vec)> = vec![ ("11111111-1111-4111-8111-000000005e01", "whatsapp", Some(("dev-se-contact-promo", "15550107301", "Promo Deals")), "", promo), ("11111111-1111-4111-8111-000000005e03", "whatsapp", Some(("dev-se-contact-asha", "15550107302", "Asha Invented")), "", vec![("a1", false, "Are we on for Sunday?", 300), ("a2", true, "Yes, at ten", 290), ("a3", false, "Bring the map", 200), ("a4", true, "Will do", 190), ("a5", false, "See you", 100)]), ]; let mut threads = Vec::new(); let mut count = 0usize; write(&client, "developer.inventSenders", |ctx| { let exists = |ctx: &mut cotton_tools::mcp::dispatch::Ctx<'_>, table: &str, id: &str| -> Result { Ok(super::cotton_chat::one(ctx, &format!("SELECT id FROM {table} WHERE workspace_id = ? AND id = ?"), &[super::cotton_chat::ws(ctx), rusqlite::types::Value::Text(id.to_string())])?.is_some()) }; let mut put = |ctx: &mut cotton_tools::mcp::dispatch::Ctx<'_>, table: &str, id: &str, columns: Value| -> Result<(), String> { if exists(ctx, table, id)? { return Ok(()); } insert_row(ctx, table, id, columns.as_object().cloned().unwrap_or_else(Map::new)).map_err(|f| f.to_string()) }; threads.clear(); count = 0; for (thread, channel, contact, title, said) in &chats { if let Some((cid, number, name)) = contact { put(ctx, "cotton_msg_contacts", cid, json!({ "phone_e164": number, "display_name": name }))?; } let chat = contact.map(|(_, number, _)| format!("{number}@s.whatsapp.net")).unwrap_or_else(|| title.to_string()); let newest = said.iter().map(|s| s.3).min().unwrap_or(0); put(ctx, "cotton_msg_threads", thread, json!({ "kind": "dm", "external_key": chat.clone(), "contact_id": contact.map(|c| c.0), "title": (!title.is_empty()).then_some(*title), "last_message_at": at(newest), "read_seq": 0 }))?; for (i, (suffix, out, words, ago)) in said.iter().enumerate() { let mut m = json!({ "thread_id": thread, "direction": if *out { "out" } else { "in" }, "channel": channel, "origin": "imported", "body": words, "sent_at": at(*ago), "seq": i as i64 + 1, "external_id": format!("DEVSE{}", suffix.to_uppercase()) }); if *channel == "whatsapp" { m["meta"] = json!({ "source": { "service": "WhatsApp linked device", "account": LINK_ACCOUNT, "chat": chat } }); } put(ctx, "cotton_msg_messages", &format!("dev-se-m-{suffix}"), m)?; count += 1; } threads.push(thread.to_string()); } Ok(Value::Null) })?; Ok(json!({ "threads": threads, "messages": count })) }