use cotton_core::records::Data; use serde_json::Value; use super::Stored; use crate::mcp::dispatch::Ctx; use crate::mcp::handlers::common::Valid; use crate::mcp::protocol::ToolFailure; pub fn read(ctx: &Ctx<'_>, id: Option<&str>) -> Valid> { let board = crate::presentation::board(ctx.db(), ctx.workspace_id, "settings") .map_err(ToolFailure::new)? .ok_or_else(|| ToolFailure::new("the Settings board is missing"))?; let mut rows = project(&board.definition, id, crate::presentation::settings_reach())?; for row in &mut rows { let value = raised_on_me(&row.data).map(|part| me_value(ctx, part)).transpose()?.flatten(); row.data.insert("value".into(), value.map_or(Value::Null, Value::from)); } Ok(rows) } /// The card a row's route raises on the Me page (`/me?raise=`): the /// row's value line is that card's value, so the row holds no copy of it. fn raised_on_me(data: &Data) -> Option<&str> { let route = data.get("route")?.as_str()?; let (path, query) = route.split_once('?')?; if path != "/me" { return None; } query.split('&').find_map(|pair| pair.strip_prefix("raise=")).filter(|part| !part.is_empty()) } /// The owner person's value of one of its columns, as its words; `None` with /// no owner linked, the owner in the bin, or the column empty. fn me_value(ctx: &Ctx<'_>, column: &str) -> Valid> { let owner = ctx .read .driver() .get( "SELECT owner_person_id FROM cotton_workspaces WHERE id = ?", &[rusqlite::types::Value::Text(ctx.workspace_id.to_string())], ) .map_err(|error| ToolFailure::new(error.to_string()))? .and_then(|row| row.text("owner_person_id").map(str::to_string)) .filter(|id| !id.is_empty()); let Some(owner) = owner else { return Ok(None) }; let person = super::super::definition(ctx, "person")?; if person.columns.iter().all(|known| known.key != column) { return Ok(None); } let Some(record) = super::super::load(ctx, &person, &owner)? else { return Ok(None) }; if record.deleted_at.is_some() { return Ok(None); } Ok(record .data .get(column) .and_then(Value::as_str) .map(str::trim) .filter(|words| !words.is_empty()) .map(str::to_string)) } /// The board's Settings rows this app can use, in board order: a row whose /// `needs` the app's manifest does not meet is not listed, and the positions /// count the rows listed. fn project(board: &Value, id: Option<&str>, reach: crate::presentation::SettingsReach) -> Valid> { let rows = board["parts"] .as_array() .and_then(|parts| { parts .iter() .find(|part| part["kind"] == "list" && part["table"] == "settings") }) .and_then(|part| part["rows"].as_array()) .ok_or_else(|| { ToolFailure::new("the Settings board needs catalogue rows on its Settings list") })?; rows.iter() .filter(|row| row["needs"].as_str().is_none_or(|need| reach.meets(need))) .enumerate() .filter(|(_, row)| id.is_none_or(|id| row["id"] == id)) .map(|(position, row)| { let id = row["id"] .as_str() .ok_or_else(|| ToolFailure::new("a Settings row needs an id"))?; let mut data: Data = row["data"] .as_object() .cloned() .ok_or_else(|| ToolFailure::new("a Settings row needs data"))?; if !data .get("name") .and_then(Value::as_str) .is_some_and(|name| !name.trim().is_empty()) || !data .get("route") .and_then(Value::as_str) .is_some_and(|route| route.starts_with('/') && !route.starts_with("//")) { return Err(ToolFailure::new( "a Settings row needs a name and an app route", )); } data.insert("position".into(), Value::from(position as u64)); Ok(Stored { id: id.into(), data, created_at: None, updated_at: None, deleted_at: None, byline: None, }) }) .collect() } #[cfg(test)] #[path = "settings_catalogue_tests.rs"] mod tests;