mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
- PUT /collections/{id}/visibility and /collections/smart/{id}/visibility
flip is_public alone. The settings switch used the full update, which
rewrote membership from the client's rom_ids and ran the cover branch.
- update_save, update_state and update_screenshot derive the file-name
columns from file_name, like update_rom does for fs_name, so the rename
path hands them a name instead of building the columns itself.
- Name fields use notBlank, AssetEditDialog picks its own title from its
type, ScreenshotEditDialog shares one canSubmit, the collection tile and
row drop a virtual guard their callers already cover, and PublicBadge
uses the pill radius token.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.base import with_file_name_parts
|
|
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.
|
|
"""
|
|
data = with_file_name_parts(data)
|
|
values = data if touch else {**data, "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
|