Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

LR-LIB-ALBUM: album.addPhotos and album.toggleTarget take photos named in ids when nothing usable is selected

Nathaniel Ledford865cca929058
diff --git a/crates/engine/src/cmd.rs b/crates/engine/src/cmd.rs
index 9b8dc3dbd..daddc7f6e 100644
--- a/crates/engine/src/cmd.rs
+++ b/crates/engine/src/cmd.rs
@@ -193,6 +193,20 @@ pub(crate) fn names_photos(p: &Value) -> bool {
["ids", "id"].iter().any(|k| p.get(k).is_some_and(|v| !v.is_null()))
}
+/// The call names its target photos in `ids`, whatever else it carries in `id` (`album.addPhotos`'s `id` is the album).
+pub(crate) fn names_photo_ids(p: &Value) -> bool {
+ p.get("ids").is_some_and(|v| !v.is_null())
+}
+
+/// For a command that opts into `explicit_targets` but whose `id` isn't a photo: [`Session::execute`] skipped the
+/// selection check because the call carries an `id`, so the command makes it itself unless `ids` names the photos.
+pub(crate) fn enabled_unless_ids(s: &Session, p: &Value, id: &str, enabled: Enabled) -> Result<()> {
+ if names_photo_ids(p) {
+ return Ok(());
+ }
+ enabled(s).map_err(|why| EngineError::Disabled(id.to_string(), why))
+}
+
/// The photos `ids` names: `None` when it is missing or null (the command then falls back to the
/// selection, or its own default), else every entry as a photo id. An `ids` that isn't a list of
/// whole numbers is an error, never `None`: falling back would act on the selection instead of
diff --git a/crates/engine/src/cmd/library/specs.rs b/crates/engine/src/cmd/library/specs.rs
index 0dcbefe19..aef3f6cdf 100644
--- a/crates/engine/src/cmd/library/specs.rs
+++ b/crates/engine/src/cmd/library/specs.rs
@@ -1022,94 +1022,107 @@ pub fn specs() -> Vec<CommandSpec> {
Ok(json!({"changed": changed}))
}
),
- cmd!(
- "album.addPhotos",
- "Add to Album",
- ["Photo"],
- None,
- "{id: albumId, ids?: [photoIds]} (default: selection) → {added, skipped}; Local photos are skipped (albums hold library photos), only Local ones is an error; ids the library doesn't have are dropped; nothing new to add changes nothing (no undo step)",
- has_library_selection,
- |s, p| {
- let id = album_param(p, "id", "album.addPhotos")?;
- let targets = crate::cmd::photo_ids(p, "album.addPhotos")?.unwrap_or_else(|| s.selected());
- let al = s.catalog.album(id).ok_or_else(|| bad("album.addPhotos", "no such album"))?;
- if al.is_smart() || al.folder {
- return Err(bad("album.addPhotos", "smart albums and folders can't hold photos"));
- }
- let (targets, skipped) = album_targets_or_err(s, "album.addPhotos", targets)?;
- let mut photos = al.photos.clone();
- let before = photos.len();
- // looked up, not searched for: adding thousands to a big album stays linear
- let mut held: std::collections::HashSet<PhotoId> = photos.iter().copied().collect();
- for t in targets {
- if held.insert(t) {
- photos.push(t);
+ CommandSpec {
+ explicit_targets: true,
+ ..cmd!(
+ "album.addPhotos",
+ "Add to Album",
+ ["Photo"],
+ None,
+ "{id: albumId, ids?: [photoIds]} (default: selection) → {added, skipped}; Local photos are skipped (albums hold library photos), only Local ones is an error; ids the library doesn't have are dropped; nothing new to add changes nothing (no undo step)",
+ has_library_selection,
+ |s, p| {
+ crate::cmd::enabled_unless_ids(s, p, "album.addPhotos", has_library_selection)?;
+ let id = album_param(p, "id", "album.addPhotos")?;
+ let targets = crate::cmd::photo_ids(p, "album.addPhotos")?.unwrap_or_else(|| s.selected());
+ let al = s.catalog.album(id).ok_or_else(|| bad("album.addPhotos", "no such album"))?;
+ if al.is_smart() || al.folder {
+ return Err(bad("album.addPhotos", "smart albums and folders can't hold photos"));
}
+ let (targets, skipped) = album_targets_or_err(s, "album.addPhotos", targets)?;
+ let mut photos = al.photos.clone();
+ let before = photos.len();
+ // looked up, not searched for: adding thousands to a big album stays linear
+ let mut held: std::collections::HashSet<PhotoId> = photos.iter().copied().collect();
+ for t in targets {
+ if held.insert(t) {
+ photos.push(t);
+ }
+ }
+ let added = photos.len() - before;
+ // all of them were there already (or none was left to add): the album is unchanged,
+ // so no undo step, and redo stays
+ if added == 0 {
+ return Ok(json!({"added": 0, "skipped": skipped}));
+ }
+ let cover = al.cover.or(photos.first().copied());
+ s.commit("Add to Album", Op::Batch { ops: vec![Op::SetAlbumPhotos { id, photos }, Op::SetAlbumCover { id, cover }] })?;
+ Ok(json!({"added": added, "skipped": skipped}))
}
- let added = photos.len() - before;
- // all of them were there already (or none was left to add): the album is unchanged,
- // so no undo step, and redo stays
- if added == 0 {
- return Ok(json!({"added": 0, "skipped": skipped}));
- }
- let cover = al.cover.or(photos.first().copied());
- s.commit("Add to Album", Op::Batch { ops: vec![Op::SetAlbumPhotos { id, photos }, Op::SetAlbumCover { id, cover }] })?;
- Ok(json!({"added": added, "skipped": skipped}))
- }
- ),
- cmd!(
- "album.toggleTarget",
- "Add to Target Album",
- [],
- None,
- "{ids?} — B: adds the photos to the target album (the Quick Collection unless one is set), or removes them when they're all in it → {album, added, toggled, skipped}; Local photos are skipped, ids the library doesn't have are dropped (none left: nothing changes and `added` is null)",
- has_library_selection,
- |s, p| {
- let targets =
- album_targets_or_err(s, "album.toggleTarget", crate::cmd::photo_ids(p, "album.toggleTarget")?.unwrap_or_else(|| s.selected()))?;
- let (targets, skipped) = targets;
- // nothing left to toggle (every id named is gone): no album is made or changed
- if targets.is_empty() {
- let existing = s
- .target_album
- .filter(|a| s.catalog.album(*a).is_some_and(|al| !al.is_smart() && !al.folder))
- .or_else(|| s.catalog.quick_collection());
- let al = existing.and_then(|a| s.catalog.album(a));
- return Ok(
- json!({"album": al.map(|a| a.id.0), "name": al.map(|a| a.name.clone()), "added": null, "toggled": 0, "count": al.map_or(0, |a| s.catalog.album_count(a.id)), "skipped": skipped}),
- );
- }
- let target = s.target_album.filter(|a| s.catalog.album(*a).is_some_and(|al| !al.is_smart() && !al.folder));
- let id = match target.or_else(|| s.catalog.quick_collection()) {
- Some(id) => id,
- None => {
- let id = s.catalog.alloc_album_id();
- let album = Album { quick: true, ..Album::new(id, "Quick Collection") };
- s.commit("New Quick Collection", Op::AddAlbum { album })?;
- id
+ )
+ },
+ CommandSpec {
+ explicit_targets: true,
+ ..cmd!(
+ "album.toggleTarget",
+ "Add to Target Album",
+ [],
+ None,
+ "{ids?} — B: adds the photos to the target album (the Quick Collection unless one is set), or removes them when they're all in it → {album, added, toggled, skipped}; Local photos are skipped, ids the library doesn't have are dropped (none left: nothing changes and `added` is null)",
+ has_library_selection,
+ |s, p| {
+ crate::cmd::enabled_unless_ids(s, p, "album.toggleTarget", has_library_selection)?;
+ let targets = album_targets_or_err(
+ s,
+ "album.toggleTarget",
+ crate::cmd::photo_ids(p, "album.toggleTarget")?.unwrap_or_else(|| s.selected()),
+ )?;
+ let (targets, skipped) = targets;
+ // nothing left to toggle (every id named is gone): no album is made or changed
+ if targets.is_empty() {
+ let existing = s
+ .target_album
+ .filter(|a| s.catalog.album(*a).is_some_and(|al| !al.is_smart() && !al.folder))
+ .or_else(|| s.catalog.quick_collection());
+ let al = existing.and_then(|a| s.catalog.album(a));
+ return Ok(
+ json!({"album": al.map(|a| a.id.0), "name": al.map(|a| a.name.clone()), "added": null, "toggled": 0, "count": al.map_or(0, |a| s.catalog.album_count(a.id)), "skipped": skipped}),
+ );
}
- };
- let al = s.catalog.album(id).ok_or_else(|| bad("album.toggleTarget", "no target album"))?;
- // looked up, not searched for: thousands toggled in a big album stay linear
- let mut held: std::collections::HashSet<PhotoId> = al.photos.iter().copied().collect();
- let all_in = targets.iter().all(|t| held.contains(t));
- let photos: Vec<PhotoId> = if all_in {
- let targets: std::collections::HashSet<PhotoId> = targets.iter().copied().collect();
- al.photos.iter().copied().filter(|x| !targets.contains(x)).collect()
- } else {
- al.photos.iter().copied().chain(targets.iter().copied().filter(|t| held.insert(*t))).collect()
- };
- let name = al.name.clone();
- // photos that went in or came out: what a toast can say (not the ones that were in already)
- let toggled = al.photos.len().abs_diff(photos.len());
- let cover = al.cover.filter(|c| photos.contains(c)).or(photos.first().copied());
- s.commit(
- if all_in { "Remove from Target Album" } else { "Add to Target Album" },
- Op::Batch { ops: vec![Op::SetAlbumPhotos { id, photos }, Op::SetAlbumCover { id, cover }] },
- )?;
- Ok(json!({"album": id.0, "name": name, "added": !all_in, "toggled": toggled, "count": s.catalog.album_count(id), "skipped": skipped}))
- }
- ),
+ let target = s.target_album.filter(|a| s.catalog.album(*a).is_some_and(|al| !al.is_smart() && !al.folder));
+ let id = match target.or_else(|| s.catalog.quick_collection()) {
+ Some(id) => id,
+ None => {
+ let id = s.catalog.alloc_album_id();
+ let album = Album { quick: true, ..Album::new(id, "Quick Collection") };
+ s.commit("New Quick Collection", Op::AddAlbum { album })?;
+ id
+ }
+ };
+ let al = s.catalog.album(id).ok_or_else(|| bad("album.toggleTarget", "no target album"))?;
+ // looked up, not searched for: thousands toggled in a big album stay linear
+ let mut held: std::collections::HashSet<PhotoId> = al.photos.iter().copied().collect();
+ let all_in = targets.iter().all(|t| held.contains(t));
+ let photos: Vec<PhotoId> = if all_in {
+ let targets: std::collections::HashSet<PhotoId> = targets.iter().copied().collect();
+ al.photos.iter().copied().filter(|x| !targets.contains(x)).collect()
+ } else {
+ al.photos.iter().copied().chain(targets.iter().copied().filter(|t| held.insert(*t))).collect()
+ };
+ let name = al.name.clone();
+ // photos that went in or came out: what a toast can say (not the ones that were in already)
+ let toggled = al.photos.len().abs_diff(photos.len());
+ let cover = al.cover.filter(|c| photos.contains(c)).or(photos.first().copied());
+ s.commit(
+ if all_in { "Remove from Target Album" } else { "Add to Target Album" },
+ Op::Batch { ops: vec![Op::SetAlbumPhotos { id, photos }, Op::SetAlbumCover { id, cover }] },
+ )?;
+ Ok(
+ json!({"album": id.0, "name": name, "added": !all_in, "toggled": toggled, "count": s.catalog.album_count(id), "skipped": skipped}),
+ )
+ }
+ )
+ },
cmd!("album.setTarget", "Set as Target Album", [], None, "{id: albumId | null} (null = the Quick Collection)", always, |s, p| {
s.target_album = match p.get("id").and_then(Value::as_u64) {
Some(a) => {
diff --git a/crates/engine/src/tests/album_missing.rs b/crates/engine/src/tests/album_missing.rs
index 560e2074c..451f7a407 100644
--- a/crates/engine/src/tests/album_missing.rs
+++ b/crates/engine/src/tests/album_missing.rs
@@ -133,3 +133,35 @@ fn a_toggle_counts_only_the_photos_it_added() {
assert_eq!((r["added"].as_bool(), r["toggled"].as_u64()), (Some(true), Some(1)), "two were in already: {r}");
assert_eq!(s.catalog.album(a).unwrap().photos.len(), 3);
}
+
+#[test]
+fn photos_named_outright_reach_the_album_commands_with_nothing_selected() {
+ let mut s = Session::with_demo();
+ let photo = s.catalog.photos().next().unwrap().id;
+ let a = album(&mut s, "Trip");
+ s.execute("album.setTarget", &json!({"id": a.0})).unwrap();
+ s.selection = Selection::default();
+
+ // ids the library lacks: the no-op result, not a refusal
+ let r = s.execute("album.addPhotos", &json!({"id": a.0, "ids": [GONE.0]})).unwrap();
+ assert_eq!((r["added"].as_u64(), r["skipped"].as_u64()), (Some(0), Some(0)), "{r}");
+ let r = s.execute("album.toggleTarget", &json!({"ids": [GONE.0]})).unwrap();
+ assert!(r["added"].is_null(), "{r}");
+
+ // ids the library has: they go in
+ let r = s.execute("album.addPhotos", &json!({"id": a.0, "ids": [photo.0]})).unwrap();
+ assert_eq!(r["added"], 1, "{r}");
+ let r = s.execute("album.toggleTarget", &json!({"ids": [photo.0]})).unwrap();
+ assert_eq!((r["added"].as_bool(), r["toggled"].as_u64()), (Some(false), Some(1)), "{r}");
+}
+
+#[test]
+fn the_album_an_add_names_is_not_a_photo_that_skips_the_selection_check() {
+ let mut s = Session::with_demo();
+ let a = album(&mut s, "Trip");
+ s.selection = Selection::default();
+ // no `ids`: the selection is what is added, and it is empty
+ assert!(matches!(s.execute("album.addPhotos", &json!({"id": a.0})), Err(crate::EngineError::Disabled(..))));
+ assert!(matches!(s.execute("album.addPhotos", &json!({"id": a.0, "ids": null})), Err(crate::EngineError::Disabled(..))));
+ assert!(matches!(s.execute("album.toggleTarget", &json!({})), Err(crate::EngineError::Disabled(..))));
+}