//! Generic writes for a person's canonical addresses. Identity changes use//! Remove and Add so the deterministic handle id never disagrees with its value.use cotton_core::definitions::TableDefinition;use cotton_core::rules::people::handles::{HandleKind, HandleSource,};use cotton_core::records::Data;use cotton_store::protocol::derived_id::person_handle_id;use rusqlite::types::ValueasSqlValue;use serde_json::{Map, Value};usecrate::mcp::dispatch::Ctx;usecrate::mcp::handlers::common::{now_iso, update_row, Valid};usecrate::mcp::handlers::records_1::person_handles::{add_person_handle, PERSON_HANDLES};usecrate::mcp::protocol::ToolFailure;usecrate::mcp::writer::Retract;pub(super) fn create( ctx: &mutCtx<'_>, _definition: &TableDefinition, values: &Data,) -> Valid<String> {if values .keys() .any(|key| !matches!(key.as_str(), "person_id" | "kind" | "value")) {returnErr(ToolFailure::new("Add a handle with its person, kind and value; Cotton sets its other facts", )); }let person = values .get("person_id") .and_then(Value::as_str) .ok_or_else(|| ToolFailure::new("A handle needs a person"))?;let kind = values .get("kind") .and_then(Value::as_str) .and_then(HandleKind::parse) .ok_or_else(|| ToolFailure::new("Choose the kind of handle"))?;let value = values .get("value") .and_then(Value::as_str) .ok_or_else(|| ToolFailure::new("A handle needs a value"))?;let normalised = crate::mcp::handlers::records_1::person_handles::handle_form(ctx, kind, value) .ok_or_else(|| ToolFailure::new(format!("{value:?} is not a {kind} handle")))?;if ctx .db() .get("SELECT id FROM cotton_people WHERE id = ?1 AND workspace_id = ?2", &[SqlValue::Text(person.to_owned()),SqlValue::Text(ctx.workspace_id.to_owned()), ], ) .map_err(super::sql_err)? .is_none() {returnErr(ToolFailure::new("That person is not in this database")); }// add_person_handle is idempotent for the current holder, but a generic// create must not claim a birth its Undo could later remove.let holder = crate::mcp::handlers::records_1::person_handles::handle_holder(ctx, kind, &normalised)?;if holder .as_ref() .is_some_and(|holder| holder.person_id == person) {returnErr(ToolFailure::new("This person already has that handle")); } add_person_handle(ctx, person, kind, value, HandleSource::Manual, None)?; ctx.commit()?;Ok(person_handle_id( ctx.file.namespace(), kind.as_str(), &normalised, ))}pub(super) fn update( ctx: &mutCtx<'_>, _definition: &TableDefinition, id: &str, patch: &Data,) -> Valid<()> {if patch.len() != 1 || !patch.contains_key("kept") {returnErr(ToolFailure::new("Keep confirms a handle; to change its address or person, remove it and add the correct handle")); }let kept = patch["kept"] .as_bool() .ok_or_else(|| ToolFailure::new("kept is true or false"))?;let row = ctx .db() .get("SELECT verified_at FROM cotton_person_handles WHERE id = ?1 AND workspace_id = ?2", &[SqlValue::Text(id.to_owned()),SqlValue::Text(ctx.workspace_id.to_owned()), ], ) .map_err(super::sql_err)? .ok_or_else(|| ToolFailure::new("That handle is no longer here"))?;if row.text("verified_at").is_some() == kept {returnOk(()); }letmut fields = Map::new(); fields.insert("verified_at".into(),if kept {Value::from(now_iso(ctx)) } else {Value::Null }, ); fields.insert("updated_at".into(), Value::from(now_iso(ctx))); update_row(ctx, PERSON_HANDLES, id, fields)?; ctx.commit()}pub(super) fn remove(ctx: &mutCtx<'_>, _definition: &TableDefinition, id: &str) -> Valid<()> { ctx.writer()?.retract(PERSON_HANDLES, id, Retract::Relation); ctx.commit()}