//! The generic verbs' writers for two stored built-ins whose own tools live //! outside `records_1`: a session's name and project, and a password's labels. (An order's and a product's writers went with the Stores //! tool, 11 Oct 2026; the file keeps its name.) //! Each is read by [`super::builtin`]'s stored-column read, so the id a page //! holds is the row's own uuid — the id every tool below takes. //! //! - **`session`** — `move_session` for `project_id` (null takes it out of any //! project), then `rename_session` for `title`. //! - **`secret`** — `update_secret`, the labels only; a delete moves it to //! the bin and its Undo takes it back out (`secrets::set_trashed`). No call //! here ever carries `value` or `fields`, so nothing here reaches the vault. use cotton_core::definitions::TableDefinition; use cotton_core::records::Data; use serde_json::{json, Map, Value}; use super::builtin::{self, cannot_clear, set_by_cotton}; use crate::mcp::dispatch::Ctx; use crate::mcp::handlers::common::Valid; use crate::mcp::handlers::owner::work; use crate::mcp::handlers::secrets; use crate::mcp::protocol::ToolFailure; /// `rename_session`'s own cap — asked before the move writes, so a name too /// long does not leave the session filed elsewhere under its old one. const SESSION_TITLE_MAX: usize = 140; // ── session ───────────────────────────────────────────────────────────────── /// `update_record` on a session: the project through `move_session`, then /// the owner's own name through `rename_session`. The move goes first because /// its refusals (a project's own conversation, a project that is not there) /// need the store; the rename's one refusal is asked here, before either. pub(super) fn session_update( ctx: &mut Ctx<'_>, definition: &TableDefinition, id: &str, patch: &Data, ) -> Valid<()> { let mut title = None; let mut project = None; for (key, value) in patch { match key.as_str() { "title" => { let words = value .as_str() .map(|title| title.split_whitespace().collect::>().join(" ")) .filter(|title| !title.is_empty()) .ok_or_else(|| cannot_clear(definition, key))?; if words.chars().count() > SESSION_TITLE_MAX { return Err(ToolFailure::new(format!( "a session's title is at most {SESSION_TITLE_MAX} characters" ))); } title = Some(words); } // Null takes the session out of any project. "project_id" => project = Some(value.clone()), other => return Err(set_by_cotton(definition, other)), } } if let Some(project) = project { builtin::call( ctx, definition, "move_session", work::move_session, json!({ "id": id, "projectId": project }), )?; } if let Some(title) = title { builtin::call( ctx, definition, "rename_session", work::rename_session, json!({ "id": id, "title": title }), )?; } Ok(()) } // ── secret ────────────────────────────────────────────────────────────────── /// `update_record` on a password: what it is called and where it belongs, /// through `update_secret`. The tool reads a blank as "unchanged", so a null /// or a blank is refused here rather than answered as a change that moved /// nothing. pub(super) fn secret_update( ctx: &mut Ctx<'_>, definition: &TableDefinition, id: &str, patch: &Data, ) -> Valid<()> { let mut args = Map::new(); args.insert("id".into(), Value::from(id)); for (key, value) in patch { let words = value.as_str().map(str::trim).filter(|words| !words.is_empty()); match (key.as_str(), words) { ("name" | "username" | "site" | "service", Some(words)) => { args.insert(key.clone(), Value::from(words)); } ("expires_at", Some(words)) => { args.insert("expiresAt".into(), Value::from(words)); } ("name" | "username" | "site" | "service" | "expires_at", None) => { return Err(cannot_clear(definition, key)) } (other, _) => return Err(set_by_cotton(definition, other)), } } builtin::call( ctx, definition, "update_secret", secrets::update_secret, Value::Object(args), ) .map(|_| ()) } /// `delete_record` on a password moves it to the bin, as the Passwords /// screen's "Move to bin" does, so its Undo can take it back out. pub(super) fn secret_trash(ctx: &mut Ctx<'_>, _definition: &TableDefinition, id: &str) -> Valid<()> { secrets::set_trashed(ctx, id, true)?; ctx.commit() } /// The Undo of [`secret_trash`]: out of the bin through the same write. pub(super) fn secret_untrash(ctx: &mut Ctx<'_>, _definition: &TableDefinition, id: &str) -> Valid<()> { secrets::set_trashed(ctx, id, false)?; ctx.commit() }