Skip to content

Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.

Notes: a pin made here shows, and an edited pinned note stays pinned

Yashwanth Kamireddia3f6eb23690c
diff --git a/crates/quince-core/src/notes.rs b/crates/quince-core/src/notes.rs
index 7ab3532..6c7e376 100644
--- a/crates/quince-core/src/notes.rs
+++ b/crates/quince-core/src/notes.rs
@@ -84,6 +84,30 @@ impl Library {
Ok(())
}
+ /// Every stored note's (pinned, locked), by guid — what a pass that carried only part of
+ /// a note's state leaves standing.
+ pub fn note_flags(&self) -> Result<std::collections::HashMap<String, (bool, bool)>> {
+ let mut stmt = self
+ .conn
+ .prepare("SELECT guid, pinned, locked FROM notes")?;
+ let rows = stmt.query_map([], |row| {
+ Ok((
+ row.get::<_, String>(0)?,
+ (row.get::<_, i64>(1)? != 0, row.get::<_, i64>(2)? != 0),
+ ))
+ })?;
+ Ok(rows.collect::<rusqlite::Result<_>>()?)
+ }
+
+ /// Pin or unpin a stored note. A note this library does not hold is left alone.
+ pub fn set_note_pinned(&mut self, guid: &str, pinned: bool) -> Result<()> {
+ self.conn.execute(
+ "UPDATE notes SET pinned = ?2 WHERE guid = ?1",
+ params![guid, i64::from(pinned)],
+ )?;
+ Ok(())
+ }
+
pub fn put_notes(&mut self, notes: &[Note]) -> Result<()> {
let tx = self.conn.transaction()?;
{
diff --git a/crates/quince-icloud/src/notes.rs b/crates/quince-icloud/src/notes.rs
index c0008b0..9ee960a 100644
--- a/crates/quince-icloud/src/notes.rs
+++ b/crates/quince-icloud/src/notes.rs
@@ -231,7 +231,7 @@ pub fn apply_cloudkit(
let mut gone: Vec<String> = Vec::new();
// Deletion and pinning live on the per-user record, keyed by a reference to the note.
let mut deleted_notes: std::collections::HashSet<String> = std::collections::HashSet::new();
- let mut pinned_notes: std::collections::HashSet<String> = std::collections::HashSet::new();
+ let mut pins: std::collections::HashMap<String, bool> = std::collections::HashMap::new();
// Apple names this record type outright, so locking is something we read rather than guess.
let mut locked_notes: std::collections::HashSet<String> = std::collections::HashSet::new();
@@ -288,8 +288,9 @@ pub fn apply_cloudkit(
}
if removed {
deleted_notes.insert(target);
- } else if field::flag(record, "IsPinned") {
- pinned_notes.insert(target);
+ } else {
+ // Pinned or not, as this record says: an unpin is as much news as a pin.
+ pins.insert(target, field::flag(record, "IsPinned"));
}
}
}
@@ -301,10 +302,23 @@ pub fn apply_cloudkit(
// says so. Without this, deleted notes would keep showing.
notes.retain(|n| !deleted_notes.contains(&n.guid));
gone.extend(deleted_notes);
+ // Pinning lives on its own record, and a pass carries only what changed: pinning a note
+ // changes that record and not the note, and editing a note changes the note and not that
+ // record. Read together only when both came in the same pass, a pin made here never showed
+ // and an edited pinned note came back unpinned. So each half stands on its own: a note in
+ // this pass keeps the pin already stored unless this pass says otherwise, and a pin in
+ // this pass reaches its note whether or not the note came too.
+ let stored = if incremental {
+ library.note_flags()?
+ } else {
+ std::collections::HashMap::new()
+ };
for note in &mut notes {
- note.pinned = pinned_notes.contains(&note.guid);
- note.locked = locked_notes.contains(&note.guid);
+ let (was_pinned, was_locked) = stored.get(&note.guid).copied().unwrap_or((false, false));
+ note.pinned = pins.get(&note.guid).copied().unwrap_or(was_pinned);
+ note.locked = locked_notes.contains(&note.guid) || was_locked;
}
+ let in_pass: std::collections::HashSet<String> = notes.iter().map(|n| n.guid.clone()).collect();
// A note may reference a folder that this page did not carry; keeping the reference
// would break the foreign key, so drop it and leave the note unfiled.
@@ -324,6 +338,11 @@ pub fn apply_cloudkit(
library.put_note_folders(&folders)?;
library.put_notes(&notes)?;
+ for (guid, pinned) in &pins {
+ if !in_pass.contains(guid) {
+ library.set_note_pinned(guid, *pinned)?;
+ }
+ }
if !gone.is_empty() {
library.forget_notes(&gone)?;
}
@@ -819,6 +838,81 @@ mod tests {
assert!(library.notes_in(None).unwrap()[0].pinned);
}
+ fn pin_record(pinned: i64) -> Value {
+ json!({ "recordName": "NoteUS/1", "recordType": "Note_UserSpecific",
+ "fields": {
+ "IsPinned": { "type": "INT64", "value": pinned },
+ "Note": { "type": "REFERENCE", "value": { "recordName": "Note/n1" } }
+ }})
+ }
+
+ async fn pass(library: &mut Library, records: Vec<Value>, since: Option<&str>) {
+ let server = serve(json!({ "zones": [{
+ "records": records, "syncToken": "t2", "moreComing": false }] }))
+ .await;
+ sync_cloudkit(
+ &reqwest::Client::new(),
+ &server.uri(),
+ vec![],
+ library,
+ since,
+ )
+ .await
+ .unwrap();
+ }
+
+ /// Pinning here writes the per-user record and not the note, so the pass after it carries
+ /// that record alone. The pin was only ever applied to notes in the same pass, so it was
+ /// dropped and the note never showed as pinned — and unpinning, likewise.
+ #[tokio::test]
+ async fn a_pin_reaches_its_note_without_the_note_in_the_pass() {
+ let mut library = Library::in_memory().unwrap();
+ pass(
+ &mut library,
+ vec![
+ folder_record("Folder/f1", "Notes"),
+ note_record("Note/n1", "Curtains", Some("x"), "Folder/f1"),
+ ],
+ None,
+ )
+ .await;
+ assert!(!library.notes_in(None).unwrap()[0].pinned);
+
+ pass(&mut library, vec![pin_record(1)], Some("t1")).await;
+ assert!(library.notes_in(None).unwrap()[0].pinned, "pinned");
+ pass(&mut library, vec![pin_record(0)], Some("t2")).await;
+ assert!(!library.notes_in(None).unwrap()[0].pinned, "and unpinned");
+ }
+
+ /// And the other way round: editing a pinned note changes the note and not its per-user
+ /// record, and the edit came back unpinned.
+ #[tokio::test]
+ async fn an_edited_pinned_note_stays_pinned() {
+ let mut library = Library::in_memory().unwrap();
+ pass(
+ &mut library,
+ vec![
+ folder_record("Folder/f1", "Notes"),
+ note_record("Note/n1", "Curtains", Some("x"), "Folder/f1"),
+ pin_record(1),
+ ],
+ None,
+ )
+ .await;
+ pass(
+ &mut library,
+ vec![note_record(
+ "Note/n1",
+ "Curtains",
+ Some("x and more"),
+ "Folder/f1",
+ )],
+ Some("t1"),
+ )
+ .await;
+ assert!(library.notes_in(None).unwrap()[0].pinned);
+ }
+
/// Locking is read from Apple's own record type, not inferred from a body that failed
/// to decode — otherwise our parsing failures would reach the user as "locked", a claim
/// we could not stand behind.