mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 02:22:44 +01:00
A filename and a timestamp cannot tell apart a 100% run from a speedrun
seed, so saves and states carry two owner-only annotations: a favorite
heart that sorts them ahead of their band, and a set of free-text labels.
Backend adds `is_favorite` and a JSON `labels` to both tables, with
`PUT /{saves,states}/{id}/{favorite,labels}` mirroring the existing
visibility toggle. Labels are trimmed, de-duplicated case-insensitively
and capped. Both routes write with `touch=False`, so annotating a save
leaves `updated_at` alone: it is not a write to the bytes, and device
sync reads that column to decide staleness.
In v2 the Save data tab grows a heart and a labels button per row, the
labels dialog suggests the labels already used on the ROM, and favorites
lead their slot or core without ever folding the newest version away.
The heart glyph shows only on the launch screens, where the toggle that
would otherwise say it is not there. Phones give the row four bands:
name, labels, facts, buttons.
Closes #942
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016bZ7R3xC73MZu1vTNuatPK
192 lines
5.7 KiB
Python
192 lines
5.7 KiB
Python
from collections.abc import Collection, Sequence
|
|
|
|
from sqlalchemy import Select, and_, delete, desc, or_, select, update
|
|
from sqlalchemy.orm import Session
|
|
|
|
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)
|
|
)
|
|
|
|
def _states_query(
|
|
self,
|
|
user_id: int,
|
|
rom_ids: Collection[int] | None = None,
|
|
platform_id: int | None = None,
|
|
) -> Select[tuple[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
|
|
)
|
|
|
|
return query
|
|
|
|
@begin_session
|
|
def get_states(
|
|
self,
|
|
user_id: int,
|
|
rom_ids: Collection[int] | None = None,
|
|
platform_id: int | None = None,
|
|
session: Session = None, # type: ignore
|
|
) -> Sequence[State]:
|
|
query = self._states_query(
|
|
user_id=user_id, rom_ids=rom_ids, platform_id=platform_id
|
|
)
|
|
return session.scalars(query).all()
|
|
|
|
@begin_session
|
|
def get_state_ids(
|
|
self,
|
|
user_id: int,
|
|
rom_ids: Collection[int] | None = None,
|
|
platform_id: int | None = None,
|
|
session: Session = None, # type: ignore
|
|
) -> list[int]:
|
|
"""Ids only, so no `State` is built and no eager rom or user join fires."""
|
|
query = self._states_query(
|
|
user_id=user_id, rom_ids=rom_ids, platform_id=platform_id
|
|
)
|
|
return list(session.scalars(query.with_only_columns(State.id)).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,
|
|
touch: bool = True,
|
|
session: Session = None, # type: ignore
|
|
) -> State:
|
|
"""Write `data` onto a state.
|
|
|
|
Args:
|
|
touch: False keeps `updated_at`, since annotating is not a write
|
|
to the bytes and device sync reads it to detect staleness.
|
|
"""
|
|
values = dict(data)
|
|
if not touch:
|
|
values.setdefault("updated_at", State.updated_at)
|
|
session.execute(
|
|
update(State)
|
|
.where(State.id == id)
|
|
.values(**values)
|
|
.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
|