From 6be7045f176da375bb20df0a65d0daf9788c0fc2 Mon Sep 17 00:00:00 2001 From: simosmik Date: Tue, 3 Mar 2026 16:28:12 +0000 Subject: [PATCH] fix: notifications orchestrator and add a notification when first enabled --- server/routes/settings.js | 31 ++++++++++++++++++++ server/services/notification-orchestrator.js | 3 +- src/components/settings/view/Settings.tsx | 2 ++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/server/routes/settings.js b/server/routes/settings.js index 8afcb4b..7eee245 100644 --- a/server/routes/settings.js +++ b/server/routes/settings.js @@ -1,6 +1,7 @@ import express from 'express'; import { apiKeysDb, credentialsDb, notificationPreferencesDb, pushSubscriptionsDb } from '../database/db.js'; import { getPublicKey } from '../services/vapid-keys.js'; +import { createNotificationEvent, notifyUserIfEnabled } from '../services/notification-orchestrator.js'; const router = express.Router(); @@ -221,7 +222,27 @@ router.post('/push/subscribe', async (req, res) => { return res.status(400).json({ error: 'Missing subscription fields' }); } pushSubscriptionsDb.saveSubscription(req.user.id, endpoint, keys.p256dh, keys.auth); + + // Enable webPush in preferences so the confirmation goes through the full pipeline + const currentPrefs = notificationPreferencesDb.getPreferences(req.user.id); + if (!currentPrefs?.channels?.webPush) { + notificationPreferencesDb.updatePreferences(req.user.id, { + ...currentPrefs, + channels: { ...currentPrefs?.channels, webPush: true }, + }); + } + res.json({ success: true }); + + // Send a confirmation push through the full notification pipeline + const event = createNotificationEvent({ + provider: 'system', + kind: 'info', + code: 'push.enabled', + meta: { message: 'Push notifications are now enabled!' }, + severity: 'info' + }); + notifyUserIfEnabled({ userId: req.user.id, event }); } catch (error) { console.error('Error saving push subscription:', error); res.status(500).json({ error: 'Failed to save push subscription' }); @@ -235,6 +256,16 @@ router.post('/push/unsubscribe', async (req, res) => { return res.status(400).json({ error: 'Missing endpoint' }); } pushSubscriptionsDb.removeSubscription(endpoint); + + // Disable webPush in preferences to match subscription state + const currentPrefs = notificationPreferencesDb.getPreferences(req.user.id); + if (currentPrefs?.channels?.webPush) { + notificationPreferencesDb.updatePreferences(req.user.id, { + ...currentPrefs, + channels: { ...currentPrefs.channels, webPush: false }, + }); + } + res.json({ success: true }); } catch (error) { console.error('Error removing push subscription:', error); diff --git a/server/services/notification-orchestrator.js b/server/services/notification-orchestrator.js index c82105a..2550729 100644 --- a/server/services/notification-orchestrator.js +++ b/server/services/notification-orchestrator.js @@ -67,7 +67,8 @@ function buildPushBody(event) { : 'Action Required: A tool needs your approval', 'run.stopped': event.meta?.stopReason || 'Run Stopped: The run has stopped', 'run.failed': event.meta?.error ? `Run Failed: ${event.meta.error}` : 'Run Failed: The run encountered an error', - 'agent.notification': event.meta?.message ? String(event.meta.message) : 'You have a new notification' + 'agent.notification': event.meta?.message ? String(event.meta.message) : 'You have a new notification', + 'push.enabled': 'Push notifications are now enabled!' }; return { diff --git a/src/components/settings/view/Settings.tsx b/src/components/settings/view/Settings.tsx index c5da46e..65d2223 100644 --- a/src/components/settings/view/Settings.tsx +++ b/src/components/settings/view/Settings.tsx @@ -96,6 +96,7 @@ function Settings({ isOpen, onClose, projects = [], initialTab = 'agents' }: Set const handleEnablePush = async () => { await pushSubscribe(); + // Server sets webPush: true in preferences on subscribe; sync local state setNotificationPreferences({ ...notificationPreferences, channels: { ...notificationPreferences.channels, webPush: true }, @@ -104,6 +105,7 @@ function Settings({ isOpen, onClose, projects = [], initialTab = 'agents' }: Set const handleDisablePush = async () => { await pushUnsubscribe(); + // Server sets webPush: false in preferences on unsubscribe; sync local state setNotificationPreferences({ ...notificationPreferences, channels: { ...notificationPreferences.channels, webPush: false },