fix(roms): list smart collections a rom belongs to

The ROM detail response only filled `user_collections` from a rom's
standard collections (the collections_roms join), so smart collections
were silently omitted from the detail overview's Collections row.

Add smart-collection membership to the response: `UserCollectionSchema`
gains an `is_smart` flag and a `smart_for_user` builder that keeps the
smart collections whose cached rom_ids include the rom (respecting own /
public visibility), and `DetailedRomSchema` appends them alongside the
standard ones. The v2 overview renders them with the smart kind badge and
routes them to the smart-collection view.

Fixes #3934

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Spinnich
2026-07-24 14:21:35 +00:00
parent ab59f32e93
commit 95d5cbfcdf
4 changed files with 173 additions and 12 deletions

View File

@@ -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

View File

@@ -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
):

View File

@@ -5,5 +5,6 @@
export type UserCollectionSchema = {
id: number;
name: string;
is_smart?: boolean;
};

View File

@@ -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<CollectionTileEntry[]>(() =>
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(() => {
<CollectionTile
v-for="c in userCollectionTiles"
:id="c.id"
:key="c.id"
:key="c.key"
:to="c.link"
:name="c.name"
:rom-count="c.rom_count"
:covers="c.covers"
kind="regular"
:kind="c.kind"
variant="row"
/>
</div>