Cotton code
105 lines · 4.1 KB · RustRaw
//! The mailbox's state (schema v33).//!//! Everything here writes through `email_row_patch` (core's//! `rules::email_state`) — the SAME function the app's own service calls. Two//! writers deciding separately what "archive" means is how a phone and a//! server end up disagreeing about where a message is, and the person watching//! is the one who finds out.//!//! The change lands in the file as `sync_state = 'pending'`; the Gmail push//! loop is what carries it to the mailbox on the next tick. Nothing here talks//! to Google: an MCP call must not wait on somebody's network.use serde_json::{Map, Value};use super::shared::{apply_email_state, target_rows};use super::validate::{validate_update_email, validate_update_emails};use crate::mcp::dispatch::{Ctx, ToolResult};use crate::mcp::protocol::ToolFailure;const NO_SUCH: &str = "no such email or email thread in this workspace";pub fn update_email(ctx: &mut Ctx<'_>, args: &Map<String, Value>) -> ToolResult {    let args = validate_update_email(args)?;    let ids: Vec<String> = args.id.iter().cloned().collect();    let thread_ids: Vec<String> = args.thread_id.iter().cloned().collect();    let rows = target_rows(ctx, &ids, &thread_ids)?;    // One answer for "absent" and "not this workspace" — the probe-resistance    // rule every email read already follows.    if rows.is_empty() {        return Err(ToolFailure::new(NO_SUCH));    }    let pushes_queued = rows        .iter()        .filter(|row| row.get("gmail_id").is_some_and(|value| !value.is_null()))        .count() as i64;    let changed = apply_email_state(ctx, &rows, args.patch)?;    ctx.commit()?;    ctx.audit("update_email", &format!("{changed} message(s)"));    let mut out = Map::new();    out.insert("changed".to_string(), Value::from(changed));    out.insert("pushesQueued".to_string(), Value::from(pushes_queued));    Ok(Value::Object(out))}pub fn update_emails(ctx: &mut Ctx<'_>, args: &Map<String, Value>) -> ToolResult {    let args = validate_update_emails(args)?;    // Per-id outcomes: one id the caller got wrong must never fail the batch.    let is_thread = args.ids.is_none();    let targets = args        .ids        .clone()        .or(args.thread_ids.clone())        .unwrap_or_default();    let mut results: Vec<Value> = Vec::with_capacity(targets.len());    let mut ok = 0_i64;    for id in &targets {        let one = vec![id.clone()];        let read = if is_thread {            target_rows(ctx, &[], &one)        } else {            target_rows(ctx, &one, &[])        };        let mut row = Map::new();        row.insert("id".to_string(), Value::from(id.clone()));        match read {            Err(error) => {                row.insert("ok".to_string(), Value::Bool(false));                row.insert(                    "error".to_string(),                    Value::from(error.message().to_string()),                );            }            Ok(rows) if rows.is_empty() => {                row.insert("ok".to_string(), Value::Bool(false));                row.insert("error".to_string(), Value::from(NO_SUCH));            }            Ok(rows) => match apply_email_state(ctx, &rows, args.patch) {                Err(error) => {                    row.insert("ok".to_string(), Value::Bool(false));                    row.insert(                        "error".to_string(),                        Value::from(error.message().to_string()),                    );                }                Ok(changed) => {                    row.insert("ok".to_string(), Value::Bool(true));                    row.insert("changed".to_string(), Value::from(changed));                    ok += 1;                }            },        }        results.push(Value::Object(row));    }    ctx.commit()?;    let total = results.len() as i64;    ctx.audit("update_emails", &format!("{ok}/{total} changed"));    let mut out = Map::new();    out.insert("results".to_string(), Value::Array(results));    out.insert("changed".to_string(), Value::from(ok));    out.insert("failed".to_string(), Value::from(total - ok));    Ok(Value::Object(out))}