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
Reminders: a repeating reminder is ticked by Apple's server, as icloud.com does — CompleteRecurringReminder keeps a completed copy and moves the reminder, still open, to its next date — verified live; a zone busy with another write (409 ZONE_BUSY) is waited out like an overloaded one
Yashwanth Kamireddif370c2ab3275
diff --git a/crates/quince-icloud/src/cloudkit.rs b/crates/quince-icloud/src/cloudkit.rsindex 5940a21..6f27672 100644--- a/crates/quince-icloud/src/cloudkit.rs+++ b/crates/quince-icloud/src/cloudkit.rs@@ -121,7 +121,11 @@ impl<'a> ZoneClient<'a> {.and_then(|v| v.to_str().ok()).and_then(|v| v.trim().parse::<u64>().ok());let text = response.text().await.unwrap_or_default();- if !matches!(status.as_u16(), 429 | 503) {+ // A zone busy with another write answers 409 ZONE_BUSY — "rejected, retry", the+ // whole request not applied — measured when a phone and this app wrote at once.+ // It is a wait like 503's, not a conflict over the record.+ let zone_busy = status.as_u16() == 409 && text.contains("\"ZONE_BUSY\"");+ if !matches!(status.as_u16(), 429 | 503) && !zone_busy {return Ok((status, text));}if tries >= BUSY_TRIES {@@ -470,6 +474,35 @@ mod tests {.unwrap();}+ /// A zone busy with another device's write answers 409 ZONE_BUSY and asks to be asked+ /// again — measured on the account — and asked again, the write goes through.+ #[tokio::test]+ async fn a_busy_zone_is_waited_out() {+ let server = MockServer::start().await;+ Mock::given(method("POST"))+ .and(path_regex(r".*/records/modify$"))+ .respond_with(ResponseTemplate::new(409).set_body_json(json!({+ "uuid": "1", "serverErrorCode": "ZONE_BUSY",+ "reason": "Sync zone CAS Op-Lock failed. There was a concurrent write and this operation was rejected. Retry"+ })))+ .up_to_n_times(1)+ .mount(&server)+ .await;+ Mock::given(method("POST"))+ .and(path_regex(r".*/records/modify$"))+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({+ "records": [{ "recordName": "R", "recordChangeTag": "t2" }]+ })))+ .mount(&server)+ .await;+ let http = reqwest::Client::new();+ let client = ZoneClient::new(&http, &server.uri(), "com.apple.reminders", vec![]);+ client+ .update_record("Reminders", "R", "Reminder", Some("t1"), json!({}))+ .await+ .unwrap();+ }+/// And one still busy after that is said in a sentence, not Apple's JSON.#[tokio::test]async fn a_cloudkit_that_stays_busy_says_so_plainly() {@@ -684,6 +717,37 @@ impl ZoneClient<'_> {}/// Send one operation to `records/modify` and surface what came back.+ /// A `records/query` in the zone: `query` is CloudKit's `{recordType, filterBy}`. For the+ /// server-side operations Apple runs as queries — completing a repeating reminder is one —+ /// as much as for reading. Answers with the records it returns.+ pub async fn records_query(&self, zone: &str, query: Value) -> Result<Vec<Value>> {+ let body = json!({+ "query": query,+ "zoneID": { "zoneName": zone, "zoneType": "REGULAR_CUSTOM_ZONE" },+ });+ let url = format!(+ "{}/database/1/{}/production/private/records/query",+ self.base, self.container+ );+ let (status, text) = self.post_json(&url, &body).await?;+ if status.as_u16() == 450 {+ return Err(Error::ReauthRequired);+ }+ if !status.is_success() {+ return Err(Error::Protocol(format!(+ "CloudKit refused the query ({status}): {}",+ text.chars().take(200).collect::<String>()+ )));+ }+ let parsed: Value = serde_json::from_str(&text)+ .map_err(|e| Error::Protocol(format!("CloudKit returned invalid JSON: {e}")))?;+ Ok(parsed+ .get("records")+ .and_then(Value::as_array)+ .cloned()+ .unwrap_or_default())+ }+async fn modify(&self, zone: &str, operation: Value) -> Result<Value> {let body = json!({"operations": [operation],diff --git a/crates/quince-icloud/src/reminders.rs b/crates/quince-icloud/src/reminders.rsindex 63e117c..2f20757 100644--- a/crates/quince-icloud/src/reminders.rs+++ b/crates/quince-icloud/src/reminders.rs@@ -1728,6 +1728,13 @@ pub async fn delete_list_cloudkit(////// Sends only the fields that change, guarded by the record's change tag, so a reminder/// edited on the phone since we read it is refused rather than overwritten.+///+/// A repeating reminder is ticked as icloud.com ticks one: by asking Apple's server, which+/// runs it as a query of the virtual type `CompleteRecurringReminder`. Measured on a real+/// account, the server then keeps a completed copy of today's occurrence, moves the reminder+/// itself — still open — to its next date, and updates the list. Doing it here instead, by+/// setting `Completed` and a computed date on the one record, matched none of that: no+/// history, and the series itself marked complete.pub async fn set_completed_cloudkit(http: &reqwest::Client,ckdatabasews_root: &str,@@ -1737,26 +1744,51 @@ pub async fn set_completed_cloudkit() -> Result<()> {let client = ZoneClient::new(http, ckdatabasews_root, CONTAINER, params);- // A repeating reminder is already held completed while its record points at the next- // occurrence, so writing Completed again changes nothing and the tick appears to do- // nothing. Ticking one means moving to the occurrence after this one.- let fields = if done && reminder.repeats() {- let Some(next) = reminder.next_occurrence() else {- return Err(Error::Protocol(- "this repeats, but its schedule could not be read".into(),- ));+ if done && reminder.repeats() {+ let current = client+ .lookup_record(ZONE, &reminder.guid)+ .await?+ .ok_or_else(|| Error::Refused("Apple no longer has that reminder.".into()))?;+ let rule = current+ .pointer("/fields/RecurrenceRuleIDs/value")+ .and_then(|v| v.as_array().and_then(|a| a.first()).or(Some(v)))+ .and_then(Value::as_str)+ .filter(|r| !r.is_empty())+ .ok_or_else(|| {+ Error::Refused("This repeats, but Apple did not say on what schedule.".into())+ })?;+ let rule = if rule.starts_with("RecurrenceRule/") {+ rule.to_string()+ } else {+ format!("RecurrenceRule/{rule}")};- serde_json::json!({- "Completed": { "value": 1, "type": "NUMBER_INT64" },- "DueDate": { "value": next.saturating_mul(1000), "type": "TIMESTAMP" },- "LastModifiedDate": { "value": now_millis(), "type": "TIMESTAMP" },- })- } else {- serde_json::json!({- "Completed": { "value": i64::from(done), "type": "NUMBER_INT64" },- "LastModifiedDate": { "value": now_millis(), "type": "TIMESTAMP" },- })- };+ let zone = reminder.timezone.clone().unwrap_or_else(|| {+ jiff::tz::TimeZone::system()+ .iana_name()+ .unwrap_or("UTC")+ .to_string()+ });+ let reference = |name: &str| serde_json::json!({ "value": { "recordName": name, "action": "VALIDATE" }, "type": "REFERENCE" });+ let answered = client+ .records_query(+ ZONE,+ serde_json::json!({+ "recordType": "CompleteRecurringReminder",+ "filterBy": [+ { "comparator": "EQUALS", "fieldName": "Reminder", "fieldValue": reference(&reminder.guid) },+ { "comparator": "EQUALS", "fieldName": "RecurrenceRule", "fieldValue": reference(&rule) },+ { "comparator": "EQUALS", "fieldName": "TimeZone", "fieldValue": { "value": zone, "type": "STRING" } },+ ],+ }),+ )+ .await?;+ if answered.is_empty() {+ return Err(Error::Refused(+ "Apple did not move this reminder on to its next date.".into(),+ ));+ }+ return Ok(());+ }client.update_record_signed(@@ -1764,7 +1796,10 @@ pub async fn set_completed_cloudkit(&reminder.guid,"Reminder",reminder.etag.as_deref(),- fields,+ serde_json::json!({+ "Completed": { "value": i64::from(done), "type": "NUMBER_INT64" },+ "LastModifiedDate": { "value": now_millis(), "type": "TIMESTAMP" },+ }),).await?;Ok(())@@ -2766,50 +2801,71 @@ mod recurring_tick_tests {.clone()}- /// The bug this fixes: Apple already holds a repeating reminder completed while its- /// record points at the next occurrence, so writing Completed again changed nothing and- /// the tick appeared to do nothing at all.+ /// A repeating reminder is ticked by Apple's server, as icloud.com ticks one: the query+ /// names the reminder, its rule and its zone, and nothing is written over the record.#[tokio::test]- async fn ticking_a_daily_reminder_moves_it_to_tomorrow() {- let server = accepting().await;- let due = FUTURE;- set_completed_cloudkit(- &reqwest::Client::new(),- &server.uri(),- vec![],- &repeating(0, 1, due),- true,- )- .await- .unwrap();+ async fn ticking_a_repeating_reminder_asks_apples_server() {+ 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": "Reminder/1", "recordType": "Reminder", "recordChangeTag": "t",+ "fields": { "RecurrenceRuleIDs": { "type": "STRING_LIST", "value": ["RULE-1"] } }+ }] })),+ )+ .mount(&server)+ .await;+ Mock::given(method("POST"))+ .and(path_regex(r".*/records/query$"))+ .respond_with(+ ResponseTemplate::new(200).set_body_json(json!({ "records": [+ { "recordName": "Reminder/1", "recordType": "Reminder" }+ ] })),+ )+ .mount(&server)+ .await;+ let mut daily = repeating(0, 1, FUTURE);+ daily.timezone = Some("Asia/Kolkata".into());+ set_completed_cloudkit(&reqwest::Client::new(), &server.uri(), vec![], &daily, true)+ .await+ .unwrap();- let sent = fields(&server).await;- assert_eq!(sent["Completed"]["value"], 1);+ let requests = server.received_requests().await.unwrap();+ assert!(+ !requests+ .iter()+ .any(|r| r.url.path().ends_with("/records/modify")),+ "nothing written over it"+ );+ let query = requests+ .iter()+ .find(|r| r.url.path().ends_with("/records/query"))+ .expect("asked");+ let body: Value = serde_json::from_slice(&query.body).unwrap();+ assert_eq!(body["query"]["recordType"], "CompleteRecurringReminder");+ let filters = &body["query"]["filterBy"];+ assert_eq!(+ filters[0]["fieldValue"]["value"]["recordName"],+ "Reminder/1"+ );assert_eq!(- sent["DueDate"]["value"],- (due + 86_400) * 1000,- "a daily reminder must advance exactly one day"+ filters[1]["fieldValue"]["value"]["recordName"],+ "RecurrenceRule/RULE-1");+ assert_eq!(filters[2]["fieldValue"]["value"], "Asia/Kolkata");}+ /// Unticking, or ticking a reminder that does not repeat, is the ordinary field write.#[tokio::test]- async fn the_interval_is_respected() {+ async fn ticking_a_one_off_writes_completed() {let server = accepting().await;- let due = FUTURE;- set_completed_cloudkit(- &reqwest::Client::new(),- &server.uri(),- vec![],- &repeating(1, 2, due),- true,- )- .await- .unwrap();- assert_eq!(- fields(&server).await["DueDate"]["value"],- (due + 14 * 86_400) * 1000,- "every two weeks means fourteen days"- );+ let mut once = repeating(0, 1, FUTURE);+ once.recur_frequency = None;+ set_completed_cloudkit(&reqwest::Client::new(), &server.uri(), vec![], &once, true)+ .await+ .unwrap();+ assert_eq!(fields(&server).await["Completed"]["value"], 1);}/// Un-ticking must not move the date; it just reopens the occurrence.diff --git a/probe/examples/reminder_recurring_complete.rs b/probe/examples/reminder_recurring_complete.rsnew file mode 100644index 0000000..41bfed3--- /dev/null+++ b/probe/examples/reminder_recurring_complete.rs@@ -0,0 +1,78 @@+//! Live, throwaway list and reminder only: a daily reminder, completed through Apple's own+//! CompleteRecurringReminder query, read back before and after — then the list deleted.+use anyhow::{anyhow, Result};+use serde_json::Value;+#[tokio::main]+async fn main() -> Result<()> {+ let dir = std::path::PathBuf::from(std::env::var("HOME")?).join(".local/share/quince-probe");+ let mut client = quince_auth::Client::new(&dir)?;+ if !client.resume().await? {+ return Err(anyhow!("no session"));+ }+ let root = client+ .session()+ .service_url("ckdatabasews")+ .ok_or_else(|| anyhow!("no ckdatabasews"))?;+ let (http, p) = (client.http().clone(), client.service_params());+ use quince_icloud::reminders as r;+ let list = r::create_list_cloudkit(&http, &root, p.clone(), "Quince repeat test").await?;+ let guid = r::create_cloudkit(&http, &root, p.clone(), &list, "Daily test").await?;+ let mut rem = quince_core::reminders::Reminder {+ guid: guid.clone(),+ list_guid: list.clone(),+ title: "Daily test".into(),+ ..Default::default()+ };+ let due = 1_792_300_000; // a moment tomorrow+ r::edit_cloudkit(+ &http,+ &root,+ p.clone(),+ &rem,+ &r::ReminderEdit {+ due_at: Some(Some(due)),+ ..Default::default()+ },+ )+ .await?;+ rem.due_at = Some(due);+ r::set_recurrence_cloudkit(+ &http,+ &root,+ p.clone(),+ &rem,+ Some(r::Repeat {+ frequency: 0,+ interval: 1,+ }),+ )+ .await?;+ let zc =+ quince_icloud::cloudkit::ZoneClient::new(&http, &root, "com.apple.reminders", p.clone());+ let show = |v: &Value| {+ format!(+ "Completed={} DueDate={} RecurrenceRuleIDs={}",+ v["fields"]["Completed"]["value"],+ v["fields"]["DueDate"]["value"],+ v["fields"]["RecurrenceRuleIDs"]["value"]+ )+ };+ let before = zc+ .lookup_record("Reminders", &guid)+ .await?+ .ok_or_else(|| anyhow!("gone"))?;+ println!("before: {}", show(&before));+ rem.recur_frequency = Some(0);+ rem.recur_interval = Some(1);+ rem.timezone = Some("Asia/Kolkata".into());+ r::set_completed_cloudkit(&http, &root, p.clone(), &rem, true).await?;+ println!("ticked through Quince's own set_completed_cloudkit");+ let after = zc+ .lookup_record("Reminders", &guid)+ .await?+ .ok_or_else(|| anyhow!("gone"))?;+ println!("after: {}", show(&after));+ r::delete_list_cloudkit(&http, &root, p, &list).await?;+ println!("test list deleted");+ Ok(())+}diff --git a/probe/examples/reminders_cleanup_tests.rs b/probe/examples/reminders_cleanup_tests.rsnew file mode 100644index 0000000..c439117--- /dev/null+++ b/probe/examples/reminders_cleanup_tests.rs@@ -0,0 +1,26 @@+//! Live: delete Reminders lists this probe made and left behind — only lists whose name+//! begins "Quince " and ends "test", nothing else.+use anyhow::{anyhow, Result};+#[tokio::main]+async fn main() -> Result<()> {+ let dir = std::path::PathBuf::from(std::env::var("HOME")?).join(".local/share/quince-probe");+ let mut client = quince_auth::Client::new(&dir)?;+ if !client.resume().await? { return Err(anyhow!("no session")); }+ let root = client.session().service_url("ckdatabasews").ok_or_else(|| anyhow!("no ckdatabasews"))?;+ let (http, p) = (client.http().clone(), client.service_params());+ let zc = quince_icloud::cloudkit::ZoneClient::new(&http, &root, "com.apple.reminders", p.clone());+ let walk = zc.all_records("Reminders", None).await?;+ let mut n = 0;+ for r in walk.records.iter().filter(|r| r["recordType"] == "List") {+ if r["fields"]["Deleted"]["value"].as_i64() == Some(1) { continue; }+ let name = r["fields"]["Name"]["value"].as_str().unwrap_or("");+ if name.starts_with("Quince ") && name.ends_with("test") {+ let guid = r["recordName"].as_str().unwrap_or("");+ quince_icloud::reminders::delete_list_cloudkit(&http, &root, p.clone(), guid).await?;+ println!("deleted leftover list {name:?} ({guid})");+ n += 1;+ }+ }+ println!("{n} leftover test list(s) removed");+ Ok(())+}