mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
* test(api): check that routes document the error statuses they raise protected_route now documents the scope guard's 401 and 403 on every route that declares scopes, appending to a route's own description when it has one. tools/route_errors.py follows each route's calls through imports, handler instances, custom exception classes and HTTPException subclasses to the statuses it can raise. test_raised_statuses_are_documented fails on a status the route's responses= leaves out, against a baseline of existing gaps that can only shrink. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(api): document error responses for roms routes Describe every 4xx and 5xx the rom routes and their helpers raise in each decorator's responses=, so the OpenAPI schema lists them. A hidden rom, platform or file is described like a missing one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(api): document error responses for saves routes Describe every 4xx/5xx the saves, states, screenshots, snapshots, play-sessions and sync routes raise in their responses=, and rewrite the states identifiers docstring that carried Args/Returns blocks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(api): document error responses for devices routes Describe the 4xx/5xx statuses that the device, device auth, install, client token, notification, notification channel, sync channel and heartbeat routes raise, and fill the empty install route entries. Rewrite the setup and heartbeat docstrings that published Args and Returns blocks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(api): document error responses for streaming routes Describe the 4xx and 5xx statuses the streaming, memory card and netplay routes raise in their responses= declarations, and replace the Args: blocks in route docstrings that FastAPI published verbatim. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(api): document error responses for admin routes Describe the 4xx and 5xx statuses the config, permissions, user, auth, tasks, logs, activity and export routes raise, and rewrite their Args/Returns docstrings as plain operation descriptions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(api): document error responses for library routes Describe the 400/403/404/409/500 statuses the collection, feed, firmware, platform, music, playlist and search routes raise, and replace route docstrings that carried Args/Returns blocks with plain sentences. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(api): answer like a missing resource for hidden play sessions and private collections DELETE /play-sessions/{id} deleted the caller's session even on a rom hidden from them, so 204 versus 404 showed the session existed. The handler now takes the caller's rom visibility. Another user's private collection, smart collection or playlist answered 403, which showed it existed. It now answers 404 on every route; a public one the caller does not own still answers 403 on write. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(api): describe private collections and playlists as missing Matches #5686, where another user's private collection or playlist answers 404 and a play session on a hidden rom answers like a missing one. The read routes drop their 403, and the write 403 now covers only a public resource another user owns. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(collections): use #5693's docstrings on the routes this PR touches Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(api): address Greptile review on route error docs Fold the nine multi-paragraph route docstrings into one line, and keep the device-scope 403 descriptions in utils/auth.py next to resolve_device, which raises them, instead of in both saves.py and states.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(api): follow route dependencies in the status analyzer Addresses Greptile's review: route_errors now follows the callable in Depends(...) and Security(...), whether it is a parameter default or inside an Annotated alias, and the module docstring is one line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(api): tighten wording of route error descriptions Give clipped responses descriptions and helper docstrings their missing subjects and verbs, use 'the caller lacks' and 'is not allowed to' consistently, and replace 'picked' with 'chosen'. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(api): drop the route error baseline Every status left in the baseline was one a client cannot get: - The export routes' empty platform list check is dead, since FastAPI answers a missing required list query with 422. Removed. - get_save_layouts went through _restore_error, whose 500 fallback it cannot reach. It raises its 400 and 503 directly now. - The notification test and confirm routes only catch ChannelError, since the cooldown and email errors come from other actions. - The analyzer now skips asyncio.gather(..., return_exceptions=True), which covers search_rom. - A '# route-errors: ignore' marker, with its reason, skips code whose errors never reach the client: the metadata heartbeat probe, the force-release teardowns, and the legacy save adoption. The check now fails on any undocumented status. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
139 lines
4.5 KiB
Python
139 lines
4.5 KiB
Python
from collections.abc import Sequence
|
|
from datetime import datetime, timezone
|
|
|
|
from sqlalchemy import and_, delete, func, or_, select
|
|
from sqlalchemy.orm import Session
|
|
|
|
from decorators.database import INJECTED_SESSION, begin_session
|
|
from handler.auth.rom_visibility import UNRESTRICTED, RomVisibilityFilter
|
|
from models.play_session import PlaySession
|
|
from models.rom import Rom
|
|
|
|
from .base_handler import DBBaseHandler, affected_rows
|
|
|
|
|
|
class DBPlaySessionsHandler(DBBaseHandler):
|
|
@begin_session
|
|
def add_sessions(
|
|
self,
|
|
play_sessions: list[PlaySession],
|
|
session: Session = INJECTED_SESSION,
|
|
) -> list[PlaySession]:
|
|
session.add_all(play_sessions)
|
|
session.flush()
|
|
return play_sessions
|
|
|
|
@begin_session
|
|
def find_existing(
|
|
self,
|
|
user_id: int,
|
|
device_id: str | None,
|
|
rom_start_pairs: list[tuple[int | None, datetime]],
|
|
session: Session = INJECTED_SESSION,
|
|
) -> set[tuple[int | None, datetime]]:
|
|
"""Return which (rom_id, start_time) pairs already exist for this user+device."""
|
|
if not rom_start_pairs:
|
|
return set()
|
|
|
|
device_clause = (
|
|
PlaySession.device_id.is_(None)
|
|
if device_id is None
|
|
else PlaySession.device_id == device_id
|
|
)
|
|
|
|
rom_clauses = []
|
|
for rom_id, start_time in rom_start_pairs:
|
|
rom_match = (
|
|
PlaySession.rom_id.is_(None)
|
|
if rom_id is None
|
|
else PlaySession.rom_id == rom_id
|
|
)
|
|
rom_clauses.append(and_(rom_match, PlaySession.start_time == start_time))
|
|
|
|
stmt = select(
|
|
PlaySession.rom_id,
|
|
PlaySession.start_time,
|
|
).where(PlaySession.user_id == user_id, device_clause, or_(*rom_clauses))
|
|
|
|
return {
|
|
(
|
|
r.rom_id,
|
|
(
|
|
r.start_time.replace(tzinfo=timezone.utc)
|
|
if r.start_time.tzinfo is None
|
|
else r.start_time
|
|
),
|
|
)
|
|
for r in session.execute(stmt).all()
|
|
}
|
|
|
|
@begin_session
|
|
def get_sessions(
|
|
self,
|
|
user_id: int,
|
|
rom_id: int | None = None,
|
|
device_id: str | None = None,
|
|
start_after: datetime | None = None,
|
|
end_before: datetime | None = None,
|
|
limit: int | None = 50,
|
|
offset: int = 0,
|
|
visibility: RomVisibilityFilter = UNRESTRICTED,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> Sequence[PlaySession]:
|
|
stmt = select(PlaySession).filter_by(user_id=user_id)
|
|
|
|
if not visibility.is_unrestricted:
|
|
# Before the slice, so hidden sessions can't shorten a page.
|
|
stmt = stmt.outerjoin(Rom, PlaySession.rom_id == Rom.id).where(
|
|
or_(PlaySession.rom_id.is_(None), and_(*visibility.clauses()))
|
|
)
|
|
|
|
if rom_id is not None:
|
|
stmt = stmt.where(PlaySession.rom_id == rom_id)
|
|
if device_id is not None:
|
|
stmt = stmt.where(PlaySession.device_id == device_id)
|
|
if start_after is not None:
|
|
stmt = stmt.where(PlaySession.start_time >= start_after)
|
|
if end_before is not None:
|
|
stmt = stmt.where(PlaySession.start_time <= end_before)
|
|
|
|
stmt = stmt.order_by(PlaySession.start_time.desc())
|
|
if limit is not None:
|
|
stmt = stmt.limit(limit)
|
|
stmt = stmt.offset(offset)
|
|
return session.scalars(stmt).all()
|
|
|
|
@begin_session
|
|
def get_total_play_time(
|
|
self,
|
|
user_id: int,
|
|
rom_id: int,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> int:
|
|
result = session.scalar(
|
|
select(func.sum(PlaySession.duration_ms)).where(
|
|
PlaySession.user_id == user_id,
|
|
PlaySession.rom_id == rom_id,
|
|
)
|
|
)
|
|
return result or 0
|
|
|
|
@begin_session
|
|
def delete_session(
|
|
self,
|
|
session_id: int,
|
|
user_id: int,
|
|
visibility: RomVisibilityFilter,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> bool:
|
|
stmt = delete(PlaySession).where(
|
|
PlaySession.id == session_id, PlaySession.user_id == user_id
|
|
)
|
|
if not visibility.is_unrestricted:
|
|
visible_roms = select(Rom.id).where(and_(*visibility.clauses()))
|
|
stmt = stmt.where(
|
|
or_(PlaySession.rom_id.is_(None), PlaySession.rom_id.in_(visible_roms))
|
|
)
|
|
result = session.execute(stmt.execution_options(synchronize_session=False))
|
|
return affected_rows(result) > 0
|