diff --git a/backend/endpoints/responses/rom.py b/backend/endpoints/responses/rom.py index 75094a90b..cc58c21f4 100644 --- a/backend/endpoints/responses/rom.py +++ b/backend/endpoints/responses/rom.py @@ -25,7 +25,7 @@ from handler.metadata.launchbox_handler.types import LaunchboxMetadata from handler.metadata.moby_handler import MobyMetadata from handler.metadata.ra_handler import RAMetadata from handler.metadata.ss_handler import SSMetadata -from models.collection import Collection +from models.collection import Collection, SmartCollection from models.rom import Rom, RomArchiveMember, RomFile, RomFileCategory, RomUserStatus from .base import BaseModel, UTCDatetime @@ -479,6 +479,7 @@ class SimpleRomSchema(RomSchema): class UserCollectionSchema(BaseModel): id: int name: str + is_smart: bool = False @classmethod def for_user( @@ -493,6 +494,22 @@ class UserCollectionSchema(BaseModel): if c.user_id == user_id or c.is_public ] + @classmethod + def smart_for_user( + cls, user_id: int, rom_id: int, smart_collections: Sequence[SmartCollection] + ) -> list["UserCollectionSchema"]: + # Smart collections have no reverse join: membership is a cached list of + # rom ids, so keep the ones that include this rom (and are visible). + return [ + UserCollectionSchema( + id=c.id, + name=c.name, + is_smart=True, + ) + for c in smart_collections + if rom_id in c.rom_ids and (c.user_id == user_id or c.is_public) + ] + class DetailedRomSchema(RomSchema): user_saves: list[SaveSchema] @@ -536,9 +553,18 @@ class DetailedRomSchema(RomSchema): for s in db_rom.screenshots if s.user_id == user_id ] - db_rom.user_collections = UserCollectionSchema.for_user( # type: ignore[assignment] - user_id, db_rom.collections - ) + from handler.database import db_collection_handler + + # Standard collections come off the join relationship; smart collections + # are matched by their cached rom-id membership (see #3934). + db_rom.user_collections = [ # type: ignore[assignment] + *UserCollectionSchema.for_user(user_id, db_rom.collections), + *UserCollectionSchema.smart_for_user( + user_id, + db_rom.id, + db_collection_handler.get_smart_collections(user_id=user_id), + ), + ] # Load notes separately using the database handler to avoid lazy loading issues from handler.database import db_rom_handler diff --git a/backend/tests/endpoints/roms/test_rom.py b/backend/tests/endpoints/roms/test_rom.py index 7d04eb52f..c90f8fe4c 100644 --- a/backend/tests/endpoints/roms/test_rom.py +++ b/backend/tests/endpoints/roms/test_rom.py @@ -16,7 +16,7 @@ from handler.metadata.launchbox_handler.types import LaunchboxRom from handler.metadata.moby_handler import MobyGamesHandler, MobyGamesRom from handler.metadata.ra_handler import RAGameRom, RAHandler from handler.metadata.ss_handler import SSHandler, SSRom -from models.collection import Collection +from models.collection import Collection, SmartCollection from models.permission import HiddenEntity, PermEntity from models.platform import Platform from models.rom import Rom, RomFile, compute_name_sort_key @@ -70,6 +70,120 @@ def test_get_rom_simple_missing_returns_404(client: TestClient, access_token: st assert response.status_code == status.HTTP_404_NOT_FOUND +def _user_collections_by_name(body: dict) -> dict: + return {c["name"]: c for c in body["user_collections"]} + + +def test_get_rom_lists_standard_and_smart_collections( + client: TestClient, access_token: str, admin_user: User, rom: Rom +): + """The detail response lists both standard and smart collections the ROM + belongs to, tagging smart ones with `is_smart` (issue #3934).""" + standard = db_collection_handler.add_collection( + Collection(name="My Standard", description="", user_id=admin_user.id) + ) + db_collection_handler.add_roms_to_collection(standard.id, [rom.id]) + + smart = db_collection_handler.add_smart_collection( + SmartCollection( + name="My Smart", + description="", + user_id=admin_user.id, + rom_ids=[rom.id], + filter_criteria={}, + ) + ) + + response = client.get( + f"/api/roms/{rom.id}", + headers={"Authorization": f"Bearer {access_token}"}, + ) + assert response.status_code == status.HTTP_200_OK + + collections = _user_collections_by_name(response.json()) + assert set(collections) == {"My Standard", "My Smart"} + assert collections["My Standard"]["id"] == standard.id + assert collections["My Standard"]["is_smart"] is False + assert collections["My Smart"]["id"] == smart.id + assert collections["My Smart"]["is_smart"] is True + + +def test_get_rom_omits_smart_collection_without_rom( + client: TestClient, access_token: str, admin_user: User, rom: Rom +): + """A smart collection whose membership doesn't include the ROM is not listed.""" + db_collection_handler.add_smart_collection( + SmartCollection( + name="Unrelated Smart", + description="", + user_id=admin_user.id, + rom_ids=[rom.id + 999], + filter_criteria={}, + ) + ) + + response = client.get( + f"/api/roms/{rom.id}", + headers={"Authorization": f"Bearer {access_token}"}, + ) + assert response.status_code == status.HTTP_200_OK + + assert "Unrelated Smart" not in _user_collections_by_name(response.json()) + + +def test_get_rom_omits_other_users_private_smart_collection( + client: TestClient, access_token: str, viewer_user: User, rom: Rom +): + """A private smart collection owned by another user is hidden even when it + contains the ROM.""" + db_collection_handler.add_smart_collection( + SmartCollection( + name="Private Smart", + description="", + user_id=viewer_user.id, + is_public=False, + rom_ids=[rom.id], + filter_criteria={}, + ) + ) + + response = client.get( + f"/api/roms/{rom.id}", + headers={"Authorization": f"Bearer {access_token}"}, + ) + assert response.status_code == status.HTTP_200_OK + + assert "Private Smart" not in _user_collections_by_name(response.json()) + + +def test_get_rom_lists_public_smart_collection_from_other_user( + client: TestClient, access_token: str, viewer_user: User, rom: Rom +): + """A public smart collection owned by another user is listed when it contains + the ROM.""" + smart = db_collection_handler.add_smart_collection( + SmartCollection( + name="Public Smart", + description="", + user_id=viewer_user.id, + is_public=True, + rom_ids=[rom.id], + filter_criteria={}, + ) + ) + + response = client.get( + f"/api/roms/{rom.id}", + headers={"Authorization": f"Bearer {access_token}"}, + ) + assert response.status_code == status.HTTP_200_OK + + collections = _user_collections_by_name(response.json()) + assert "Public Smart" in collections + assert collections["Public Smart"]["id"] == smart.id + assert collections["Public Smart"]["is_smart"] is True + + def test_download_multi_file_rom_content( client: TestClient, access_token: str, multi_file_rom: Rom ): diff --git a/frontend/src/__generated__/models/UserCollectionSchema.ts b/frontend/src/__generated__/models/UserCollectionSchema.ts index 58debd924..1d44af500 100644 --- a/frontend/src/__generated__/models/UserCollectionSchema.ts +++ b/frontend/src/__generated__/models/UserCollectionSchema.ts @@ -5,5 +5,6 @@ export type UserCollectionSchema = { id: number; name: string; + is_smart?: boolean; }; diff --git a/frontend/src/v2/components/GameDetails/OverviewTab.vue b/frontend/src/v2/components/GameDetails/OverviewTab.vue index c0bbfc761..62fab707c 100644 --- a/frontend/src/v2/components/GameDetails/OverviewTab.vue +++ b/frontend/src/v2/components/GameDetails/OverviewTab.vue @@ -77,11 +77,13 @@ const hasHltb = computed(() => { ); }); -// Enrich the slim `{ id, name }` user_collections payload from the -// ROM with the full Collection record (cover paths, rom_count) the -// store already holds — so we can render real CollectionTile mosaics -// instead of stripped chips. Falls back to a bare entry if the store -// is empty (e.g. deep-link before the AppLayout fetch resolves). +// Enrich the slim `{ id, name, is_smart }` user_collections payload from +// the ROM with the full record (cover paths, rom_count) the store already +// holds — so we can render real CollectionTile mosaics instead of stripped +// chips. Smart collections (#3934) resolve from their own store slice and +// route, and carry the "smart" kind so the tile shows its flash badge. +// Falls back to a bare entry if the store is empty (e.g. deep-link before +// the AppLayout fetch resolves). const { t } = useI18n(); const collectionsStore = storeCollections(); const { toWebp } = useWebpSupport(); @@ -95,21 +97,39 @@ const videos = computed(() => type CollectionTileEntry = { id: number; + // Regular and smart collection ids come from separate tables and can + // collide, so the render key folds in the kind to stay unique. + key: string; name: string; rom_count: number; covers: string[]; link: string; + kind: "regular" | "smart"; }; const userCollectionTiles = computed(() => props.userCollections.map((c) => { + if (c.is_smart) { + const full = collectionsStore.getSmartCollection(c.id); + return { + id: c.id, + key: `smart-${c.id}`, + name: full?.name ?? c.name, + rom_count: full?.rom_count ?? 0, + covers: full ? collectionCoverList(full, toWebp) : [], + link: `/collection/smart/${c.id}`, + kind: "smart", + }; + } const full = collectionsStore.getCollection(c.id); return { id: c.id, + key: `regular-${c.id}`, name: full?.name ?? c.name, rom_count: full?.rom_count ?? 0, covers: full ? collectionCoverList(full, toWebp) : [], link: `/collection/${c.id}`, + kind: "regular", }; }), ); @@ -214,12 +234,12 @@ const coverSource = computed(() => {