From 02f35cb2c54488c5e423c09764ec27c049fe1b09 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 4 Nov 2026 02:44:03 +0000 Subject: [PATCH] fix(steam): show imported games in collections that ignore Steam A sync refreshed only the importing user's smart collections filtering on owned_on_steam, and left every other collection to the metadata scan it queues. A collection holding all Windows games therefore kept its old count and covers until that scan ran, and never updated at all when no scan worker was running. Store-only roms are library rows like any other, so the sync now refreshes the collections they match as soon as they are committed. `add_store_only_roms` reports the ids it inserted instead of how many, which the sync result still counts. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01NH5s4gb7QNE7xa4pRUsBzx --- .../handler/database/steam_library_handler.py | 10 ++++---- backend/handler/steam_library_handler.py | 14 ++++++++++- .../handler/test_steam_library_handler.py | 23 +++++++++++++++++++ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/backend/handler/database/steam_library_handler.py b/backend/handler/database/steam_library_handler.py index 467df75f3..6b4a0fdcf 100644 --- a/backend/handler/database/steam_library_handler.py +++ b/backend/handler/database/steam_library_handler.py @@ -44,7 +44,7 @@ def _still_linked(session: Session, user_id: int, steam_id: str) -> bool: class StoreOnlyRoms(NamedTuple): rom_ids: dict[int, int] - created: int + created_ids: list[int] class DBSteamLibraryHandler(DBBaseHandler): @@ -76,10 +76,10 @@ class DBSteamLibraryHandler(DBBaseHandler): """Insert store-only roms, reusing the row already at a rom's path. Returns: - Each app's rom id, and how many rows were inserted. + Each app's rom id, and the ids of the rows that were inserted. """ rom_ids: dict[int, int] = {} - created = 0 + created: list[int] = [] for rom in roms: app_id = rom.steam_id if app_id is None: @@ -105,8 +105,8 @@ class DBSteamLibraryHandler(DBBaseHandler): rom_ids[app_id] = existing.id continue rom_ids[app_id] = rom.id - created += 1 - return StoreOnlyRoms(rom_ids=rom_ids, created=created) + created.append(rom.id) + return StoreOnlyRoms(rom_ids=rom_ids, created_ids=created) @begin_session def get_unmatched_store_only_rom_ids( diff --git a/backend/handler/steam_library_handler.py b/backend/handler/steam_library_handler.py index f871f5bc7..5c9a1022e 100644 --- a/backend/handler/steam_library_handler.py +++ b/backend/handler/steam_library_handler.py @@ -130,6 +130,17 @@ def _refresh_ownership_collections(user_id: int, app_ids: Sequence[int]) -> None log.error(f"Couldn't refresh smart collections for Steam apps {app_ids}: {e}") +def _refresh_library_collections(rom_ids: Sequence[int]) -> None: + """Follow roms added to the library into every smart collection they match.""" + if not rom_ids: + return + try: + db_collection_handler.refresh_smart_collections_for_roms(rom_ids) + except Exception as e: + # The roms are committed, so a stale count is the worst this costs. + log.error(f"Couldn't refresh smart collections for new Steam roms: {e}") + + def _forget_library(user_id: int) -> None: _refresh_ownership_collections( user_id, db_steam_library_handler.delete_entries(user_id) @@ -258,7 +269,7 @@ async def sync_steam_library(user: User) -> SteamSyncResult: ] ) rom_ids.update(store_only.rom_ids) - result.created = store_only.created + result.created = len(store_only.created_ids) entries = [ SteamLibraryEntry( @@ -278,6 +289,7 @@ async def sync_steam_library(user: User) -> SteamSyncResult: ) result.removed = len(changes.removed) _refresh_ownership_collections(user.id, [*changes.added, *changes.removed]) + _refresh_library_collections(store_only.created_ids) result.unmatched_rom_ids = ( db_steam_library_handler.get_unmatched_store_only_rom_ids( [entry.rom_id for entry in entries] diff --git a/backend/tests/handler/test_steam_library_handler.py b/backend/tests/handler/test_steam_library_handler.py index b97f240d7..516b5ff2a 100644 --- a/backend/tests/handler/test_steam_library_handler.py +++ b/backend/tests/handler/test_steam_library_handler.py @@ -207,6 +207,29 @@ async def test_prefers_a_rom_with_a_file_over_one_without( assert entry.rom_id == with_file.id +async def test_imported_games_reach_collections_that_do_not_filter_on_steam( + steam_api: MagicMock, steam_user: User, win: Platform +): + """A store-only rom is a library row, so it cannot wait on the metadata scan.""" + collection = db_collection_handler.add_smart_collection( + SmartCollection( + name="Windows games", + description="", + user_id=steam_user.id, + filter_criteria={"platform_ids": [win.id]}, + ) + ) + db_collection_handler.refresh_smart_collections() + seeded = db_collection_handler.get_smart_collection(collection.id) + assert seeded is not None and seeded.rom_count == 0 + steam_api.get_owned_games.return_value = [_game(220, "Half-Life 2")] + + await sync_steam_library(steam_user) + + after = db_collection_handler.get_smart_collection(collection.id) + assert after is not None and after.rom_count == 1 + + async def test_a_second_sync_reuses_the_store_only_rom( steam_api: MagicMock, steam_user: User, win: Platform ):