Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.
Rust apple-notesapple-reminderscloudkitcrdtdesktop-appicloudicloud-calendaricloud-driveicloud-notesicloud-photosiphonelinuxlinux-desktoplocal-firstoffline-firstprivacyrusttauritauri-appwindows
Notes: a save reads Apple's text exactly as stored and refuses if it cannot (an undecodable note read as 'all deleted'); a body that failed to download keeps the stored one; new notes no longer inherit the template note's formatting; ticks rebuilt from Apple's copy and written exactly, never a stale resend; Delete Now spares a note recovered elsewhere
Yashwanth Kamireddi339f89b379c7
diff --git a/crates/quince-core/src/notes.rs b/crates/quince-core/src/notes.rsindex ecb9fe2..db90516 100644--- a/crates/quince-core/src/notes.rs+++ b/crates/quince-core/src/notes.rs@@ -160,10 +160,17 @@ impl Library {VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13)ON CONFLICT(guid) DO UPDATE SETfolder_guid = excluded.folder_guid, title = excluded.title,- snippet = excluded.snippet, body = excluded.body,+ snippet = excluded.snippet,+ -- A body that did not arrive — a long note's file whose download failed —+ -- is not an empty note: what is stored stays, formatting and all, until+ -- one arrives. A note locked since is the exception: its text goes.+ body = CASE WHEN excluded.locked THEN NULL ELSE COALESCE(excluded.body, body) END,+ runs = CASE WHEN excluded.locked THEN NULL+ WHEN excluded.body IS NULL THEN runs ELSE excluded.runs END,+ editable = CASE WHEN excluded.body IS NULL AND NOT excluded.locked AND body IS NOT NULL+ THEN editable ELSE excluded.editable END,locked = excluded.locked, pinned = excluded.pinned,created_at = excluded.created_at, modified_at = excluded.modified_at,- editable = excluded.editable, runs = excluded.runs,etag = excluded.etag, updated_at = excluded.updated_at",)?;let stamp = now();@@ -472,6 +479,34 @@ mod search_tests {}}+ /// A pass in which a long note's text failed to download says nothing about its text: the+ /// stored body and formatting stay, and it stays editable. Locked since, its text goes.+ #[test]+ fn a_body_that_did_not_arrive_keeps_the_stored_one() {+ let mut lib = library();+ let mut n = note("N", "F", "Long", "the whole long text");+ n.runs = Some("[]".into());+ lib.put_notes(&[n.clone()]).unwrap();+ let mut empty = n.clone();+ empty.body = None;+ empty.runs = None;+ empty.editable = false;+ lib.put_notes(&[empty.clone()]).unwrap();+ assert_eq!(+ lib.note_body("N").unwrap().as_deref(),+ Some("the whole long text")+ );+ assert_eq!(lib.note_runs("N").unwrap().as_deref(), Some("[]"));+ assert!(lib.note("N").unwrap().unwrap().editable);+ empty.locked = true;+ lib.put_notes(&[empty]).unwrap();+ assert_eq!(+ lib.note_body("N").unwrap(),+ None,+ "a locked note keeps no text here"+ );+ }+fn library() -> Library {let mut lib = Library::in_memory().unwrap();lib.put_note_folders(&[diff --git a/crates/quince-icloud/src/ckdocument.rs b/crates/quince-icloud/src/ckdocument.rsindex a0ab5d5..44b2f51 100644--- a/crates/quince-icloud/src/ckdocument.rs+++ b/crates/quince-icloud/src/ckdocument.rs@@ -164,6 +164,18 @@ pub fn text_from_bytes(raw: &[u8]) -> Option<String> {usable_text(&plain)}+/// The document's text exactly as it is stored — what an edit is made to, unlike+/// [`text_from_base64`], which declines text with unusual characters in it. `None` only for+/// a document that is not one.+#[must_use]+pub fn stored_text_from_base64(value: &str) -> Option<String> {+ let raw = B64.decode(value.trim()).ok()?;+ let plain = inflate(&raw)?;+ std::str::from_utf8(field_at(&plain, TEXT_PATH)?)+ .ok()+ .map(ToString::to_string)+}+/// Valid, non-empty UTF-8 with nothing unprintable in it.////// The printability check is what stops a protobuf being mistaken for text: raw wire bytes@@ -1203,7 +1215,9 @@ pub fn without_history(original: &str) -> Option<String> {let key = reader.varint()?;let (field, wire) = (key >> 3, key & 7);reader.skip(wire)?;- if field != PROVENANCE {+ // Its formatting goes too: the borrowed note's first run — a checklist with that+ // note's item id, a link to its URL — became the whole new note's.+ if field != PROVENANCE && field != ATTRIBUTE_RUN {kept.extend_from_slice(note.get(start..reader.at)?);}}diff --git a/crates/quince-icloud/src/cloudkit.rs b/crates/quince-icloud/src/cloudkit.rsindex 80cdde8..b38d3a1 100644--- a/crates/quince-icloud/src/cloudkit.rs+++ b/crates/quince-icloud/src/cloudkit.rs@@ -839,6 +839,30 @@ impl ZoneClient<'_> {record_name: &str,record_type: &str,change: impl Fn(&Value) -> Option<Value>,+ ) -> Result<Option<Value>> {+ self.edit_record_inner(zone, record_name, record_type, change, true)+ .await+ }++ /// [`Self::edit_record_signed`] for records Apple does not sign — notes.+ pub async fn edit_record(+ &self,+ zone: &str,+ record_name: &str,+ record_type: &str,+ change: impl Fn(&Value) -> Option<Value>,+ ) -> Result<Option<Value>> {+ self.edit_record_inner(zone, record_name, record_type, change, false)+ .await+ }++ async fn edit_record_inner(+ &self,+ zone: &str,+ record_name: &str,+ record_type: &str,+ change: impl Fn(&Value) -> Option<Value>,+ signed: bool,) -> Result<Option<Value>> {for _ in 0..5 {let Some(current) = self.lookup_record(zone, record_name).await? else {@@ -850,7 +874,9 @@ impl ZoneClient<'_> {let Some(tag) = current.get("recordChangeTag").and_then(Value::as_str) else {return Err(Error::Conflict);};- stamp_tokens(&mut changes, Some(¤t));+ if signed {+ stamp_tokens(&mut changes, Some(¤t));+ }match self.update_record_exact(zone, record_name, record_type, tag, changes).awaitdiff --git a/crates/quince-icloud/src/notes.rs b/crates/quince-icloud/src/notes.rsindex b2acd8d..a43d55d 100644--- a/crates/quince-icloud/src/notes.rs+++ b/crates/quince-icloud/src/notes.rs@@ -501,7 +501,15 @@ pub async fn edit_rich_cloudkit_from(.ok_or_else(|| {Error::Refused("That note has no body Apple will let us read.".into())})?;- let theirs = crate::ckdocument::text_from_base64(encoded).unwrap_or_default();+ // Apple's text exactly as stored. Read through the display decoder, a note holding a+ // character that decoder declines read as empty — "everything deleted on the phone"+ // — and the merge kept only what was typed here.+ let theirs = crate::ckdocument::stored_text_from_base64(encoded).ok_or_else(|| {+ Error::Refused(+ "Quince couldn't read this note's text as Apple holds it, so nothing was saved."+ .into(),+ )+ })?;let target = match base {Some(base) if base != theirs => merge_edits(base, body, &theirs).ok_or_else(|| {Error::Refused(@@ -664,35 +672,45 @@ pub async fn set_paragraph_cloudkit(expect: Option<&str>,) -> Result<()> {let client = ZoneClient::new(http, ckdatabasews_root, CONTAINER, params);- let current = client- .lookup_record(ZONE, ¬e.guid)- .await?- .ok_or_else(|| Error::Refused("Apple no longer has that note.".into()))?;- let encoded = current- .pointer("/fields/TextDataEncrypted/value")- .and_then(Value::as_str)- .ok_or_else(|| Error::Refused("That note has no body Apple will let us read.".into()))?;- let rewritten = crate::ckdocument::set_paragraph_in_base64(encoded, start, change, expect).ok_or_else(|| {- Error::Refused("That line has changed since it was shown, so nothing was changed. Try again in a moment.".into())- })?;- fits_inline(&rewritten)?;- let tag = current- .get("recordChangeTag")- .and_then(Value::as_str)- .or(note.etag.as_deref());- client- .update_record(- ZONE,- ¬e.guid,- "Note",- tag,- serde_json::json!({+ // Rebuilt from the note as Apple holds it at the moment of writing, and written over that+ // version only: a plain retry resent a document built from an older read over a phone's+ // edit. `expect` is required here — without it, a tick landed on whatever line was at+ // that offset now.+ let Some(expect) = expect else {+ return Err(Error::Refused(+ "Which line was meant is not known, so nothing was changed.".into(),+ ));+ };+ let refused = std::sync::Mutex::new(None::<Error>);+ let written = client+ .edit_record(ZONE, ¬e.guid, "Note", |current| {+ let Some(encoded) = current.pointer("/fields/TextDataEncrypted/value").and_then(Value::as_str) else {+ *refused.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = (Some(Error::Refused("That note has no body Apple will let us read.".into())));+ return None;+ };+ let Some(rewritten) = crate::ckdocument::set_paragraph_in_base64(encoded, start, change, Some(expect)) else {+ *refused.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = (Some(Error::Refused("That line has changed since it was shown, so nothing was changed. Try again in a moment.".into())));+ return None;+ };+ if let Err(e) = fits_inline(&rewritten) {+ *refused.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = (Some(e));+ return None;+ }+ Some(serde_json::json!({"TextDataEncrypted": { "type": "ENCRYPTED_BYTES", "value": rewritten },"ModificationDate": { "type": "TIMESTAMP", "value": now_millis() },- }),- )+ }))+ }).await?;- Ok(())+ match (+ written,+ refused+ .into_inner()+ .unwrap_or_else(std::sync::PoisonError::into_inner),+ ) {+ (None, Some(e)) => Err(e),+ _ => Ok(()),+ }}/// Write a new note, optionally into a folder.@@ -1046,17 +1064,45 @@ pub async fn delete_cloudkit(note: &Note,) -> Result<()> {let client = ZoneClient::new(http, ckdatabasews_root, CONTAINER, params);- let fields = if note.folder_guid.as_deref() == Some(TRASH_FOLDER) {+ if note.folder_guid.as_deref() == Some(TRASH_FOLDER) {// Already in Recently Deleted: this is "Delete Now". Apple marks the record rather- // than removing it, and so does this.- serde_json::json!({- "Deleted": { "type": "INT64", "value": 1 },- "ModificationDate": { "type": "TIMESTAMP", "value": now_millis() },- })- } else {- // Delete means Recently Deleted, where it waits thirty days to be recovered.- filed_in(&client, TRASH_FOLDER)- };+ // than removing it, and so does this — only while Apple still has it there. Recovered+ // on the phone since this window last looked, it is a live note, and deleting it+ // for good would destroy what someone just chose to keep.+ let still_there = std::sync::atomic::AtomicBool::new(true);+ client+ .edit_record(ZONE, ¬e.guid, "Note", |current| {+ let in_trash = current+ .pointer("/fields/Folders/value")+ .and_then(Value::as_array)+ .is_some_and(|f| {+ f.iter().any(|r| {+ r.get("recordName").and_then(Value::as_str) == Some(TRASH_FOLDER)+ })+ })+ || current+ .pointer("/fields/Folder/value/recordName")+ .and_then(Value::as_str)+ == Some(TRASH_FOLDER);+ if !in_trash {+ still_there.store(false, std::sync::atomic::Ordering::Relaxed);+ return None;+ }+ Some(serde_json::json!({+ "Deleted": { "type": "INT64", "value": 1 },+ "ModificationDate": { "type": "TIMESTAMP", "value": now_millis() },+ }))+ })+ .await?;+ if !still_there.load(std::sync::atomic::Ordering::Relaxed) {+ return Err(Error::Refused(+ "This note was recovered on another device, so it was kept.".into(),+ ));+ }+ return Ok(());+ }+ // Delete means Recently Deleted, where it waits thirty days to be recovered.+ let fields = filed_in(&client, TRASH_FOLDER);client.update_record(ZONE, ¬e.guid, "Note", note.etag.as_deref(), fields).await?;@@ -1787,7 +1833,7 @@ mod write_tests {/// Deleting again from Recently Deleted marks it, as the phone does; still no removal.#[tokio::test]async fn deleting_from_recently_deleted_marks_the_record() {- let server = server("x").await;+ let server = in_folder(TRASH_FOLDER).await;let mut trashed = note("Note/1");trashed.folder_guid = Some(TRASH_FOLDER.into());delete_cloudkit(&reqwest::Client::new(), &server.uri(), vec![], &trashed)@@ -1800,6 +1846,47 @@ mod write_tests {assert_eq!(ops[0]["record"]["fields"]["Deleted"]["value"], 1);}+ /// Recovered on the phone since this window looked, the note is live again: Delete Now+ /// writes nothing and says so.+ #[tokio::test]+ async fn delete_now_spares_a_note_recovered_elsewhere() {+ let server = in_folder(DEFAULT_FOLDER).await;+ let mut trashed = note("Note/1");+ trashed.folder_guid = Some(TRASH_FOLDER.into());+ let err = delete_cloudkit(&reqwest::Client::new(), &server.uri(), vec![], &trashed)+ .await+ .unwrap_err();+ assert!(err.to_string().contains("recovered"), "{err}");+ assert!(operations(&server).await.is_empty(), "nothing is written");+ }++ /// A note Apple holds in `folder`.+ async fn in_folder(folder: &str) -> MockServer {+ let server = MockServer::start().await;+ Mock::given(method("POST"))+ .and(path_regex(r".*/records/lookup$"))+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({+ "records": [{+ "recordName": "Note/1", "recordType": "Note", "recordChangeTag": "fresh",+ "fields": {+ "TextDataEncrypted": { "type": "ENCRYPTED_BYTES", "value": document("x") },+ "Folders": { "type": "REFERENCE_LIST", "value": [{ "recordName": folder }] },+ }+ }]+ })))+ .mount(&server)+ .await;+ Mock::given(method("POST"))+ .and(path_regex(r".*/records/modify$"))+ .respond_with(+ ResponseTemplate::new(200)+ .set_body_json(json!({ "records": [{ "recordName": "Note/1" }] })),+ )+ .mount(&server)+ .await;+ server+ }+/// The phone reads `Folders`; writing only `Folder` left a moved note where it was.#[tokio::test]async fn moving_writes_the_folder_the_phone_reads() {