Files
romm/backend/endpoints/states.py
Georges-Antoine Assi 4159710e79 docs(api): document the error responses routes raise (#5693)
* 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>

* 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(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>

* docs(api): plain-prose pass on route error descriptions

Spell out clipped error descriptions, replace internal slang (picked,
taken, live, moved past), name the parameter an ID comes from, and split
the long save conversion 400s into sentences.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 09:11:20 -05:00

582 lines
18 KiB
Python

from datetime import datetime, timezone
from typing import Annotated
from fastapi import Body, File, HTTPException
from fastapi import Path as PathVar
from fastapi import Query, Request, UploadFile, status
from fastapi.responses import FileResponse
from decorators.auth import protected_route
from endpoints.responses.assets import StateSchema
from endpoints.roms import refresh_affected_smart_collections
from exceptions.endpoint_exceptions import RomNotFoundInDatabaseException
from handler.asset_store import (
assert_backup,
assert_backups,
release_thumbnail,
remove_asset_file,
rename_asset,
store_screenshot,
store_state_file,
)
from handler.auth.constants import Scope
from handler.auth.dependencies import (
get_rom_visibility_filter,
owned_rom_asset_or_404,
readable_rom_asset_or_404,
visible_rom_or_404,
)
from handler.database import (
db_rom_handler,
db_screenshot_handler,
db_state_handler,
)
from handler.filesystem import fs_asset_handler
from handler.filesystem.assets_handler import (
build_asset_file_response,
check_upload_archive,
)
from logger.formatter import BLUE
from logger.formatter import highlight as hl
from logger.logger import log
from models.assets import EMULATOR_MAX_LENGTH, EMULATOR_VERSION_MAX_LENGTH, State
from models.base import FILE_NAME_MAX_LENGTH
from utils.assets import normalize_asset_labels
from utils.auth import DEVICE_WRITE_FORBIDDEN, resolve_device
from utils.router import APIRouter, RomIdQuery
from utils.uploads import (
check_asset_upload_size,
check_emulator_folder_name,
sanitize_asset_filename,
)
from utils.validation import RomIdScope, narrow_rom_id_scope
async def _delete_state(state: State) -> None:
"""Drop a state row with its file and screenshot."""
# Read first: a screenshot linked by `state_id` goes with the row.
screenshot = state.screenshot
db_state_handler.delete_state(state.id)
await remove_asset_file(state.full_path, "State file")
await release_thumbnail(screenshot)
router = APIRouter(
prefix="/states",
tags=["states"],
)
STATE_FILE_UPLOAD = File(..., description="State file to upload.")
STATE_SCREENSHOT_UPLOAD = File(
default=None,
description="Screenshot file associated with this state.",
)
STATE_FILE_UPDATE = File(default=None, description="Updated state file content.")
STATE_SCREENSHOT_UPDATE = File(default=None, description="Updated screenshot file.")
StateIdPath = Annotated[int, PathVar(description="The ID of the state.")]
OWNED_STATE_NOT_FOUND = "The caller has no state with this ID that it can see."
STATE_FILE_TOO_LARGE = "The state file or screenshot is larger than the server allows."
STATE_IN_CHANNEL = "The state belongs to a sync channel."
@protected_route(
router.post,
"",
[Scope.ASSETS_WRITE],
responses={
status.HTTP_400_BAD_REQUEST: {
"description": (
"The state file has no name, the state or screenshot file name is"
" not valid, or `emulator` is not a valid folder name."
)
},
status.HTTP_403_FORBIDDEN: {"description": DEVICE_WRITE_FORBIDDEN},
status.HTTP_404_NOT_FOUND: {
"description": (
"No rom the caller can see has this ID, or the caller has no device"
" with this `device_id`."
)
},
status.HTTP_409_CONFLICT: {
"description": (
"The upload would replace a state that belongs to a sync channel."
)
},
status.HTTP_413_CONTENT_TOO_LARGE: {"description": STATE_FILE_TOO_LARGE},
},
)
async def add_state(
request: Request,
rom_id: RomIdQuery,
emulator: Annotated[
str | None,
Query(
max_length=EMULATOR_MAX_LENGTH,
description="The emulator that wrote the state, such as `mednafen_psx_hw`.",
),
] = None,
emulator_version: Annotated[
str | None,
Query(
max_length=EMULATOR_VERSION_MAX_LENGTH,
),
] = None,
core: Annotated[
str | None,
Query(
max_length=EMULATOR_MAX_LENGTH,
description="The libretro core that wrote the state, when one did.",
),
] = None,
core_version: Annotated[
str | None,
Query(
max_length=EMULATOR_VERSION_MAX_LENGTH,
),
] = None,
device_id: Annotated[
str | None, Query(description="The uploading device, recorded as its origin.")
] = None,
stateFile: UploadFile = STATE_FILE_UPLOAD,
screenshotFile: UploadFile | None = STATE_SCREENSHOT_UPLOAD,
) -> StateSchema:
"""Upload a state file for a ROM."""
check_asset_upload_size(stateFile, "State file")
check_asset_upload_size(screenshotFile, "Screenshot file")
check_upload_archive(stateFile, "State file")
device = resolve_device(request, device_id, Scope.DEVICES_WRITE)
rom = visible_rom_or_404(request, db_rom_handler.get_rom(rom_id), rom_id)
if not stateFile.filename:
log.error("State file has no filename")
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, detail="State file has no filename"
)
sanitized_state_filename = sanitize_asset_filename(stateFile.filename, "state")
sanitized_screenshot_filename = (
sanitize_asset_filename(screenshotFile.filename, "screenshot")
if screenshotFile and screenshotFile.filename
else ""
)
check_emulator_folder_name(emulator)
log.info(
f"Uploading state {hl(sanitized_state_filename)} for {hl(str(rom.name), color=BLUE)}"
)
reported = {
"emulator_version": emulator_version,
"core": core,
"core_version": core_version,
"origin_device_id": device.id if device else None,
}
db_state = await store_state_file(
request.user,
rom,
emulator,
stateFile,
sanitized_state_filename,
{key: value for key, value in reported.items() if value is not None},
)
if screenshotFile and sanitized_screenshot_filename:
await store_screenshot(
request.user,
rom,
screenshotFile,
sanitized_screenshot_filename,
is_public=db_state.is_public,
)
# Set the last played time for the current user
rom_user = db_rom_handler.get_rom_user(rom_id=rom.id, user_id=request.user.id)
if not rom_user:
rom_user = db_rom_handler.add_rom_user(rom_id=rom.id, user_id=request.user.id)
db_rom_handler.update_rom_user(
rom_user.id, {"last_played": datetime.now(timezone.utc)}
)
# Refetch the rom to get updated states
refreshed = db_rom_handler.get_rom(rom_id)
if not refreshed:
raise RomNotFoundInDatabaseException(rom_id)
rom = refreshed
refresh_affected_smart_collections([rom.id], membership_only=True)
return StateSchema.model_validate(db_state)
@protected_route(router.get, "", [Scope.ASSETS_READ])
def get_states(
request: Request,
rom_id: int | None = None,
rom_ids: Annotated[
RomIdScope,
Query(
description=(
"ROM IDs to scope the results to, for clients syncing a known "
"set of ROMs. Multiple values are allowed by repeating the "
"parameter. Combined with `rom_id` when both are given."
),
),
] = None,
platform_id: int | None = None,
) -> list[StateSchema]:
"""Retrieve states for the current user."""
states = db_state_handler.get_states(
user_id=request.user.id,
rom_ids=narrow_rom_id_scope(rom_id, rom_ids),
platform_id=platform_id,
visibility=get_rom_visibility_filter(request),
)
return [StateSchema.model_validate(state) for state in states]
@protected_route(router.get, "/identifiers", [Scope.ASSETS_READ])
def get_state_identifiers(
request: Request,
) -> list[int]:
"""Retrieve the IDs of the caller's states."""
return db_state_handler.get_state_ids(
user_id=request.user.id, visibility=get_rom_visibility_filter(request)
)
@protected_route(
router.get,
"/{id}",
[Scope.ASSETS_READ],
responses={status.HTTP_404_NOT_FOUND: {"description": OWNED_STATE_NOT_FOUND}},
)
def get_state(request: Request, id: StateIdPath) -> StateSchema:
"""Retrieve a state by ID."""
state = owned_rom_asset_or_404(
request, db_state_handler.get_state_by_id(id), f"State with ID {id} not found"
)
return StateSchema.model_validate(state)
@protected_route(
router.get,
"/{id}/content",
[Scope.ASSETS_READ],
responses={
status.HTTP_404_NOT_FOUND: {
"description": (
"No state the caller can read has this ID, or the state's file is"
" missing."
)
}
},
)
def download_state(request: Request, id: StateIdPath) -> FileResponse:
"""Download a state file. Owner can download any of their states; everyone
else only public ones."""
state = readable_rom_asset_or_404(
request, db_state_handler.get_state_by_id(id), f"State with ID {id} not found"
)
try:
file_path = fs_asset_handler.validate_path(state.full_path)
except ValueError:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="State file not found",
) from None
if not file_path.exists() or not file_path.is_file():
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="State file not found on disk",
)
return build_asset_file_response(file_path, filename=state.file_name)
@protected_route(
router.put,
"/{id}",
[Scope.ASSETS_WRITE],
responses={
status.HTTP_400_BAD_REQUEST: {
"description": "The screenshot file name is not valid."
},
status.HTTP_403_FORBIDDEN: {"description": DEVICE_WRITE_FORBIDDEN},
status.HTTP_404_NOT_FOUND: {
"description": (
"The caller has no state with this ID that it can see, or no device"
" with this `device_id`."
)
},
status.HTTP_409_CONFLICT: {
"description": (
"The request uploads a state file, and the state belongs to a sync"
" channel."
)
},
status.HTTP_413_CONTENT_TOO_LARGE: {"description": STATE_FILE_TOO_LARGE},
},
)
async def update_state(
request: Request,
id: StateIdPath,
device_id: Annotated[
str | None,
Query(description="The device writing new content, recorded as its origin."),
] = None,
stateFile: UploadFile | None = STATE_FILE_UPDATE,
screenshotFile: UploadFile | None = STATE_SCREENSHOT_UPDATE,
) -> StateSchema:
"""Update a state file."""
check_asset_upload_size(stateFile, "State file")
check_asset_upload_size(screenshotFile, "Screenshot file")
check_upload_archive(stateFile, "State file")
device = resolve_device(request, device_id, Scope.DEVICES_WRITE)
db_state = owned_rom_asset_or_404(
request, db_state_handler.get_state_by_id(id), f"State with ID {id} not found"
)
if stateFile:
assert_backup(db_state)
sanitized_screenshot_filename = (
sanitize_asset_filename(screenshotFile.filename, "screenshot")
if screenshotFile and screenshotFile.filename
else ""
)
if stateFile:
await fs_asset_handler.write_file(
file=stateFile, path=db_state.file_path, filename=db_state.file_name
)
db_state = db_state_handler.update_state(
db_state.id,
{
"file_size_bytes": stateFile.size,
"content_hash": await fs_asset_handler.compute_content_hash(
db_state.full_path
),
# The origin follows the content, so a client naming no device
# leaves it unknown rather than credited to the previous writer.
"origin_device_id": device.id if device else None,
},
)
if screenshotFile and sanitized_screenshot_filename:
await store_screenshot(
request.user,
db_state.attached_rom,
screenshotFile,
sanitized_screenshot_filename,
is_public=db_state.is_public,
)
# Set the last played time for the current user
rom_id = db_state.attached_rom_id
rom_user = db_rom_handler.get_rom_user(rom_id, request.user.id)
if not rom_user:
rom_user = db_rom_handler.add_rom_user(rom_id, request.user.id)
db_rom_handler.update_rom_user(
rom_user.id, {"last_played": datetime.now(timezone.utc)}
)
# Refetch the state to get updated fields
return StateSchema.model_validate(db_state)
@protected_route(
router.put,
"/{id}/visibility",
[Scope.ASSETS_WRITE],
responses={
status.HTTP_404_NOT_FOUND: {"description": OWNED_STATE_NOT_FOUND},
status.HTTP_409_CONFLICT: {"description": STATE_IN_CHANNEL},
},
)
def update_state_visibility(
request: Request,
id: StateIdPath,
is_public: Annotated[
bool,
Body(embed=True),
],
) -> StateSchema:
"""Toggle a state's public/private visibility (owner only). A state a
channel holds is shared with the channel."""
state = owned_rom_asset_or_404(
request, db_state_handler.get_state_by_id(id), f"State with ID {id} not found"
)
assert_backup(state)
updated = db_state_handler.update_state(id, {"is_public": is_public}, touch=False)
# Keep the auto-captured thumbnail's visibility in sync so a shared state
# still renders its preview for other users.
if state.screenshot:
db_screenshot_handler.update_screenshot(
state.screenshot.id, {"is_public": is_public}
)
# Sharing a state exposes it to every other user's `has_states` filter.
if state.rom_id is not None:
refresh_affected_smart_collections([state.rom_id], membership_only=True)
return StateSchema.model_validate(updated)
@protected_route(
router.put,
"/{id}/favorite",
[Scope.ASSETS_WRITE],
responses={status.HTTP_404_NOT_FOUND: {"description": OWNED_STATE_NOT_FOUND}},
)
def update_state_favorite(
request: Request,
id: StateIdPath,
is_favorite: Annotated[
bool,
Body(embed=True),
],
) -> StateSchema:
"""Favorite a state, sorting it ahead of the rest (owner only)."""
owned_rom_asset_or_404(
request, db_state_handler.get_state_by_id(id), f"State with ID {id} not found"
)
return StateSchema.model_validate(
db_state_handler.update_state(id, {"is_favorite": is_favorite}, touch=False)
)
@protected_route(
router.put,
"/{id}/labels",
[Scope.ASSETS_WRITE],
responses={status.HTTP_404_NOT_FOUND: {"description": OWNED_STATE_NOT_FOUND}},
)
def update_state_labels(
request: Request,
id: StateIdPath,
labels: Annotated[
list[str],
Body(
embed=True,
description="The new list of labels. It replaces the existing labels.",
),
],
) -> StateSchema:
"""Replace a state's free-text labels (owner only)."""
owned_rom_asset_or_404(
request, db_state_handler.get_state_by_id(id), f"State with ID {id} not found"
)
return StateSchema.model_validate(
db_state_handler.update_state(
id, {"labels": normalize_asset_labels(labels)}, touch=False
)
)
@protected_route(
router.put,
"/{id}/file-name",
[Scope.ASSETS_WRITE],
responses={
status.HTTP_400_BAD_REQUEST: {
"description": "The new file name is not valid or has no name before the extension."
},
status.HTTP_404_NOT_FOUND: {
"description": (
"The caller has no state with this ID that it can see, or the"
" state's file is missing."
)
},
status.HTTP_409_CONFLICT: {
"description": (
"Another state of the rom, or a screenshot that is not this state's,"
" already has the new name."
)
},
},
)
async def rename_state(
request: Request,
id: StateIdPath,
file_name: Annotated[
str,
Body(
embed=True,
max_length=FILE_NAME_MAX_LENGTH,
),
],
) -> StateSchema:
"""Rename a state's file, its screenshot following along (owner only)."""
state = owned_rom_asset_or_404(
request, db_state_handler.get_state_by_id(id), f"State with ID {id} not found"
)
return StateSchema.model_validate(await rename_asset(state, file_name))
@protected_route(
router.post,
"/delete",
[Scope.ASSETS_WRITE],
responses={
status.HTTP_400_BAD_REQUEST: {"description": "The list of states is empty."},
status.HTTP_404_NOT_FOUND: {
"description": "The caller has no state that it can see with one of the given IDs."
},
status.HTTP_409_CONFLICT: {
"description": "One of the states belongs to a sync channel."
},
},
)
async def delete_states(
request: Request,
states: Annotated[
list[int],
Body(
description="List of states ids to delete from database.",
embed=True,
),
],
) -> list[int]:
"""Delete states."""
if not states:
error = "No states were provided"
log.error(error)
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=error)
affected_rom_ids: set[int] = set()
# Resolved up front, so a bad id fails the batch before anything is removed.
owned = [
owned_rom_asset_or_404(
request,
db_state_handler.get_state_by_id(state_id),
f"State with ID {state_id} not found",
)
for state_id in states
]
assert_backups(owned)
for state in owned:
affected_rom_ids.add(state.attached_rom_id)
log.info(
f"Deleting state {hl(state.file_name)} [{state.attached_rom.platform_slug}] from filesystem"
)
await _delete_state(state)
refresh_affected_smart_collections(list(affected_rom_ids), membership_only=True)
return states