Cotton code
58 lines · 2.6 KB · RustRaw
//! `google.*` — Google Calendar on the person's own object.//!//! The grant, the cursor and every figure the Connected line draws live in the//! object on the Worker (`apps/business/src/google.ts`, `google_calendar.ts`)://! it keeps the refresh token sealed, pulls the calendar on its own alarm and//! says how each account stands. This family asks it, through the door//! `personal.connect` held ([`ObjectDoor`]), and hands its JSON to Swift as it//! came; it keeps nothing.//!//! - `google.status {}` → `{ available, accounts: [{ googleEmail, connectedAt,//!   calendar: { phase, syncing, syncedAt, lastPassAt, lastError,//!   lastErrorMessage, eventCount, pulled } }] }`//! - `google.connect {}` → `{ authorizeUrl, state }`: the caller opens the URL//!   in the system browser; Google brings the browser back to the Worker,//!   which trades the code and says so on its own page. There is no second call.//! - `google.disconnect { account? }` → `{ ok, account }`: revoked at Google//!   and forgotten; the events pulled stay.//!//! Calendar only: mail comes in through the Mac's importer.use serde_json::{json, Value};use cotton_client::transport::object::routes;use crate::handle::CottonHandle;use crate::methods::MethodError;use crate::phone::personal_object::ObjectDoor;pub const METHODS: [&str; 3] = ["google.connect", "google.disconnect", "google.status"];pub fn call(handle: &CottonHandle, method: &str, request: &Value) -> Result<Value, MethodError> {    let door = handle        .client()        .object_door()        .ok_or_else(|| MethodError::new("not_connected", "this phone is not connected to its database yet"))?;    over(&door, method, request)}/// The same, through a NAMED door: a suite drives one over a scripted exchange.pub fn over(door: &ObjectDoor, method: &str, request: &Value) -> Result<Value, MethodError> {    match method {        "google.status" => door.send("GET", routes::GOOGLE_STATUS, None),        "google.connect" => door.send("GET", routes::GOOGLE_CONNECT, None),        "google.disconnect" => {            // No account is the one connected; with several the object asks which.            let mut body = json!({});            if let Some(account) = request.get("account").and_then(Value::as_str).map(str::trim).filter(|a| !a.is_empty()) {                body["account"] = json!(account);            }            door.send("POST", routes::GOOGLE_DISCONNECT, Some(&body))        }        other => Err(MethodError::new("unknown_method", format!("`{other}` is not a Google method"))),    }}#[cfg(test)]#[path = "google_tests.rs"]mod tests;