//! `media.describe` — the owner tapped Describe on a photo or a video. //! //! One signed call to the serving Mac (`cotton-server` `routes/media.rs`, //! `POST /v1/media/{id}/describe`), which holds the model key and the bytes, //! asks for one sentence and writes it onto the row; this device then pulls //! once so the page it draws shows the sentence. A refusal is the Mac's own //! code and sentence. There is no describe job: a picture is described only //! when somebody asks (owner, 14 Oct 2026). //! //! It runs only as the picture's command (`COMMAND_METHODS`): the picture is //! the row the owner has open, bound by the build's own copy of the definition. use serde_json::{json, Value}; use crate::handle::CottonHandle; use crate::methods::MethodError; use crate::phone::owner; pub const METHODS: [&str; 2] = ["media.describe", "media.share"]; /// The path one call posts to, or `None` for a request that names no picture. pub fn path_for(request: &Value) -> Option { let id = request.get("id").and_then(Value::as_str).map(str::trim).filter(|id| !id.is_empty())?; Some(format!("/v1/media/{}/describe", owner::segment(id))) } pub fn call(handle: &CottonHandle, method: &str, request: &Value) -> Result { if method == "media.share" { if let Some(document) = request.get("document").and_then(Value::as_str).filter(|id| !id.is_empty()) { return share_document(handle, document); } let answer = crate::phone::media::ensure(handle, request)?; let path = answer.get("path").and_then(Value::as_str) .filter(|path| !path.is_empty()) .ok_or_else(|| MethodError::new("not_found", "The picture is not on this phone."))?; return Ok(json!({"platform_action": "share_file", "path": path})); } let path = path_for(request) .ok_or_else(|| MethodError::new("bad_request", format!("`{method}` needs the picture's id")))?; let answer = owner::with_fetch(handle, |fetch| owner::send(fetch, owner::post(path, &json!({}))))?; // The Mac wrote the sentence: one pull, best effort, so the page shows it. let _ = crate::methods::sync::now(handle, &Value::Null); Ok(answer) } /// The table a written document is (Podcasts' Export OPML), and the columns /// its share reads: its file, and the name the object gave it. const DOCUMENT_TABLE: &str = "document"; const DOCUMENT_FILE: &str = "storage_path"; const DOCUMENT_NAME: &str = "original_name"; /// `media.share {document}` — a tool act answered `{document, said}`: the /// document the object wrote comes by sync, so until its row is here the /// answer is `{arrived: false}` and the shell asks again when data moves; /// once it is, its file is fetched as a picture's Share fetches one and /// answered `{platform_action: share_file, path, name}`, `name` the one the /// object gave it (never its key's), for the share sheet to hand on. fn share_document(handle: &CottonHandle, id: &str) -> Result { let got = match crate::methods::dispatch(handle, "records.get", &json!({"table": DOCUMENT_TABLE, "id": id})) { Ok(got) => got, Err(error) if crate::phone::copy_standing::absent(&error.code) => return Ok(json!({"arrived": false})), Err(error) => return Err(error), }; let data = got["data"].as_object().or_else(|| got["record"]["data"].as_object()).cloned().unwrap_or_default(); let key = match data.get(DOCUMENT_FILE) { Some(Value::String(key)) if !key.is_empty() => key.clone(), Some(Value::Object(file)) => file.get("ref").and_then(Value::as_str).filter(|key| !key.is_empty()).map(str::to_string) .ok_or_else(|| MethodError::new("not_found", "The document has no file."))?, _ => return Err(MethodError::new("not_found", "The document has no file.")), }; let answer = crate::phone::media::ensure(handle, &json!({"key": key}))?; let path = answer.get("path").and_then(Value::as_str).filter(|path| !path.is_empty()) .ok_or_else(|| MethodError::new("not_found", "The document is not on this phone."))?; let name = data.get(DOCUMENT_NAME).and_then(Value::as_str).map(str::trim).filter(|name| !name.is_empty()); Ok(json!({"platform_action": "share_file", "path": path, "name": name})) } #[cfg(test)] mod tests { use super::*; /// RULE: Describe is one signed POST naming the picture, and a request that names none dials nothing. #[test] fn describe_is_one_post_naming_the_picture() { assert_eq!(path_for(&json!({"id": "photo:ab cd"})).as_deref(), Some("/v1/media/photo%3Aab%20cd/describe")); assert!(path_for(&json!({"id": " "})).is_none()); assert!(path_for(&json!({})).is_none()); } }