//! What this device keeps of each picture in the library (`media`): Cotton//! Photos' On this phone (owner, 12 Nov 2026).//!//! A picture is kept here when its own bytes — the photo's, or the film's//! (never only its poster) — sit in this device's media store: everything a//! phone not signed in has taken in, and what a signed-in phone has not yet//! let go of after it drained to the object. `on_phone` is this device's//! answer, never a row of the file (`DEVICE_ANSWERED_COLUMNS`): a record//! answer is told it ([`answer_on_phone`]), and a list read by it reads as//! the prints this device keeps ([`narrow_where`]).use std::collections::BTreeMap;use rusqlite::types::ValueasSql;use serde_json::{json, Map, Value};usecrate::methods::MethodError;usecrate::shell::client::ClientController;/// The library: one row per photo or film (`media.json`).pubconst MEDIA_TABLE: &str = "media";/// The column the library's print is read by: a photo's key, a film's poster.pubconst PRINT_COLUMN: &str = "key";/// What a Kept view matches when this device keeps nothing: no picture's/// print is ever this, since every one is a stored key.pubconst NOTHING_KEPT: &str = "about:nothing-kept";pubusesuper::films::{KEPT, ON_PHONE};pubconst ABSENT: &str = "absent";/// Every picture whose own bytes this device holds, by its id, with the print/// the library reads it by. A ledger row whose file has gone from the disk/// keeps nothing.fn kept_in(driver: &cotton_store::driver::Driver, workspace_id: &str) -> Result<BTreeMap<String, Option<String>>, String> {let workspace = Sql::Text(workspace_id.to_string());let rows = driver .all("SELECT p.id AS id, p.key AS print, m.local_path AS path FROM cotton_photos p \ JOIN _media m ON m.key = p.key \ WHERE p.workspace_id = ? AND p.trashed_at IS NULL AND m.local_path IS NOT NULL \ UNION ALL \ SELECT v.id, v.poster_key, m.local_path FROM cotton_videos v \ JOIN _media m ON m.key = v.key \ WHERE v.workspace_id = ? AND v.trashed_at IS NULL AND m.local_path IS NOT NULL", &[workspace.clone(), workspace], ) .map_err(|error| error.to_string())?;Ok(rows .iter() .filter(|row| row.text("path").is_some_and(|path| std::path::Path::new(path).exists())) .filter_map(|row| Some((row.text("id")?.to_string(), row.text("print").map(str::to_string)))) .collect())}fn kept(client: &ClientController) -> Result<BTreeMap<String, Option<String>>, MethodError> { client.with_file(|file| kept_in(file.driver, file.workspace_id)).map_err(|reason| MethodError::new("unreadable", reason))}/// Fill `on_phone` on every picture of a library answer (a list's records, or/// one record): `kept` when this device holds its bytes, else `absent`.pub(crate) fn answer_on_phone(client: &ClientController, answer: &mutValue) -> Result<(), MethodError> {let records: Vec<&mutValue> = match answer.get_mut("records").and_then(Value::as_array_mut) {Some(records) => records.iter_mut().collect(),None => vec![answer], };letmut held: Option<BTreeMap<String, Option<String>>> = None;for record in records {letSome(id) = record.get("id").and_then(Value::as_str).map(str::to_string) else { continue };letSome(data) = record.get_mut("data").and_then(Value::as_object_mut) else { continue };if held.is_none() { held = Some(kept(client)?); }let here = held.as_ref().is_some_and(|held| held.contains_key(&id)); data.insert(ON_PHONE.to_string(), Value::from(if here { KEPT } else { ABSENT })); }Ok(())}/// A library list read by `where.on_phone`: `kept` reads as the prints of the/// pictures this device holds, `absent` as every other. Other tables, and a/// read that names no `on_phone`, pass unchanged.pub(crate) fn narrow_where(client: &ClientController, args: &mutMap<String, Value>) -> Result<(), MethodError> { narrow(args, || kept(client))}/// [`narrow_where`] inside a read already open on the file (a tool's view/// counts): the same rule over that read's own driver.pub(crate) fn narrow_where_in(driver: &cotton_store::driver::Driver, workspace_id: &str, args: &mutMap<String, Value>) -> Result<(), MethodError> { narrow(args, || kept_in(driver, workspace_id).map_err(|reason| MethodError::new("unreadable", reason)))}fn narrow(args: &mutMap<String, Value>, kept: implFnOnce() -> Result<BTreeMap<String, Option<String>>, MethodError>) -> Result<(), MethodError> {if args.get("table").and_then(Value::as_str) != Some(MEDIA_TABLE) {returnOk(()); }letSome(conditions) = args.get_mut("where").and_then(Value::as_object_mut) else { returnOk(()) };letSome(wanted) = conditions.remove(ON_PHONE) else { returnOk(()) };let bad = || MethodError::new("bad_request", "a picture's on_phone reads kept or absent");let state = match &wanted {Value::String(state) => state.clone(),Value::Array(states) if states.len() == 1 => states[0].as_str().ok_or_else(bad)?.to_string(), _ => returnErr(bad()), };if conditions.contains_key(PRINT_COLUMN) {returnErr(MethodError::new("bad_request", "a list reads on_phone or key, not both")); }let prints: Vec<Value> = kept()?.into_values().flatten().map(Value::from).collect();let condition = match state.as_str() { KEPT => Value::Array(if prints.is_empty() { vec![Value::from(NOTHING_KEPT)] } else { prints }), ABSENT if prints.is_empty() => json!({ "present": true }), ABSENT => json!({ "not": prints }), _ => returnErr(bad()), }; conditions.insert(PRINT_COLUMN.to_string(), condition);Ok(())}#[cfg(test)]#[path = "pictures_here_tests.rs"]mod tests;