diff --git a/src/database/mod.rs b/src/database/mod.rs index 8dfaa5c..696ffbc 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -1222,7 +1222,38 @@ impl ExifDao for SqliteExifDao { } if !include_duplicates { - query = query.filter(duplicate_of_hash.is_null()); + if library_ids.is_empty() { + // Unscoped (all-libraries) view — every survivor is + // reachable somewhere, so a soft-marked row is + // genuinely a duplicate from the user's perspective. + // Hide it. + query = query.filter(duplicate_of_hash.is_null()); + } else { + // Scoped to specific libraries: only hide a + // soft-marked row when the survivor is reachable + // *in this view*. If the survivor lives in a + // library the user can't see right now, the + // demoted file is the only copy of those bytes + // they have access to — keep it visible. + // + // Implemented as a correlated NOT EXISTS subquery + // over an aliased image_exif. Library ids are i32 + // so format!-inlining the integer list is safe. + use diesel::sql_types::Bool; + let lib_list = library_ids + .iter() + .map(i32::to_string) + .collect::>() + .join(","); + let raw = format!( + "(image_exif.duplicate_of_hash IS NULL OR NOT EXISTS \ + (SELECT 1 FROM image_exif AS survivor \ + WHERE survivor.content_hash = image_exif.duplicate_of_hash \ + AND survivor.library_id IN ({})))", + lib_list + ); + query = query.filter(diesel::dsl::sql::(&raw)); + } } query