feat(saves): scope save discovery to a set of ROM IDs

Sync clients that hold a subset of the library had no bounded way to ask
for the saves relevant to the ROMs on the device: `GET /api/saves` filters
one ROM at a time, and negotiation always evaluated the user's whole
slotted save set, offering downloads for ROMs the device cannot play.

Add a `rom_ids` scope to both. `GET /api/saves` takes a comma-separated
list (matching the `/roms/download` convention) and `POST /api/sync/negotiate`
takes a list in the request body, so a client with many ROMs is not bound by
URL length. Negotiation widens the scope with the ROMs the client sent saves
for, so a client save is never misread as an upload because its ROM was
omitted. The scope is read-only: an omitted ROM is outside the request, never
a deletion signal.

Both share one limit and one validation rule in the saves handler, and both
reject a scope that is non-positive or over the cap rather than truncating.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-08-29 15:44:44 -04:00
parent 55065ef893
commit e36d7646b2
7 changed files with 554 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
from collections.abc import Sequence
from typing import Literal
from collections.abc import Collection, Iterable, Sequence
from typing import Final, Literal
from sqlalchemy import and_, asc, delete, desc, func, or_, select, update
from sqlalchemy.orm import QueryableAttribute, Session, load_only
@@ -10,6 +10,36 @@ from models.rom import Rom
from .base_handler import DBBaseHandler
# Upper bound on the `rom_ids` scope accepted by save queries, to keep the IN
# clause within driver parameter limits. Documented so sync clients can batch.
MAX_ROM_IDS_PER_QUERY: Final = 500
def normalize_rom_id_scope(rom_ids: Iterable[int]) -> list[int]:
"""Validate and deduplicate a `rom_ids` query scope, preserving order.
Args:
rom_ids: ROM IDs the caller wants to scope a save query to.
Returns:
The deduplicated IDs.
Raises:
ValueError: If any ID is not positive, or the scope exceeds
`MAX_ROM_IDS_PER_QUERY`.
"""
unique = list(dict.fromkeys(rom_ids))
if any(rom_id <= 0 for rom_id in unique):
raise ValueError("ROM IDs must be positive integers")
if len(unique) > MAX_ROM_IDS_PER_QUERY:
raise ValueError(
f"Too many ROM IDs: at most {MAX_ROM_IDS_PER_QUERY} per request"
)
return unique
class DBSavesHandler(DBBaseHandler):
@begin_session
@@ -85,6 +115,7 @@ class DBSavesHandler(DBBaseHandler):
self,
user_id: int,
rom_id: int | None = None,
rom_ids: Collection[int] | None = None,
platform_id: int | None = None,
slot: str | None = None,
slot_not_null: bool = False,
@@ -98,6 +129,10 @@ class DBSavesHandler(DBBaseHandler):
if rom_id:
query = query.filter_by(rom_id=rom_id)
# An empty collection is an explicit empty scope, not an absent filter.
if rom_ids is not None:
query = query.filter(Save.rom_id.in_(rom_ids))
if platform_id:
query = query.join(Rom, Save.rom_id == Rom.id).filter(
Rom.platform_id == platform_id