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.

Instant sync: Apple's CloudKit push for Notes and Reminders

Yashwanth Kamireddie432ca52040c
diff --git a/check.sh b/check.sh
index 1e6ed20..aa095ca 100755
--- a/check.sh
+++ b/check.sh
@@ -66,6 +66,7 @@ step "contacts lists" node .uiverify/contacts-lists.mjs
step "drive keys" node .uiverify/drive-keys.mjs
step "drive drag" node .uiverify/drive-drag.mjs
step "drive recents" node .uiverify/drive-recents.mjs
+step "drive gone folder" node .uiverify/drive-gone.mjs
step "reminder colours" node .uiverify/calendar-reminder-colour.mjs
step "calendar dblclick" node .uiverify/calendar-dblclick.mjs
step "menus" node .uiverify/menus.mjs
diff --git a/crates/quince-icloud/src/lib.rs b/crates/quince-icloud/src/lib.rs
index 099338a..39a74e5 100644
--- a/crates/quince-icloud/src/lib.rs
+++ b/crates/quince-icloud/src/lib.rs
@@ -11,6 +11,7 @@ pub mod drive;
pub mod mergeable;
pub mod notes;
pub mod occasions;
+pub mod push;
pub mod reminders;
pub mod sync;
pub mod table;
diff --git a/crates/quince-icloud/src/push.rs b/crates/quince-icloud/src/push.rs
new file mode 100644
index 0000000..d40fd6d
--- /dev/null
+++ b/crates/quince-icloud/src/push.rs
@@ -0,0 +1,231 @@
+//! CloudKit's push channel: how a change made on a phone reaches this computer in about a
+//! second, rather than at the next timed pass.
+//!
+//! As icloud.com's CloudKit JS does it, measured against the real service:
+//!
+//! - `POST {ckdevice}/device/1/<container>/production/tokens/create` with
+//! `{apnsEnvironment: "production", clientId}` answers `{apnsToken, webcourierURL}`.
+//! - The same token is added to more containers with `.../tokens/register` and
+//! `{apnsEnvironment, apnsToken, clientId}`, so one connection carries Notes and Reminders.
+//! - `GET webcourierURL` is parked: Apple holds it open and answers when something changed,
+//! with `{"aps": {...}, "ck": {"cid": "<container>", "met": {"zid": "<zone>", ...}}}`.
+//! It is parked again straight away for the next one.
+//!
+//! No subscription has to be made: the account's own devices keep a database subscription
+//! (`DatabaseSubscription-Private`) that fires for every change. That includes this app's own
+//! writes, which is harmless: they come back as a quick delta with nothing new.
+
+use crate::{Error, Result};
+use serde_json::{json, Value};
+
+/// A push token, and the address to wait on for what it carries.
+#[derive(Debug, Clone)]
+pub struct Channel {
+ pub token: String,
+ pub url: String,
+}
+
+fn query(params: &[(String, String)]) -> Vec<(String, String)> {
+ let mut q = params.to_vec();
+ q.push(("ckjsBuildVersion".into(), "17DProjectDev77".into()));
+ q.push(("ckjsVersion".into(), "2.6.1".into()));
+ q
+}
+
+async fn post(
+ http: &reqwest::Client,
+ url: &str,
+ params: &[(String, String)],
+ body: &Value,
+) -> Result<Value> {
+ let response = http
+ .post(url)
+ .query(&query(params))
+ .json(body)
+ .send()
+ .await?;
+ let status = response.status();
+ if status.as_u16() == 450 || status.as_u16() == 401 {
+ return Err(Error::ReauthRequired);
+ }
+ let text = response.text().await.unwrap_or_default();
+ if !status.is_success() {
+ return Err(Error::Protocol(format!(
+ "the push channel answered {status}: {}",
+ text.chars().take(160).collect::<String>()
+ )));
+ }
+ serde_json::from_str(&text)
+ .map_err(|e| Error::Protocol(format!("the push channel sent invalid JSON: {e}")))
+}
+
+/// A token for the first container that also carries the rest, ready to wait on.
+pub async fn open(
+ http: &reqwest::Client,
+ ckdevice_root: &str,
+ params: &[(String, String)],
+ client_id: &str,
+ containers: &[&str],
+) -> Result<Channel> {
+ let root = ckdevice_root.trim_end_matches('/');
+ let (first, rest) = containers
+ .split_first()
+ .ok_or_else(|| Error::Protocol("no container to listen to".into()))?;
+ let made = post(
+ http,
+ &format!("{root}/device/1/{first}/production/tokens/create"),
+ params,
+ &json!({ "apnsEnvironment": "production", "clientId": client_id }),
+ )
+ .await?;
+ let field = |key: &str| {
+ made.get(key)
+ .and_then(Value::as_str)
+ .map(str::to_string)
+ .ok_or_else(|| Error::Protocol(format!("the push token came without {key}")))
+ };
+ let channel = Channel {
+ token: field("apnsToken")?,
+ url: field("webcourierURL")?,
+ };
+ for container in rest {
+ post(
+ http,
+ &format!("{root}/device/1/{container}/production/tokens/register"),
+ params,
+ &json!({ "apnsEnvironment": "production", "apnsToken": channel.token, "clientId": client_id }),
+ )
+ .await?;
+ }
+ Ok(channel)
+}
+
+/// Wait on the channel for one push, for at most `longest`. `Ok(None)` is a wait that ended
+/// with nothing to say — the connection closed or the time ran out — and is simply parked
+/// again. Otherwise the container the change was in.
+pub async fn wait(
+ http: &reqwest::Client,
+ channel: &Channel,
+ longest: std::time::Duration,
+) -> Result<Option<String>> {
+ let response = match http.get(&channel.url).timeout(longest).send().await {
+ Ok(response) => response,
+ Err(e) if e.is_timeout() => return Ok(None),
+ Err(e) => return Err(e.into()),
+ };
+ let status = response.status();
+ if !status.is_success() {
+ return Err(Error::Protocol(format!(
+ "the push channel answered {status}"
+ )));
+ }
+ let text = response.text().await.unwrap_or_default();
+ if text.trim().is_empty() {
+ return Ok(None);
+ }
+ let body: Value = serde_json::from_str(&text)
+ .map_err(|e| Error::Protocol(format!("the push channel sent invalid JSON: {e}")))?;
+ Ok(container_of(&body))
+}
+
+/// Which container a push is about.
+#[must_use]
+pub fn container_of(push: &Value) -> Option<String> {
+ push.pointer("/ck/cid")
+ .and_then(Value::as_str)
+ .map(str::to_string)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use wiremock::matchers::{method, path};
+ use wiremock::{Mock, MockServer, Request, ResponseTemplate};
+
+ /// The push Apple actually sent for a Reminders change.
+ fn measured() -> Value {
+ json!({"aps":{"content-available":1},"ck":{"ce":1,"ckuserid":"_u","aux":{},"nid":"n",
+ "met":{"zid":"Reminders","dbs":1,"zoid":"_u","sid":"DatabaseSubscription-Private"},
+ "cid":"com.apple.reminders"}})
+ }
+
+ #[test]
+ fn a_push_names_its_container() {
+ assert_eq!(
+ container_of(&measured()).as_deref(),
+ Some("com.apple.reminders")
+ );
+ assert_eq!(container_of(&json!({"aps":{}})), None);
+ }
+
+ #[tokio::test]
+ async fn one_token_is_made_and_shared_with_the_other_containers() {
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .and(path("/device/1/com.apple.notes/production/tokens/create"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({
+ "apnsEnvironment": "production", "apnsToken": "T1",
+ "webcourierURL": format!("{}/aps?tok=T1", server.uri())
+ })))
+ .expect(1)
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path(
+ "/device/1/com.apple.reminders/production/tokens/register",
+ ))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({})))
+ .expect(1)
+ .mount(&server)
+ .await;
+ Mock::given(method("GET"))
+ .and(path("/aps"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(measured()))
+ .mount(&server)
+ .await;
+ let http = reqwest::Client::new();
+ let channel = open(
+ &http,
+ &server.uri(),
+ &[],
+ "c1",
+ &["com.apple.notes", "com.apple.reminders"],
+ )
+ .await
+ .unwrap();
+ assert_eq!(channel.token, "T1");
+ let registered: Vec<Request> = server.received_requests().await.unwrap();
+ let body: Value = registered
+ .iter()
+ .find(|r| r.url.path().ends_with("/register"))
+ .unwrap()
+ .body_json()
+ .unwrap();
+ assert_eq!(body["apnsToken"], "T1", "the one token, not a second");
+ let got = wait(&http, &channel, std::time::Duration::from_secs(5))
+ .await
+ .unwrap();
+ assert_eq!(got.as_deref(), Some("com.apple.reminders"));
+ }
+
+ #[tokio::test]
+ async fn a_wait_that_runs_out_is_nothing_not_a_failure() {
+ let server = MockServer::start().await;
+ Mock::given(method("GET"))
+ .and(path("/aps"))
+ .respond_with(ResponseTemplate::new(200).set_delay(std::time::Duration::from_secs(3)))
+ .mount(&server)
+ .await;
+ let channel = Channel {
+ token: "T".into(),
+ url: format!("{}/aps", server.uri()),
+ };
+ let got = wait(
+ &reqwest::Client::new(),
+ &channel,
+ std::time::Duration::from_millis(300),
+ )
+ .await;
+ assert!(matches!(got, Ok(None)), "{got:?}");
+ }
+}
diff --git a/probe/examples/ca_idle.rs b/probe/examples/ca_idle.rs
new file mode 100644
index 0000000..0445f0c
--- /dev/null
+++ b/probe/examples/ca_idle.rs
@@ -0,0 +1,66 @@
+//! Read-only: whether `/ca/idle` (icloud.com Calendar's push-or-poll call) holds the request
+//! open until a calendar changes, and what it answers when one does.
+use anyhow::{anyhow, Result};
+#[tokio::main]
+async fn main() -> Result<()> {
+ let wait: u64 = std::env::args()
+ .nth(1)
+ .and_then(|s| s.parse().ok())
+ .unwrap_or(60);
+ 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("calendar")
+ .ok_or_else(|| anyhow!("no calendar"))?;
+ let (http, p) = (client.http().clone(), client.service_params());
+ let cal = quince_icloud::calendar::CalendarClient::new(&http, &root, p.clone());
+ let snap = cal
+ .startup("2026-10-01", "2026-10-31", "Asia/Kolkata")
+ .await?;
+ let state: Vec<_> = snap
+ .calendars
+ .iter()
+ .filter(|c| c.ctag.is_some())
+ .map(|c| serde_json::json!({ "guid": c.guid, "ctag": c.ctag }))
+ .collect();
+ println!("{} calendars with a ctag", state.len());
+ let mut q = p.clone();
+ q.push(("clientVersion".into(), "5.1".into()));
+ q.push(("lang".into(), "en-us".into()));
+ q.push(("usertz".into(), "Asia/Kolkata".into()));
+ q.push(("requestID".into(), "9".into()));
+ q.push(("startDate".into(), "2026-10-01".into()));
+ q.push(("endDate".into(), "2026-10-31".into()));
+ let now = || {
+ std::time::SystemTime::now()
+ .duration_since(std::time::UNIX_EPOCH)
+ .unwrap()
+ .as_secs_f64()
+ };
+ let mut state = state;
+ for round in 0..3 {
+ let t = now();
+ println!("round {round}: idle sent at {t:.3} with {} collections", state.len());
+ let r = http.post(format!("{root}/ca/idle")).query(&q).timeout(std::time::Duration::from_secs(wait))
+ .json(&serde_json::json!({ "ClientState": { "Collection": state, "alarmRange": 60, "fullState": true, "userTime": t as i64 } }))
+ .send().await;
+ match r {
+ Ok(r) => {
+ let st = r.status();
+ let v: serde_json::Value = r.json().await.unwrap_or_default();
+ println!(" answered after {:.1}s: {st} {}", now() - t, v.to_string().chars().take(400).collect::<String>());
+ for c in v.pointer("/ChangeSet/inserts/Collection").and_then(|c| c.as_array()).cloned().unwrap_or_default()
+ .into_iter().chain(v.pointer("/ChangeSet/updates/Collection").and_then(|c| c.as_array()).cloned().unwrap_or_default()) {
+ state.retain(|s| s["guid"] != c["guid"]);
+ state.push(serde_json::json!({ "guid": c["guid"], "ctag": c["ctag"] }));
+ }
+ }
+ Err(e) => { println!(" no answer after {:.1}s: {e}", now() - t); break; }
+ }
+ }
+ Ok(())
+}
diff --git a/probe/examples/ck_push.rs b/probe/examples/ck_push.rs
new file mode 100644
index 0000000..7e2cd12
--- /dev/null
+++ b/probe/examples/ck_push.rs
@@ -0,0 +1,100 @@
+//! Read-only: CloudKit's push channel as icloud.com's CloudKit JS uses it — a token from
+//! `device/1/<container>/production/tokens/create`, the subscriptions the account already
+//! has, then one parked long-poll on the `webcourierURL` the token came with.
+use anyhow::{anyhow, Result};
+#[tokio::main]
+async fn main() -> Result<()> {
+ let container = std::env::args()
+ .nth(1)
+ .unwrap_or_else(|| "com.apple.notes".into());
+ let wait: u64 = std::env::args()
+ .nth(2)
+ .and_then(|s| s.parse().ok())
+ .unwrap_or(90);
+ 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 ck = client
+ .session()
+ .service_url("ckdatabasews")
+ .ok_or_else(|| anyhow!("no ck"))?;
+ let ck = ck.trim_end_matches('/');
+ let dev = client
+ .session()
+ .service_url("ckdeviceservice")
+ .ok_or_else(|| anyhow!("no ckdevice"))?;
+ let http = client.http().clone();
+ let mut q = client.service_params();
+ q.push(("ckjsBuildVersion".into(), "17DProjectDev77".into()));
+ q.push(("ckjsVersion".into(), "2.6.1".into()));
+ let client_id = uuid::Uuid::new_v4().to_string();
+ let r = http
+ .post(format!(
+ "{dev}/device/1/{container}/production/tokens/create"
+ ))
+ .query(&q)
+ .json(&serde_json::json!({ "apnsEnvironment": "production", "clientId": client_id }))
+ .send()
+ .await?;
+ let s = r.status();
+ let token: serde_json::Value = r.json().await.unwrap_or_default();
+ println!(
+ "tokens/create {s}: {}",
+ token.to_string().chars().take(300).collect::<String>()
+ );
+ let r = http
+ .get(format!(
+ "{ck}/database/1/{container}/production/private/subscriptions/list"
+ ))
+ .query(&q)
+ .send()
+ .await?;
+ let s = r.status();
+ let subs: serde_json::Value = r.json().await.unwrap_or_default();
+ println!(
+ "subscriptions/list {s}: {}",
+ subs.to_string().chars().take(800).collect::<String>()
+ );
+ // The same token for more containers, as CloudKit JS shares one connector between them.
+ for other in std::env::args().skip(3) {
+ let r = http.post(format!("{dev}/device/1/{other}/production/tokens/register")).query(&q)
+ .json(&serde_json::json!({ "apnsEnvironment": "production", "apnsToken": token["apnsToken"], "clientId": client_id })).send().await?;
+ println!(
+ "tokens/register {other} {}: {}",
+ r.status(),
+ r.text()
+ .await
+ .unwrap_or_default()
+ .chars()
+ .take(200)
+ .collect::<String>()
+ );
+ }
+ let Some(url) = token["webcourierURL"].as_str() else {
+ return Ok(());
+ };
+ println!("parking on the push channel for up to {wait}s…");
+ let t = std::time::Instant::now();
+ let r = http
+ .get(url)
+ .timeout(std::time::Duration::from_secs(wait))
+ .send()
+ .await;
+ match r {
+ Ok(r) => println!(
+ "after {:?}: {} {}",
+ t.elapsed(),
+ r.status(),
+ r.text()
+ .await
+ .unwrap_or_default()
+ .chars()
+ .take(600)
+ .collect::<String>()
+ ),
+ Err(e) => println!("after {:?}: {e}", t.elapsed()),
+ }
+ Ok(())
+}
diff --git a/probe/examples/push_latency.rs b/probe/examples/push_latency.rs
new file mode 100644
index 0000000..dde9d5f
--- /dev/null
+++ b/probe/examples/push_latency.rs
@@ -0,0 +1,41 @@
+//! Live: the app's push code (quince_icloud::push) parked on Notes + Reminders, printing the
+//! moment each push lands, so a change made elsewhere can be timed end to end.
+use anyhow::{anyhow, Result};
+#[tokio::main]
+async fn main() -> Result<()> {
+ let secs: u64 = std::env::args()
+ .nth(1)
+ .and_then(|s| s.parse().ok())
+ .unwrap_or(60);
+ 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("ckdeviceservice")
+ .ok_or_else(|| anyhow!("no ckdevice"))?;
+ let (http, p) = (client.http().clone(), client.service_params());
+ let ch = quince_icloud::push::open(
+ &http,
+ &root,
+ &p,
+ "PROBE",
+ &["com.apple.notes", "com.apple.reminders"],
+ )
+ .await?;
+ let now = || {
+ std::time::SystemTime::now()
+ .duration_since(std::time::UNIX_EPOCH)
+ .unwrap()
+ .as_secs_f64()
+ };
+ println!("parked at {:.3}", now());
+ let end = std::time::Instant::now() + std::time::Duration::from_secs(secs);
+ while std::time::Instant::now() < end {
+ let got = quince_icloud::push::wait(&http, &ch, end - std::time::Instant::now()).await?;
+ println!("push at {:.3}: {got:?}", now());
+ }
+ Ok(())
+}
diff --git a/probe/examples/ws_map.rs b/probe/examples/ws_map.rs
new file mode 100644
index 0000000..3c3e158
--- /dev/null
+++ b/probe/examples/ws_map.rs
@@ -0,0 +1,32 @@
+//! Read-only: the names of the services in Apple's webservices map (hosts only, no tokens).
+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"));
+ }
+ for k in [
+ "ckdatabasews",
+ "ckdeviceservice",
+ "push",
+ "pushws",
+ "webcourier",
+ "ckdeviceservicews",
+ "keyvalue",
+ "calendar",
+ "contacts",
+ "drivews",
+ "docws",
+ ] {
+ println!(
+ "{k}: {:?}",
+ client
+ .session()
+ .service_url(k)
+ .map(|u| u.split('?').next().unwrap_or("").to_string())
+ );
+ }
+ Ok(())
+}
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs
index 5da3fe5..ea67793 100644
--- a/src-tauri/src/main.rs
+++ b/src-tauri/src/main.rs
@@ -2,6 +2,7 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
mod alerts;
+mod push;
mod refresh;
use quince_auth::{Client, SignInOutcome, TwoFactorOptions};
@@ -85,6 +86,12 @@ pub static CALENDAR_PASS: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new
/// edit, made from it, was refused as a conflict.
pub static CONTACTS_PASS: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
+/// One Drive folder fetched and stored at a time. A folder opened just before a rename is
+/// fetched with the old name; finishing after the rename's own re-read, it stored that old
+/// listing last and the rename looked undone. Held from fetch to store, the re-read after a
+/// write — which starts only once the write is made — always lands last.
+pub static DRIVE_PASS: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
+
async fn refresh_reminders(
http: &reqwest::Client,
root: &str,
@@ -1181,7 +1188,10 @@ async fn note_table(
// does not bring — so asked of Apple now, and kept.
let (http, root, params) = cloudkit_context(&auth).await?;
let client = quince_icloud::cloudkit::ZoneClient::new(&http, &root, "com.apple.notes", params);
- let record = client.lookup_record("Notes", &id).await.map_err(|e| e.to_string())?;
+ let record = client
+ .lookup_record("Notes", &id)
+ .await
+ .map_err(|e| e.to_string())?;
let cells = record
.as_ref()
.and_then(|r| r.pointer("/fields/MergeableDataEncrypted/value"))
@@ -1190,7 +1200,9 @@ async fn note_table(
if let Some(cells) = &cells {
let mut library = data.0.lock().await;
if let Ok(json) = serde_json::to_string(cells) {
- library.set_state(&quince_icloud::table::state_key(&id), &json).map_err(|e| e.to_string())?;
+ library
+ .set_state(&quince_icloud::table::state_key(&id), &json)
+ .map_err(|e| e.to_string())?;
}
}
Ok(cells)
@@ -1799,9 +1811,20 @@ async fn drive_sync_folder(
.ok_or("Apple hasn't offered iCloud Drive for this account.")?;
(client.http().clone(), root, client.service_params())
};
- let items = quince_icloud::drive::fetch_folder(&http, &root, params, &folder_id)
- .await
- .map_err(|e| e.to_string())?;
+ let _one = DRIVE_PASS.lock().await;
+ let items = match quince_icloud::drive::fetch_folder(&http, &root, params, &folder_id).await {
+ Ok(items) => items,
+ // Deleted or moved on another device: forgotten here too, so the screen can step back
+ // out of it rather than show a folder that is not there.
+ Err(quince_icloud::Error::NotFound) => {
+ let mut library = data.0.lock().await;
+ library
+ .forget_drive_folder(&folder_id)
+ .map_err(|e| e.to_string())?;
+ return Err("This folder was deleted or moved on another device.".into());
+ }
+ Err(e) => return Err(e.to_string()),
+ };
let mut library = data.0.lock().await;
quince_icloud::drive::store_folder(&mut library, &folder_id, &items).map_err(|e| e.to_string())
}
@@ -1841,6 +1864,7 @@ async fn resync_drive_folder(
data: &State<'_, Data>,
folder_id: &str,
) -> Result<(), String> {
+ let _one = DRIVE_PASS.lock().await;
let items = quince_icloud::drive::fetch_folder(http, root, params, folder_id)
.await
.map_err(|e| e.to_string())?;
@@ -2833,6 +2857,7 @@ fn main() {
});
spawn_holidays(app.handle().clone());
spawn_heartbeat(Arc::clone(&client));
+ push::spawn(app.handle().clone(), Arc::clone(&client));
spawn_auto_sync(app.handle().clone(), client);
alerts::spawn(app.handle().clone());
alerts::spawn_events(app.handle().clone());
diff --git a/src-tauri/src/push.rs b/src-tauri/src/push.rs
new file mode 100644
index 0000000..50a16b6
--- /dev/null
+++ b/src-tauri/src/push.rs
@@ -0,0 +1,144 @@
+//! Apple's pushes, turned into refreshes of exactly what changed.
+//!
+//! A change made on a phone used to wait for the next five-minute pass. Now a parked
+//! connection hears of it from Apple within about a second (see
+//! `quince_icloud::push`), and only the service it was in is refreshed. The timed pass stays
+//! as the floor beneath this, for the services Apple does not push here and for any push
+//! that went missing.
+
+use crate::{alerts, refresh, Data, SYNCED};
+use quince_auth::Client;
+use std::sync::Arc;
+use std::time::{Duration, Instant};
+use tauri::{Emitter, Manager};
+use tokio::sync::{mpsc, Mutex};
+
+/// The containers one connection listens to, and the service each one is.
+const CONTAINERS: [(&str, &str); 2] = [
+ ("com.apple.notes", "notes"),
+ ("com.apple.reminders", "reminders"),
+];
+
+/// How long one wait is left parked before it is parked afresh. Apple answers sooner when
+/// something changes; this only bounds a connection that went quiet without closing.
+const LONGEST_WAIT: Duration = Duration::from_secs(10 * 60);
+
+/// A token is made for a day (`ttl=86400`); a new one is made a little before.
+const TOKEN_LIFE: Duration = Duration::from_secs(23 * 60 * 60);
+
+/// A phone sends a burst of pushes for one edit, one per record it saves. They are taken
+/// together, so one edit is one refresh.
+const SETTLE: Duration = Duration::from_millis(250);
+
+/// The service a container's changes belong to.
+fn service_of(container: &str) -> Option<&'static str> {
+ CONTAINERS
+ .iter()
+ .find(|(c, _)| *c == container)
+ .map(|(_, s)| *s)
+}
+
+pub fn spawn(app: tauri::AppHandle, client: Arc<Mutex<Client>>) {
+ let (tx, rx) = mpsc::unbounded_channel::<&'static str>();
+ tauri::async_runtime::spawn(refresher(app, Arc::clone(&client), rx));
+ tauri::async_runtime::spawn(listen(client, tx));
+}
+
+/// Refresh what the pushes named, a burst at a time.
+async fn refresher(
+ app: tauri::AppHandle,
+ client: Arc<Mutex<Client>>,
+ mut rx: mpsc::UnboundedReceiver<&'static str>,
+) {
+ while let Some(first) = rx.recv().await {
+ let mut services = vec![first];
+ tokio::time::sleep(SETTLE).await;
+ while let Ok(more) = rx.try_recv() {
+ if !services.contains(&more) {
+ services.push(more);
+ }
+ }
+ let data = app.state::<Data>();
+ refresh::some(&client, &data.0, &services).await;
+ alerts::changed();
+ // Every service's freshness, not only the ones refreshed: the window shows the whole.
+ app.emit(SYNCED, refresh::status(&data.0).await).ok();
+ }
+}
+
+/// Keep a connection parked on Apple's push channel for as long as the app runs.
+async fn listen(client: Arc<Mutex<Client>>, tx: mpsc::UnboundedSender<&'static str>) {
+ let client_id = uuid::Uuid::new_v4().to_string().to_uppercase();
+ let containers: Vec<&str> = CONTAINERS.iter().map(|(c, _)| *c).collect();
+ let mut backoff = Duration::from_secs(1);
+ let mut first = true;
+ loop {
+ let session = {
+ let guard = client.lock().await;
+ let s = guard.session();
+ s.webservices
+ .is_some()
+ .then(|| s.service_url("ckdeviceservice"))
+ .flatten()
+ .map(|root| (guard.http().clone(), guard.service_params(), root))
+ };
+ let Some((http, params, root)) = session else {
+ // Not signed in, or no CloudKit for this account: look again shortly.
+ tokio::time::sleep(Duration::from_secs(30)).await;
+ continue;
+ };
+ let channel =
+ match quince_icloud::push::open(&http, &root, &params, &client_id, &containers).await {
+ Ok(channel) => channel,
+ Err(e) => {
+ eprintln!("push: could not open the channel: {e}");
+ tokio::time::sleep(backoff).await;
+ backoff = (backoff * 2).min(Duration::from_secs(5 * 60));
+ continue;
+ }
+ };
+ backoff = Duration::from_secs(1);
+ // Whatever changed while no connection was parked was never pushed: one pass catches
+ // it up. Not at launch, which runs a full pass of its own.
+ if !first {
+ for (_, service) in CONTAINERS {
+ tx.send(service).ok();
+ }
+ }
+ first = false;
+ let opened = Instant::now();
+ let mut failures = 0;
+ while opened.elapsed() < TOKEN_LIFE {
+ match quince_icloud::push::wait(&http, &channel, LONGEST_WAIT).await {
+ Ok(Some(container)) => {
+ failures = 0;
+ if let Some(service) = service_of(&container) {
+ tx.send(service).ok();
+ }
+ }
+ Ok(None) => failures = 0,
+ Err(e) => {
+ failures += 1;
+ eprintln!("push: the wait failed ({failures}): {e}");
+ if failures >= 3 {
+ // Three in a row is a channel gone bad, not a blip: make a new one.
+ break;
+ }
+ tokio::time::sleep(backoff * failures).await;
+ }
+ }
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn each_container_is_its_service() {
+ assert_eq!(service_of("com.apple.notes"), Some("notes"));
+ assert_eq!(service_of("com.apple.reminders"), Some("reminders"));
+ assert_eq!(service_of("com.apple.photos.cloud"), None);
+ }
+}
diff --git a/src-tauri/src/refresh.rs b/src-tauri/src/refresh.rs
index 78b529b..b949fa6 100644
--- a/src-tauri/src/refresh.rs
+++ b/src-tauri/src/refresh.rs
@@ -120,123 +120,144 @@ async fn context(client: &Arc<Mutex<Client>>) -> Context {
/// Never returns an error: a refresh that partly failed is a normal outcome with a partial
/// result, not an exception, and the interface has to be able to show exactly that.
pub async fn all(client: &Arc<Mutex<Client>>, data: &Mutex<Library>) -> Refresh {
+ run(client, data, &COVERED.map(|(service, _)| service)).await
+}
+
+/// Refresh only the services `wanted` names — what a push from Apple says changed — and
+/// report on those alone.
+pub async fn some(client: &Arc<Mutex<Client>>, data: &Mutex<Library>, wanted: &[&str]) -> Refresh {
+ run(client, data, wanted).await
+}
+
+async fn run(client: &Arc<Mutex<Client>>, data: &Mutex<Library>, services: &[&str]) -> Refresh {
+ let wanted = |service: &str| services.contains(&service);
let started = std::time::Instant::now();
let ctx = context(client).await;
let (from, to, zone) = default_window();
let mut reports = Vec::with_capacity(COVERED.len());
- reports.push(match ctx.calendar.as_deref() {
- None => unavailable("calendar", "Calendar"),
- Some(root) => {
- // Fetch and store as one, never across a save: see `CALENDAR_PASS`.
- let _one = crate::CALENDAR_PASS.lock().await;
- let fetched = quince_icloud::sync::fetch_calendar(
- &ctx.http,
- root,
- ctx.params.clone(),
- &from,
- &to,
- &zone,
- )
- .await;
- store(
- "calendar",
- "Calendar",
- data,
- fetched,
- |library, snapshot| {
- let summary = quince_icloud::sync::apply_calendar(library, snapshot)?;
- Ok(format!(
- "{} event{} in {} calendar{}",
- summary.events,
- plural(summary.events),
- summary.calendars,
- plural(summary.calendars)
- ))
- },
- )
- .await
- }
- });
+ if wanted("calendar") {
+ reports.push(match ctx.calendar.as_deref() {
+ None => unavailable("calendar", "Calendar"),
+ Some(root) => {
+ // Fetch and store as one, never across a save: see `CALENDAR_PASS`.
+ let _one = crate::CALENDAR_PASS.lock().await;
+ let fetched = quince_icloud::sync::fetch_calendar(
+ &ctx.http,
+ root,
+ ctx.params.clone(),
+ &from,
+ &to,
+ &zone,
+ )
+ .await;
+ store(
+ "calendar",
+ "Calendar",
+ data,
+ fetched,
+ |library, snapshot| {
+ let summary = quince_icloud::sync::apply_calendar(library, snapshot)?;
+ Ok(format!(
+ "{} event{} in {} calendar{}",
+ summary.events,
+ plural(summary.events),
+ summary.calendars,
+ plural(summary.calendars)
+ ))
+ },
+ )
+ .await
+ }
+ });
+ }
+
+ if wanted("reminders") {
+ reports.push(match ctx.cloudkit.as_deref() {
+ None => unavailable("reminders", "Reminders"),
+ Some(root) => {
+ let _one = crate::REMINDERS_PASS.lock().await;
+ let since = token(data, "reminders").await;
+ let full = since.is_none();
+ let fetched = quince_icloud::reminders::fetch_cloudkit(
+ &ctx.http,
+ root,
+ ctx.params.clone(),
+ since.as_deref(),
+ )
+ .await;
+ store(
+ "reminders",
+ "Reminders",
+ data,
+ fetched,
+ move |library, f| {
+ let (lists, count, _) =
+ quince_icloud::reminders::apply_cloudkit(library, f)?;
+ if full {
+ library.mark_full_sync("reminders")?;
+ }
+ Ok(format!(
+ "{count} reminder{} in {lists} list{}",
+ plural(count),
+ plural(lists)
+ ))
+ },
+ )
+ .await
+ }
+ });
+ }
- reports.push(match ctx.cloudkit.as_deref() {
- None => unavailable("reminders", "Reminders"),
- Some(root) => {
- let _one = crate::REMINDERS_PASS.lock().await;
- let since = token(data, "reminders").await;
- let full = since.is_none();
- let fetched = quince_icloud::reminders::fetch_cloudkit(
- &ctx.http,
- root,
- ctx.params.clone(),
- since.as_deref(),
- )
- .await;
- store(
- "reminders",
- "Reminders",
- data,
- fetched,
- move |library, f| {
- let (lists, count, _) = quince_icloud::reminders::apply_cloudkit(library, f)?;
+ if wanted("notes") {
+ reports.push(match ctx.cloudkit.as_deref() {
+ None => unavailable("notes", "Notes"),
+ Some(root) => {
+ // One notes pass at a time, from reading the position through storing what came
+ // back — see `NOTES_PASS`.
+ let _one = crate::NOTES_PASS.lock().await;
+ let since = token(data, "notes").await;
+ let full = since.is_none();
+ let fetched = quince_icloud::notes::fetch_cloudkit(
+ &ctx.http,
+ root,
+ ctx.params.clone(),
+ since.as_deref(),
+ )
+ .await;
+ store("notes", "Notes", data, fetched, move |library, f| {
+ let (folders, notes, _) = quince_icloud::notes::apply_cloudkit(library, f)?;
if full {
- library.mark_full_sync("reminders")?;
+ library.mark_full_sync("notes")?;
}
Ok(format!(
- "{count} reminder{} in {lists} list{}",
- plural(count),
- plural(lists)
+ "{notes} note{} in {folders} folder{}",
+ plural(notes),
+ plural(folders)
))
- },
- )
- .await
- }
- });
-
- reports.push(match ctx.cloudkit.as_deref() {
- None => unavailable("notes", "Notes"),
- Some(root) => {
- // One notes pass at a time, from reading the position through storing what came
- // back — see `NOTES_PASS`.
- let _one = crate::NOTES_PASS.lock().await;
- let since = token(data, "notes").await;
- let full = since.is_none();
- let fetched = quince_icloud::notes::fetch_cloudkit(
- &ctx.http,
- root,
- ctx.params.clone(),
- since.as_deref(),
- )
- .await;
- store("notes", "Notes", data, fetched, move |library, f| {
- let (folders, notes, _) = quince_icloud::notes::apply_cloudkit(library, f)?;
- if full {
- library.mark_full_sync("notes")?;
- }
- Ok(format!(
- "{notes} note{} in {folders} folder{}",
- plural(notes),
- plural(folders)
- ))
- })
- .await
- }
- });
+ })
+ .await
+ }
+ });
+ }
- reports.push(match ctx.contacts.as_deref() {
- None => unavailable("contacts", "Contacts"),
- Some(root) => {
- // Fetch and store as one, never across a save: see `CONTACTS_PASS`.
- let _one = crate::CONTACTS_PASS.lock().await;
- let fetched = quince_icloud::contacts::fetch(&ctx.http, root, ctx.params.clone()).await;
- store("contacts", "Contacts", data, fetched, |library, people| {
- let count = quince_icloud::contacts::apply(library, people)?;
- Ok(format!("{count} contact{}", plural(count)))
- })
- .await
- }
- });
+ if wanted("contacts") {
+ reports.push(match ctx.contacts.as_deref() {
+ None => unavailable("contacts", "Contacts"),
+ Some(root) => {
+ // Fetch and store as one, never across a save: see `CONTACTS_PASS`.
+ let _one = crate::CONTACTS_PASS.lock().await;
+ let fetched =
+ quince_icloud::contacts::fetch(&ctx.http, root, ctx.params.clone()).await;
+ store("contacts", "Contacts", data, fetched, |library, people| {
+ let count = quince_icloud::contacts::apply(library, people)?;
+ Ok(format!("{count} contact{}", plural(count)))
+ })
+ .await
+ }
+ });
+ }
let available: Vec<&str> = reports
.iter()