Cotton code
82 lines · 3.5 KB · RustRaw
//! The serving Mac's sender, from its own row: `device.push {deviceId}` reads//! the line its Sync line says (when it first rang the owner, or why it cannot//! yet), and `device.testPush {deviceId}` is the act "Test push".//!//! On the serving Mac both run through the RUNNING server's own deps — a Mac//! holding the file has no connection record, so the signed transport would//! die at `no_server` there (`methods.rs`, the `devices.*` branch). Anywhere//! else they go to the serving Mac over the signed transport, whose routes//! (`cotton-server` `routes/first_push.rs`) are owner-only.use cotton_client::transport::ClientRequest;use serde_json::{json, Value};use crate::handle::CottonHandle;use crate::methods::MethodError;const PUSH_PATH: &str = "/v1/devices/self/push";const TEST_PUSH_PATH: &str = "/v1/devices/self/test-push";const PUSH_ACK_PATH: &str = "/v1/devices/self/push-ack";/// The device the act was pressed on.fn pressed_on(request: &Value, method: &str) -> Result<String, MethodError> {    crate::phone::read_args(request)        .get("deviceId")        .and_then(Value::as_str)        .filter(|id| !id.is_empty())        .map(str::to_string)        .ok_or_else(|| MethodError::new("bad_request", format!("{method} needs deviceId")))}/// `device.push {deviceId}` → `{ deviceId, push: { pushedFirstAt, line } }`.pub fn status(handle: &CottonHandle, request: &Value) -> Result<Value, MethodError> {    let device = pressed_on(request, "device.push")?;    // The serving Mac answers for itself: the route names no device.    let _ = device;    crate::phone::owner::with_fetch(handle, |fetch| {        crate::phone::owner::send(            fetch,            ClientRequest {                method: "GET",                path: PUSH_PATH.to_string(),                body: None,                content_type: None,                accept_encoding: None,                ceiling_ms: None,                range: None,            },        )    })}/// `device.testPush {deviceId}` → `{ deviceId, sent, arrived, receipt, push }`, or/// refused `cannot_push` with the reason nothing was sent.pub fn test_push(handle: &CottonHandle, request: &Value) -> Result<Value, MethodError> {    let device = pressed_on(request, "device.testPush")?;    crate::phone::owner::with_fetch(handle, |fetch| {        crate::phone::owner::send(fetch, crate::phone::owner::post(TEST_PUSH_PATH.to_string(), &json!({ "deviceId": device })))    })}/// `device.pushAck {pushId, receivedAt?, how?}` → `{ recorded }`: this phone/// showed a Cotton push (`pushId` is the payload's `notificationId`), and the/// serving Mac keeps its receipt for THIS signing device — the proof a push/// reached the owner, which Apple's 2xx alone is not (architect, 12 Oct 2026).pub fn push_ack(handle: &CottonHandle, request: &Value) -> Result<Value, MethodError> {    let args = crate::phone::read_args(request);    let push_id = args        .get("pushId")        .and_then(Value::as_str)        .filter(|id| !id.trim().is_empty())        .ok_or_else(|| MethodError::new("bad_request", "device.pushAck needs pushId"))?;    let mut body = json!({ "pushId": push_id });    for field in ["receivedAt", "how"] {        if let Some(value) = args.get(field).and_then(Value::as_str) {            body[field] = json!(value);        }    }    crate::phone::owner::with_fetch(handle, |fetch| {        crate::phone::owner::send(fetch, crate::phone::owner::post(PUSH_ACK_PATH.to_string(), &body))    })}