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.

Contacts: every write carries a live sync token, never one an earlier write spent

Yashwanth Kamireddi4289ff6c08c6
diff --git a/crates/quince-icloud/src/contacts.rs b/crates/quince-icloud/src/contacts.rs
index b565822..c38aabe 100644
--- a/crates/quince-icloud/src/contacts.rs
+++ b/crates/quince-icloud/src/contacts.rs
@@ -372,6 +372,98 @@ pub fn parse(body: &Value) -> Vec<Contact> {
.unwrap_or_default()
}
+/// The tokens the next write to Contacts carries, kept through a run of writes.
+///
+/// Every write changes the address book, and with it the sync token: the token a write was
+/// sent with is spent once Apple takes it. A run of writes (an import in batches, a merge, the
+/// queue sent in order) therefore never reuses one. A write Apple answers with new tokens
+/// leaves those for the next; one answered without them, or not taken, leaves none, and the
+/// next write reads the book again first. Tokens a read of the book just returned are fresh,
+/// and are what the first write carries.
+#[derive(Debug, Default)]
+pub struct Ledger {
+ held: std::sync::Mutex<Option<Tokens>>,
+}
+
+impl Ledger {
+ /// No tokens yet: the first write reads them.
+ #[must_use]
+ pub fn new() -> Self {
+ Self::default()
+ }
+
+ /// Tokens read with the book a moment ago, for the next write.
+ #[must_use]
+ pub fn holding(tokens: Tokens) -> Self {
+ Ledger {
+ held: std::sync::Mutex::new(Some(tokens)),
+ }
+ }
+
+ /// Tokens a read of the book just returned: the next write carries them.
+ pub fn hold(&self, tokens: Tokens) {
+ *self.held() = Some(tokens);
+ }
+
+ /// Whether it holds tokens no write has spent yet. Without them the next write reads the
+ /// book first.
+ #[must_use]
+ pub fn has_tokens(&self) -> bool {
+ self.held().is_some()
+ }
+
+ /// A write made outside the ledger (with tokens it read itself) has moved the book on:
+ /// the tokens held are spent.
+ pub fn spend(&self) {
+ *self.held() = None;
+ }
+
+ fn held(&self) -> std::sync::MutexGuard<'_, Option<Tokens>> {
+ self.held
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ }
+
+ /// What the next write carries: tokens not yet spent, or the book's own, read now.
+ async fn tokens(
+ &self,
+ http: &reqwest::Client,
+ root: &str,
+ params: Vec<(String, String)>,
+ ) -> Result<Tokens> {
+ if let Some(tokens) = self.held().clone() {
+ return Ok(tokens);
+ }
+ let body = startup(http, root, params).await?;
+ self.read(&body)
+ }
+
+ /// A read of the whole book: its tokens are the current ones.
+ fn read(&self, body: &Value) -> Result<Tokens> {
+ let tokens = Tokens::of_startup(body)?;
+ *self.held() = Some(tokens.clone());
+ Ok(tokens)
+ }
+
+ /// After a write: the tokens Apple answered with, if it did; otherwise none — the next
+ /// write reads them again rather than send these twice.
+ fn answered(&self, body: Option<&Value>) {
+ let mut held = self.held();
+ let next = body.and_then(|b| {
+ let sync = b.get("syncToken").and_then(Value::as_str)?.to_string();
+ let pref = b
+ .get("prefToken")
+ .and_then(Value::as_str)
+ .map(ToString::to_string)
+ .or_else(|| held.as_ref().map(|t| t.pref.clone()))?;
+ // The same token back is no new token: the write may still have moved the book.
+ (held.as_ref().map(|t| t.sync.as_str()) != Some(sync.as_str()))
+ .then_some(Tokens { sync, pref })
+ });
+ *held = next;
+ }
+}
+
/// The tokens Apple hands out at startup, which every write has to carry.
///
/// `syncToken` is how the service knows which version of the address book a change is based
@@ -384,6 +476,20 @@ pub struct Tokens {
}
impl Tokens {
+ /// The tokens a `/co/startup` answer carries.
+ fn of_startup(body: &Value) -> Result<Self> {
+ let token = |key: &str| {
+ body.get(key)
+ .and_then(Value::as_str)
+ .map(ToString::to_string)
+ .ok_or_else(|| Error::Protocol(format!("/co/startup carried no {key}")))
+ };
+ Ok(Tokens {
+ sync: token("syncToken")?,
+ pref: token("prefToken")?,
+ })
+ }
+
/// As kept in the library's sync position for Contacts.
#[must_use]
pub fn stored(&self) -> String {
@@ -784,6 +890,20 @@ fn merge_methods(
}
async fn write(
+ http: &reqwest::Client,
+ root: &str,
+ params: Vec<(String, String)>,
+ ledger: &Ledger,
+ method: reqwest::Method,
+ card: Value,
+) -> Result<Value> {
+ let tokens = ledger.tokens(http, root, params.clone()).await?;
+ let answer = write_with(http, root, params, &tokens, method, card).await;
+ ledger.answered(answer.as_ref().ok());
+ answer
+}
+
+async fn write_with(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
@@ -839,7 +959,7 @@ pub async fn create(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
- tokens: &Tokens,
+ ledger: &Ledger,
contact: &Contact,
) -> Result<String> {
// Apple keeps a card that is only a number or an address — "No Name" on the phone — so
@@ -857,7 +977,7 @@ pub async fn create(
// Apple's own client mints the id rather than asking the service for one.
let guid = new_contact_id();
// A fresh id: no card can hold it yet.
- write_new(http, root, params, tokens, &guid, contact).await?;
+ write_new(http, root, params, ledger, &guid, contact).await?;
Ok(guid)
}
@@ -882,7 +1002,7 @@ pub async fn create_named(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
- tokens: &Tokens,
+ ledger: &Ledger,
guid: &str,
contact: &Contact,
) -> Result<()> {
@@ -891,28 +1011,31 @@ pub async fn create_named(
"Add a name, a phone number or an email address first.".into(),
));
}
- if card(http, root, params.clone(), guid).await?.is_some() {
+ if card(http, root, params.clone(), ledger, guid)
+ .await?
+ .is_some()
+ {
return Ok(());
}
- write_new(http, root, params, tokens, guid, contact).await
+ write_new(http, root, params, ledger, guid, contact).await
}
async fn write_new(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
- tokens: &Tokens,
+ ledger: &Ledger,
guid: &str,
contact: &Contact,
) -> Result<()> {
- write_new_card(http, root, params, tokens, guid, contact, None).await
+ write_new_card(http, root, params, ledger, guid, contact, None).await
}
async fn write_new_card(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
- tokens: &Tokens,
+ ledger: &Ledger,
guid: &str,
contact: &Contact,
details: Option<&ContactDetails>,
@@ -925,7 +1048,7 @@ async fn write_new_card(
http,
root,
params,
- tokens,
+ ledger,
reqwest::Method::POST,
card_written(&fresh, details, None, None, None),
)
@@ -938,7 +1061,7 @@ pub async fn create_card_named(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
- tokens: &Tokens,
+ ledger: &Ledger,
guid: &str,
card: &Card,
) -> Result<()> {
@@ -947,7 +1070,7 @@ pub async fn create_card_named(
"Add a name, a phone number or an email address first.".into(),
));
}
- if self::card(http, root, params.clone(), guid)
+ if self::card(http, root, params.clone(), ledger, guid)
.await?
.is_some()
{
@@ -957,7 +1080,7 @@ pub async fn create_card_named(
http,
root,
params,
- tokens,
+ ledger,
guid,
&card.contact,
Some(&card.details),
@@ -971,7 +1094,7 @@ pub async fn update_card(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
- tokens: &Tokens,
+ ledger: &Ledger,
card: &Card,
existing: Option<&Value>,
base: Option<&Card>,
@@ -985,7 +1108,7 @@ pub async fn update_card(
http,
root,
params,
- tokens,
+ ledger,
reqwest::Method::PUT,
card_written(
&card.contact,
@@ -1007,11 +1130,11 @@ pub async fn update(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
- tokens: &Tokens,
+ ledger: &Ledger,
contact: &Contact,
existing: Option<&Value>,
) -> Result<()> {
- update_from(http, root, params, tokens, contact, existing, None).await
+ update_from(http, root, params, ledger, contact, existing, None).await
}
/// [`update`], writing only what changed from `base` — the card as the form opened it — over
@@ -1021,7 +1144,7 @@ pub async fn update_from(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
- tokens: &Tokens,
+ ledger: &Ledger,
contact: &Contact,
existing: Option<&Value>,
base: Option<&Contact>,
@@ -1035,7 +1158,7 @@ pub async fn update_from(
http,
root,
params,
- tokens,
+ ledger,
reqwest::Method::PUT,
card_merged(contact, existing, base),
)
@@ -1048,13 +1171,13 @@ pub async fn delete(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
- tokens: &Tokens,
+ ledger: &Ledger,
contact: &Contact,
) -> Result<()> {
// The tag Apple holds now, not the one stored at the last sync: a card edited on the
// phone since then was refused as a conflict, and the delete failed for no reason a
// person could see. Gone already is done.
- let Some(current) = card(http, root, params.clone(), &contact.guid).await? else {
+ let Some(current) = card(http, root, params.clone(), ledger, &contact.guid).await? else {
return Ok(());
};
let etag = current
@@ -1068,7 +1191,7 @@ pub async fn delete(
http,
root,
query,
- tokens,
+ ledger,
reqwest::Method::POST,
serde_json::json!({
"contactId": contact.guid,
@@ -1079,14 +1202,20 @@ pub async fn delete(
Ok(())
}
-/// Apple's card for one person, as it currently stands.
+/// Apple's card for one person, as it currently stands. The book is read whole for it, so
+/// its tokens go to `ledger`: the write that follows carries the tokens of the very version
+/// it was made against.
pub async fn card(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
+ ledger: &Ledger,
guid: &str,
) -> Result<Option<Value>> {
let body = startup(http, root, params).await?;
+ if ledger.read(&body).is_err() {
+ *ledger.held() = None;
+ }
Ok(body
.get("contacts")
.and_then(Value::as_array)
@@ -1183,6 +1312,20 @@ pub fn stored_lists(library: &quince_core::Library) -> Vec<ContactList> {
/// an id made here, a rename or a change of members is the full list `PUT` back with its
/// etag, a delete names it. Membership travels as the entire set of contact ids.
async fn write_list(
+ http: &reqwest::Client,
+ root: &str,
+ params: Vec<(String, String)>,
+ ledger: &Ledger,
+ method: Option<&str>,
+ group: Value,
+) -> Result<Value> {
+ let tokens = ledger.tokens(http, root, params.clone()).await?;
+ let answer = write_list_with(http, root, params, &tokens, method, group).await;
+ ledger.answered(answer.as_ref().ok());
+ answer
+}
+
+async fn write_list_with(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
@@ -1255,7 +1398,7 @@ pub async fn create_list(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
- tokens: &Tokens,
+ ledger: &Ledger,
name: &str,
members: &[String],
) -> Result<String> {
@@ -1268,7 +1411,7 @@ pub async fn create_list(
http,
root,
params,
- tokens,
+ ledger,
None,
serde_json::json!({ "groupId": guid, "name": name, "contactIds": members }),
)
@@ -1281,14 +1424,14 @@ pub async fn update_list(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
- tokens: &Tokens,
+ ledger: &Ledger,
list: &ContactList,
) -> Result<()> {
write_list(
http,
root,
params,
- tokens,
+ ledger,
Some("PUT"),
serde_json::json!({ "groupId": list.guid, "etag": list.etag, "name": list.name, "contactIds": list.members }),
)
@@ -1301,14 +1444,14 @@ pub async fn delete_list(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
- tokens: &Tokens,
+ ledger: &Ledger,
guid: &str,
) -> Result<()> {
write_list(
http,
root,
params,
- tokens,
+ ledger,
Some("DELETE"),
serde_json::json!({ "groupId": guid }),
)
@@ -1319,6 +1462,25 @@ pub async fn delete_list(
/// A plain-text call to Contacts with the address book's tokens, as icloud.com makes its
/// export, import and my-card calls. Answers with Apple's reply as text.
async fn co_text(
+ http: &reqwest::Client,
+ root: &str,
+ params: Vec<(String, String)>,
+ ledger: &Ledger,
+ method: reqwest::Method,
+ path: &str,
+ body: String,
+) -> Result<String> {
+ let tokens = ledger.tokens(http, root, params.clone()).await?;
+ let answer = co_text_with(http, root, params, &tokens, method, path, body).await;
+ let json = answer
+ .as_ref()
+ .ok()
+ .and_then(|t| serde_json::from_str::<Value>(t).ok());
+ ledger.answered(json.as_ref().filter(|_| answer.is_ok()));
+ answer
+}
+
+async fn co_text_with(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
@@ -1364,7 +1526,7 @@ pub async fn export_vcards(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
- tokens: &Tokens,
+ ledger: &Ledger,
ids: &[String],
file_name: &str,
) -> Result<String> {
@@ -1373,7 +1535,7 @@ pub async fn export_vcards(
http,
root,
params,
- tokens,
+ ledger,
reqwest::Method::POST,
"co/export/",
body.to_string(),
@@ -1772,7 +1934,7 @@ pub async fn import_vcards(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
- tokens: &Tokens,
+ ledger: &Ledger,
vcard: &str,
) -> Result<(Vec<String>, usize)> {
// Apple's own ceiling for an import, checked here so the refusal says why.
@@ -1788,7 +1950,7 @@ pub async fn import_vcards(
http,
root,
params,
- tokens,
+ ledger,
reqwest::Method::POST,
"co/import/",
vcard.to_string(),
@@ -1817,7 +1979,7 @@ pub async fn set_me_card(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
- tokens: &Tokens,
+ ledger: &Ledger,
id: &str,
) -> Result<()> {
let body = serde_json::json!({ "meCardId": id });
@@ -1825,7 +1987,7 @@ pub async fn set_me_card(
http,
root,
params,
- tokens,
+ ledger,
reqwest::Method::PUT,
"co/mecardid",
body.to_string(),
@@ -2586,11 +2748,11 @@ mod write_tests {
}
}
- fn tokens() -> Tokens {
- Tokens {
+ fn tokens() -> Ledger {
+ Ledger::holding(Tokens {
sync: "SYNC".into(),
pref: "PREF".into(),
- }
+ })
}
async fn server() -> MockServer {
@@ -2801,7 +2963,8 @@ mod write_tests {
let server = server().await;
Mock::given(path_regex(r".*/co/startup$"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
- "contacts": [{ "contactId": "C1", "etag": "v2" }]
+ "contacts": [{ "contactId": "C1", "etag": "v2" }],
+ "syncToken": "SYNC-NOW", "prefToken": "PREF",
})))
.mount(&server)
.await;
@@ -2824,10 +2987,152 @@ mod write_tests {
let query: std::collections::HashMap<_, _> = request.url.query_pairs().collect();
let body: Value = serde_json::from_slice(&request.body).unwrap();
assert_eq!(query["method"], "DELETE");
+ // The tokens of the read the etag came from, not older ones held before it.
+ assert_eq!(query["syncToken"], "SYNC-NOW");
assert_eq!(body["contacts"][0]["contactId"], "C1");
assert_eq!(body["contacts"][0]["etag"], "v2");
}
+ /// A run of writes never sends a token twice: the next write carries the tokens Apple
+ /// answered the last with, and where Apple's answer carried none the book is read again
+ /// for fresh ones before the next write goes.
+ #[tokio::test]
+ async fn a_run_of_writes_never_sends_a_spent_token() {
+ let server = MockServer::start().await;
+ // The first write is answered with new tokens; the second without any.
+ Mock::given(path_regex(r".*/co/contacts/card$"))
+ .and(wiremock::matchers::query_param("syncToken", "S1"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({
+ "contacts": [], "syncToken": "S2", "prefToken": "P1",
+ })))
+ .mount(&server)
+ .await;
+ Mock::given(path_regex(r".*/co/contacts/card$"))
+ .and(wiremock::matchers::query_param("syncToken", "S2"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({ "contacts": [] })))
+ .mount(&server)
+ .await;
+ Mock::given(path_regex(r".*/co/contacts/card$"))
+ .and(wiremock::matchers::query_param("syncToken", "S3"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({ "contacts": [] })))
+ .mount(&server)
+ .await;
+ // A spent token is refused as Apple refuses one.
+ Mock::given(path_regex(r".*/co/contacts/card$"))
+ .respond_with(ResponseTemplate::new(412))
+ .mount(&server)
+ .await;
+ Mock::given(path_regex(r".*/co/startup$"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({
+ "contacts": [], "syncToken": "S3", "prefToken": "P1",
+ })))
+ .mount(&server)
+ .await;
+ let http = reqwest::Client::new();
+ let ledger = Ledger::holding(Tokens {
+ sync: "S1".into(),
+ pref: "P0".into(),
+ });
+ for _ in 0..3 {
+ create(&http, &server.uri(), vec![], &ledger, &person())
+ .await
+ .expect("every write carries a live token");
+ }
+ let sent: Vec<(String, String)> = server
+ .received_requests()
+ .await
+ .unwrap()
+ .iter()
+ .map(|r| {
+ let q: std::collections::HashMap<_, _> = r.url.query_pairs().collect();
+ (
+ r.url
+ .path()
+ .rsplit('/')
+ .next()
+ .unwrap_or_default()
+ .to_string(),
+ q.get("syncToken")
+ .map(ToString::to_string)
+ .unwrap_or_default(),
+ )
+ })
+ .collect();
+ let pairs: Vec<(&str, &str)> = sent.iter().map(|(a, b)| (a.as_str(), b.as_str())).collect();
+ assert_eq!(
+ pairs,
+ [
+ ("card", "S1"),
+ ("card", "S2"),
+ ("startup", ""),
+ ("card", "S3")
+ ]
+ );
+
+ // A write that fails leaves no token behind either.
+ let ledger = Ledger::holding(Tokens {
+ sync: "OLD".into(),
+ pref: "P0".into(),
+ });
+ assert!(matches!(
+ create(&http, &server.uri(), vec![], &ledger, &person()).await,
+ Err(Error::Conflict)
+ ));
+ assert!(ledger.held().is_none());
+ }
+
+ /// An import in batches: the second batch went with the first's token, which the first
+ /// had spent. It now carries the token the first was answered with, or reads one.
+ #[tokio::test]
+ async fn an_import_in_batches_carries_a_live_token_for_each() {
+ let server = MockServer::start().await;
+ Mock::given(path_regex(r".*/co/import/$"))
+ .and(wiremock::matchers::query_param("syncToken", "S1"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({
+ "importedContactIds": ["N1"], "syncToken": "S2", "prefToken": "P",
+ })))
+ .mount(&server)
+ .await;
+ Mock::given(path_regex(r".*/co/import/$"))
+ .and(wiremock::matchers::query_param("syncToken", "S2"))
+ .respond_with(
+ ResponseTemplate::new(200).set_body_json(json!({ "importedContactIds": ["N2"] })),
+ )
+ .mount(&server)
+ .await;
+ Mock::given(path_regex(r".*/co/import/$"))
+ .and(wiremock::matchers::query_param("syncToken", "S3"))
+ .respond_with(
+ ResponseTemplate::new(200).set_body_json(json!({ "importedContactIds": ["N3"] })),
+ )
+ .mount(&server)
+ .await;
+ Mock::given(path_regex(r".*/co/import/$"))
+ .respond_with(ResponseTemplate::new(412))
+ .mount(&server)
+ .await;
+ Mock::given(path_regex(r".*/co/startup$"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({
+ "contacts": [], "syncToken": "S3", "prefToken": "P",
+ })))
+ .mount(&server)
+ .await;
+ let http = reqwest::Client::new();
+ let ledger = Ledger::holding(Tokens {
+ sync: "S1".into(),
+ pref: "P".into(),
+ });
+ let vcard = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:A\r\nEND:VCARD\r\n";
+ let mut ids = Vec::new();
+ for _ in 0..3 {
+ let (got, _) = import_vcards(&http, &server.uri(), vec![], &ledger, vcard)
+ .await
+ .expect("each batch carries a live token");
+ ids.extend(got);
+ }
+ assert_eq!(ids, ["N1", "N2", "N3"]);
+ }
+
/// A card Apple no longer has is already deleted: nothing is sent and nothing fails.
#[tokio::test]
async fn deleting_a_card_already_gone_succeeds_quietly() {
diff --git a/crates/quince-icloud/src/contacts_address.rs b/crates/quince-icloud/src/contacts_address.rs
index 59bfad4..7361510 100644
--- a/crates/quince-icloud/src/contacts_address.rs
+++ b/crates/quince-icloud/src/contacts_address.rs
@@ -7,7 +7,7 @@
//! sent with the card's current etag and the book's sync token, so Apple refuses it rather
//! than overwrite a change it has not seen.
-use super::{address_wire, postal_from, startup, write, Tokens};
+use super::{address_wire, card, postal_from, write, Ledger};
use crate::{Error, Result};
use quince_core::contacts::{country_code_for, PostalAddress, ADDRESS_FIELDS};
use serde::{Deserialize, Serialize};
@@ -333,29 +333,11 @@ pub async fn edit(
return Ok(after);
}
let mut attempt = 0;
+ let ledger = Ledger::new();
loop {
attempt += 1;
- let body = startup(http, root, params.clone()).await?;
- let token = |key: &str| {
- body.get(key)
- .and_then(Value::as_str)
- .map(ToString::to_string)
- .ok_or_else(|| Error::Protocol(format!("/co/startup carried no {key}")))
- };
- let tokens = Tokens {
- sync: token("syncToken")?,
- pref: token("prefToken")?,
- };
- let Some(card) = body
- .get("contacts")
- .and_then(Value::as_array)
- .and_then(|cards| {
- cards
- .iter()
- .find(|c| c.get("contactId").and_then(Value::as_str) == Some(guid))
- })
- .cloned()
- else {
+ // Read with the book's tokens, so the write goes against this very version.
+ let Some(card) = card(http, root, params.clone(), &ledger, guid).await? else {
return Err(Error::Refused(
"Apple doesn't have this contact (yet, or any more), so the address wasn't saved."
.into(),
@@ -371,7 +353,7 @@ pub async fn edit(
http,
root,
params.clone(),
- &tokens,
+ &ledger,
reqwest::Method::PUT,
card,
)
diff --git a/crates/quince-icloud/src/contacts_merge.rs b/crates/quince-icloud/src/contacts_merge.rs
index 3d89038..9bf7c1b 100644
--- a/crates/quince-icloud/src/contacts_merge.rs
+++ b/crates/quince-icloud/src/contacts_merge.rs
@@ -10,7 +10,7 @@
use super::{
address, card_written, contact_from, entry_label, entry_value, parse_lists, postal_from,
- startup, string, write, ContactList, Tokens, METHOD_KEYS,
+ startup, string, write, ContactList, Ledger, Tokens, METHOD_KEYS,
};
use crate::{Error, Result};
use quince_core::contacts::merge::{joined_notes, method_key};
@@ -213,10 +213,10 @@ pub async fn put(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
- tokens: &Tokens,
+ ledger: &Ledger,
card: Value,
) -> Result<()> {
- write(http, root, params, tokens, reqwest::Method::PUT, card).await?;
+ write(http, root, params, ledger, reqwest::Method::PUT, card).await?;
Ok(())
}
@@ -228,7 +228,7 @@ pub async fn delete_card(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
- tokens: &Tokens,
+ ledger: &Ledger,
card: &Value,
) -> Result<()> {
let Some(guid) = string(card, "contactId") else {
@@ -240,7 +240,7 @@ pub async fn delete_card(
http,
root,
query,
- tokens,
+ ledger,
reqwest::Method::POST,
serde_json::json!({ "contactId": guid, "etag": card.get("etag") }),
)
diff --git a/probe/examples/contact_photo_live.rs b/probe/examples/contact_photo_live.rs
index f108341..d16d72e 100644
--- a/probe/examples/contact_photo_live.rs
+++ b/probe/examples/contact_photo_live.rs
@@ -23,7 +23,7 @@ async fn main() -> Result<()> {
let read = |guid: String| {
let (http, root, p) = (http.clone(), root.clone(), p.clone());
async move {
- let card = c::card(&http, &root, p, &guid).await?;
+ let card = c::card(&http, &root, p, &c::Ledger::new(), &guid).await?;
let contact = card.as_ref().and_then(|v| {
c::parse_cards(&serde_json::json!({ "contacts": [v] }))
.pop()
@@ -50,6 +50,7 @@ async fn main() -> Result<()> {
methods: vec![],
};
let (_, tokens) = c::fetch_with_tokens(&http, &root, p.clone()).await?;
+ let tokens = c::Ledger::holding(tokens);
let guid = c::create(&http, &root, p.clone(), &tokens, &throwaway).await?;
let result = async {
// A picture added, as the form sends one: the JPEG as a data: URL with its square.
@@ -62,6 +63,7 @@ async fn main() -> Result<()> {
));
with.photo_crop = Some("0,0,600,600".into());
let (_, tokens) = c::fetch_with_tokens(&http, &root, p.clone()).await?;
+ let tokens = c::Ledger::holding(tokens);
c::update_from(
&http,
&root,
@@ -102,6 +104,7 @@ async fn main() -> Result<()> {
let mut moved = now.clone();
moved.photo_crop = Some("100,100,400,400".into());
let (_, tokens) = c::fetch_with_tokens(&http, &root, p.clone()).await?;
+ let tokens = c::Ledger::holding(tokens);
c::update_from(
&http,
&root,
@@ -129,6 +132,7 @@ async fn main() -> Result<()> {
none.photo_crop = None;
none.photo_signature = None;
let (_, tokens) = c::fetch_with_tokens(&http, &root, p.clone()).await?;
+ let tokens = c::Ledger::holding(tokens);
c::update_from(
&http,
&root,
@@ -154,6 +158,7 @@ async fn main() -> Result<()> {
let (_, last) = read(guid.clone()).await?;
if let Some(last) = last {
let (_, tokens) = c::fetch_with_tokens(&http, &root, p.clone()).await?;
+ let tokens = c::Ledger::holding(tokens);
c::delete(&http, &root, p.clone(), &tokens, &last).await?;
}
let (card, _) = read(guid).await?;
diff --git a/probe/examples/contacts_dedupe_import.rs b/probe/examples/contacts_dedupe_import.rs
index 6036d7f..28a0ab8 100644
--- a/probe/examples/contacts_dedupe_import.rs
+++ b/probe/examples/contacts_dedupe_import.rs
@@ -75,7 +75,14 @@ async fn main() -> Result<()> {
return Ok(());
}
let (first, original) = &copies[0];
- c::delete(&http, &root, p.clone(), &tokens, first).await?;
+ c::delete(
+ &http,
+ &root,
+ p.clone(),
+ &c::Ledger::holding(tokens.clone()),
+ first,
+ )
+ .await?;
let (book2, mut tokens) = c::fetch_with_tokens(&http, &root, p.clone()).await?;
let kept = book2.iter().any(|x| &x.guid == original);
let gone = !book2.iter().any(|x| x.guid == first.guid);
@@ -90,7 +97,15 @@ async fn main() -> Result<()> {
if !book2.iter().any(|x| x.guid == original) {
continue;
}
- match c::delete(&http, &root, p.clone(), &tokens, &copy).await {
+ match c::delete(
+ &http,
+ &root,
+ p.clone(),
+ &c::Ledger::holding(tokens.clone()),
+ &copy,
+ )
+ .await
+ {
// Each delete moves the book's sync token on: read it before the next.
Ok(()) => {
done += 1;
diff --git a/probe/examples/contacts_import_known_live.rs b/probe/examples/contacts_import_known_live.rs
index 2a64a3b..3a78ed1 100644
--- a/probe/examples/contacts_import_known_live.rs
+++ b/probe/examples/contacts_import_known_live.rs
@@ -17,6 +17,7 @@ async fn main() -> Result<()> {
use quince_icloud::contacts as c;
let card = "BEGIN:VCARD\r\nVERSION:3.0\r\nN:test;Quince dup;;;\r\nFN:Quince dup test\r\nTEL;type=CELL:+1 555 0199\r\nEND:VCARD\r\n";
let (book, tokens) = c::fetch_with_tokens(&http, &root, p.clone()).await?;
+ let tokens = c::Ledger::holding(tokens);
println!("before: known {}", c::already_in(card, &book));
let (ids, _) = c::import_vcards(&http, &root, p.clone(), &tokens, card).await?;
let id = ids
@@ -24,6 +25,7 @@ async fn main() -> Result<()> {
.ok_or_else(|| anyhow!("nothing imported"))?
.clone();
let (book, tokens) = c::fetch_with_tokens(&http, &root, p.clone()).await?;
+ let tokens = c::Ledger::holding(tokens);
println!("after import: known {}", c::already_in(card, &book));
let other = card.replace("0199", "0123");
println!(
diff --git a/probe/examples/contacts_list_roundtrip.rs b/probe/examples/contacts_list_roundtrip.rs
index bcac23c..09c2f92 100644
--- a/probe/examples/contacts_list_roundtrip.rs
+++ b/probe/examples/contacts_list_roundtrip.rs
@@ -15,6 +15,7 @@ async fn main() -> Result<()> {
let (http, p) = (client.http().clone(), client.service_params());
use quince_icloud::contacts as c;
let (people, tokens) = c::fetch_with_tokens(&http, &root, p.clone()).await?;
+ let tokens = c::Ledger::holding(tokens);
let (a, b) = (people[0].guid.clone(), people[1].guid.clone());
let show = |label: &str, lists: &[c::ContactList]| {
let mine: Vec<_> = lists
@@ -42,6 +43,7 @@ async fn main() -> Result<()> {
.cloned()
.ok_or_else(|| anyhow!("not listed"))?;
let (_, tokens) = c::fetch_with_tokens(&http, &root, p.clone()).await?;
+ let tokens = c::Ledger::holding(tokens);
list.name = "Quince list test, renamed".into();
list.members.push(b);
c::update_list(&http, &root, p.clone(), &tokens, &list).await?;
@@ -50,6 +52,7 @@ async fn main() -> Result<()> {
&c::fetch(&http, &root, p.clone()).await?.lists,
);
let (_, tokens) = c::fetch_with_tokens(&http, &root, p.clone()).await?;
+ let tokens = c::Ledger::holding(tokens);
c::delete_list(&http, &root, p.clone(), &tokens, &list.guid).await?;
show("deleted", &c::fetch(&http, &root, p.clone()).await?.lists);
Ok(())
diff --git a/probe/examples/contacts_pass_live.rs b/probe/examples/contacts_pass_live.rs
index 777b83e..cadbacb 100644
--- a/probe/examples/contacts_pass_live.rs
+++ b/probe/examples/contacts_pass_live.rs
@@ -69,7 +69,14 @@ async fn main() -> Result<()> {
value: "[email protected]".into(),
}],
};
- let guid = contacts::create(&http, &root, p.clone(), &tokens, &throwaway).await?;
+ let guid = contacts::create(
+ &http,
+ &root,
+ p.clone(),
+ &contacts::Ledger::holding(tokens.clone()),
+ &throwaway,
+ )
+ .await?;
let (after, ms) = run(Some(tokens.clone())).await?;
say("after a card was made", &after, ms);
let ok = matches!(&after, Pass::Read(b, _) if b.contacts.len() == before + 1 && b.contacts.iter().any(|c| c.guid == guid));
@@ -83,7 +90,14 @@ async fn main() -> Result<()> {
};
let made = made
.ok_or_else(|| anyhow!("throwaway not read back — delete it by hand: Quince mount test"))?;
- contacts::delete(&http, &root, p.clone(), &tokens, &made).await?;
+ contacts::delete(
+ &http,
+ &root,
+ p.clone(),
+ &contacts::Ledger::holding(tokens.clone()),
+ &made,
+ )
+ .await?;
let (gone, ms) = run(Some(tokens)).await?;
say("after it was deleted", &gone, ms);
let ok = matches!(&gone, Pass::Read(b, _) if b.contacts.len() == before && !b.contacts.iter().any(|c| c.guid == guid));
diff --git a/probe/examples/contacts_vcard.rs b/probe/examples/contacts_vcard.rs
index cfb6fbe..57a8668 100644
--- a/probe/examples/contacts_vcard.rs
+++ b/probe/examples/contacts_vcard.rs
@@ -15,6 +15,7 @@ async fn main() -> Result<()> {
let (http, p) = (client.http().clone(), client.service_params());
use quince_icloud::contacts as c;
let (_, tokens) = c::fetch_with_tokens(&http, &root, p.clone()).await?;
+ let tokens = c::Ledger::holding(tokens);
let card = "BEGIN:VCARD\r\nVERSION:3.0\r\nN:test;Quince vCard;;;\r\nFN:Quince vCard test\r\nTEL;type=CELL:+1 555 0100\r\nEND:VCARD\r\n";
let (ids, failed) = c::import_vcards(&http, &root, p.clone(), &tokens, card).await?;
println!("imported {ids:?}, {failed} failed");
@@ -23,6 +24,7 @@ async fn main() -> Result<()> {
.ok_or_else(|| anyhow!("nothing imported"))?
.clone();
let (_, tokens) = c::fetch_with_tokens(&http, &root, p.clone()).await?;
+ let tokens = c::Ledger::holding(tokens);
let out = c::export_vcards(
&http,
&root,
@@ -39,6 +41,7 @@ async fn main() -> Result<()> {
out.contains("555")
);
let (people, tokens) = c::fetch_with_tokens(&http, &root, p.clone()).await?;
+ let tokens = c::Ledger::holding(tokens);
let mine = people
.iter()
.find(|x| x.guid == id)
diff --git a/probe/src/main.rs b/probe/src/main.rs
index 500bd1f..f8d2e0a 100644
--- a/probe/src/main.rs
+++ b/probe/src/main.rs
@@ -1762,6 +1762,7 @@ async fn main() -> Result<()> {
match quince_icloud::contacts::fetch_with_tokens(&http, &root, params.clone()).await {
Err(e) => println!(" \x1b[31mFAIL\x1b[0m tokens: {e}"),
Ok((_, tokens)) => {
+ let tokens = quince_icloud::contacts::Ledger::holding(tokens);
let draft = quince_core::contacts::Contact {
guid: String::new(),
first_name: Some("Quince".into()),
@@ -1814,11 +1815,12 @@ async fn main() -> Result<()> {
params.clone(),
)
.await
- .map(|(_, t)| t);
+ .map(|(_, t)| quince_icloud::contacts::Ledger::holding(t));
let card = quince_icloud::contacts::card(
&http,
&root,
params.clone(),
+ &quince_icloud::contacts::Ledger::new(),
&guid,
)
.await
@@ -1858,7 +1860,7 @@ async fn main() -> Result<()> {
params.clone(),
)
.await
- .map(|(_, t)| t);
+ .map(|(_, t)| quince_icloud::contacts::Ledger::holding(t));
match (latest, tokens) {
(Some(c), Ok(tokens)) => match quince_icloud::contacts::delete(
&http, &root, params.clone(), &tokens, &c,
diff --git a/src-tauri/src/contacts_merge.rs b/src-tauri/src/contacts_merge.rs
index 5f266a6..b005136 100644
--- a/src-tauri/src/contacts_merge.rs
+++ b/src-tauri/src/contacts_merge.rs
@@ -228,12 +228,16 @@ pub async fn send(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
+ ledger: &c::Ledger,
p: &Pending,
) -> Result<(), Error> {
let m: ContactMerge = serde_json::from_str(&p.payload)
.map_err(|e| Error::Refused(format!("A queued merge could not be read: {e}")))?;
let book = match apple::read_book(http, root, params.clone()).await {
- Ok(book) => book,
+ Ok(book) => {
+ ledger.hold(book.tokens.clone());
+ book
+ }
Err(e) => {
// Nothing of it reached Apple: it can still be undone.
if unreached(&e) {
@@ -267,7 +271,7 @@ pub async fn send(
}
}
if card != kept {
- apple::put(http, root, params.clone(), &book.tokens, card).await?;
+ apple::put(http, root, params.clone(), ledger, card).await?;
}
// Into every list the others were in.
@@ -277,6 +281,7 @@ pub async fn send(
.map(|a| a.card.contact.guid.as_str())
.collect();
let book = apple::read_book(http, root, params.clone()).await?;
+ ledger.hold(book.tokens.clone());
for mut list in book.lists() {
let had = list
.members
@@ -284,24 +289,25 @@ pub async fn send(
.any(|x| absorbed.iter().any(|a| same(a, x)));
if had && !list.members.iter().any(|x| same(x, &p.record)) {
list.members.push(p.record.clone());
- let tokens = apple::read_book(http, root, params.clone()).await?.tokens;
- c::update_list(http, root, params.clone(), &tokens, &list).await?;
+ c::update_list(http, root, params.clone(), ledger, &list).await?;
}
}
// And the others gone. My Card moves to the kept card first, if it was one of them.
let book = apple::read_book(http, root, params.clone()).await?;
+ ledger.hold(book.tokens.clone());
let me = book.me();
if me
.as_deref()
.is_some_and(|me| absorbed.iter().any(|a| same(a, me)))
{
- c::set_me_card(http, root, params.clone(), &book.tokens, &p.record).await?;
+ c::set_me_card(http, root, params.clone(), ledger, &p.record).await?;
}
for guid in absorbed {
let book = apple::read_book(http, root, params.clone()).await?;
+ ledger.hold(book.tokens.clone());
if let Some(card) = book.card(guid) {
- apple::delete_card(http, root, params.clone(), &book.tokens, &card).await?;
+ apple::delete_card(http, root, params.clone(), ledger, &card).await?;
}
}
Ok(())
@@ -420,11 +426,11 @@ pub async fn contacts_merge_undo(kept: String, data: State<'_, Data>) -> UiResul
}
#[cfg(test)]
-mod tests {
+pub(crate) mod tests {
use super::*;
use quince_core::contacts::ContactMethod;
- fn person(guid: &str, first: &str, phone: &str) -> Card {
+ pub(crate) fn person(guid: &str, first: &str, phone: &str) -> Card {
Card {
contact: Contact {
guid: guid.into(),
@@ -568,7 +574,7 @@ mod tests {
/// Apple's contacts service, enough of it to merge against: it keeps the cards and lists
/// it is sent, and counts every write.
- mod apple_stub {
+ pub(crate) mod apple_stub {
use serde_json::{json, Value};
use std::sync::{Arc, Mutex as StdMutex};
use wiremock::{Request, Respond, ResponseTemplate};
@@ -579,6 +585,8 @@ mod tests {
pub groups: Vec<Value>,
pub writes: Vec<String>,
pub version: u64,
+ /// Whether a write is answered with the new tokens, as Apple may or may not.
+ pub answers_tokens: bool,
}
#[derive(Clone)]
@@ -602,8 +610,23 @@ mod tests {
"contacts": b.cards, "groups": b.groups, "contactsOrder": order,
}));
}
+ // As Apple: a write carrying a sync token an earlier write spent is refused.
+ let token = req
+ .url
+ .query_pairs()
+ .find(|(k, _)| k == "syncToken")
+ .map(|(_, v)| v.to_string());
+ if token.as_deref() != Some(format!("S{}", b.version).as_str()) {
+ b.writes.push(format!("refused, spent token {token:?}"));
+ return ResponseTemplate::new(412);
+ }
b.version += 1;
let v = b.version;
+ let answer = if b.answers_tokens {
+ json!({ "syncToken": format!("S{v}"), "prefToken": "P" })
+ } else {
+ json!({})
+ };
if path.contains("/co/contacts/card") {
let card = body["contacts"][0].clone();
let id = card["contactId"].as_str().unwrap_or("").to_string();
@@ -623,7 +646,7 @@ mod tests {
b.groups.retain(|x| x["groupId"] != g["groupId"]);
b.groups.push(g);
}
- ResponseTemplate::new(200).set_body_json(json!({}))
+ ResponseTemplate::new(200).set_body_json(answer)
}
}
}
@@ -632,6 +655,17 @@ mod tests {
/// other is gone; sent again — a send cut off, retried — nothing more is written.
#[test]
fn a_merge_sent_twice_lands_once_and_loses_nothing() {
+ merge_sent_twice(false);
+ }
+
+ /// The same, where Apple answers each write with its new tokens: the writes in a row go
+ /// with those, and none is refused for a token an earlier write spent.
+ #[test]
+ fn a_merge_carries_the_tokens_each_write_is_answered_with() {
+ merge_sent_twice(true);
+ }
+
+ fn merge_sent_twice(answers_tokens: bool) {
use serde_json::json;
tauri::async_runtime::block_on(async {
let server = wiremock::MockServer::start().await;
@@ -650,7 +684,9 @@ mod tests {
],
groups: vec![
json!({ "groupId": "L1", "name": "Family", "contactIds": ["A", "C"], "etag": "g1" }),
+ json!({ "groupId": "L2", "name": "Work", "contactIds": ["A"], "etag": "g2" }),
],
+ answers_tokens,
..Default::default()
},
)));
@@ -664,7 +700,9 @@ mod tests {
assert_eq!(kept, "B", "the card holding the most");
let p = lib.lock().await.outbox_pending(CONTACTS).unwrap().remove(0);
let http = reqwest::Client::new();
- send(&lib, &http, &server.uri(), vec![], &p).await.unwrap();
+ send(&lib, &http, &server.uri(), vec![], &c::Ledger::new(), &p)
+ .await
+ .unwrap();
{
let b = apple.0.lock().unwrap();
@@ -691,11 +729,14 @@ mod tests {
.as_array()
.unwrap()
.contains(&json!(kept)));
- assert_eq!(b.writes.len(), 3, "{:?}", b.writes);
+ // The card, two lists, the other card gone — and not one write refused.
+ assert_eq!(b.writes.len(), 4, "{:?}", b.writes);
}
// Sent again: nothing more to write.
- send(&lib, &http, &server.uri(), vec![], &p).await.unwrap();
- assert_eq!(apple.0.lock().unwrap().writes.len(), 3);
+ send(&lib, &http, &server.uri(), vec![], &c::Ledger::new(), &p)
+ .await
+ .unwrap();
+ assert_eq!(apple.0.lock().unwrap().writes.len(), 4);
});
}
}
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs
index 9481fe8..115ce8f 100644
--- a/src-tauri/src/main.rs
+++ b/src-tauri/src/main.rs
@@ -4048,6 +4048,7 @@ async fn contact_list_write(
quince_icloud::contacts::fetch_book_with_tokens(&http, &root, params.clone())
.await
.map_err(|e| e.to_string())?;
+ let tokens = quince_icloud::contacts::Ledger::holding(tokens);
let written = match guid.as_deref().filter(|g| !g.is_empty()) {
None => {
quince_icloud::contacts::create_list(
@@ -4212,9 +4213,8 @@ async fn contacts_export(
return Err("Choose someone to export.".into());
}
let (http, root, params) = contacts_context(&auth).await?;
- let (_, tokens) = quince_icloud::contacts::fetch_with_tokens(&http, &root, params.clone())
- .await
- .map_err(|e| e.to_string())?;
+ // An export changes nothing; its tokens are read with the book on the way.
+ let tokens = quince_icloud::contacts::Ledger::new();
let vcard =
quince_icloud::contacts::export_vcards(&http, &root, params, &tokens, &guids, &file_name)
.await
@@ -4274,6 +4274,9 @@ async fn contacts_import(
let (book, tokens) = quince_icloud::contacts::fetch_with_tokens(&http, &root, params.clone())
.await
.map_err(|e| e.to_string())?;
+ // Each batch is a write that moves the book on: the next carries the tokens Apple
+ // answered it with, or reads them again — never the first batch's spent ones.
+ let tokens = quince_icloud::contacts::Ledger::holding(tokens);
// Someone already in Contacts is left out, as Apple's Contacts leaves them: an export of
// this same book imported back once doubled every person in it.
let before = cards.len();
@@ -4317,9 +4320,7 @@ async fn contact_set_me(
) -> UiResult<()> {
let _one = CONTACTS_PASS.lock().await;
let (http, root, params) = contacts_context(&auth).await?;
- let (_, tokens) = quince_icloud::contacts::fetch_with_tokens(&http, &root, params.clone())
- .await
- .map_err(|e| e.to_string())?;
+ let tokens = quince_icloud::contacts::Ledger::new();
quince_icloud::contacts::set_me_card(&http, &root, params.clone(), &tokens, &guid)
.await
.map_err(|e| e.to_string())?;
diff --git a/src-tauri/src/outbox.rs b/src-tauri/src/outbox.rs
index 11e3dfd..9597b9e 100644
--- a/src-tauri/src/outbox.rs
+++ b/src-tauri/src/outbox.rs
@@ -1753,10 +1753,13 @@ async fn drain_contacts(app: &AppHandle) -> Outcome {
let Some(account) = crate::asked_as(&params) else {
return Outcome::Later;
};
- // The book read once for the run, not once for each change in it.
+ // The book read once for the run, not once for each change in it — while Apple's answers
+ // keep the run in tokens. The ledger holds the tokens the next write carries: the read's,
+ // then each write's answer's, never one an earlier write spent.
let book = std::sync::Mutex::new(Book::default());
+ let ledger = quince_icloud::contacts::Ledger::new();
let report = drain_as(&data.0, CONTACTS, &crate::CONTACTS_PASS, &account, |p| {
- send_contact(&data.0, &http, &root, params.clone(), p, &book)
+ send_contact(&data.0, &http, &root, params.clone(), p, &book, &ledger)
})
.await;
tell_refused(app, CONTACTS, &report);
@@ -1769,25 +1772,28 @@ async fn drain_contacts(app: &AppHandle) -> Outcome {
/// The address book a run of queued contact changes is sent from: read once for the run, not
/// once for each change — every read is the whole book, every card in it. A card the run has
-/// written since is read again before it is written again (the copy here is from before), and
-/// a write Apple says was made against an old book is sent once more from a fresh read.
+/// written since is read again before it is written again (the copy here is from before); so
+/// is the book when the last write's answer left no tokens to carry on with (see
+/// [`quince_icloud::contacts::Ledger`]); and a write Apple says was made against an old book
+/// is sent once more from a fresh read.
#[derive(Default)]
struct Book {
read: Option<std::sync::Arc<BookRead>>,
written: std::collections::HashSet<String>,
}
-/// One read of the book: each card as Apple wrote it, by id, and the tokens a write needs.
+/// One read of the book: each card as Apple wrote it, by id.
struct BookRead {
cards: std::collections::HashMap<String, serde_json::Value>,
- tokens: quince_icloud::contacts::Tokens,
}
impl Book {
- /// The book to send a change to `record` from, read now if the run has none it can use;
- /// and whether it was read just now.
+ /// The book to send a change to `record` from, read now if the run has none it can use —
+ /// none read yet, `record` written since, or no live tokens left to write with; and
+ /// whether it was read just now. A read hands its tokens to `ledger`.
async fn for_record(
book: &std::sync::Mutex<Self>,
+ ledger: &quince_icloud::contacts::Ledger,
http: &reqwest::Client,
root: &str,
params: &[(String, String)],
@@ -1797,13 +1803,16 @@ impl Book {
let b = book
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
- b.read.clone().filter(|_| !b.written.contains(record))
+ b.read
+ .clone()
+ .filter(|_| !b.written.contains(record) && ledger.has_tokens())
};
if let Some(read) = kept {
return Ok((read, false));
}
let (cards, tokens) =
quince_icloud::contacts::cards_with_tokens(http, root, params.to_vec()).await?;
+ ledger.hold(tokens);
let cards = cards
.into_iter()
.filter_map(|card| {
@@ -1814,7 +1823,7 @@ impl Book {
let mut b = book
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
- let read = std::sync::Arc::new(BookRead { cards, tokens });
+ let read = std::sync::Arc::new(BookRead { cards });
b.read = Some(std::sync::Arc::clone(&read));
b.written.clear();
Ok((read, true))
@@ -1843,28 +1852,30 @@ async fn send_contact(
params: Vec<(String, String)>,
p: Pending,
book: &std::sync::Mutex<Book>,
+ ledger: &quince_icloud::contacts::Ledger,
) -> Result<(), Error> {
use quince_icloud::contacts as c;
if p.kind == "merge" {
// A merge writes and deletes cards of its own, read by itself.
Book::forget(book);
- return crate::contacts_merge::send(library, http, root, params, &p).await;
+ return crate::contacts_merge::send(library, http, root, params, ledger, &p).await;
}
if p.kind == "address" {
let a: ContactAddress = serde_json::from_str(&p.payload).map_err(|e| unreadable(&e))?;
Book::wrote(book, &p.record);
- return c::address::edit(http, root, params, &p.record, &a.base, &a.edits)
- .await
- .map(|_| ());
+ // It reads the card and its tokens itself: the run's tokens are spent by its write.
+ let edited = c::address::edit(http, root, params, &p.record, &a.base, &a.edits).await;
+ ledger.spend();
+ return edited.map(|_| ());
}
- let (read, fresh) = Book::for_record(book, http, root, &params, &p.record).await?;
- let sent = send_contact_from(http, root, params.clone(), &p, &read).await;
+ let (read, fresh) = Book::for_record(book, ledger, http, root, &params, &p.record).await?;
+ let sent = send_contact_from(http, root, params.clone(), &p, &read, ledger).await;
if matches!(sent, Err(Error::Conflict)) && !fresh {
// Made against a book read earlier in the run, which Apple says is old now: from a
// fresh read, once.
Book::forget(book);
- let (read, _) = Book::for_record(book, http, root, &params, &p.record).await?;
- let again = send_contact_from(http, root, params, &p, &read).await;
+ let (read, _) = Book::for_record(book, ledger, http, root, &params, &p.record).await?;
+ let again = send_contact_from(http, root, params, &p, &read, ledger).await;
Book::wrote(book, &p.record);
return again;
}
@@ -1872,24 +1883,25 @@ async fn send_contact(
sent
}
-/// One queued card change sent, from `read`: Apple's card as it was read, and its tokens.
+/// One queued card change sent, from `read`: Apple's card as it was read, with the tokens
+/// `ledger` holds.
async fn send_contact_from(
http: &reqwest::Client,
root: &str,
params: Vec<(String, String)>,
p: &Pending,
read: &BookRead,
+ ledger: &quince_icloud::contacts::Ledger,
) -> Result<(), Error> {
use quince_icloud::contacts as c;
- let tokens = &read.tokens;
let card = read.cards.get(&p.record);
if p.kind == "delete" {
- // Gone already — deleted elsewhere, or by an earlier send of this one.
- let Some(card) = card.and_then(|v| c::parse(&serde_json::json!({ "contacts": [v] })).pop())
- else {
+ // Gone already — deleted elsewhere, or by an earlier send of this one. Otherwise
+ // against the version read: changed since, Apple refuses it as a conflict.
+ let Some(card) = card else {
return Ok(());
};
- return c::delete(http, root, params, tokens, &card).await;
+ return c::merge::delete_card(http, root, params, ledger, card).await;
}
let s: ContactSave = serde_json::from_str(&p.payload).map_err(|e| unreadable(&e))?;
if let Some(details) = &s.details {
@@ -1899,7 +1911,11 @@ async fn send_contact_from(
};
let mine = whole(&s.contact, details);
if s.create {
- return c::create_card_named(http, root, params, tokens, &p.record, &mine).await;
+ // Already made — an earlier send of this one — is done.
+ if card.is_some() {
+ return Ok(());
+ }
+ return c::create_card_named(http, root, params, ledger, &p.record, &mine).await;
}
// A base from before the form showed these has none: compared with Apple's own, so
// only what this form changed is written.
@@ -1916,16 +1932,19 @@ async fn send_contact_from(
.base
.as_ref()
.map(|b| whole(b, &base_details.clone().unwrap_or_default()));
- return c::update_card(http, root, params, tokens, &mine, card, base.as_ref()).await;
+ return c::update_card(http, root, params, ledger, &mine, card, base.as_ref()).await;
}
if s.create {
- return c::create_named(http, root, params, tokens, &p.record, &s.contact).await;
+ if card.is_some() {
+ return Ok(());
+ }
+ return c::create_named(http, root, params, ledger, &p.record, &s.contact).await;
}
c::update_from(
http,
root,
params,
- tokens,
+ ledger,
&s.contact,
card,
s.base.as_ref(),
@@ -3706,8 +3725,9 @@ mod tests {
}
/// Three cards edited offline go in one run from one read of the address book — each read
- /// is the whole book — not two reads for each card; a card edited twice is read again
- /// before its second write, its copy from before the first being old.
+ /// is the whole book — not two reads for each card, while Apple answers each write with
+ /// the tokens for the next; a card edited twice is read again before its second write, its
+ /// copy from before the first being old.
#[tokio::test]
async fn queued_card_edits_are_sent_from_one_read_of_the_book() {
use wiremock::matchers::{method, path, path_regex};
@@ -3722,10 +3742,14 @@ mod tests {
})))
.mount(&server)
.await;
+ let answered = std::sync::atomic::AtomicU64::new(0);
Mock::given(path_regex(r".*/co/contacts/card$"))
- .respond_with(
- ResponseTemplate::new(200).set_body_json(serde_json::json!({ "contacts": [] })),
- )
+ .respond_with(move |_: &wiremock::Request| {
+ let n = answered.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
+ ResponseTemplate::new(200).set_body_json(serde_json::json!({
+ "contacts": [], "syncToken": format!("S{n}"), "prefToken": "P",
+ }))
+ })
.mount(&server)
.await;
@@ -3742,9 +3766,10 @@ mod tests {
let lib = crate::library_lock::LibraryLock::new(l, None);
let http = reqwest::Client::new();
let book = std::sync::Mutex::new(Book::default());
+ let ledger = quince_icloud::contacts::Ledger::new();
let root = server.uri();
let report = drain_as(&lib, CONTACTS, &Mutex::new(()), "me", |p| {
- send_contact(&lib, &http, &root, Vec::new(), p, &book)
+ send_contact(&lib, &http, &root, Vec::new(), p, &book, &ledger)
})
.await;
assert!(report.refused.is_empty(), "{:?}", report.refused);
@@ -4063,4 +4088,81 @@ mod held_tests {
);
assert!(!held(NOTES, &[]), "approved: sent");
}
+
+ /// Contacts waiting in the queue go one after another, each write moving the book's sync
+ /// token on. Against an Apple that refuses a spent token, every one lands — whether Apple
+ /// answers a write with its new tokens or not — and none is sent with the token an
+ /// earlier one spent.
+ #[test]
+ fn queued_contact_writes_never_send_a_spent_token() {
+ use crate::contacts_merge::tests::{apple_stub, person};
+ use serde_json::json;
+ for answers_tokens in [false, true] {
+ tauri::async_runtime::block_on(async {
+ let server = wiremock::MockServer::start().await;
+ let apple = apple_stub::Apple(std::sync::Arc::new(std::sync::Mutex::new(
+ apple_stub::Book {
+ cards: vec![
+ json!({ "contactId": "A", "etag": "ea", "firstName": "Zoya" }),
+ json!({ "contactId": "B", "etag": "eb", "firstName": "Ravi" }),
+ json!({ "contactId": "C", "etag": "ec", "firstName": "Old" }),
+ ],
+ answers_tokens,
+ ..Default::default()
+ },
+ )));
+ wiremock::Mock::given(wiremock::matchers::any())
+ .respond_with(apple.clone())
+ .mount(&server)
+ .await;
+ let mut l = Library::in_memory().unwrap();
+ let (a, b, c) = (
+ person("A", "Zoya", "+91 90000 00001").contact,
+ person("B", "Ravi", "+91 90000 00002").contact,
+ person("C", "Old", "+91 90000 00003").contact,
+ );
+ l.put_contacts(&[a.clone(), b.clone(), c.clone()]).unwrap();
+ let mut a2 = a.clone();
+ a2.first_name = Some("Zoya R".into());
+ let mut b2 = b.clone();
+ b2.first_name = Some("Ravi K".into());
+ record_contact_save(&mut l, &a2, Some(a)).unwrap();
+ record_contact_save(&mut l, &b2, Some(b)).unwrap();
+ record_contact_delete(&mut l, &c).unwrap();
+ let lib = crate::library_lock::LibraryLock::new(l, None);
+ let http = reqwest::Client::new();
+ let ledger = quince_icloud::contacts::Ledger::new();
+ let pass = Mutex::new(());
+ let uri = server.uri();
+ let book = std::sync::Mutex::new(Book::default());
+ let report = drain(&lib, CONTACTS, &pass, |p| {
+ send_contact(&lib, &http, &uri, vec![], p, &book, &ledger)
+ })
+ .await;
+ assert!(lib
+ .lock()
+ .await
+ .outbox_pending(CONTACTS)
+ .unwrap()
+ .is_empty());
+ let book = apple.0.lock().unwrap();
+ assert!(report.refused.is_empty(), "{:?}", report.refused);
+ assert!(
+ !book.writes.iter().any(|w| w.starts_with("refused")),
+ "answers_tokens {answers_tokens}: {:?}",
+ book.writes
+ );
+ assert_eq!(book.writes.len(), 3, "{:?}", book.writes);
+ let first = |id: &str| {
+ book.cards
+ .iter()
+ .find(|c| c["contactId"] == id)
+ .map(|c| c["firstName"].clone())
+ };
+ assert_eq!(first("A"), Some(json!("Zoya R")));
+ assert_eq!(first("B"), Some(json!("Ravi K")));
+ assert_eq!(first("C"), None);
+ });
+ }
+ }
}