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:
@@ -117,6 +117,73 @@ class PluginRegistry:
|
||||
"""List all discovered plugin names."""
|
||||
return list(self._plugins.keys())
|
||||
|
||||
# ── Notification Type Sync ──
|
||||
|
||||
async def sync_notification_types(self, db: AsyncSession) -> None:
|
||||
"""Sync notification types from all active plugins to DB.
|
||||
|
||||
For each active plugin, calls get_notification_types() and upserts
|
||||
the returned types into the notification_types table. Removes types
|
||||
belonging to plugins that are no longer active.
|
||||
"""
|
||||
from app.models.notification import NotificationType
|
||||
|
||||
# Collect all notification types from active plugins
|
||||
active_types: dict[str, dict[str, Any]] = {} # type_key -> type_data
|
||||
for name, plugin in self._plugins.items():
|
||||
record = await self._get_plugin_record(db, name)
|
||||
if record is None or not record.active:
|
||||
continue
|
||||
for nt in plugin.get_notification_types():
|
||||
key = nt.get("type_key", "")
|
||||
if not key:
|
||||
continue
|
||||
active_types[key] = {
|
||||
"type_key": key,
|
||||
"plugin_name": name,
|
||||
"category": nt.get("category", "general"),
|
||||
"label": nt.get("label", key),
|
||||
"description": nt.get("description"),
|
||||
"is_enabled_by_default": nt.get("is_enabled_by_default", True),
|
||||
}
|
||||
|
||||
# Fetch existing types from DB
|
||||
result = await db.execute(select(NotificationType))
|
||||
existing = {row.type_key: row for row in result.scalars().all()}
|
||||
|
||||
# Upsert active types
|
||||
for key, data in active_types.items():
|
||||
if key in existing:
|
||||
row = existing[key]
|
||||
row.plugin_name = data["plugin_name"]
|
||||
row.category = data["category"]
|
||||
row.label = data["label"]
|
||||
row.description = data["description"]
|
||||
row.is_enabled_by_default = data["is_enabled_by_default"]
|
||||
else:
|
||||
new_row = NotificationType(
|
||||
type_key=key,
|
||||
plugin_name=data["plugin_name"],
|
||||
category=data["category"],
|
||||
label=data["label"],
|
||||
description=data["description"],
|
||||
is_enabled_by_default=data["is_enabled_by_default"],
|
||||
)
|
||||
db.add(new_row)
|
||||
|
||||
# Remove types from plugins that are no longer active
|
||||
active_keys = set(active_types.keys())
|
||||
for key, row in existing.items():
|
||||
if key not in active_keys:
|
||||
# Check if the plugin is still active
|
||||
plugin_name = row.plugin_name
|
||||
plugin_record = await self._get_plugin_record(db, plugin_name)
|
||||
if plugin_record is None or not plugin_record.active:
|
||||
await db.delete(row)
|
||||
|
||||
await db.flush()
|
||||
logger.info("Synced %d notification types from active plugins", len(active_types))
|
||||
|
||||
# ── DB Status Sync ──
|
||||
|
||||
async def sync_db_status(self, db: AsyncSession) -> None:
|
||||
@@ -418,6 +485,9 @@ class PluginRegistry:
|
||||
mounted_routes.append(r)
|
||||
self._mounted_routes[name] = mounted_routes
|
||||
|
||||
# Sync notification types from this plugin
|
||||
await self.sync_notification_types(db)
|
||||
|
||||
# Update DB record
|
||||
record.status = "active"
|
||||
record.active = True
|
||||
|
||||
Reference in New Issue
Block a user