fix(tags): fandom views aggregate images via their characters
CI / lint (push) Successful in 2s
CI / frontend-build (push) Successful in 18s
CI / backend-lint-and-test (push) Successful in 28s
CI / integration (push) Successful in 3m24s

A fandom owns characters via Tag.fandom_id, but every image<->tag query
went purely through direct image_tag rows, so a fandom only surfaced
images literally tagged with it — images carrying one of its characters
were invisible to its browse count, previews, and gallery filter.

Derive membership at query time instead of materializing fandom rows
(which would drift on every reassign/merge/remove). Add one shared
predicate in tag_query.py — image_in_tag_scope / image_in_any_tag_scope:
an image belongs to a tag if tagged with it directly OR (when the tag is
a fandom) carrying a character whose fandom_id is that tag. The character
leg is empty for non-fandom tags, so it applies uniformly with no kind
branching. Route all read sites through it:

- gallery _apply_scope: include, OR-groups, and symmetric exclude
- directory image_count: correlated COUNT(DISTINCT) scalar subquery
- directory previews: UNION direct + via-character, then ROW_NUMBER<=3
- cleanup count_tag_associations: Tier-B delete prompt now reports a
  fandom's true blast radius (was 0 for fandoms with no direct rows)

find_unused_tags already protected fandoms via used_via_fandom; left as is.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-26 00:17:25 -04:00
parent b85327a79d
commit 10434509d3
7 changed files with 198 additions and 41 deletions
+26
View File
@@ -103,6 +103,32 @@ async def test_scroll_with_tag_filter(db):
assert page.images[0].id == images[0].id
@pytest.mark.asyncio
async def test_scroll_fandom_includes_images_via_its_characters(db):
# A fandom owns a character (Tag.fandom_id). Filtering by the fandom must
# surface images that carry the CHARACTER but were never tagged with the
# fandom directly — plus images tagged with the fandom directly. Excluding
# the fandom is symmetric: it hides both.
images = await _seed_images(db, 4)
fandom = Tag(name="Naruto", kind=TagKind.fandom)
db.add(fandom)
await db.flush()
char = Tag(name="Sasuke", kind=TagKind.character, fandom_id=fandom.id)
other = Tag(name="unrelated", kind=TagKind.general)
db.add_all([char, other])
await db.flush()
await db.execute(image_tag.insert().values([
{"image_record_id": images[0].id, "tag_id": char.id, "source": "manual"},
{"image_record_id": images[1].id, "tag_id": fandom.id, "source": "manual"},
{"image_record_id": images[2].id, "tag_id": other.id, "source": "manual"},
]))
svc = GalleryService(db)
via = await svc.scroll(cursor=None, limit=10, tag_ids=[fandom.id])
assert {i.id for i in via.images} == {images[0].id, images[1].id}
excluded = await svc.scroll(cursor=None, limit=10, tag_exclude=[fandom.id])
assert {i.id for i in excluded.images} == {images[2].id, images[3].id}
@pytest.mark.asyncio
async def test_timeline_groups_by_month(db):
await _seed_images(db, 3)