feat(provenance): per-image artist on gallery scroll payload

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-18 20:15:07 -04:00
parent 7014e6827d
commit f6d5353b3b
3 changed files with 140 additions and 1 deletions
+1
View File
@@ -43,6 +43,7 @@ async def scroll():
"height": i.height,
"created_at": i.created_at.isoformat(),
"thumbnail_url": i.thumbnail_url,
"artist": i.artist,
}
for i in page.images
],
+45 -1
View File
@@ -13,7 +13,7 @@ from datetime import datetime
from sqlalchemy import and_, exists, func, or_, select
from sqlalchemy.ext.asyncio import AsyncSession
from ..models import ImageProvenance, ImageRecord, Source, Tag
from ..models import Artist, ImageProvenance, ImageRecord, Post, Source, Tag
from ..models.tag import image_tag
CURSOR_SEPARATOR = "|"
@@ -43,6 +43,7 @@ class GalleryImage:
height: int | None
created_at: datetime
thumbnail_url: str
artist: dict | None = None
@dataclass(frozen=True)
@@ -92,6 +93,47 @@ def _provenance_clause(post_id, artist_id):
return None
async def _artists_for(session, image_ids: list[int]) -> dict[int, dict]:
"""Map image_id -> {"name","slug"} using the primary-post path first,
then the lowest-id ImageProvenance row as fallback. Bounded by page
size (<= 200) → two indexed selects, no N+1."""
if not image_ids:
return {}
out: dict[int, dict] = {}
primary = (
select(ImageRecord.id, Artist.name, Artist.slug)
.join(Post, Post.id == ImageRecord.primary_post_id)
.join(Source, Source.id == Post.source_id)
.join(Artist, Artist.id == Source.artist_id)
.where(ImageRecord.id.in_(image_ids))
)
for img_id, name, slug in (await session.execute(primary)).all():
out[img_id] = {"name": name, "slug": slug}
remaining = [i for i in image_ids if i not in out]
if remaining:
fb = (
select(
ImageProvenance.image_record_id,
ImageProvenance.id,
Artist.name,
Artist.slug,
)
.join(Source, Source.id == ImageProvenance.source_id)
.join(Artist, Artist.id == Source.artist_id)
.where(ImageProvenance.image_record_id.in_(remaining))
.order_by(
ImageProvenance.image_record_id.asc(),
ImageProvenance.id.asc(),
)
)
for img_id, _pid, name, slug in (await session.execute(fb)).all():
if img_id not in out: # first (lowest id) wins
out[img_id] = {"name": name, "slug": slug}
return out
class GalleryService:
def __init__(self, session: AsyncSession):
self.session = session
@@ -135,6 +177,7 @@ class GalleryService:
next_cursor = encode_cursor(last.created_at, last.id)
rows = rows[:limit]
artists = await _artists_for(self.session, [r.id for r in rows])
images = [
GalleryImage(
id=r.id,
@@ -145,6 +188,7 @@ class GalleryService:
height=r.height,
created_at=r.created_at,
thumbnail_url=thumbnail_url(r.sha256, r.mime),
artist=artists.get(r.id),
)
for r in rows
]