//! B3 (owner ruling, ~/decision-browser-session-source.md): Cotton never //! notifies on a new WhatsApp message or status — and that includes an //! assistant that READ one and then reached for `notify_owner`. //! //! No code path turns a chat row into a push (`cotton-core` //! `no_push_and_no_reminder_is_about_a_chat_message`). The one door left was //! an assistant's own choice: read the chats, then ring the owner's phone with //! what it found. So [`dispatch::call_tool`](super::dispatch::call_tool) //! remembers, per connection, when a call answered chat content, and the push //! tools ask [`read_chat_lately`] before they ring: a connection that read //! chats in the last [`QUIET_FOR`] gets a refusal that tells it to say it in //! its reply instead. //! //! In memory, per process: a restart forgets, which is the safe direction for //! everything except this one rule, and the window is long enough that a job //! reading chats and reporting hours later is still held. use std::collections::HashMap; use std::sync::{Mutex, OnceLock}; use chrono::{DateTime, Duration, Utc}; use serde_json::{Map, Value}; /// How long a connection that read chat content may not push. pub const QUIET_FOR: Duration = Duration::hours(24); /// The tools whose whole answer is chat content. pub const CHAT_READ_TOOLS: [&str; 3] = ["list_message_threads", "read_message_thread", "search_messages"]; /// The tables (definition ids and the store's names) that hold chats, /// statuses and posts from WhatsApp, Instagram and the other chat sources. pub const CHAT_TABLES: [&str; 10] = [ "message", "thread", "thread_member", "post", "story", "cotton_msg_messages", "cotton_msg_threads", "cotton_msg_thread_members", "cotton_msg_posts", "cotton_stories", ]; /// The hit kinds a mixed answer (`search_records`, `brief`, …) marks chat with. pub const CHAT_KINDS: [&str; 4] = ["message", "thread", "story", "post"]; /// The words a refused push answers with. pub const REFUSAL: &str = "Cotton never notifies the owner about chat messages, statuses or posts, and this \ connection read some in the last day — say what you wanted to say in your reply instead. Do not retry."; fn seen() -> &'static Mutex>> { static SEEN: OnceLock>>> = OnceLock::new(); SEEN.get_or_init(Mutex::default) } /// Did this call answer chat content? Its name, the table it named, or a hit /// in its answer of a chat kind. pub fn reads_chat(name: &str, args: &Map, answer: &Value) -> bool { if CHAT_READ_TOOLS.contains(&name) { return true; } let names_chat = |key: &str| args.get(key).and_then(Value::as_str).is_some_and(|table| CHAT_TABLES.contains(&table)); names_chat("table") || names_chat("kind") || holds_chat_kind(answer) } fn holds_chat_kind(value: &Value) -> bool { match value { Value::Object(fields) => { fields.get("kind").and_then(Value::as_str).is_some_and(|kind| CHAT_KINDS.contains(&kind)) || fields.values().any(holds_chat_kind) } Value::Array(items) => items.iter().any(holds_chat_kind), _ => false, } } /// Remember that this connection read chat content at `at`. pub fn note_read(workspace_id: &str, token_id: &str, at: DateTime) { let mut seen = seen().lock().unwrap_or_else(|poisoned| poisoned.into_inner()); seen.retain(|_, when| at - *when < QUIET_FOR); seen.insert((workspace_id.to_string(), token_id.to_string()), at); } /// Has this connection read chat content within [`QUIET_FOR`] of `now`? pub fn read_chat_lately(workspace_id: &str, token_id: &str, now: DateTime) -> bool { let seen = seen().lock().unwrap_or_else(|poisoned| poisoned.into_inner()); seen.get(&(workspace_id.to_string(), token_id.to_string())).is_some_and(|when| now - *when < QUIET_FOR) } #[cfg(test)] #[path = "chat_reads_tests.rs"] mod tests;