mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
* 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>
137 lines
4.1 KiB
Python
137 lines
4.1 KiB
Python
from fastapi import HTTPException, Request, status
|
|
|
|
from decorators.auth import protected_route
|
|
from endpoints.responses.notification import (
|
|
NotificationCreatePayload,
|
|
NotificationIdsPayload,
|
|
NotificationSchema,
|
|
)
|
|
from handler.auth.constants import Scope
|
|
from handler.auth.dependencies import assert_admin
|
|
from handler.database import db_notification_handler
|
|
from handler.notification_handler import (
|
|
NOTIFICATIONS_DISMISSED_EVENT,
|
|
NOTIFICATIONS_READ_EVENT,
|
|
UnknownRecipientsError,
|
|
resolve_recipients,
|
|
send,
|
|
)
|
|
from handler.socket_handler import socket_handler
|
|
from utils.router import APIRouter
|
|
|
|
router = APIRouter(
|
|
prefix="/notifications",
|
|
tags=["notifications"],
|
|
)
|
|
|
|
|
|
@protected_route(router.get, "", [Scope.ME_READ])
|
|
def get_notifications(request: Request) -> list[NotificationSchema]:
|
|
"""The caller's notifications, newest first."""
|
|
return [
|
|
NotificationSchema.model_validate(n)
|
|
for n in db_notification_handler.get_notifications(request.user.id)
|
|
]
|
|
|
|
|
|
@protected_route(
|
|
router.post,
|
|
"",
|
|
[Scope.ME_WRITE],
|
|
status_code=status.HTTP_201_CREATED,
|
|
responses={
|
|
status.HTTP_403_FORBIDDEN: {
|
|
"description": (
|
|
"The notification is for other users, and the caller is not an admin"
|
|
" or lacks the `users.write` scope."
|
|
)
|
|
},
|
|
status.HTTP_404_NOT_FOUND: {
|
|
"description": "A user in `recipients` does not exist or is disabled."
|
|
},
|
|
},
|
|
)
|
|
async def create_notification(
|
|
request: Request, payload: NotificationCreatePayload
|
|
) -> list[NotificationSchema]:
|
|
"""Notify the caller or, for an admin, other users; returns what was sent.
|
|
|
|
Other users see the caller as its sender.
|
|
"""
|
|
sender_id = request.user.id
|
|
if payload.recipients not in (None, [sender_id]):
|
|
assert_admin(request)
|
|
# An admin's token scoped below users.write must not speak for them to others.
|
|
if Scope.USERS_WRITE not in request.auth.scopes:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Notifying other users needs the users.write scope",
|
|
)
|
|
|
|
try:
|
|
user_ids = resolve_recipients(sender_id, payload.recipients)
|
|
except UnknownRecipientsError as exc:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail=str(exc)
|
|
) from exc
|
|
|
|
return await send(
|
|
sender_id,
|
|
user_ids,
|
|
payload.kind,
|
|
payload.level,
|
|
payload.data,
|
|
title=payload.title,
|
|
body=payload.body,
|
|
link=payload.link,
|
|
icon=payload.icon,
|
|
)
|
|
|
|
|
|
@protected_route(router.post, "/read", [Scope.ME_WRITE])
|
|
async def mark_notifications_read(
|
|
request: Request, payload: NotificationIdsPayload
|
|
) -> None:
|
|
"""Mark the given notifications read, or all of them when `ids` is null."""
|
|
db_notification_handler.mark_read(request.user.id, payload.ids)
|
|
await socket_handler.emit_to_user(
|
|
request.user.id, NOTIFICATIONS_READ_EVENT, {"ids": payload.ids}
|
|
)
|
|
|
|
|
|
@protected_route(
|
|
router.delete,
|
|
"/{notification_id}",
|
|
[Scope.ME_WRITE],
|
|
responses={
|
|
status.HTTP_404_NOT_FOUND: {
|
|
"description": "The caller has no notification with this ID."
|
|
}
|
|
},
|
|
)
|
|
async def dismiss_notification(
|
|
request: Request,
|
|
notification_id: int,
|
|
) -> None:
|
|
"""Dismiss one of the caller's notifications."""
|
|
rows = db_notification_handler.delete_notifications(
|
|
request.user.id, [notification_id]
|
|
)
|
|
if rows == 0:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Notification not found",
|
|
)
|
|
await socket_handler.emit_to_user(
|
|
request.user.id, NOTIFICATIONS_DISMISSED_EVENT, {"ids": [notification_id]}
|
|
)
|
|
|
|
|
|
@protected_route(router.delete, "", [Scope.ME_WRITE])
|
|
async def dismiss_all_notifications(request: Request) -> None:
|
|
"""Dismiss all of the caller's notifications."""
|
|
db_notification_handler.delete_notifications(request.user.id)
|
|
await socket_handler.emit_to_user(
|
|
request.user.id, NOTIFICATIONS_DISMISSED_EVENT, {"ids": None}
|
|
)
|