42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
"""In-app notifications. Mail is out-of-scope for v1 (spec §4)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
from datetime import datetime
|
|
from typing import Any, TYPE_CHECKING
|
|
|
|
from sqlalchemy import DateTime, ForeignKey, Index, String, Uuid
|
|
from sqlalchemy.dialects.postgresql import JSONB
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.db.base import Base
|
|
from app.db.mixins import TimestampMixin, UuidPkMixin
|
|
|
|
if TYPE_CHECKING:
|
|
from app.models.auth import User
|
|
|
|
|
|
class Notification(Base, UuidPkMixin, TimestampMixin):
|
|
__tablename__ = "notifications"
|
|
|
|
user_id: Mapped[uuid.UUID] = mapped_column(
|
|
Uuid(as_uuid=True),
|
|
ForeignKey("users.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
)
|
|
type: Mapped[str] = mapped_column(String(64), nullable=False)
|
|
payload: Mapped[Any] = mapped_column(JSONB, nullable=False, server_default="{}")
|
|
read_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
|
|
user: Mapped["User"] = relationship(back_populates="notifications")
|
|
|
|
__table_args__ = (
|
|
Index(
|
|
"ix_notifications_user_unread",
|
|
"user_id",
|
|
"created_at",
|
|
postgresql_where="read_at IS NULL",
|
|
),
|
|
)
|