feat: notification preferences system with plugin registry

- Add NotificationType and NotificationPreference models
- Add get_notification_types() to BasePlugin for plugin registration
- Add sync_notification_types() to PluginRegistry
- Update create_notification() to check user preferences (returns Notification|None)
- Add API endpoints: GET /types, GET /preferences, PATCH /preferences/{type_key}
- Add NotificationPreferenceUpdate schema
- Mail plugin registers 10 notification types with metadata
- Add Alembic migration 0017 for new tables + seed data
- Frontend: SettingsNotifications page with toggle switches
- Frontend: Settings tab, route, hooks for notification preferences
- i18n: notification settings keys (de/en)
This commit is contained in:
Agent Zero
2026-07-15 21:00:32 +02:00
parent c4d0ec6f7f
commit b0e987f790
15 changed files with 652 additions and 11 deletions
+126
View File
@@ -5,6 +5,7 @@ from __future__ import annotations
import uuid
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy import and_, select
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.db import get_db
@@ -14,6 +15,11 @@ from app.core.notifications import (
mark_notification_read,
)
from app.deps import get_current_user
from app.models.notification import (
NotificationPreference,
NotificationType,
)
from app.schemas.common import NotificationPreferenceUpdate
router = APIRouter(prefix="/api/v1/notifications", tags=["notifications"])
@@ -71,3 +77,123 @@ async def unread_count_endpoint(
user_id = uuid.UUID(current_user["user_id"])
count = await get_unread_count(db, tenant_id, user_id)
return {"count": count}
@router.get("/types")
async def list_notification_types(
db: AsyncSession = Depends(get_db),
current_user: dict = Depends(get_current_user),
):
"""List all registered notification types with the user's preference."""
tenant_id = uuid.UUID(current_user["tenant_id"])
user_id = uuid.UUID(current_user["user_id"])
# Fetch all registered types
types_result = await db.execute(select(NotificationType).order_by(NotificationType.plugin_name, NotificationType.category))
types = types_result.scalars().all()
# Fetch user's preferences
prefs_result = await db.execute(
select(NotificationPreference).where(
and_(
NotificationPreference.user_id == user_id,
NotificationPreference.tenant_id == tenant_id,
)
)
)
prefs = {p.type_key: p.is_enabled for p in prefs_result.scalars().all()}
items = []
for t in types:
if t.type_key in prefs:
is_enabled = prefs[t.type_key]
else:
is_enabled = t.is_enabled_by_default
items.append({
"type_key": t.type_key,
"plugin_name": t.plugin_name,
"category": t.category,
"label": t.label,
"description": t.description,
"is_enabled_by_default": t.is_enabled_by_default,
"is_enabled": is_enabled,
})
return {"items": items}
@router.get("/preferences")
async def list_preferences(
db: AsyncSession = Depends(get_db),
current_user: dict = Depends(get_current_user),
):
"""List user's notification preferences."""
tenant_id = uuid.UUID(current_user["tenant_id"])
user_id = uuid.UUID(current_user["user_id"])
result = await db.execute(
select(NotificationPreference).where(
and_(
NotificationPreference.user_id == user_id,
NotificationPreference.tenant_id == tenant_id,
)
)
)
prefs = result.scalars().all()
return {
"items": [
{"type_key": p.type_key, "is_enabled": p.is_enabled}
for p in prefs
]
}
@router.patch("/preferences/{type_key}")
async def update_preference(
type_key: str,
data: NotificationPreferenceUpdate,
db: AsyncSession = Depends(get_db),
current_user: dict = Depends(get_current_user),
):
"""Update user's preference for a notification type (upsert)."""
tenant_id = uuid.UUID(current_user["tenant_id"])
user_id = uuid.UUID(current_user["user_id"])
# Check that the type exists
type_result = await db.execute(
select(NotificationType).where(NotificationType.type_key == type_key)
)
type_row = type_result.scalar_one_or_none()
if type_row is None:
raise HTTPException(
404,
detail={"detail": f"Notification type '{type_key}' not found", "code": "not_found"},
)
# Upsert preference
result = await db.execute(
select(NotificationPreference).where(
and_(
NotificationPreference.user_id == user_id,
NotificationPreference.type_key == type_key,
NotificationPreference.tenant_id == tenant_id,
)
)
)
pref = result.scalar_one_or_none()
if pref is None:
pref = NotificationPreference(
tenant_id=tenant_id,
user_id=user_id,
type_key=type_key,
is_enabled=data.is_enabled,
)
db.add(pref)
else:
pref.is_enabled = data.is_enabled
await db.flush()
return {"type_key": type_key, "is_enabled": pref.is_enabled}