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
Photos: edits keep what another device changed meanwhile
Yashwanth Kamireddifb11ada1a5de
diff --git a/crates/quince-icloud/src/photos.rs b/crates/quince-icloud/src/photos.rsindex 65a448d..0111297 100644--- a/crates/quince-icloud/src/photos.rs+++ b/crates/quince-icloud/src/photos.rs@@ -268,9 +268,10 @@ fn with_asset(mut p: Photo, asset: &Value) -> Photo {p.burst = burst_of(asset);p.burst_pick = burst_pick_of(asset);}- if let Some((lat, lon)) = location_of(asset) {- (p.latitude, p.longitude) = (Some(lat), Some(lon));- }+ // The asset is where a place is kept (as the full read takes it), and a changed asset+ // comes whole: none there is none at all — removed on the phone, it goes here too.+ let place = location_of(asset);+ (p.latitude, p.longitude) = (place.map(|l| l.0), place.map(|l| l.1));p.expunge_at = if p.deleted {millis(asset, "dateExpunged").or(p.expunge_at)} else {@@ -553,22 +554,32 @@ pub async fn set_title_and_caption(ckdatabasews_root: &str,params: Vec<(String, String)>,asset: &str,- change_tag: Option<&str>,- title: &str,- caption: &str,-) -> Result<Value> {+ title: Option<&str>,+ caption: Option<&str>,+) -> Result<Option<Value>> {+ // Only what was changed here is written, over the photo as Apple holds it now: sending+ // both, and sending them again over a version another device had just written, put back+ // an old caption over one added on the phone.+ let mut fields = serde_json::Map::new();+ if let Some(t) = title {+ fields.insert(+ "captionEnc".into(),+ serde_json::json!({ "value": album_name(t) }),+ );+ }+ if let Some(c) = caption {+ fields.insert(+ "extendedDescEnc".into(),+ serde_json::json!({ "value": album_name(c) }),+ );+ }+ if fields.is_empty() {+ return Ok(None);+ }+ let fields = Value::Object(fields);let client = ZoneClient::new(http, ckdatabasews_root, CONTAINER, params);client- .update_record(- ZONE,- asset,- "CPLAsset",- change_tag,- serde_json::json!({- "captionEnc": { "value": album_name(title) },- "extendedDescEnc": { "value": album_name(caption) },- }),- )+ .edit_record(ZONE, asset, "CPLAsset", |_| Some(fields.clone())).await}@@ -601,25 +612,55 @@ pub async fn set_keywords(ckdatabasews_root: &str,params: Vec<(String, String)>,asset: &str,- change_tag: Option<&str>,- keywords: &[String],-) -> Result<Value> {- let list = plist::Value::Array(keywords.iter().cloned().map(plist::Value::String).collect());- let mut bytes = Vec::new();- plist::to_writer_binary(&mut bytes, &list)- .map_err(|e| crate::Error::Protocol(format!("keywords could not be written: {e}")))?;+ base: &[String],+ wanted: &[String],+) -> Result<(Value, Vec<String>)> {+ // The change made here — words added, words taken away from `base`, the list as it was+ // read — laid over the list Apple holds now, so a word added on another device meanwhile+ // stays. (The whole list sent, and sent again on a conflict, took it away.)+ let same = |a: &str, b: &str| a.eq_ignore_ascii_case(b);+ let added: Vec<String> = wanted+ .iter()+ .filter(|w| !base.iter().any(|b| same(b, w)))+ .cloned()+ .collect();+ let removed: Vec<String> = base+ .iter()+ .filter(|b| !wanted.iter().any(|w| same(w, b)))+ .cloned()+ .collect();+ let result = std::sync::Mutex::new(Vec::new());let client = ZoneClient::new(http, ckdatabasews_root, CONTAINER, params);- client- .update_record(- ZONE,- asset,- "CPLAsset",- change_tag,- serde_json::json!({+ let written = client+ .edit_record(ZONE, asset, "CPLAsset", |current| {+ let mut words: Vec<String> = keywords_of(current)+ .into_iter()+ .filter(|w| !removed.iter().any(|r| same(r, w)))+ .collect();+ for a in &added {+ if !words.iter().any(|w| same(w, a)) {+ words.push(a.clone());+ }+ }+ let list =+ plist::Value::Array(words.iter().cloned().map(plist::Value::String).collect());+ let mut bytes = Vec::new();+ plist::to_writer_binary(&mut bytes, &list).ok()?;+ *result+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner) = words;+ Some(serde_json::json!({"keywordsEnc": { "value": base64::engine::general_purpose::STANDARD.encode(bytes) }- }),- )- .await+ }))+ })+ .await?+ .ok_or_else(|| crate::Error::Protocol("keywords could not be written".into()))?;+ let words = std::mem::take(+ &mut *result+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner),+ );+ Ok((written, words))}/// When a photo was taken, changed, as icloud.com's Adjust Date and Time changes it: `assetDate`,@@ -767,16 +808,19 @@ pub async fn add_to_album(album: &str,assets: &[String],last_position: Option<i64>,-) -> Result<Vec<(String, i64)>> {+) -> (Vec<(String, i64)>, Result<()>) {let client = ZoneClient::new(http, ckdatabasews_root, CONTAINER, params);let mut at = last_position.unwrap_or(ITEM_SPACING);+ // What went in, even when a later one fails: the library records those, so they are not+ // shown as missing and asked for again.let mut added = Vec::new();for asset in assets {at = (at + ITEM_SPACING).min(MAX_POSITION);- client+ let name = format!("{asset}-IN-{album}");+ let made = client.create_record(ZONE,- &format!("{asset}-IN-{album}"),+ &name,"CPLContainerRelation",serde_json::json!({"itemId": { "value": asset },@@ -784,10 +828,34 @@ pub async fn add_to_album("containerId": { "value": album },}),)- .await?;+ .await;+ if let Err(e) = made {+ // Already there — added before a failure, or taken out on another device (marked,+ // not removed): in the album now, revived if it was taken out.+ match client.lookup_record(ZONE, &name).await {+ Ok(Some(existing)) => {+ if field::int(&existing, "isDeleted") == Some(1) {+ let revived = client+ .edit_record(ZONE, &name, "CPLContainerRelation", |_| {+ Some(serde_json::json!({+ "isDeleted": { "value": 0 },+ "position": { "value": at },+ }))+ })+ .await;+ if let Err(e) = revived {+ return (added, Err(e));+ }+ } else if let Some(p) = field::int(&existing, "position") {+ at = p;+ }+ }+ _ => return (added, Err(e)),+ }+ }added.push((asset.clone(), at));}- Ok(added)+ (added, Ok(()))}/// Photos taken out of an album, as icloud.com takes them: their relation records removeddiff --git a/probe/examples/photos_albums_roundtrip.rs b/probe/examples/photos_albums_roundtrip.rsindex 2ea38e2..3307d4b 100644--- a/probe/examples/photos_albums_roundtrip.rs+++ b/probe/examples/photos_albums_roundtrip.rs@@ -50,7 +50,8 @@ async fn main() -> Result<()> {made.pointer("/fields/position/value"));- let added = photos::add_to_album(&http, &ck, p.clone(), &album.id, &assets, None).await?;+ let (added, done) = photos::add_to_album(&http, &ck, p.clone(), &album.id, &assets, None).await;+ done?;println!("added {added:?}");for a in &assets {let r = zcdiff --git a/probe/examples/photos_keywords_roundtrip.rs b/probe/examples/photos_keywords_roundtrip.rsindex 9131bd0..1624848 100644--- a/probe/examples/photos_keywords_roundtrip.rs+++ b/probe/examples/photos_keywords_roundtrip.rs@@ -38,20 +38,11 @@ async fn main() -> Result<()> {.await?.ok_or_else(|| anyhow!("gone"))?;println!("before: {:?}", words(&before));- let tag = before.get("recordChangeTag").and_then(|t| t.as_str());- photos::set_keywords(- &http,- &ck,- p.clone(),- &asset,- tag,- &["spring".into(), "quince test ✿".into()],- )- .await?;+ let wanted: Vec<String> = vec!["spring".into(), "quince test ✿".into()];+ photos::set_keywords(&http, &ck, p.clone(), &asset, &[], &wanted).await?;let now = zc.lookup_record(ZONE, &asset).await?.unwrap();println!("set: {:?}", words(&now));- let tag = now.get("recordChangeTag").and_then(|t| t.as_str());- photos::set_keywords(&http, &ck, p.clone(), &asset, tag, &[]).await?;+ photos::set_keywords(&http, &ck, p.clone(), &asset, &wanted, &[]).await?;let back = zc.lookup_record(ZONE, &asset).await?.unwrap();println!("clear: {:?}", words(&back));Ok(())diff --git a/probe/examples/photos_text_roundtrip.rs b/probe/examples/photos_text_roundtrip.rsindex 7fb626a..504e984 100644--- a/probe/examples/photos_text_roundtrip.rs+++ b/probe/examples/photos_text_roundtrip.rs@@ -43,21 +43,18 @@ async fn main() -> Result<()> {.await?.ok_or_else(|| anyhow!("gone"))?;println!("before: {}", show(&before));- let tag = before.get("recordChangeTag").and_then(|t| t.as_str());photos::set_title_and_caption(&http,&ck,p.clone(),&asset,- tag,- "Spring bloom",- "Quince test — a caption ✿",+ Some("Spring bloom"),+ Some("Quince test — a caption ✿"),).await?;let now = zc.lookup_record(ZONE, &asset).await?.unwrap();println!("set: {}", show(&now));- let tag = now.get("recordChangeTag").and_then(|t| t.as_str());- photos::set_title_and_caption(&http, &ck, p.clone(), &asset, tag, "", "").await?;+ photos::set_title_and_caption(&http, &ck, p.clone(), &asset, Some(""), Some("")).await?;let back = zc.lookup_record(ZONE, &asset).await?.unwrap();println!("clear: {}", show(&back));Ok(())diff --git a/src-tauri/src/photos.rs b/src-tauri/src/photos.rsindex 854973a..ddf2135 100644--- a/src-tauri/src/photos.rs+++ b/src-tauri/src/photos.rs@@ -1219,13 +1219,9 @@ pub async fn photos_album_add(return Ok(());}let (http, root, params) = crate::cloudkit_context(&auth).await?;- let added =+ let (placed, added) =quince_icloud::photos::add_to_album(&http, &root, params, &album, &fresh, last).await;// What went in before a failure is in the album on Apple; the library says so too.- let placed = match &added {- Ok(a) => a.clone(),- Err(_) => Vec::new(),- };data.0.lock().await@@ -1236,7 +1232,7 @@ pub async fn photos_album_add(.collect::<Vec<_>>(),).map_err(|e| e.to_string())?;- added.map(|_| ()).map_err(|e| e.to_string())+ added.map_err(|e| e.to_string())}/// Photos taken out of an album. They stay in the library.@@ -1422,9 +1418,10 @@ pub async fn photos_set_flag(let answers = match sent {Ok(a) => a,Err(e) => {- // Nothing reached Apple: all of it as it was.+ // Nothing reached Apple: the flags as they were, on each photo as it is now.let mut library = data.0.lock().await;- library.put_photos(&before).map_err(|e| e.to_string())?;+ let back = flags_back(&library, &before, None);+ library.put_photos(&back).map_err(|e| e.to_string())?;return Err(e);}};@@ -1462,9 +1459,14 @@ pub async fn photos_set_flag(}}{+ // Only the flags and Apple's new version are laid on each photo as the library holds+ // it now: written whole from the copy read before the wait, a title or caption that a+ // sync pass brought in meanwhile was put back as it had been.let mut library = data.0.lock().await;- library.put_photos(&now_are).map_err(|e| e.to_string())?;- library.put_photos(&refused).map_err(|e| e.to_string())?;+ let done = flags_back(&library, &now_are, Some(()));+ let back = flags_back(&library, &refused, None);+ library.put_photos(&done).map_err(|e| e.to_string())?;+ library.put_photos(&back).map_err(|e| e.to_string())?;}if let Some(why) = why {let n = refused.len();@@ -1477,6 +1479,28 @@ pub async fn photos_set_flag(Ok(now_are)}+/// `from`'s flags — favourite, hidden, in Recently Deleted and its dates — laid on each photo as+/// the library holds it now; with `tag`, its change tag too. A photo no longer there is `from`+/// as it was.+fn flags_back(library: &quince_core::Library, from: &[Photo], tag: Option<()>) -> Vec<Photo> {+ from.iter()+ .map(|f| match library.photo(&f.id).ok().flatten() {+ Some(mut now) => {+ now.favorite = f.favorite;+ now.hidden = f.hidden;+ now.deleted = f.deleted;+ now.deleted_at = f.deleted_at;+ now.expunge_at = f.expunge_at;+ if tag.is_some() {+ now.change_tag.clone_from(&f.change_tag);+ }+ now+ }+ None => f.clone(),+ })+ .collect()+}+/// A photo's title and caption, as typed in its Info: Apple's answer taken at once. Empty clears one.#[tauri::command]pub async fn photo_set_text(@@ -1495,20 +1519,29 @@ pub async fn photo_set_text(.map_err(|e| e.to_string())?.ok_or("That photo is no longer in the library.")?;let (title, caption) = (title.trim().to_string(), caption.trim().to_string());+ // Only what was changed here goes: the other field may have been changed on another device.+ let title_changed = photo.title.as_deref().unwrap_or("") != title;+ let caption_changed = photo.caption.as_deref().unwrap_or("") != caption;let (http, root, params) = crate::cloudkit_context(&auth).await?;let answer = quince_icloud::photos::set_title_and_caption(&http,&root,params,&id,- photo.change_tag.as_deref(),- &title,- &caption,+ title_changed.then_some(title.as_str()),+ caption_changed.then_some(caption.as_str()),).await.map_err(|e| e.to_string())?;- photo.title = (!title.is_empty()).then_some(title);- photo.caption = (!caption.is_empty()).then_some(caption);+ let Some(answer) = answer else {+ return Ok(photo);+ };+ if title_changed {+ photo.title = (!title.is_empty()).then_some(title);+ }+ if caption_changed {+ photo.caption = (!caption.is_empty()).then_some(caption);+ }photo.change_tag = answer.get("recordChangeTag").and_then(serde_json::Value::as_str)@@ -1646,17 +1679,12 @@ pub async fn photo_set_keywords(.map_err(|e| e.to_string())?.ok_or("That photo is no longer in the library.")?;let (http, root, params) = crate::cloudkit_context(&auth).await?;- let answer = quince_icloud::photos::set_keywords(- &http,- &root,- params,- &id,- photo.change_tag.as_deref(),- &words,- )- .await- .map_err(|e| e.to_string())?;- photo.keywords = words;+ let (answer, kept) =+ quince_icloud::photos::set_keywords(&http, &root, params, &id, &photo.keywords, &words)+ .await+ .map_err(|e| e.to_string())?;+ // Apple's list now: this change, and any word another device added meanwhile.+ photo.keywords = kept;photo.change_tag = answer.get("recordChangeTag").and_then(serde_json::Value::as_str)@@ -2217,23 +2245,17 @@ async fn one(app: &AppHandle, u: quince_core::photos::Upload) -> bool {return false;};// Resumed after a quit: a photo Apple had already named is not made twice.- if let Some(asset) = u.asset.as_deref() {- let zc = quince_icloud::cloudkit::ZoneClient::new(- &http,- &ck,- quince_icloud::photos::CONTAINER,- params.clone(),- );- if matches!(- zc.lookup_record(quince_icloud::photos::ZONE, asset).await,- Ok(Some(_))- ) {- set("done", None, Some(asset)).await;- into_album(app, &http, &ck, ¶ms, &u, asset).await;+ match already_made(app, &http, &ck, ¶ms, &u.id).await {+ Made::Yes(asset) => {+ set("done", None, Some(&asset)).await;+ into_album(app, &http, &ck, ¶ms, &u, &asset).await;say(app, &u, "done", 1, 1, None);refresh_photos(app).await;return true;}+ // Not known yet: asked again later, never taken for "not uploaded" and sent twice.+ Made::Unknown => return false,+ Made::No => {}}let path = PathBuf::from(&u.path);let Ok(meta) = tokio::fs::metadata(&path).await else {@@ -2384,6 +2406,19 @@ async fn one(app: &AppHandle, u: quince_core::photos::Upload) -> bool {Err(e) if attempt < 3 && !cancelled(&u) => {quiet!("photos upload: try {attempt} failed: {e}");tokio::time::sleep(Duration::from_secs(2 * attempt)).await;+ // Apple may have made the photo before the try failed (waiting on its+ // processing, say): then it is done, and the file is never sent again.+ match already_made(app, &http, &ck, ¶ms, &u.id).await {+ Made::Yes(asset) => {+ set("done", None, Some(&asset)).await;+ into_album(app, &http, &ck, ¶ms, &u, &asset).await;+ say(app, &u, "done", size, size, None);+ refresh_photos(app).await;+ return true;+ }+ Made::Unknown => return false,+ Made::No => {}+ }}Err(e) => {let words = e.to_string();@@ -2395,6 +2430,47 @@ async fn one(app: &AppHandle, u: quince_core::photos::Upload) -> bool {}}+/// Whether Apple has already made the photo an upload was sending.+enum Made {+ /// Yes: the photo's record, by name.+ Yes(String),+ /// No photo was named for it, or Apple says there is none.+ No,+ /// Apple could not be asked just now.+ Unknown,+}++/// Asked of Apple by the name it gave the photo when the upload was first sent (kept with the+/// upload as soon as Apple names it), never assumed: a failed check read as "not made" sent the+/// whole file again, and Apple kept a second copy.+async fn already_made(+ app: &AppHandle,+ http: &reqwest::Client,+ ck: &str,+ params: &[(String, String)],+ id: &str,+) -> Made {+ let asset = {+ let data = app.state::<Data>();+ let library = data.0.lock().await;+ library.upload(id).ok().flatten().and_then(|u| u.asset)+ };+ let Some(asset) = asset else {+ return Made::No;+ };+ let zc = quince_icloud::cloudkit::ZoneClient::new(+ http,+ ck,+ quince_icloud::photos::CONTAINER,+ params.to_vec(),+ );+ match zc.lookup_record(quince_icloud::photos::ZONE, &asset).await {+ Ok(Some(_)) => Made::Yes(asset),+ Ok(None) => Made::No,+ Err(_) => Made::Unknown,+ }+}+/// A photo added while an album was open goes into that album too, after what is there, as/// Photos files an import. The upload itself has already succeeded: if the album is gone, or/// Apple refuses, the photo stays in the library and only this step is skipped.@@ -2432,7 +2508,7 @@ async fn into_album().await{- Ok(placed) => {+ (placed, Ok(())) => {data.0.lock().await@@ -2444,7 +2520,7 @@ async fn into_album().ok();}- Err(e) => quiet!("photos upload: not put in its album: {e}"),+ (_, Err(e)) => quiet!("photos upload: not put in its album: {e}"),}}