fix(collections): refresh smart collections on per-user state changes

Removing the read-time recompute left the cached membership blind to
everything that is not a ROM edit. A save, a state, a status, or moving
a ROM in or out of a regular collection all change what `has_saves`,
`has_states`, `statuses`, `collection_id` and `favorite` match, so the
stored counts, covers and ROM-detail chips went stale until an unrelated
edit or the next scan.

Refresh from those paths too: save and state upload, delete and
visibility toggle, `update_rom_user` when a status is set, and adding or
removing collection members. Sharing an asset is included because
`has_saves` matches other users' public saves, so it moves collections
that are not the uploader's.

These changes leave the ROM row alone, so they pass `membership_only`
and only recount a collection when the ROM actually joined or left.
Autosaves land constantly, and without it every one of them would force
a full recount of any collection the ROM already belonged to.

Reported by Greptile on #4039.

Implemented with Claude Code (Opus 5), tests first, then reviewed and
verified by me.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Spinnich
2026-07-31 22:18:53 +00:00
parent b5d276e2ed
commit 7399c19479
7 changed files with 157 additions and 11 deletions

View File

@@ -637,15 +637,21 @@ class DBCollectionsHandler(DBBaseHandler):
def refresh_smart_collections_for_roms(
self,
rom_ids: Sequence[int],
membership_only: bool = False,
session: Session = None, # type: ignore
) -> int:
"""Refresh the collections a handful of edited or deleted ROMs touch.
"""Refresh the collections a handful of changed ROMs touch.
Editing one ROM rarely moves any collection, and asking whether given
ids match is an indexed lookup, so only the collections that actually
hold one of them pay for a recount. Membership is then recomputed whole,
since a ROM that stayed a member can still have changed the stored order
or the cover mosaic.
hold one of them pay for a recount.
`membership_only` is for changes that leave the ROM row alone, like a
save, a state, a status or collection membership: nothing about the ROM
can have moved except whether it matches, so a member that stayed one
needs no recount. That matters because autosaves land constantly. The
default suits a library change, where a ROM that stayed a member can
still have a new name, sort position or cover to reflect.
"""
from handler.database import db_rom_handler
@@ -661,7 +667,9 @@ class DBCollectionsHandler(DBBaseHandler):
user_id=smart_collection.user_id,
session=session,
)
if matching or candidates & set(smart_collection.rom_ids):
cached = candidates & set(smart_collection.rom_ids)
moved = matching != cached if membership_only else bool(matching or cached)
if moved:
self.refresh_smart_collection(smart_collection.id, session=session)
refreshed += 1