Files
romm/backend/endpoints/configs.py
zurdi d9d65d8b56 refactor(audit): apply the pr-ready simplify pass
- record() takes the request as its actor, and a target can be a
  function: the lookups that name it now run inside the recorder's error
  handling, and for downloads only once the dedupe lets them record.
  That also restores the minimal visibility query on the file route.
- One changed_fields/change pair for rom, user, collection and platform
  edits, one collection helper, AuditActor.anonymous, a shared
  count_in_window and current_device_id, one test helper for recorded
  events and one fixture for audit events.
- The describers share simple/counted/visibility builders and the
  eventData readers with notifications, join lists per locale, and treat
  any *.delete as unlinked.
- EventLog builds one item list with the phase folded in, keeps each
  event's sentence across pages, formats days once, and loads users
  through the users store.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 02:45:12 +02:00

341 lines
12 KiB
Python

from fastapi import HTTPException, Request, status
from pydantic import BaseModel, field_validator
from config.config_manager import (
DEFAULT_EXCLUDED_EXTENSIONS,
DEFAULT_EXCLUDED_FILES,
DEFAULT_EXCLUDED_MULTI_FILE_DIRS,
DEFAULT_EXCLUDED_PLATFORM_DIRS,
VALID_GAMELIST_IMAGE_TYPES,
VALID_GAMELIST_THUMBNAIL_TYPES,
VALID_SCAN_PRIORITY_SOURCES,
ExclusionType,
MetadataMediaType,
)
from config.config_manager import config_manager as cm
from decorators.auth import protected_route
from endpoints.responses.config import ConfigResponse
from exceptions.config_exceptions import ConfigNotWritableException
from handler.audit_handler import AuditTarget, record
from handler.auth.constants import Scope
from handler.database import db_rom_handler
from handler.filesystem import fs_platform_handler
from logger.logger import log
from models.audit_event import AuditAction, AuditTargetType
from utils.router import APIRouter
router = APIRouter(
prefix="/config",
tags=["config"],
)
class PlatformBindingPayload(BaseModel):
fs_slug: str
slug: str
class ExclusionPayload(BaseModel):
exclusion_value: str
# Left a string rather than the enum to keep the generated frontend type one.
exclusion_type: str
@field_validator("exclusion_type")
@classmethod
def validate_exclusion_type(cls, value: str) -> str:
if value not in ExclusionType:
raise ValueError(
f"Unknown exclusion type, expected one of {[t.value for t in ExclusionType]}"
)
return value
class ScanSettingsPayload(BaseModel):
"""Full replacement of the scan.* config section.
The three artwork override lists (cover/screenshot/manual) are optional:
a null value clears the override so that field falls back to
`artwork_priority`.
"""
metadata_priority: list[str]
artwork_priority: list[str]
cover_priority: list[str] | None = None
screenshot_priority: list[str] | None = None
manual_priority: list[str] | None = None
region_priority: list[str]
language_priority: list[str]
media: list[MetadataMediaType]
gamelist_export: bool
gamelist_thumbnail: MetadataMediaType
gamelist_image: MetadataMediaType
pegasus_export: bool
@field_validator(
"metadata_priority",
"artwork_priority",
"cover_priority",
"screenshot_priority",
"manual_priority",
)
@classmethod
def validate_sources(cls, value: list[str] | None) -> list[str] | None:
if value is None:
return value
invalid = [s for s in value if s not in VALID_SCAN_PRIORITY_SOURCES]
if invalid:
raise ValueError(f"Unknown metadata source(s): {sorted(set(invalid))}")
# Drop duplicates while preserving priority order.
return list(dict.fromkeys(value))
@field_validator("region_priority", "language_priority")
@classmethod
def validate_codes(cls, value: list[str]) -> list[str]:
# Region/language codes are open sets (provider-defined), so we only
# normalize to lowercase, trim blanks, and drop duplicates.
cleaned = [code.strip().lower() for code in value if code and code.strip()]
return list(dict.fromkeys(cleaned))
@field_validator("gamelist_thumbnail")
@classmethod
def validate_thumbnail(cls, value: MetadataMediaType) -> MetadataMediaType:
if value not in VALID_GAMELIST_THUMBNAIL_TYPES:
raise ValueError(
f"Invalid gamelist thumbnail; valid options: "
f"{sorted(o.value for o in VALID_GAMELIST_THUMBNAIL_TYPES)}"
)
return value
@field_validator("gamelist_image")
@classmethod
def validate_image(cls, value: MetadataMediaType) -> MetadataMediaType:
if value not in VALID_GAMELIST_IMAGE_TYPES:
raise ValueError(
f"Invalid gamelist image; valid options: "
f"{sorted(o.value for o in VALID_GAMELIST_IMAGE_TYPES)}"
)
return value
@router.get("")
def get_config(request: Request) -> ConfigResponse:
"""Get config endpoint
Returns:
ConfigResponse: RomM's configuration
"""
cfg = cm.get_config()
return ConfigResponse(
CONFIG_FILE_MOUNTED=cfg.CONFIG_FILE_MOUNTED,
CONFIG_FILE_WRITABLE=cfg.CONFIG_FILE_WRITABLE,
# Raw parser error may leak the config file path, so only send when authenticated
CONFIG_FILE_PARSE_ERROR=(
cfg.CONFIG_FILE_PARSE_ERROR if request.user.is_authenticated else None
),
EXCLUDED_PLATFORMS=cfg.EXCLUDED_PLATFORMS,
EXCLUDED_SINGLE_EXT=cfg.EXCLUDED_SINGLE_EXT,
EXCLUDED_SINGLE_FILES=cfg.EXCLUDED_SINGLE_FILES,
EXCLUDED_MULTI_FILES=cfg.EXCLUDED_MULTI_FILES,
EXCLUDED_MULTI_PARTS_EXT=cfg.EXCLUDED_MULTI_PARTS_EXT,
EXCLUDED_MULTI_PARTS_FILES=cfg.EXCLUDED_MULTI_PARTS_FILES,
DEFAULT_EXCLUDED_PLATFORM_DIRS=list(DEFAULT_EXCLUDED_PLATFORM_DIRS),
DEFAULT_EXCLUDED_MULTI_FILE_DIRS=list(DEFAULT_EXCLUDED_MULTI_FILE_DIRS),
DEFAULT_EXCLUDED_FILES=list(DEFAULT_EXCLUDED_FILES),
DEFAULT_EXCLUDED_EXTENSIONS=list(DEFAULT_EXCLUDED_EXTENSIONS),
PLATFORMS_BINDING=cfg.PLATFORMS_BINDING,
PLATFORMS_VERSIONS=cfg.PLATFORMS_VERSIONS,
SKIP_HASH_CALCULATION=cfg.SKIP_HASH_CALCULATION,
EJS_DEBUG=cfg.EJS_DEBUG,
EJS_CACHE_LIMIT=cfg.EJS_CACHE_LIMIT,
EJS_DISABLE_AUTO_UNLOAD=cfg.EJS_DISABLE_AUTO_UNLOAD,
EJS_DISABLE_BATCH_BOOTUP=cfg.EJS_DISABLE_BATCH_BOOTUP,
EJS_ENABLE_AUTO_SAVE_SYNC=cfg.EJS_ENABLE_AUTO_SAVE_SYNC,
EJS_NETPLAY_ENABLED=cfg.EJS_NETPLAY_ENABLED,
# Contains credentials, so only send when authenticated
EJS_NETPLAY_ICE_SERVERS=(
cfg.EJS_NETPLAY_ICE_SERVERS if request.user.is_authenticated else []
),
EJS_CONTROLS=cfg.EJS_CONTROLS,
EJS_DEFAULT_CORES=cfg.EJS_DEFAULT_CORES,
EJS_SETTINGS=cfg.EJS_SETTINGS,
SCAN_METADATA_PRIORITY=cfg.SCAN_METADATA_PRIORITY,
SCAN_ARTWORK_PRIORITY=cfg.SCAN_ARTWORK_PRIORITY,
SCAN_ARTWORK_PRIORITY_OVERRIDES=cfg.SCAN_ARTWORK_PRIORITY_OVERRIDES,
SCAN_REGION_PRIORITY=cfg.SCAN_REGION_PRIORITY,
SCAN_LANGUAGE_PRIORITY=cfg.SCAN_LANGUAGE_PRIORITY,
SCAN_MEDIA=cfg.SCAN_MEDIA,
GAMELIST_AUTO_EXPORT_ON_SCAN=cfg.GAMELIST_AUTO_EXPORT_ON_SCAN,
GAMELIST_MEDIA_THUMBNAIL=cfg.GAMELIST_MEDIA_THUMBNAIL,
GAMELIST_MEDIA_IMAGE=cfg.GAMELIST_MEDIA_IMAGE,
PEGASUS_AUTO_EXPORT_ON_SCAN=cfg.PEGASUS_AUTO_EXPORT_ON_SCAN,
)
async def _reject_ambiguous_folder(fs_slug: str) -> None:
"""Refuse a mapping that several folders on disk would share."""
ambiguous = await fs_platform_handler.find_ambiguous_folders(fs_slug)
if ambiguous:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail=(
f"Folders {', '.join(ambiguous)} differ only by case and share a "
"single mapping. Rename one to map them to different platforms."
),
)
def _record_config(request: Request, setting: str, op: str, **values: str) -> None:
record(
AuditAction.CONFIG_UPDATE,
request,
AuditTarget(AuditTargetType.CONFIG, setting, None),
{"setting": setting, "op": op, **values},
)
@protected_route(router.post, "/system/platforms", [Scope.PLATFORMS_WRITE])
async def add_platform_binding(
request: Request, payload: PlatformBindingPayload
) -> None:
"""Add platform binding to the configuration"""
fs_slug = payload.fs_slug
slug = payload.slug
await _reject_ambiguous_folder(fs_slug)
try:
cm.add_platform_binding(fs_slug, slug)
except ConfigNotWritableException as exc:
log.critical(exc.message)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=exc.message
) from exc
_record_config(request, "platform_binding", "add", fs_slug=fs_slug, slug=slug)
@protected_route(router.delete, "/system/platforms/{fs_slug}", [Scope.PLATFORMS_WRITE])
async def delete_platform_binding(request: Request, fs_slug: str) -> None:
"""Delete platform binding from the configuration"""
try:
cm.remove_platform_binding(fs_slug)
except ConfigNotWritableException as exc:
log.critical(exc.message)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=exc.message
) from exc
_record_config(request, "platform_binding", "remove", fs_slug=fs_slug)
@protected_route(router.post, "/system/versions", [Scope.PLATFORMS_WRITE])
async def add_platform_version(
request: Request, payload: PlatformBindingPayload
) -> None:
"""Add platform version to the configuration"""
fs_slug = payload.fs_slug
slug = payload.slug
await _reject_ambiguous_folder(fs_slug)
try:
cm.add_platform_version(fs_slug, slug)
except ConfigNotWritableException as exc:
log.critical(exc.message)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=exc.message
) from exc
_record_config(request, "platform_version", "add", fs_slug=fs_slug, slug=slug)
@protected_route(router.delete, "/system/versions/{fs_slug}", [Scope.PLATFORMS_WRITE])
async def delete_platform_version(request: Request, fs_slug: str) -> None:
"""Delete platform version from the configuration"""
try:
cm.remove_platform_version(fs_slug)
except ConfigNotWritableException as exc:
log.critical(exc.message)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=exc.message
) from exc
_record_config(request, "platform_version", "remove", fs_slug=fs_slug)
@protected_route(router.post, "/exclude", [Scope.PLATFORMS_WRITE])
async def add_exclusion(request: Request, payload: ExclusionPayload) -> None:
"""Add platform exclusion to the configuration"""
exclusion_value = payload.exclusion_value
exclusion_type = ExclusionType(payload.exclusion_type)
try:
cm.add_exclusion(exclusion_type, exclusion_value)
except ConfigNotWritableException as exc:
log.critical(exc.message)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=exc.message
) from exc
_record_config(
request, "exclusion", "add", type=exclusion_type, value=exclusion_value
)
@protected_route(
router.delete,
"/exclude/{exclusion_type}/{exclusion_value}",
[Scope.PLATFORMS_WRITE],
)
async def delete_exclusion(
request: Request, exclusion_type: ExclusionType, exclusion_value: str
) -> None:
"""Delete platform binding from the configuration"""
try:
cm.remove_exclusion(exclusion_type, exclusion_value)
except ConfigNotWritableException as exc:
log.critical(exc.message)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=exc.message
) from exc
_record_config(
request, "exclusion", "remove", type=exclusion_type, value=exclusion_value
)
@protected_route(router.put, "/scan", [Scope.PLATFORMS_WRITE])
async def update_scan_settings(request: Request, payload: ScanSettingsPayload) -> None:
"""Replace the scan.* section of the configuration"""
region_priority_changed = (
cm.get_config().SCAN_REGION_PRIORITY != payload.region_priority
)
try:
cm.update_scan_settings(
metadata_priority=payload.metadata_priority,
artwork_priority=payload.artwork_priority,
artwork_overrides={
"cover": payload.cover_priority,
"screenshot": payload.screenshot_priority,
"manual": payload.manual_priority,
},
region_priority=payload.region_priority,
language_priority=payload.language_priority,
media=[str(m) for m in payload.media],
gamelist_export=payload.gamelist_export,
gamelist_thumbnail=str(payload.gamelist_thumbnail),
gamelist_image=str(payload.gamelist_image),
pegasus_export=payload.pegasus_export,
)
except ConfigNotWritableException as exc:
log.critical(exc.message)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=exc.message
) from exc
_record_config(request, "scan_settings", "update")
# Region priority picks the primary rom of each sibling group, so every
# cached gallery sidecar is stale the moment the order changes.
if region_priority_changed:
db_rom_handler.invalidate_filter_values_cache()