Files
romm/backend/handler/database/notifications_handler.py
zurdi 57311dc99a fix: apply the security-audit and code-review passes
Security:
- The netplay socket server shared the main server's Redis channel while
  letting unauthenticated clients name their own rooms, so a netplay socket
  in room `user:N` or `admin` received that user's pushes and the admin log
  stream. Netplay now has a channel of its own.
- Notifying other users also requires users.write on the token, so an admin
  token scoped to me.write can only notify its owner.
- The create payload rejects NUL characters, NaN/Infinity in data and an
  empty recipients list instead of failing in the database; mark-read takes
  at most as many ids as an inbox holds.

Correctness and cleanup:
- The inbox is cleared whenever the signed-in user changes, so a failed
  fetch can't leave the previous user's notifications on screen.
- Enter no longer sends the admin form, where an unfinished title would go
  out to everyone.
- A notification without its own icon shows its level's, in the list and in
  a pushed toast alike, from one LEVEL_ICONS map.
- One write-only Redis manager per event loop instead of one per push.
- Tests for the inbox socket wiring; em-dashes and long docstrings trimmed.

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

87 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 MAX_NOTIFICATIONS_PER_USER, Notification
from .base_handler import DBBaseHandler, affected_rows
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)