Files
romm/backend/models/notification_channel.py
zurdi 2c38ca1918 refactor: simplify notification channels after review
- A channel's min_level is its own enum (info, warning, error), so the
  API refuses `success` and neither the list nor the dialog maps it.
- One home each for the admin-reaches-the-LAN rule (the DB handler now
  returns the owner's role) and for describing a failed delivery.
- One webhook config builder for create and update, which drops the
  secret of a Discord webhook on both.
- SMTP_SECURITY outside starttls/tls/none leaves email off, so the
  heartbeat says so, with a warning at startup.
- The attempts counter gets its TTL in the same round trip as INCR.
- Deliveries group channels by user and serialize a notification only
  when a channel wants it.
- The v2 side reuses errorMessage (now with a fallback) instead of its
  own detail parsing, mirrors the backend length limits as constants,
  shares one channel test factory, and derives `created` on save from
  the list instead of carrying it on the event.
- Drop RBadge's inline override, which nothing needs now that an inline
  badge gets no anchor class, and trim docstrings to one sentence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 18:17:13 +02:00

71 lines
2.4 KiB
Python

from __future__ import annotations
import enum
from datetime import datetime
from typing import Final
from sqlalchemy import TIMESTAMP, Boolean, ForeignKey, Integer, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from models.base import BaseModel
from utils.database import CustomJSON
class NotificationChannelType(enum.StrEnum):
WEBHOOK = "webhook"
EMAIL = "email"
class WebhookFormat(enum.StrEnum):
JSON = "json"
DISCORD = "discord"
NTFY = "ntfy"
class NotificationChannelMinLevel(enum.StrEnum):
"""The least severe notification a channel forwards; success ranks as info."""
INFO = "info"
WARNING = "warning"
ERROR = "error"
NOTIFICATION_CHANNEL_NAME_MAX_LENGTH: Final = 100
NOTIFICATION_CHANNEL_URL_MAX_LENGTH: Final = 2000
NOTIFICATION_CHANNEL_SECRET_MAX_LENGTH: Final = 255
NOTIFICATION_CHANNEL_ERROR_MAX_LENGTH: Final = 1000
NOTIFICATION_CHANNEL_ADDRESS_MAX_LENGTH: Final = 320
NOTIFICATION_CHANNEL_CODE_MAX_LENGTH: Final = 16
MAX_NOTIFICATION_CHANNELS_PER_USER: Final = 20
# A channel that fails this many deliveries in a row turns itself off.
MAX_CONSECUTIVE_DELIVERY_FAILURES: Final = 10
class NotificationChannel(BaseModel):
__tablename__ = "notification_channels"
__table_args__ = {"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, index=True
)
type: Mapped[str] = mapped_column(String(16))
name: Mapped[str] = mapped_column(String(NOTIFICATION_CHANNEL_NAME_MAX_LENGTH))
# Sealed with `utils.secret_box`: a webhook URL can carry its own token.
config: Mapped[str] = mapped_column(Text)
min_level: Mapped[str] = mapped_column(String(16), default="info")
# None forwards every topic.
topics: Mapped[list[str] | None] = mapped_column(CustomJSON(), nullable=True)
enabled: Mapped[bool] = mapped_column(Boolean, default=True)
# An email address proves it belongs to the user with a code first.
confirmed_at: Mapped[datetime | None] = mapped_column(
TIMESTAMP(timezone=True), nullable=True
)
last_delivered_at: Mapped[datetime | None] = mapped_column(
TIMESTAMP(timezone=True), nullable=True
)
last_error: Mapped[str | None] = mapped_column(
String(NOTIFICATION_CHANNEL_ERROR_MAX_LENGTH), nullable=True
)
consecutive_failures: Mapped[int] = mapped_column(Integer, default=0)