mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
Each user can add channels that forward their notifications: a webhook posting RomM's JSON (optionally HMAC-signed), a Discord embed or an ntfy message, or an email address confirmed with a code. A channel filters by minimum level and by topic, and every stored notification that passes goes out in its own RQ job, retried three times; ten failures in a row turn the channel off and tell its owner. Channel configs are sealed with a key derived from ROMM_AUTH_SECRET_KEY, since a webhook URL carries its own token. Only an admin's webhooks may reach private addresses; everyone else's go through the SSRF guard. Email goes out over the SMTP_* settings, which the heartbeat reports as NOTIFICATIONS.EMAIL_ENABLED. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
113 lines
3.9 KiB
Python
113 lines
3.9 KiB
Python
from __future__ import annotations
|
|
|
|
import enum
|
|
from datetime import datetime
|
|
from typing import TYPE_CHECKING, Any, Final
|
|
|
|
from sqlalchemy import TIMESTAMP, ForeignKey, Index, String
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from models.base import BaseModel
|
|
from utils.database import CustomJSON
|
|
|
|
if TYPE_CHECKING:
|
|
from models.user import User
|
|
|
|
|
|
class NotificationLevel(enum.StrEnum):
|
|
INFO = "info"
|
|
SUCCESS = "success"
|
|
WARNING = "warning"
|
|
ERROR = "error"
|
|
|
|
|
|
class NotificationKind(enum.StrEnum):
|
|
"""What happened, translated by the client; any other kind shows its own title and body."""
|
|
|
|
CUSTOM = "custom"
|
|
SCAN_COMPLETED = "scan_completed"
|
|
SCAN_FAILED = "scan_failed"
|
|
TASK_COMPLETED = "task_completed"
|
|
TASK_FAILED = "task_failed"
|
|
STREAMING_SESSION_ENDED = "streaming_session_ended"
|
|
ROLE_CHANGED = "role_changed"
|
|
CHANNEL_DISABLED = "channel_disabled"
|
|
|
|
|
|
class NotificationTopic(enum.StrEnum):
|
|
"""The groups a notification channel picks what it forwards by."""
|
|
|
|
SCANS = "scans"
|
|
TASKS = "tasks"
|
|
STREAMING = "streaming"
|
|
ACCOUNT = "account"
|
|
CUSTOM = "custom"
|
|
|
|
|
|
_KIND_TOPICS: Final[dict[str, NotificationTopic]] = {
|
|
NotificationKind.SCAN_COMPLETED: NotificationTopic.SCANS,
|
|
NotificationKind.SCAN_FAILED: NotificationTopic.SCANS,
|
|
NotificationKind.TASK_COMPLETED: NotificationTopic.TASKS,
|
|
NotificationKind.TASK_FAILED: NotificationTopic.TASKS,
|
|
NotificationKind.STREAMING_SESSION_ENDED: NotificationTopic.STREAMING,
|
|
NotificationKind.ROLE_CHANGED: NotificationTopic.ACCOUNT,
|
|
NotificationKind.CHANNEL_DISABLED: NotificationTopic.ACCOUNT,
|
|
}
|
|
|
|
|
|
def topic_of(kind: str) -> NotificationTopic:
|
|
"""The topic a kind belongs to; any kind RomM doesn't define is custom."""
|
|
return _KIND_TOPICS.get(kind, NotificationTopic.CUSTOM)
|
|
|
|
|
|
# A user who never clears their inbox keeps only this many, newest first.
|
|
MAX_NOTIFICATIONS_PER_USER: Final = 200
|
|
|
|
NOTIFICATION_KIND_MAX_LENGTH: Final = 64
|
|
NOTIFICATION_TITLE_MAX_LENGTH: Final = 255
|
|
NOTIFICATION_BODY_MAX_LENGTH: Final = 1000
|
|
NOTIFICATION_LINK_MAX_LENGTH: Final = 1000
|
|
NOTIFICATION_ICON_MAX_LENGTH: Final = 64
|
|
# Cap on the `data` API clients send, measured as serialized JSON.
|
|
NOTIFICATION_DATA_MAX_LENGTH: Final = 4096
|
|
|
|
|
|
class Notification(BaseModel):
|
|
__tablename__ = "notifications"
|
|
__table_args__ = (
|
|
Index("ix_notifications_user_created", "user_id", "created_at"),
|
|
{"extend_existing": True},
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
user_id: Mapped[int] = mapped_column(
|
|
ForeignKey("users.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
# The user whose action caused it, when it wasn't a background process.
|
|
actor_id: Mapped[int | None] = mapped_column(
|
|
ForeignKey("users.id", ondelete="SET NULL"), nullable=True, index=True
|
|
)
|
|
# Strings rather than database enums, so a new kind needs no migration.
|
|
kind: Mapped[str] = mapped_column(String(NOTIFICATION_KIND_MAX_LENGTH))
|
|
level: Mapped[str] = mapped_column(String(16))
|
|
title: Mapped[str | None] = mapped_column(
|
|
String(NOTIFICATION_TITLE_MAX_LENGTH), nullable=True
|
|
)
|
|
body: Mapped[str | None] = mapped_column(
|
|
String(NOTIFICATION_BODY_MAX_LENGTH), nullable=True
|
|
)
|
|
# An in-app path (`/rom/12`), never an external URL.
|
|
link: Mapped[str | None] = mapped_column(
|
|
String(NOTIFICATION_LINK_MAX_LENGTH), nullable=True
|
|
)
|
|
icon: Mapped[str | None] = mapped_column(
|
|
String(NOTIFICATION_ICON_MAX_LENGTH), nullable=True
|
|
)
|
|
# The values the kind's text is built from (names, counts, ids to link to).
|
|
data: Mapped[dict[str, Any]] = mapped_column(CustomJSON(), default=dict)
|
|
read_at: Mapped[datetime | None] = mapped_column(
|
|
TIMESTAMP(timezone=True), nullable=True
|
|
)
|
|
|
|
actor: Mapped[User | None] = relationship(lazy="raise", foreign_keys=[actor_id])
|