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.

Photos backend: folders of albums

Yashwanth Kamireddid53049da3acd
diff --git a/crates/quince-core/src/photos.rs b/crates/quince-core/src/photos.rs
index 6c712ba..97721f3 100644
--- a/crates/quince-core/src/photos.rs
+++ b/crates/quince-core/src/photos.rs
@@ -98,6 +98,12 @@ pub struct Album {
pub name: String,
pub position: i64,
pub change_tag: Option<String>,
+ /// A folder of albums (Apple's albumType 3), not an album of photos.
+ #[serde(default)]
+ pub folder: bool,
+ /// The folder it sits in; none at the top.
+ #[serde(default)]
+ pub parent: Option<String>,
}
/// A file on its way into Photos: waiting, sending, verifying (Apple making the photo), done,
@@ -469,8 +475,16 @@ impl Library {
tx.execute("DELETE FROM photo_albums", [])?;
for a in albums {
tx.execute(
- "INSERT INTO photo_albums (id, name, position, change_tag) VALUES (?1, ?2, ?3, ?4)",
- params![a.id, a.name, a.position, a.change_tag],
+ "INSERT INTO photo_albums (id, name, position, change_tag, folder, parent)
+ VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
+ params![
+ a.id,
+ a.name,
+ a.position,
+ a.change_tag,
+ i64::from(a.folder),
+ a.parent
+ ],
)?;
}
tx.commit()?;
@@ -529,10 +543,18 @@ impl Library {
/// One album added or changed, as Apple answered a write.
pub fn put_album(&mut self, a: &Album) -> Result<()> {
self.conn.execute(
- "INSERT INTO photo_albums (id, name, position, change_tag) VALUES (?1, ?2, ?3, ?4)
+ "INSERT INTO photo_albums (id, name, position, change_tag, folder, parent)
+ VALUES (?1, ?2, ?3, ?4, ?5, ?6)
ON CONFLICT(id) DO UPDATE SET name = excluded.name, position = excluded.position,
- change_tag = excluded.change_tag",
- params![a.id, a.name, a.position, a.change_tag],
+ change_tag = excluded.change_tag, folder = excluded.folder, parent = excluded.parent",
+ params![
+ a.id,
+ a.name,
+ a.position,
+ a.change_tag,
+ i64::from(a.folder),
+ a.parent
+ ],
)?;
Ok(())
}
@@ -555,7 +577,7 @@ impl Library {
/// order the albums are listed.
pub fn albums_counted(&self) -> Result<Vec<(Album, i64, Option<String>)>> {
let mut stmt = self.conn.prepare(
- "SELECT a.id, a.name, a.position, a.change_tag,
+ "SELECT a.id, a.name, a.position, a.change_tag, a.folder, a.parent,
(SELECT COUNT(*) FROM photo_album_items i JOIN photos p ON p.id = i.asset
WHERE i.album = a.id AND p.deleted = 0 AND p.hidden = 0),
(SELECT i.asset FROM photo_album_items i JOIN photos p ON p.id = i.asset
@@ -570,14 +592,37 @@ impl Library {
name: r.get(1)?,
position: r.get(2)?,
change_tag: r.get(3)?,
+ folder: r.get::<_, i64>(4)? != 0,
+ parent: r.get(5)?,
},
- r.get(4)?,
- r.get(5)?,
+ r.get(6)?,
+ r.get(7)?,
))
})?;
Ok(rows.collect::<std::result::Result<_, _>>()?)
}
+ /// Albums whose folder is gone, gone with it — and theirs in turn. Apple keeps an album in a
+ /// deleted folder (checked live), where every Photos shows it as deleted with the folder.
+ pub fn forget_orphan_albums(&mut self) -> Result<()> {
+ loop {
+ let orphans: Vec<String> = {
+ let mut stmt = self.conn.prepare(
+ "SELECT id FROM photo_albums
+ WHERE parent IS NOT NULL AND parent NOT IN (SELECT id FROM photo_albums)",
+ )?;
+ let rows = stmt.query_map([], |r| r.get(0))?;
+ rows.collect::<std::result::Result<_, _>>()?
+ };
+ if orphans.is_empty() {
+ return Ok(());
+ }
+ for id in orphans {
+ self.forget_album(&id)?;
+ }
+ }
+ }
+
/// Where the album's last photo sits, so what is added goes after it.
pub fn album_last_position(&self, album: &str) -> Result<Option<i64>> {
Ok(self.conn.query_row(
@@ -589,7 +634,8 @@ impl Library {
pub fn albums(&self) -> Result<Vec<Album>> {
let mut stmt = self.conn.prepare(
- "SELECT id, name, position, change_tag FROM photo_albums ORDER BY position, name",
+ "SELECT id, name, position, change_tag, folder, parent FROM photo_albums
+ ORDER BY position, name",
)?;
let rows = stmt.query_map([], |r| {
Ok(Album {
@@ -597,6 +643,8 @@ impl Library {
name: r.get(1)?,
position: r.get(2)?,
change_tag: r.get(3)?,
+ folder: r.get::<_, i64>(4)? != 0,
+ parent: r.get(5)?,
})
})?;
Ok(rows.collect::<std::result::Result<_, _>>()?)
@@ -713,6 +761,8 @@ mod tests {
name: "Trip".into(),
position: 0,
change_tag: None,
+ folder: false,
+ parent: None,
}])
.unwrap();
assert_eq!(
diff --git a/crates/quince-core/src/schema.rs b/crates/quince-core/src/schema.rs
index 87fc669..60750e7 100644
--- a/crates/quince-core/src/schema.rs
+++ b/crates/quince-core/src/schema.rs
@@ -475,4 +475,10 @@ pub const MIGRATIONS: &[&str] = &[
ALTER TABLE photos ADD COLUMN longitude REAL;
UPDATE sync_state SET token = NULL WHERE service = 'photos';
"#,
+ // Folders of albums, and which folder an album sits in; Photos read again so they come.
+ r#"
+ ALTER TABLE photo_albums ADD COLUMN folder INTEGER NOT NULL DEFAULT 0;
+ ALTER TABLE photo_albums ADD COLUMN parent TEXT;
+ UPDATE sync_state SET token = NULL WHERE service = 'photos';
+ "#,
];
diff --git a/crates/quince-icloud/src/photos.rs b/crates/quince-icloud/src/photos.rs
index 49840cc..3a7102c 100644
--- a/crates/quince-icloud/src/photos.rs
+++ b/crates/quince-icloud/src/photos.rs
@@ -311,22 +311,27 @@ pub fn apply(
if let Some(id) = field::record_name(r) {
albums_touched.push(id);
}
- // Folders and smart albums are not albums a photo is put in.
- if field::int(r, "isDeleted") != Some(1)
- && field::int(r, "albumType").unwrap_or(0) == 0
- {
+ // Albums (type 0) and folders of them (type 3); smart albums and the rest are
+ // not the person's own. The root folder is the top, not a folder to show.
+ let kind = field::int(r, "albumType").unwrap_or(0);
+ if field::int(r, "isDeleted") != Some(1) && (kind == 0 || kind == 3) {
if let (Some(id), Some(name)) =
(field::record_name(r), decoded(r, "albumNameEnc"))
{
- albums.push(Album {
- id,
- name,
- position: field::int(r, "position").unwrap_or(0),
- change_tag: r
- .get("recordChangeTag")
- .and_then(Value::as_str)
- .map(str::to_string),
- });
+ if !id.starts_with("----") {
+ albums.push(Album {
+ id,
+ name,
+ position: field::int(r, "position").unwrap_or(0),
+ change_tag: r
+ .get("recordChangeTag")
+ .and_then(Value::as_str)
+ .map(str::to_string),
+ folder: kind == 3,
+ parent: field::string(r, "parentId")
+ .filter(|p| !p.is_empty() && !p.starts_with("----")),
+ });
+ }
}
}
}
@@ -369,6 +374,7 @@ pub fn apply(
}
all.extend(albums);
library.replace_albums(&all)?;
+ library.forget_orphan_albums()?;
}
} else if complete {
// A full pass that finished is the whole truth; one that stopped early is not, and
@@ -376,6 +382,7 @@ pub fn apply(
library.replace_photos(&photos)?;
library.replace_albums(&albums)?;
library.replace_album_items(&in_albums)?;
+ library.forget_orphan_albums()?;
} else {
library.put_photos(&photos)?;
library.put_album_items(&in_albums)?;
@@ -527,14 +534,17 @@ pub const MAX_POSITION: i64 = 9_007_199_254_740_991 - 1024;
/// Gap between items added to an album, and the first item's position.
const ITEM_SPACING: i64 = 1024;
-/// A new album, made as icloud.com makes one: CloudKit names it, it sorts first (two below
-/// the first album there is), and its photos sort by date. Answers the album.
+/// A new album — or folder — made as icloud.com makes one: CloudKit names it, it sorts first
+/// (two below the first there is), its photos sort by date; a folder is albumType 3, and either
+/// may sit in a folder (`parentId`). Answers the album.
pub async fn create_album(
http: &reqwest::Client,
ckdatabasews_root: &str,
params: Vec<(String, String)>,
name: &str,
first_position: Option<i64>,
+ parent: Option<&str>,
+ folder: bool,
) -> Result<Album> {
let client = ZoneClient::new(http, ckdatabasews_root, CONTAINER, params);
let position = first_position.map_or(MAX_POSITION, |p| (p - 2).max(0));
@@ -544,13 +554,22 @@ pub async fn create_album(
"CPLAlbum",
serde_json::json!({
"albumNameEnc": { "value": album_name(name) },
- "albumType": { "value": 0 },
+ "albumType": { "value": if folder { 3 } else { 0 } },
"isDeleted": { "value": 0 },
"isExpunged": { "value": 0 },
"position": { "value": position },
"sortAscending": { "value": 1 },
"sortType": { "value": 1 },
- }),
+ })
+ .as_object()
+ .cloned()
+ .map(|mut f| {
+ if let Some(p) = parent {
+ f.insert("parentId".into(), serde_json::json!({ "value": p }));
+ }
+ Value::Object(f)
+ })
+ .unwrap_or_default(),
)
.await?;
Ok(Album {
@@ -563,6 +582,8 @@ pub async fn create_album(
.get("recordChangeTag")
.and_then(Value::as_str)
.map(str::to_string),
+ folder,
+ parent: parent.map(str::to_string),
})
}
@@ -1044,4 +1065,50 @@ mod tests {
"https://o/IMG_0001.HEIC"
);
}
+
+ /// Folders come through as folders, an album in one knows it, and when the folder is
+ /// deleted elsewhere the album inside goes with it — though Apple keeps that album.
+ #[test]
+ fn folders_and_what_they_hold() {
+ let mut lib = quince_core::Library::in_memory().unwrap();
+ let album = |id: &str, name: &str, kind: i64, parent: Option<&str>, deleted: i64| {
+ let mut f = json!({
+ "albumNameEnc": {"value": base64::engine::general_purpose::STANDARD.encode(name)},
+ "albumType": {"value": kind}, "position": {"value": 1}, "isDeleted": {"value": deleted}});
+ if let Some(p) = parent {
+ f["parentId"] = json!({"value": p});
+ }
+ json!({"recordName": id, "recordType": "CPLAlbum", "fields": f})
+ };
+ let pass = |records, incremental| PhotosFetch {
+ records,
+ token: Some("T".into()),
+ incremental,
+ complete: true,
+ };
+ apply(
+ &mut lib,
+ pass(
+ vec![
+ album("F", "Trips", 3, Some("----Root-Folder----"), 0),
+ album("A", "Goa", 0, Some("F"), 0),
+ ],
+ false,
+ ),
+ )
+ .unwrap();
+ let all = lib.albums().unwrap();
+ let f = all.iter().find(|a| a.id == "F").unwrap();
+ let a = all.iter().find(|a| a.id == "A").unwrap();
+ assert!(f.folder && f.parent.is_none(), "a folder at the top");
+ assert!(
+ !a.folder && a.parent.as_deref() == Some("F"),
+ "an album in it"
+ );
+ apply(&mut lib, pass(vec![album("F", "Trips", 3, None, 1)], true)).unwrap();
+ assert!(
+ lib.albums().unwrap().is_empty(),
+ "the folder deleted takes the album inside with it"
+ );
+ }
}
diff --git a/probe/examples/photos_albums_roundtrip.rs b/probe/examples/photos_albums_roundtrip.rs
index b1ac3a9..2ea38e2 100644
--- a/probe/examples/photos_albums_roundtrip.rs
+++ b/probe/examples/photos_albums_roundtrip.rs
@@ -29,7 +29,16 @@ async fn main() -> Result<()> {
.map(|b| String::from_utf8_lossy(&b).into_owned())
};
- let album = photos::create_album(&http, &ck, p.clone(), "Quince probe album", None).await?;
+ let album = photos::create_album(
+ &http,
+ &ck,
+ p.clone(),
+ "Quince probe album",
+ None,
+ None,
+ false,
+ )
+ .await?;
let made = zc
.lookup_record(ZONE, &album.id)
.await?
diff --git a/probe/examples/photos_folder_roundtrip.rs b/probe/examples/photos_folder_roundtrip.rs
new file mode 100644
index 0000000..ee53a52
--- /dev/null
+++ b/probe/examples/photos_folder_roundtrip.rs
@@ -0,0 +1,96 @@
+//! Live, throwaways only: a folder made through Quince's own create_album (albumType 3), an
+//! album made inside it (parentId), each read back; the folder renamed; then the folder deleted
+//! as icloud.com deletes one (isDeleted on it alone) — and what that does to the album inside,
+//! read back. Whatever is left is deleted. No photo is touched.
+use anyhow::{anyhow, Result};
+use base64::Engine;
+use quince_icloud::photos::{self, CONTAINER, ZONE};
+
+#[tokio::main]
+async fn main() -> Result<()> {
+ let dir = std::path::PathBuf::from(std::env::var("HOME")?).join(".local/share/quince-probe");
+ let mut client = quince_auth::Client::new(&dir)?;
+ if !client.resume().await? {
+ return Err(anyhow!("no session"));
+ }
+ let ck = client
+ .session()
+ .service_url("ckdatabasews")
+ .ok_or_else(|| anyhow!("no ck"))?;
+ let (http, p) = (client.http().clone(), client.service_params());
+ let zc = quince_icloud::cloudkit::ZoneClient::new(&http, &ck, CONTAINER, p.clone());
+ let show = |r: &serde_json::Value| {
+ let name = r
+ .pointer("/fields/albumNameEnc/value")
+ .and_then(|v| v.as_str())
+ .and_then(|v| base64::engine::general_purpose::STANDARD.decode(v).ok())
+ .map(|b| String::from_utf8_lossy(&b).into_owned());
+ format!(
+ "name={name:?} type={:?} parent={:?} deleted={:?}",
+ r.pointer("/fields/albumType/value"),
+ r.pointer("/fields/parentId/value"),
+ r.pointer("/fields/isDeleted/value")
+ )
+ };
+ let folder = photos::create_album(
+ &http,
+ &ck,
+ p.clone(),
+ "Quince probe folder",
+ None,
+ None,
+ true,
+ )
+ .await?;
+ println!(
+ "folder: {}",
+ show(&zc.lookup_record(ZONE, &folder.id).await?.unwrap())
+ );
+ let inner = photos::create_album(
+ &http,
+ &ck,
+ p.clone(),
+ "Quince probe inner",
+ None,
+ Some(&folder.id),
+ false,
+ )
+ .await?;
+ println!(
+ "inner: {}",
+ show(&zc.lookup_record(ZONE, &inner.id).await?.unwrap())
+ );
+ let tag = photos::rename_album(
+ &http,
+ &ck,
+ p.clone(),
+ &folder.id,
+ folder.change_tag.as_deref(),
+ "Quince probe folder ✓",
+ )
+ .await?;
+ println!(
+ "renamed: {}",
+ show(&zc.lookup_record(ZONE, &folder.id).await?.unwrap())
+ );
+ photos::delete_album(&http, &ck, p.clone(), &folder.id, tag.as_deref()).await?;
+ println!(
+ "folder after delete: {}",
+ show(&zc.lookup_record(ZONE, &folder.id).await?.unwrap())
+ );
+ let inner_now = zc.lookup_record(ZONE, &inner.id).await?.unwrap();
+ println!("inner after the folder's delete: {}", show(&inner_now));
+ if inner_now
+ .pointer("/fields/isDeleted/value")
+ .and_then(|v| v.as_i64())
+ != Some(1)
+ {
+ let t = inner_now.get("recordChangeTag").and_then(|x| x.as_str());
+ photos::delete_album(&http, &ck, p.clone(), &inner.id, t).await?;
+ println!(
+ "inner deleted too: {}",
+ show(&zc.lookup_record(ZONE, &inner.id).await?.unwrap())
+ );
+ }
+ Ok(())
+}
diff --git a/src-tauri/src/photos.rs b/src-tauri/src/photos.rs
index a103d0f..1f94f72 100644
--- a/src-tauri/src/photos.rs
+++ b/src-tauri/src/photos.rs
@@ -272,10 +272,13 @@ pub async fn photos_in_album(album: String, data: State<'_, Data>) -> UiResult<V
.map_err(|e| e.to_string())
}
-/// A new album, first in the list as Photos puts one. Answers it as Apple made it.
+/// A new album — or folder, `folder` — first in its list as Photos puts one, at the top or in
+/// the folder `parent`. Answers it as Apple made it.
#[tauri::command]
pub async fn photos_album_create(
name: String,
+ parent: Option<String>,
+ folder: Option<bool>,
auth: State<'_, Auth>,
data: State<'_, Data>,
) -> UiResult<quince_core::photos::Album> {
@@ -294,9 +297,17 @@ pub async fn photos_album_create(
.map(|a| a.position)
.min();
let (http, root, params) = crate::cloudkit_context(&auth).await?;
- let album = quince_icloud::photos::create_album(&http, &root, params, name, first)
- .await
- .map_err(|e| e.to_string())?;
+ let album = quince_icloud::photos::create_album(
+ &http,
+ &root,
+ params,
+ name,
+ first,
+ parent.as_deref(),
+ folder.unwrap_or(false),
+ )
+ .await
+ .map_err(|e| e.to_string())?;
data.0
.lock()
.await
@@ -339,7 +350,7 @@ pub async fn photos_album_rename(
.map_err(|e| e.to_string())
}
-/// An album deleted. Its photos stay in the library.
+/// An album, or a folder with all it holds, deleted. Their photos stay in the library.
#[tauri::command]
pub async fn photos_album_delete(
id: String,
@@ -347,16 +358,45 @@ pub async fn photos_album_delete(
data: State<'_, Data>,
) -> UiResult<()> {
let _one = crate::PHOTOS_PASS.lock().await;
- let album = album_of(&data, &id).await?;
+ album_of(&data, &id).await?;
+ // A folder goes with everything in it, as icloud.com says it will ("…will also delete all
+ // albums inside this folder"). Apple's server does not do that itself — checked live: an
+ // album inside a deleted folder stayed, its folder gone — so the ones inside are deleted
+ // here, the deepest first, and the folder last.
+ let all = data.0.lock().await.albums().map_err(|e| e.to_string())?;
+ let mut order: Vec<quince_core::photos::Album> = Vec::new();
+ fn within(
+ all: &[quince_core::photos::Album],
+ id: &str,
+ out: &mut Vec<quince_core::photos::Album>,
+ ) {
+ for a in all.iter().filter(|a| a.parent.as_deref() == Some(id)) {
+ within(all, &a.id, out);
+ out.push(a.clone());
+ }
+ }
+ within(&all, &id, &mut order);
+ if let Some(me) = all.iter().find(|a| a.id == id) {
+ order.push(me.clone());
+ }
let (http, root, params) = crate::cloudkit_context(&auth).await?;
- quince_icloud::photos::delete_album(&http, &root, params, &id, album.change_tag.as_deref())
+ for a in order {
+ quince_icloud::photos::delete_album(
+ &http,
+ &root,
+ params.clone(),
+ &a.id,
+ a.change_tag.as_deref(),
+ )
.await
.map_err(|e| e.to_string())?;
- data.0
- .lock()
- .await
- .forget_album(&id)
- .map_err(|e| e.to_string())
+ data.0
+ .lock()
+ .await
+ .forget_album(&a.id)
+ .map_err(|e| e.to_string())?;
+ }
+ Ok(())
}
/// Photos put in an album after what is there. One already in it is not added twice.