AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
style(desktop): format notebook appearance sidecar code
tianma-if9effb3e7c9d3
diff --git a/crates/desktop-sidecar/src/catalog.rs b/crates/desktop-sidecar/src/catalog.rsindex 847e7b40..176b73fa 100644--- a/crates/desktop-sidecar/src/catalog.rs+++ b/crates/desktop-sidecar/src/catalog.rs@@ -79,7 +79,9 @@ pub(crate) fn create_notebook(database: &Connection, params: &Value) -> Result<Vpub(crate) fn update_notebook(database: &Connection, params: &Value) -> Result<Value, String> {let notebook_id = string_param(params, "notebookId")?;// Keep the notebook and its queued sync operation atomic, including rejected payloads.- let transaction = database.unchecked_transaction().map_err(|e| e.to_string())?;+ let transaction = database+ .unchecked_transaction()+ .map_err(|e| e.to_string())?;let database = &transaction;if let Some(name) = params.get("name").and_then(Value::as_str) {@@ -103,19 +105,36 @@ pub(crate) fn update_notebook(database: &Connection, params: &Value) -> Result<Vif let Some(sort_order) = params.get("sortOrder").and_then(Value::as_i64) {database.execute("UPDATE notebooks SET sort_order = ?1, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE id = ?2 AND is_deleted = 0", rusqlite::params![sort_order, notebook_id]).map_err(|e| e.to_string())?;}- for (key, column) in [("icon", "icon"), ("color", "color"), ("textColor", "text_color"), ("colorTarget", "color_target")] {+ for (key, column) in [+ ("icon", "icon"),+ ("color", "color"),+ ("textColor", "text_color"),+ ("colorTarget", "color_target"),+ ] {if let Some(value) = params.get(key) {let text = value.as_str();if !value.is_null() && text.is_none() {return Err(format!("Invalid notebook {key}"));}- if matches!(key, "color" | "textColor") && text.is_some_and(|color|- color.len() != 7 || !color.starts_with('#') || !color[1..].bytes().all(|c| c.is_ascii_hexdigit())) {+ if matches!(key, "color" | "textColor")+ && text.is_some_and(|color| {+ color.len() != 7+ || !color.starts_with('#')+ || !color[1..].bytes().all(|c| c.is_ascii_hexdigit())+ })+ {return Err(format!("Invalid notebook {key}"));}- if key == "icon" && text.is_some_and(|icon|- icon.is_empty() || icon.len() > 80 || !icon.as_bytes()[0].is_ascii_lowercase()- || !icon.bytes().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == b'-')) {+ if key == "icon"+ && text.is_some_and(|icon| {+ icon.is_empty()+ || icon.len() > 80+ || !icon.as_bytes()[0].is_ascii_lowercase()+ || !icon+ .bytes()+ .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == b'-')+ })+ {return Err("Invalid notebook icon".to_owned());}if key == "colorTarget" && !matches!(text, Some("icon" | "name" | "both")) {@@ -128,15 +147,21 @@ pub(crate) fn update_notebook(database: &Connection, params: &Value) -> Result<V}}if let Some(value) = params.get("isBold") {- let bold = value.as_bool().ok_or_else(|| "Invalid notebook bold value".to_owned())?;+ let bold = value+ .as_bool()+ .ok_or_else(|| "Invalid notebook bold value".to_owned())?;database.execute("UPDATE notebooks SET is_bold = ?1, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE id = ?2 AND is_deleted = 0", rusqlite::params![i64::from(bold), notebook_id]).map_err(|e| e.to_string())?;}if let Some(value) = params.get("iconInverted") {- let inverted = value.as_bool().ok_or_else(|| "Invalid notebook inverted icon value".to_owned())?;+ let inverted = value+ .as_bool()+ .ok_or_else(|| "Invalid notebook inverted icon value".to_owned())?;database.execute("UPDATE notebooks SET icon_inverted = ?1, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE id = ?2 AND is_deleted = 0", rusqlite::params![i64::from(inverted), notebook_id]).map_err(|e| e.to_string())?;}if let Some(value) = params.get("rowInverted") {- let inverted = value.as_bool().ok_or_else(|| "Invalid notebook inverted icon value".to_owned())?;+ let inverted = value+ .as_bool()+ .ok_or_else(|| "Invalid notebook inverted icon value".to_owned())?;database.execute("UPDATE notebooks SET row_inverted = ?1, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE id = ?2 AND is_deleted = 0", rusqlite::params![i64::from(inverted), notebook_id]).map_err(|e| e.to_string())?;}let notebook = notebook_value(database, ¬ebook_id, true)?;@@ -545,7 +570,8 @@ mod appearance_tests {let id = created["id"].as_str().unwrap();let bold = update_notebook(&database, &json!({"notebookId": id, "isBold": true, "iconInverted": true, "rowInverted": true, "icon": "house", "color": "#2563eb", "textColor": "#dc2626"})).unwrap();assert_eq!(bold["isBold"], true);- let renamed = update_notebook(&database, &json!({"notebookId": id, "name": "Network"})).unwrap();+ let renamed =+ update_notebook(&database, &json!({"notebookId": id, "name": "Network"})).unwrap();assert_eq!(renamed["isBold"], true);assert_eq!(renamed["iconInverted"], true);assert_eq!(renamed["rowInverted"], true);@@ -553,40 +579,83 @@ mod appearance_tests {assert_eq!(renamed["color"], "#2563eb");assert_eq!(renamed["textColor"], "#dc2626");let before = notebook_value(&database, id, false).unwrap();- let queued: i64 = database.query_row("SELECT COUNT(*) FROM _edgeever_sidecar_outbox", [], |row| row.get(0)).unwrap();- for invalid in [json!({"rowInverted": "yes"}), json!({"color": "red"}), json!({"textColor": 5}), json!({"icon": "../house"})] {+ let queued: i64 = database+ .query_row("SELECT COUNT(*) FROM _edgeever_sidecar_outbox", [], |row| {+ row.get(0)+ })+ .unwrap();+ for invalid in [+ json!({"rowInverted": "yes"}),+ json!({"color": "red"}),+ json!({"textColor": 5}),+ json!({"icon": "../house"}),+ ] {let mut params = invalid;params["notebookId"] = json!(id);params["name"] = json!("Must roll back");assert!(update_notebook(&database, ¶ms).is_err());assert_eq!(notebook_value(&database, id, false).unwrap(), before);- assert_eq!(database.query_row("SELECT COUNT(*) FROM _edgeever_sidecar_outbox", [], |row| row.get::<_, i64>(0)).unwrap(), queued);+ assert_eq!(+ database+ .query_row("SELECT COUNT(*) FROM _edgeever_sidecar_outbox", [], |row| {+ row.get::<_, i64>(0)+ })+ .unwrap(),+ queued+ );}let payload: String = database.query_row("SELECT payload_json FROM _edgeever_sidecar_outbox WHERE kind = 'notebook.update' ORDER BY id LIMIT 1",[], |row| row.get(0),).unwrap();- assert_eq!(serde_json::from_str::<Value>(&payload).unwrap()["isBold"], true);- let plain = update_notebook(&database, &json!({"notebookId": id, "isBold": false})).unwrap();+ assert_eq!(+ serde_json::from_str::<Value>(&payload).unwrap()["isBold"],+ true+ );+ let plain =+ update_notebook(&database, &json!({"notebookId": id, "isBold": false})).unwrap();assert_eq!(plain["isBold"], false);let mut remote = plain.clone();remote["isBold"] = json!(true);remote["textColor"] = json!("#16a34a");remote["iconInverted"] = json!(false);remote["rowInverted"] = json!(false);- crate::sync::apply_sync_changes(&database, &json!({"changes": [{- "entityType": "notebook", "entityId": id, "operation": "upsert", "notebook": remote,- }]})).unwrap();- assert_eq!(notebook_value(&database, id, false).unwrap()["isBold"], true);- assert_eq!(notebook_value(&database, id, false).unwrap()["iconInverted"], false);- assert_eq!(notebook_value(&database, id, false).unwrap()["rowInverted"], false);- assert_eq!(notebook_value(&database, id, false).unwrap()["textColor"], "#16a34a");+ crate::sync::apply_sync_changes(+ &database,+ &json!({"changes": [{+ "entityType": "notebook", "entityId": id, "operation": "upsert", "notebook": remote,+ }]}),+ )+ .unwrap();+ assert_eq!(+ notebook_value(&database, id, false).unwrap()["isBold"],+ true+ );+ assert_eq!(+ notebook_value(&database, id, false).unwrap()["iconInverted"],+ false+ );+ assert_eq!(+ notebook_value(&database, id, false).unwrap()["rowInverted"],+ false+ );+ assert_eq!(+ notebook_value(&database, id, false).unwrap()["textColor"],+ "#16a34a"+ );remote.as_object_mut().unwrap().remove("isBold");- crate::sync::apply_sync_changes(&database, &json!({"changes": [{- "entityType": "notebook", "entityId": id, "operation": "upsert", "notebook": remote,- }]})).unwrap();- assert_eq!(notebook_value(&database, id, false).unwrap()["isBold"], false);+ crate::sync::apply_sync_changes(+ &database,+ &json!({"changes": [{+ "entityType": "notebook", "entityId": id, "operation": "upsert", "notebook": remote,+ }]}),+ )+ .unwrap();+ assert_eq!(+ notebook_value(&database, id, false).unwrap()["isBold"],+ false+ );drop(database);std::fs::remove_dir_all(root).unwrap();}