feat(gallery): composable scroll filter (multi-tag AND, media, sort)
Phase 1 backend for the gallery filter bar. Extends scroll/timeline/jump
from a single mutually-exclusive filter to a composable one:
- tag_ids: image must carry ALL of them (one correlated EXISTS per tag —
AND, no row multiplication), replacing the single-tag JOIN.
- artist_id composes with tags; media_type ('image'|'video') narrows by
mime; post_id stays the exclusive post-detail path.
- sort ('newest'|'oldest') flips the effective_date/id cursor comparison
and ordering; the cursor value is unchanged (direction comes from the
request). jump_cursor honors sort too.
- Shared _apply_scope helper applied across scroll/timeline/jump so the
timeline sidebar reflects the filtered set. API _parse_filters parses
tag_id (comma list), artist_id, media, sort.
Tests: multi-tag AND, media filter, sort reversal (service + API);
post_id-excludes-others; single tag_id back-compat.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -59,3 +59,21 @@ async def test_jump_requires_year_month(client):
|
||||
async def test_image_detail_404_when_missing(client):
|
||||
resp = await client.get("/api/gallery/image/99999")
|
||||
assert resp.status_code == 404
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_scroll_sort_param_reverses(client, db):
|
||||
await _seed(db, 3)
|
||||
newest = await (await client.get("/api/gallery/scroll?limit=10&sort=newest")).get_json()
|
||||
oldest = await (await client.get("/api/gallery/scroll?limit=10&sort=oldest")).get_json()
|
||||
ids_new = [i["id"] for i in newest["images"]]
|
||||
ids_old = [i["id"] for i in oldest["images"]]
|
||||
assert ids_old == list(reversed(ids_new))
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_scroll_media_param(client, db):
|
||||
await _seed(db, 2) # only image/jpeg seeded
|
||||
resp = await client.get("/api/gallery/scroll?limit=10&media=video")
|
||||
assert resp.status_code == 200
|
||||
assert (await resp.get_json())["images"] == []
|
||||
|
||||
@@ -81,10 +81,10 @@ async def test_scroll_artist_id_filter(db):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_mutually_exclusive_filters_raise(db):
|
||||
async def test_post_id_excludes_other_filters(db):
|
||||
svc = GalleryService(db)
|
||||
with pytest.raises(ValueError):
|
||||
await svc.scroll(cursor=None, limit=10, tag_id=1, post_id=2)
|
||||
await svc.scroll(cursor=None, limit=10, tag_ids=[1], post_id=2)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
@@ -87,7 +87,7 @@ async def test_scroll_with_tag_filter(db):
|
||||
)
|
||||
|
||||
svc = GalleryService(db)
|
||||
page = await svc.scroll(cursor=None, limit=10, tag_id=tag.id)
|
||||
page = await svc.scroll(cursor=None, limit=10, tag_ids=[tag.id])
|
||||
assert len(page.images) == 1
|
||||
assert page.images[0].id == images[0].id
|
||||
|
||||
@@ -272,3 +272,53 @@ async def test_get_image_with_tags_includes_posted_at_when_present(db):
|
||||
assert payload["posted_at"] is not None
|
||||
# Image's own created_at is still surfaced separately.
|
||||
assert payload["created_at"] != payload["posted_at"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_scroll_multi_tag_and(db):
|
||||
images = await _seed_images(db, 5)
|
||||
a = Tag(name="aa", kind=TagKind.general)
|
||||
b = Tag(name="bb", kind=TagKind.general)
|
||||
db.add_all([a, b])
|
||||
await db.flush()
|
||||
# images[0] carries BOTH tags; images[1] carries only a.
|
||||
await db.execute(image_tag.insert().values([
|
||||
{"image_record_id": images[0].id, "tag_id": a.id, "source": "manual"},
|
||||
{"image_record_id": images[0].id, "tag_id": b.id, "source": "manual"},
|
||||
{"image_record_id": images[1].id, "tag_id": a.id, "source": "manual"},
|
||||
]))
|
||||
svc = GalleryService(db)
|
||||
both = await svc.scroll(cursor=None, limit=10, tag_ids=[a.id, b.id])
|
||||
assert [i.id for i in both.images] == [images[0].id] # AND: only the both-tagged
|
||||
just_a = await svc.scroll(cursor=None, limit=10, tag_ids=[a.id])
|
||||
assert {i.id for i in just_a.images} == {images[0].id, images[1].id}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_scroll_media_filter(db):
|
||||
imgs = await _seed_images(db, 2) # image/jpeg
|
||||
vid = ImageRecord(
|
||||
path="/images/test/v.mp4", sha256="v" * 64, size_bytes=1,
|
||||
mime="video/mp4", width=1, height=1,
|
||||
origin="imported_filesystem", integrity_status="unknown",
|
||||
)
|
||||
vid.created_at = _now()
|
||||
vid.effective_date = vid.created_at
|
||||
db.add(vid)
|
||||
await db.flush()
|
||||
svc = GalleryService(db)
|
||||
vids = await svc.scroll(cursor=None, limit=10, media_type="video")
|
||||
assert [i.id for i in vids.images] == [vid.id]
|
||||
pics = await svc.scroll(cursor=None, limit=10, media_type="image")
|
||||
assert {i.id for i in pics.images} == {i.id for i in imgs}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_scroll_sort_oldest_reverses_order(db):
|
||||
images = await _seed_images(db, 4) # images[0] newest ... images[3] oldest
|
||||
svc = GalleryService(db)
|
||||
newest = await svc.scroll(cursor=None, limit=10)
|
||||
oldest = await svc.scroll(cursor=None, limit=10, sort="oldest")
|
||||
newest_ids = [i.id for i in newest.images]
|
||||
assert newest_ids[0] == images[0].id
|
||||
assert [i.id for i in oldest.images] == list(reversed(newest_ids))
|
||||
|
||||
Reference in New Issue
Block a user