From 15e4835ef511b994bbd16d20f08f91fbbf5f1851 Mon Sep 17 00:00:00 2001 From: TheMightyTRexUK <196487809+TheMightyTRex@users.noreply.github.com> Date: Fri, 30 Oct 2026 10:14:49 +0000 Subject: [PATCH] fix(library): leave removed and confirmed-missing copies off the bookshelf My Library listed every book Dewarr had ever seen a file for. After a library reorganisation, files marked intentionally-removed or missing-confirmed showed up as unticked books under their old file names, next to the same books under their new names. Title-family grouping could also pull a removed copy back in when its title matched a book still in the library. The bookshelf (GET /api/library/books) now leaves out copies in CLOSED_STATES unless that state is asked for (?state=missing-confirmed still lists them). The admin asset list, Library review and ownership checks are unchanged. Co-Authored-By: Claude Opus 5.5 --- services/app/api/library.py | 18 ++++++++++---- services/app/domain/availability.py | 8 ++++++- tests/integration/test_library_browsing.py | 28 +++++++++++++++++++++- 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/services/app/api/library.py b/services/app/api/library.py index 3fecb1ca..4163f115 100644 --- a/services/app/api/library.py +++ b/services/app/api/library.py @@ -26,6 +26,7 @@ from app.domain.catalog_titles import title_narrators from app.domain.corrections import asset_state, correct_asset, revision from app.domain.library_access import lock_access +from app.domain.library_review import CLOSED_STATES from app.domain.visibility import visible_library, visible_work from app.domain.work_graph import canonical_map @@ -195,7 +196,7 @@ async def replace_grants(library_id: UUID, body: GrantInput, admin: Admin, db: D await db.commit() -def asset_conditions(user, work_id, library_id, needs_review, q, medium, state): +def asset_conditions(user, work_id, library_id, needs_review, q, medium, state, hide_closed=False): conditions = [ visible_library(user), Integration.enabled.is_(True), @@ -217,13 +218,21 @@ def asset_conditions(user, work_id, library_id, needs_review, q, medium, state): conditions.append(LibraryAsset.medium == medium) if state != "any": conditions.append(LibraryAsset.state == state) + elif hide_closed: + conditions.append(LibraryAsset.state.not_in(CLOSED_STATES)) if q.strip(): # Search only declared title/credit fields, never arbitrary provider payloads. pattern = ( "%" + q.strip().replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_") + "%" ) mapping = canonical_map( - library_work_ids(user, library_id=library_id, medium=medium, state=state) + library_work_ids( + user, + library_id=library_id, + medium=medium, + state=state, + hide_closed=hide_closed, + ) ) catalog_matches = ( select(AssetContains.asset_id) @@ -266,7 +275,8 @@ async def library_books( limit: int = Query(default=40, ge=1, le=100), ): mapping = display_map( - user, library_work_ids(user, library_id=library_id, medium=medium, state=state) + user, + library_work_ids(user, library_id=library_id, medium=medium, state=state, hide_closed=True), ) matching = ( select(mapping.c.work_id, func.max(LibraryAsset.created_at).label("observed_at")) @@ -275,7 +285,7 @@ async def library_books( .join(Integration, Library.integration_id == Integration.id) .join(AssetContains, AssetContains.asset_id == LibraryAsset.id) .join(mapping, mapping.c.origin_id == AssetContains.work_id) - .where(*asset_conditions(user, None, library_id, False, q, medium, state)) + .where(*asset_conditions(user, None, library_id, False, q, medium, state, hide_closed=True)) .group_by(mapping.c.work_id) .subquery() ) diff --git a/services/app/domain/availability.py b/services/app/domain/availability.py index b6ee7689..6ecded5c 100644 --- a/services/app/domain/availability.py +++ b/services/app/domain/availability.py @@ -15,12 +15,16 @@ Version, ) from app.domain.catalog_display import display_map +from app.domain.library_review import CLOSED_STATES from app.domain.primary_editions import asset_narrators, edition_order, primary_choices -def library_work_ids(user, *, library_id=None, medium="any", state="any"): +def library_work_ids(user, *, library_id=None, medium="any", state="any", hide_closed=False): """Origins in accessible inventory, including incomplete and missing copies. + hide_closed leaves out copies that are gone for good (CLOSED_STATES) when no state is + asked for, so a bookshelf doesn't list removed files as unowned books. + This bounds library browsing projections; fulfillment still uses the stricter availability_rows() relation to establish complete, verified ownership. """ @@ -41,6 +45,8 @@ def library_work_ids(user, *, library_id=None, medium="any", state="any"): query = query.where(LibraryAsset.medium == medium) if state != "any": query = query.where(LibraryAsset.state == state) + elif hide_closed: + query = query.where(LibraryAsset.state.not_in(CLOSED_STATES)) return query diff --git a/tests/integration/test_library_browsing.py b/tests/integration/test_library_browsing.py index 4bffda98..96496365 100644 --- a/tests/integration/test_library_browsing.py +++ b/tests/integration/test_library_browsing.py @@ -12,7 +12,7 @@ Operation, Work, ) -from tests.integration.test_discovery import login_member +from tests.integration.test_discovery import add_owned, login_member from tests.integration.test_library_discovery import NOW from tests.integration.test_library_discovery import add as add_observed @@ -182,3 +182,29 @@ async def test_file_locations_only_appear_for_accessible_library_copies(client, ] await login_member(client) assert (await browse(client, work_id=work_id))["items"] == [] + + +async def test_bookshelf_leaves_out_books_whose_only_copies_are_gone(client, admin, database): + async with database() as db, db.begin(): + await add(db, "Still on disk") + await add(db, "Deleted file", state="intentionally-removed") + await add(db, "Lost file", state="missing-confirmed") + await add(db, "Same title") + await add(db, "Same title", state="intentionally-removed") + reorganised, _, _ = await add(db, "Reorganised copy") + old_library = await add_owned(db, reorganised) + await db.execute( + update(LibraryAsset) + .where(LibraryAsset.library_id == old_library.id) + .values(state="intentionally-removed") + ) + books = (await client.get("/api/library/books")).json() + assert sorted(item["title"] for item in books["items"]) == [ + "Reorganised copy", + "Same title", + "Still on disk", + ] + assert books["total"] == 3 + lost = (await client.get("/api/library/books", params={"state": "missing-confirmed"})).json() + assert [item["title"] for item in lost["items"]] == ["Lost file"] + assert (await browse(client))["total"] == 7