Files
romm/backend/handler/database/notifications_handler.py
zurdi ff6b160117 feat: persistent notifications
Toasts disappear after a few seconds, and nothing reaches a user who has no
tab open. Notifications are now stored per user and kept until dismissed, so
what a background job or another user did is waiting the next time RomM is
opened.

Backend:
- `notifications` table (0130) with kind, level, data, actor and read_at, capped
  at the newest 200 per user.
- /api/notifications: list, mark read, dismiss one, dismiss all.
- notify()/notify_admins() store the row and push `notifications:new` to the
  user's socket room; read/dismiss push to the user's other tabs.
- Producers: a scan's end (to whoever started it; an unattended scan tells
  the admins when it failed or found new games), a manual task's end (to who
  ran it; a scheduled task's failure to the admins), an admin ending someone's
  stream, and an admin changing someone's role.

Frontend (v2):
- Unread count badge on the navbar avatar, and a Notifications entry in the
  user menu and settings sidebar.
- Notifications view: marks what it shows read, links each row to what it is
  about, dismiss one or all.
- Live toast for a new notification, except scans, which already toast.
- The text is rendered from the kind in the reader's language (18 locales).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 10:35:33 +02:00

90 lines
2.8 KiB
Python

from collections.abc import Sequence
from datetime import datetime, timezone
from sqlalchemy import delete, select, update
from sqlalchemy.orm import Session
from decorators.database import begin_session
from models.notification import Notification
from .base_handler import DBBaseHandler, affected_rows
# A user who never clears their inbox keeps only this many, newest first.
MAX_NOTIFICATIONS_PER_USER = 200
class DBNotificationsHandler(DBBaseHandler):
@begin_session
def add_notification(
self,
notification: Notification,
session: Session = None, # type: ignore
) -> Notification:
session.add(notification)
session.flush()
overflow = (
select(Notification.id)
.where(Notification.user_id == notification.user_id)
.order_by(Notification.created_at.desc(), Notification.id.desc())
.offset(MAX_NOTIFICATIONS_PER_USER)
)
stale_ids = session.scalars(overflow).all()
if stale_ids:
session.execute(
delete(Notification)
.where(Notification.id.in_(stale_ids))
.execution_options(synchronize_session=False)
)
session.refresh(notification)
return notification
@begin_session
def get_notifications(
self,
user_id: int,
session: Session = None, # type: ignore
) -> Sequence[Notification]:
return session.scalars(
select(Notification)
.where(Notification.user_id == user_id)
.order_by(Notification.created_at.desc(), Notification.id.desc())
).all()
@begin_session
def mark_read(
self,
user_id: int,
ids: list[int] | None = None,
session: Session = None, # type: ignore
) -> int:
"""Mark the user's unread notifications read, all of them when `ids` is None."""
stmt = update(Notification).where(
Notification.user_id == user_id, Notification.read_at.is_(None)
)
if ids is not None:
stmt = stmt.where(Notification.id.in_(ids))
result = session.execute(
stmt.values(read_at=datetime.now(timezone.utc)).execution_options(
synchronize_session=False
)
)
return affected_rows(result)
@begin_session
def delete_notifications(
self,
user_id: int,
ids: list[int] | None = None,
session: Session = None, # type: ignore
) -> int:
"""Delete the user's notifications, all of them when `ids` is None."""
stmt = delete(Notification).where(Notification.user_id == user_id)
if ids is not None:
stmt = stmt.where(Notification.id.in_(ids))
result = session.execute(stmt.execution_options(synchronize_session=False))
return affected_rows(result)