Cotton code
57 lines · 1.9 KB · RustRaw
//! WhatsApp from the phone. The linked numbers and a thread's history go//! through the owner routes of `cotton-server/src/routes/whatsapp.rs`, one//! method each.//!//! Sending a text is Comms' (`chat.act send` in a WhatsApp chat: an outbox row//! the Mac drains, `phone/chat_whatsapp.rs`), never a method here.use serde_json::{json, Value};use cotton_client::transport::{ClientRequest, ServerFetch};use crate::handle::CottonHandle;use crate::methods::MethodError;use crate::phone::owner;pub const METHODS: [&str; 3] = [    "whatsapp.accounts",    "whatsapp.history",    "whatsapp.unlink",];pub fn call(handle: &CottonHandle, method: &str, request: &Value) -> Result<Value, MethodError> {    owner::with_fetch(handle, |fetch| over(fetch, method, request))}pub fn over(fetch: &dyn ServerFetch, method: &str, request: &Value) -> Result<Value, MethodError> {    match method {        "whatsapp.accounts" => owner::send(fetch, ClientRequest::get("/v1/whatsapp/accounts")),        // `{ threadId, count? }` — ask the linked number's phone for what came        // before the oldest message this thread holds from it.        "whatsapp.history" => {            let thread = owner::text(request, "threadId")?;            let mut body = json!({ "threadId": thread });            if let Some(count) = request.get("count").and_then(Value::as_u64) {                body["count"] = json!(count);            }            owner::send(fetch, owner::post("/v1/whatsapp/history".to_string(), &body))        }        "whatsapp.unlink" => {            let account = owner::text(request, "account")?;            owner::send(                fetch,                owner::delete(format!(                    "/v1/whatsapp/accounts/{}",                    owner::segment(&account)                )),            )        }        other => Err(MethodError::new(            "unknown_method",            format!("{other} is not a WhatsApp method"),        )),    }}