mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
The saves handler carried two ROM filters that ANDed together, and the states handler had only the singular one, so the same "which ROMs" question had three answers across two sibling handlers. Collapse both onto rom_ids: a single-ROM lookup is just a one-element scope, which is what the three internal callers in the saves endpoint now pass. That leaves the routes to fold their own public rom_id param into the scope. Rather than write the fold twice, narrow_rom_id_scope joins utils/validation beside RomIdScope, keeping the documented "combined with rom_id" semantics: both filters still apply together, so the effective scope is their intersection. GET /api/states gains the rom_ids param GET /api/saves got in #4304, declared with the same shared RomIdScope, so a client syncing a known set of ROMs can discover states the same way it discovers saves, under the same cap and the same 422 on a bad scope. One behaviour shift falls out of the handler's old falsy check: rom_id=0 used to be ignored and return the whole library, and now scopes to nothing, which is what RomIdScope's positive-ID rule says a zero means. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
161 lines
4.7 KiB
Python
161 lines
4.7 KiB
Python
from collections.abc import Collection, Sequence
|
|
|
|
from sqlalchemy import and_, delete, desc, or_, select, update
|
|
from sqlalchemy.orm import QueryableAttribute, Session, load_only
|
|
|
|
from decorators.database import begin_session
|
|
from models.assets import State
|
|
from models.rom import Rom
|
|
|
|
from .base_handler import DBBaseHandler
|
|
|
|
|
|
class DBStatesHandler(DBBaseHandler):
|
|
@begin_session
|
|
def add_state(
|
|
self,
|
|
state: State,
|
|
session: Session = None, # type: ignore
|
|
) -> State:
|
|
return session.merge(state)
|
|
|
|
@begin_session
|
|
def get_state(
|
|
self,
|
|
user_id: int,
|
|
id: int,
|
|
session: Session = None, # type: ignore
|
|
) -> State | None:
|
|
return session.scalar(select(State).filter_by(user_id=user_id, id=id).limit(1))
|
|
|
|
@begin_session
|
|
def get_state_by_filename(
|
|
self,
|
|
user_id: int,
|
|
rom_id: int,
|
|
file_name: str,
|
|
session: Session = None, # type: ignore
|
|
) -> State | None:
|
|
return session.scalar(
|
|
select(State)
|
|
.filter_by(rom_id=rom_id, user_id=user_id, file_name=file_name)
|
|
.limit(1)
|
|
)
|
|
|
|
@begin_session
|
|
def get_states(
|
|
self,
|
|
user_id: int,
|
|
rom_ids: Collection[int] | None = None,
|
|
platform_id: int | None = None,
|
|
only_fields: Sequence[QueryableAttribute] | None = None,
|
|
session: Session = None, # type: ignore
|
|
) -> Sequence[State]:
|
|
query = select(State).filter_by(user_id=user_id)
|
|
|
|
# An empty collection is an explicit empty scope, not an absent filter.
|
|
if rom_ids is not None:
|
|
query = query.filter(State.rom_id.in_(rom_ids))
|
|
|
|
if platform_id:
|
|
query = query.join(Rom, State.rom_id == Rom.id).filter(
|
|
Rom.platform_id == platform_id
|
|
)
|
|
|
|
if only_fields:
|
|
query = query.options(load_only(*only_fields))
|
|
|
|
return session.scalars(query).all()
|
|
|
|
@begin_session
|
|
def get_state_by_id(
|
|
self,
|
|
id: int,
|
|
session: Session = None, # type: ignore
|
|
) -> State | None:
|
|
"""Fetch a state by id without scoping to an owner. Used for the
|
|
visibility toggle and community downloads, where the caller may not own
|
|
the state. Mirrors db_screenshot_handler.get_screenshot_by_id."""
|
|
return session.get(State, id)
|
|
|
|
@begin_session
|
|
def get_rom_shared_states(
|
|
self,
|
|
rom_id: int,
|
|
user_id: int,
|
|
public_only: bool = False,
|
|
session: Session = None, # type: ignore
|
|
) -> Sequence[State]:
|
|
"""States for a ROM visible to the requesting user: own (public +
|
|
private) plus other users' public ones. Mirrors
|
|
db_screenshot_handler.get_rom_gallery_screenshots."""
|
|
query = select(State).filter(State.rom_id == rom_id)
|
|
|
|
if public_only:
|
|
query = query.filter(State.is_public)
|
|
else:
|
|
query = query.filter(or_(State.user_id == user_id, State.is_public))
|
|
|
|
query = query.order_by(desc(State.updated_at))
|
|
return session.scalars(query).all()
|
|
|
|
@begin_session
|
|
def update_state(
|
|
self,
|
|
id: int,
|
|
data: dict,
|
|
session: Session = None, # type: ignore
|
|
) -> State:
|
|
session.execute(
|
|
update(State)
|
|
.where(State.id == id)
|
|
.values(**data)
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|
|
return session.query(State).filter_by(id=id).one()
|
|
|
|
@begin_session
|
|
def delete_state(
|
|
self,
|
|
id: int,
|
|
session: Session = None, # type: ignore
|
|
) -> None:
|
|
session.execute(
|
|
delete(State)
|
|
.where(State.id == id)
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|
|
|
|
@begin_session
|
|
def mark_missing_states(
|
|
self,
|
|
rom_id: int,
|
|
user_id: int,
|
|
states_to_keep: list[str],
|
|
session: Session = None, # type: ignore
|
|
) -> Sequence[State]:
|
|
missing_states = session.scalars(
|
|
select(State).filter(
|
|
and_(
|
|
State.rom_id == rom_id,
|
|
State.user_id == user_id,
|
|
State.file_name.not_in(states_to_keep),
|
|
)
|
|
)
|
|
).all()
|
|
|
|
session.execute(
|
|
update(State)
|
|
.where(
|
|
and_(
|
|
State.rom_id == rom_id,
|
|
State.user_id == user_id,
|
|
State.file_name.not_in(states_to_keep),
|
|
)
|
|
)
|
|
.values(**{"missing_from_fs": True})
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|
|
|
|
return missing_states
|