Files
romm/backend/endpoints/roms/notes.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

182 lines
5.0 KiB
Python

from typing import Annotated, Any
from fastapi import Body, HTTPException
from fastapi import Path as PathVar
from fastapi import Query, Request, status
from decorators.auth import protected_route
from endpoints.responses.rom import UserNoteSchema
from handler.auth.constants import Scope
from handler.auth.dependencies import visible_rom_or_404
from handler.database import db_rom_handler
from utils.router import APIRouter, RomIdPath
router = APIRouter()
DEFAULT_PUBLIC_ONLY = Query(False, description="Only return public notes")
DEFAULT_SEARCH = Query(None, description="Search notes by title or content")
DEFAULT_TAGS = Query(None, description="Filter by tags")
@protected_route(
router.get,
"/{id}/notes",
[Scope.ROMS_READ],
responses={
status.HTTP_404_NOT_FOUND: {
"description": "No rom the caller can see has this ID."
},
},
)
async def get_rom_notes(
request: Request,
id: RomIdPath,
public_only: bool = DEFAULT_PUBLIC_ONLY,
search: str = DEFAULT_SEARCH,
tags: list[str] = DEFAULT_TAGS,
) -> list[UserNoteSchema]:
"""Get all notes for a ROM."""
visible_rom_or_404(request, db_rom_handler.get_rom_visibility(id), id)
if tags is None:
tags = []
notes = db_rom_handler.get_rom_notes(
rom_id=id,
user_id=request.user.id,
public_only=public_only,
search=search,
tags=tags,
)
return [UserNoteSchema.from_rom_note(note) for note in notes]
@protected_route(
router.get,
"/{id}/notes/identifiers",
[Scope.ROMS_READ],
responses={
status.HTTP_404_NOT_FOUND: {
"description": "No rom the caller can see has this ID."
},
},
)
async def get_rom_note_identifiers(
request: Request,
id: RomIdPath,
) -> list[int]:
"""Get all note identifiers for a ROM."""
visible_rom_or_404(request, db_rom_handler.get_rom_visibility(id), id)
return db_rom_handler.get_rom_note_ids(
rom_id=id,
user_id=request.user.id,
)
@protected_route(
router.post,
"/{id}/notes",
[Scope.ROMS_USER_WRITE],
responses={
status.HTTP_404_NOT_FOUND: {
"description": "No rom the caller can see has this ID."
},
},
)
async def create_rom_note(
request: Request,
id: RomIdPath,
note_data: Annotated[dict[str, Any], Body()],
) -> UserNoteSchema:
"""Create a new note for a ROM."""
visible_rom_or_404(request, db_rom_handler.get_rom_visibility(id), id)
note = db_rom_handler.create_rom_note(
rom_id=id,
user_id=request.user.id,
title=note_data["title"],
content=note_data.get("content", ""),
is_public=note_data.get("is_public", False),
tags=note_data.get("tags", []),
)
# Add author identity to the note data
note["username"] = request.user.username
note["user_avatar_path"] = request.user.avatar_path
note["user_updated_at"] = request.user.updated_at
return UserNoteSchema.model_validate(note)
@protected_route(
router.put,
"/{id}/notes/{note_id}",
[Scope.ROMS_USER_WRITE],
responses={
status.HTTP_404_NOT_FOUND: {
"description": "No rom the caller can see has this ID, or the caller has no note with this note ID on the rom."
},
},
)
async def update_rom_note(
request: Request,
id: RomIdPath,
note_id: Annotated[int, PathVar(description="Note id.", ge=1)],
note_data: Annotated[dict[str, Any], Body()],
) -> UserNoteSchema:
"""Update a ROM note."""
rom = visible_rom_or_404(request, db_rom_handler.get_rom_visibility(id), id)
note = db_rom_handler.update_rom_note(
note_id=note_id,
user_id=request.user.id,
rom_id=rom.id,
**{
k: v
for k, v in note_data.items()
if k in ["title", "content", "is_public", "tags"]
},
)
if not note:
raise HTTPException(
status_code=404, detail="Note not found or not owned by user"
)
# Add author identity to the note data
note["username"] = request.user.username
note["user_avatar_path"] = request.user.avatar_path
note["user_updated_at"] = request.user.updated_at
return UserNoteSchema.model_validate(note)
@protected_route(
router.delete,
"/{id}/notes/{note_id}",
[Scope.ROMS_USER_WRITE],
responses={
status.HTTP_404_NOT_FOUND: {
"description": "No rom the caller can see has this ID, or the caller has no note with this note ID on the rom."
},
},
)
async def delete_rom_note(
request: Request,
id: RomIdPath,
note_id: Annotated[int, PathVar(description="Note id.", ge=1)],
) -> dict[str, Any]:
"""Delete a ROM note."""
rom = visible_rom_or_404(request, db_rom_handler.get_rom_visibility(id), id)
success = db_rom_handler.delete_rom_note(
note_id=note_id, user_id=request.user.id, rom_id=rom.id
)
if not success:
raise HTTPException(
status_code=404, detail="Note not found or not owned by user"
)
return {"message": "Note deleted successfully"}