mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 01:58:41 +01:00
- Hide events on roms of a hidden platform from non-admins. - Pin later pages to the highest matching id the API returns, not the first row's: rows are ordered by when they happened, so a play synced late has a higher id than newer events and was dropped. - Cap recorded failed sign-ins per address, dedupe reset requests and record them in the background with the email, and hash claim keys. - Keep another user's private collection by id only in bulk downloads. - Check a Range start without int(), which raises past 4300 digits. - Clear the list when a reload fails, load more only once it's shown, report a cover change only when the user made one, label only rom and platform unhides, accept an actor kind the API doesn't know. - One client IP helper, date-fns for today and yesterday, one time formatter for the list, no em-dashes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
156 lines
5.6 KiB
Python
156 lines
5.6 KiB
Python
from collections.abc import Collection, Sequence
|
|
from dataclasses import dataclass, field
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import ColumnElement, String, cast, delete, func, or_, select
|
|
from sqlalchemy.orm import Session, joinedload
|
|
|
|
from decorators.database import begin_session
|
|
from models.audit_event import AuditEvent
|
|
from models.device import Device
|
|
from models.rom import Rom
|
|
from models.user import User
|
|
from utils.database import LIKE_ESCAPE_CHAR, escape_like
|
|
|
|
from .base_handler import DBBaseHandler, affected_rows
|
|
|
|
|
|
def _with_actor():
|
|
# Name and avatar only, the rest of a user row is large JSON.
|
|
return joinedload(AuditEvent.actor).load_only(
|
|
User.id, User.username, User.avatar_path, User.updated_at
|
|
)
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class AuditEventFilters:
|
|
actor_ids: Collection[int] | None = None
|
|
actions: Collection[str] | None = None
|
|
target_type: str | None = None
|
|
target_id: str | None = None
|
|
since: datetime | None = None
|
|
until: datetime | None = None
|
|
# Pins a paged read to the rows that existed when its first page was fetched.
|
|
max_id: int | None = None
|
|
search: str | None = None
|
|
hidden_rom_ids: Collection[int] = field(default_factory=frozenset)
|
|
hidden_platform_ids: Collection[int] = field(default_factory=frozenset)
|
|
|
|
|
|
def _not_targeting(target_type: str, ids: Collection[int]):
|
|
return or_(
|
|
AuditEvent.target_type.is_(None),
|
|
AuditEvent.target_type != target_type,
|
|
AuditEvent.target_id.is_(None),
|
|
AuditEvent.target_id.not_in([str(i) for i in ids]),
|
|
)
|
|
|
|
|
|
class DBAuditEventsHandler(DBBaseHandler):
|
|
@begin_session
|
|
def add_events(
|
|
self,
|
|
events: Sequence[AuditEvent],
|
|
session: Session = None, # type: ignore
|
|
) -> None:
|
|
session.add_all(events)
|
|
|
|
@begin_session
|
|
def get_events(
|
|
self,
|
|
filters: AuditEventFilters,
|
|
limit: int,
|
|
offset: int,
|
|
session: Session = None, # type: ignore
|
|
) -> tuple[list[tuple[AuditEvent, str | None]], int, int | None]:
|
|
"""A page of events, newest first, each with its device's name, the total,
|
|
and the highest id among the matches for later pages to be pinned to."""
|
|
clauses: list[ColumnElement[bool]] = []
|
|
if filters.actor_ids is not None:
|
|
clauses.append(AuditEvent.actor_id.in_(filters.actor_ids))
|
|
if filters.actions is not None:
|
|
clauses.append(AuditEvent.action.in_(filters.actions))
|
|
if filters.target_type is not None:
|
|
clauses.append(AuditEvent.target_type == filters.target_type)
|
|
if filters.target_id is not None:
|
|
clauses.append(AuditEvent.target_id == filters.target_id)
|
|
if filters.since is not None:
|
|
clauses.append(AuditEvent.occurred_at >= filters.since)
|
|
if filters.until is not None:
|
|
clauses.append(AuditEvent.occurred_at < filters.until)
|
|
if filters.max_id is not None:
|
|
clauses.append(AuditEvent.id <= filters.max_id)
|
|
if filters.search:
|
|
like = f"%{escape_like(filters.search.lower())}%"
|
|
clauses.append(
|
|
or_(
|
|
*(
|
|
func.lower(column).like(like, escape=LIKE_ESCAPE_CHAR)
|
|
for column in (
|
|
AuditEvent.actor_name,
|
|
AuditEvent.target_name,
|
|
AuditEvent.ip_address,
|
|
)
|
|
)
|
|
)
|
|
)
|
|
if filters.hidden_rom_ids:
|
|
clauses.append(_not_targeting("rom", filters.hidden_rom_ids))
|
|
if filters.hidden_platform_ids:
|
|
clauses.append(_not_targeting("platform", filters.hidden_platform_ids))
|
|
# A platform's hide covers its roms too.
|
|
clauses.append(
|
|
or_(
|
|
AuditEvent.target_type.is_(None),
|
|
AuditEvent.target_type != "rom",
|
|
AuditEvent.target_id.is_(None),
|
|
AuditEvent.target_id.not_in(
|
|
select(cast(Rom.id, String)).where(
|
|
Rom.platform_id.in_(filters.hidden_platform_ids)
|
|
)
|
|
),
|
|
)
|
|
)
|
|
|
|
total, highest_id = session.execute(
|
|
select(func.count(), func.max(AuditEvent.id)).where(*clauses)
|
|
).one()
|
|
rows = session.execute(
|
|
select(AuditEvent, Device.name)
|
|
.options(_with_actor())
|
|
.outerjoin(Device, Device.id == AuditEvent.device_id)
|
|
.where(*clauses)
|
|
.order_by(AuditEvent.occurred_at.desc(), AuditEvent.id.desc())
|
|
.limit(limit)
|
|
.offset(offset)
|
|
).all()
|
|
return (
|
|
[(event, device_name) for event, device_name in rows],
|
|
total or 0,
|
|
highest_id,
|
|
)
|
|
|
|
@begin_session
|
|
def delete_batch_before(
|
|
self,
|
|
cutoff: datetime,
|
|
batch_size: int,
|
|
session: Session = None, # type: ignore
|
|
) -> int:
|
|
"""Delete up to `batch_size` of the oldest events from before `cutoff`."""
|
|
# Ids first: MySQL refuses a LIMIT inside an IN subquery.
|
|
ids = session.scalars(
|
|
select(AuditEvent.id)
|
|
.where(AuditEvent.occurred_at < cutoff)
|
|
.order_by(AuditEvent.occurred_at)
|
|
.limit(batch_size)
|
|
).all()
|
|
if not ids:
|
|
return 0
|
|
result = session.execute(
|
|
delete(AuditEvent)
|
|
.where(AuditEvent.id.in_(ids))
|
|
.execution_options(synchronize_session=False)
|
|
)
|
|
return affected_rows(result)
|