//! Core-owned authorization for a generic page's one optional live read. use crate::{handle::CottonHandle, methods::MethodError}; use cotton_core::{definitions::TableDefinition, records::Data}; use cotton_core::records::live::{LiveAnswer, LiveRefusal}; use serde_json::{json, Value}; pub(crate) fn call(handle: &CottonHandle, request: &Value) -> Result { let table = request["table"].as_str().filter(|s| !s.is_empty()) .ok_or_else(|| MethodError::new("bad_request", "records.live needs a table"))?; let id = request["id"].as_str().filter(|s| !s.is_empty()) .ok_or_else(|| MethodError::new("bad_request", "records.live needs a record id"))?; let client = handle.client(); let raw = client.read_rows_snapshot("records.live.definition", &mut |ctx| { let definition = cotton_tools::mcp::handlers::tables::definition(ctx, table).map_err(|e| e.to_string())?; serde_json::to_value(definition).map_err(|e| e.to_string()) }).map_err(|e| MethodError::new("unreadable", e))?; let definition: TableDefinition = serde_json::from_value(raw).map_err(|e| MethodError::new("unreadable", e.to_string()))?; // No internal page or record-get path calls this verb. The stored read is // local, and the one authorized engine invocation happens below. let stored_request = json!({"table":table,"id":id}); let record = match super::settings_devices::records_read(handle, "records.get", &stored_request) { Some(answer) => answer?, None => super::records::call(&client, "records.get", &stored_request)?, }; let data: Data = record["data"].as_object().cloned().unwrap_or_default(); let answer = live(handle, &definition, id, &data).map_err(|e| MethodError::new("live_read_refused", e.to_string()))?; serde_json::to_value(answer).map_err(|e| MethodError::new("unreadable", e.to_string())) } /// RULE: A table that declares a live read is read WITH it wherever its /// records are read for drawing — `records.get` and `records.list`, so the /// cards a deck deals and the record a card opens on carry the live columns /// their card names (This device's build and sync rows), as its page and /// Home's sync card (`device.facts`) do. Read without them, a device card /// opened from Home lost every figure but its platform (Cotton 348's walk). /// A live read that fails leaves the stored columns. pub(crate) fn overlay(handle: &CottonHandle, definition: &TableDefinition, answer: &mut Value) { if definition.live_read.is_none() { return; } let records: Vec<&mut Value> = match answer { Value::Object(object) if object.contains_key("records") => { object.get_mut("records").and_then(Value::as_array_mut).map(|rows| rows.iter_mut().collect()).unwrap_or_default() } one => vec![one], }; for record in records { if record["table"].as_str().is_some_and(|table| table != definition.id) { continue; } let Some(id) = record["id"].as_str().map(str::to_string) else { continue }; let Some(data) = record.get_mut("data").and_then(Value::as_object_mut) else { continue }; if let Ok(answer) = live(handle, definition, &id, data) { data.extend(answer.data); } } } /// The definition's live read of record `id` over its stored columns: the /// one read-only engine method it may name, bound by the core. fn live(handle: &CottonHandle, definition: &TableDefinition, id: &str, stored: &Data) -> Result { // A device still asking to join has no facts of its own to read. let waiting = stored.get("status").and_then(Value::as_str) == Some("waiting"); cotton_core::records::live::read(definition, id, stored, |method, args| { if waiting { return Ok(Data::new()); } let answer = match method { "device.facts" => super::settings_devices::facts(handle, &Value::Object(args.clone())), _ => return Err(format!("{method} is not a read-only live engine method")), }.map_err(|e| e.message)?; answer["data"].as_object().cloned().ok_or_else(|| "The live engine returned no fields".to_string()) }) }