feat(notifications): Web Push delivers notifications to every device, even with TREK closed

A fourth built-in channel next to email, webhook and ntfy. Browsers
register per device, the server encrypts per RFC 8291 and signs per RFC
8292 with node:crypto and sends only to known push services through the
SSRF guard. The event matrix decides per category what arrives by push;
admins switch the channel on under Notifications.

VAPID keys come from the environment or are generated once, only while
neither stored row exists, and kept encrypted. A pair that does not
decrypt or match is never overwritten and never swapped for another one:
push stays off with an error in the log and every device stays
registered until the key is fixed. The subject is VAPID_SUBJECT, then an
https APP_URL, then the project URL.

Only chat messages collapse into one notification per trip, and a
replacement still alerts. A 403 removes a device only after three
failures in a row; 404 and 410 remove it at once. Password change, reset
and an admin setting a password drop the user's devices with the other
credentials. The request log redacts subscriptions and endpoints, boot
errors print *** for every secret variable, and logout forgets the
device with a plain fetch so an expired session cannot redirect midway.

Addresses discussion #894.
This commit is contained in:
Maurice
2026-09-24 23:17:03 +00:00
parent 7ce31756a3
commit 650451bdf7
177 changed files with 7809 additions and 271 deletions
@@ -1,6 +1,7 @@
import { authApi, notificationsApi } from '../../../api/client'
import type { TranslationFn } from '../../../types'
import type { useAdmin } from '../../../pages/admin/useAdmin'
import { SWITCH_ONLY_CHANNELS, useNotificationChannels } from '../../../components/Admin/useNotificationChannels'
import MToggle from '../../components/MToggle'
import MAdminNotifyMatrix from './MAdminNotifyMatrix'
import { MAdminButton, MAdminCard, MAdminCardHead, MAdminField, MAdminInput, MAdminRow } from './MAdminUi'
@@ -18,39 +19,18 @@ const SMTP_FIELDS = [
{ key: 'smtp_from', label: 'From Address', placeholder: 'trek@example.com' },
]
// Notifications section: channel toggles (email/webhook/ntfy/in-app), SMTP
// credentials, trip reminders, admin webhook + ntfy targets and the per-event
// preference matrix — the desktop notifications tab in mobile cards.
// Notifications section: channel toggles (email/webhook/ntfy/web push/in-app),
// SMTP credentials, trip reminders, admin webhook + ntfy targets and the
// per-event preference matrix: the desktop notifications tab in mobile cards.
// The channel switches come from useNotificationChannels, shared with desktop.
export default function MAdminNotificationsSection({ admin, t }: MAdminNotificationsSectionProps) {
const { toast, smtpValues, setSmtpValues, smtpLoaded, setTripRemindersEnabled } = admin
// Derive active channels from notification_channels (plural) with fallback
// to notification_channel (singular) for existing installs.
const rawChannels = smtpValues.notification_channels ?? smtpValues.notification_channel ?? 'none'
const activeChans = rawChannels === 'none' ? [] : rawChannels.split(',').map((c: string) => c.trim())
const emailActive = activeChans.includes('email')
const webhookActive = activeChans.includes('webhook')
const ntfyActive = activeChans.includes('ntfy')
const channels = useNotificationChannels(admin, t)
const emailActive = channels.isActive('email')
const tripRemindersActive = smtpValues.notify_trip_reminder !== 'false'
const smtpConfigured = !!smtpValues.smtp_host?.trim()
const setChannels = async (email: boolean, webhook: boolean, ntfy: boolean) => {
// Preserve channel ids this toggle doesn't know about instead of
// rebuilding the CSV from just these three booleans.
const others = activeChans.filter((c: string) => c !== 'email' && c !== 'webhook' && c !== 'ntfy')
const chans = [email && 'email', webhook && 'webhook', ntfy && 'ntfy', ...others].filter(Boolean).join(',') || 'none'
setSmtpValues((prev) => ({ ...prev, notification_channels: chans }))
try {
await authApi.updateAppSettings({ notification_channels: chans })
} catch {
const reverted =
[emailActive && 'email', webhookActive && 'webhook', ntfyActive && 'ntfy', ...others].filter(Boolean).join(',') ||
'none'
setSmtpValues((prev) => ({ ...prev, notification_channels: reverted }))
toast.error(t('common.error'))
}
}
const saveSmtp = async () => {
// Saves credentials only — channel activation is auto-saved by the toggle.
const notifKeys = ['smtp_host', 'smtp_port', 'smtp_user', 'smtp_pass', 'smtp_from', 'smtp_skip_tls_verify']
@@ -179,7 +159,7 @@ export default function MAdminNotificationsSection({ admin, t }: MAdminNotificat
<MToggle
checked={emailActive}
ariaLabel={t('admin.notifications.emailPanel.title')}
onChange={() => setChannels(!emailActive, webhookActive, ntfyActive)}
onChange={() => channels.toggle('email')}
/>
}
/>
@@ -219,35 +199,22 @@ export default function MAdminNotificationsSection({ admin, t }: MAdminNotificat
</div>
</MAdminCard>
{/* Webhook */}
<MAdminCard>
<MAdminCardHead
title={t('admin.notifications.webhookPanel.title')}
hint={t('admin.webhook.hint')}
trailing={
<MToggle
checked={webhookActive}
ariaLabel={t('admin.notifications.webhookPanel.title')}
onChange={() => setChannels(emailActive, !webhookActive, ntfyActive)}
/>
}
/>
</MAdminCard>
{/* Ntfy */}
<MAdminCard>
<MAdminCardHead
title={t('admin.notifications.ntfy')}
hint={t('admin.ntfy.hint')}
trailing={
<MToggle
checked={ntfyActive}
ariaLabel={t('admin.notifications.ntfy')}
onChange={() => setChannels(emailActive, webhookActive, !ntfyActive)}
/>
}
/>
</MAdminCard>
{/* Webhook, Ntfy and Web Push: a title, a hint and the switch each */}
{SWITCH_ONLY_CHANNELS.map((ch) => (
<MAdminCard key={ch.id}>
<MAdminCardHead
title={t(ch.titleKey)}
hint={t(ch.hintKey)}
trailing={
<MToggle
checked={channels.isActive(ch.id)}
ariaLabel={t(ch.titleKey)}
onChange={() => channels.toggle(ch.id)}
/>
}
/>
</MAdminCard>
))}
{/* In-App (always on) */}
<MAdminCard>
@@ -1,11 +1,13 @@
import { useEffect, useState } from 'react'
import { useNavigate } from 'react-router'
import { Bell, Link2, Send } from 'lucide-react'
import { WEB_PUSH_CHANNEL_ID } from '@trek/shared'
import { useTranslation } from '../../../i18n'
import { notificationsApi, settingsApi } from '../../../api/client'
import { useToast } from '../../../components/shared/Toast'
import { MSetCard, MSetEyebrow, MSetInput, MSetButton, MSetHint } from './MSettingsUi'
import MChip from '../../components/MChip'
import MWebPushCard from './MWebPushCard'
interface ChannelDescriptor {
id: string
@@ -52,8 +54,8 @@ const MASKED = '••••••••'
/**
* "Notifications" section — NotificationsTab parity: webhook + ntfy channel
* credentials, plugin channels and the event/channel preference matrix,
* rendered as chip rows instead of the desktop grid.
* credentials, push on this device, plugin channels and the event/channel
* preference matrix, rendered as chip rows instead of the desktop grid.
*/
export default function MSettingsNotifications() {
const { t } = useTranslation()
@@ -282,6 +284,8 @@ export default function MSettingsNotifications() {
</div>
)}
{hasChannel(WEB_PUSH_CHANNEL_ID) && <MWebPushCard />}
{pluginChannels.map((ch) => (
<div key={ch.id} className="mb-3 rounded-xl border border-[color:var(--m-rowbr)] bg-[color:var(--m-sheet)] p-3">
<MSetEyebrow className="mb-1">{channelLabel(ch, t)}</MSetEyebrow>
@@ -0,0 +1,38 @@
import { BellRing, BellOff, Send } from 'lucide-react'
import { useTranslation } from '../../../i18n'
import { useWebPush } from '../../../hooks/useWebPush'
import { MSetButton, MSetEyebrow, MSetHint } from './MSettingsUi'
/**
* Phone twin of the desktop `WebPushCard`: markup only, over the same
* `useWebPush` hook. MSettingsNotifications renders it while the admin has the
* push channel switched on.
*/
export default function MWebPushCard() {
const { t } = useTranslation()
const push = useWebPush()
return (
<div className="mb-3 rounded-xl border border-[color:var(--m-rowbr)] bg-[color:var(--m-sheet)] p-3">
<MSetEyebrow className="mb-1">{t('settings.webPush.title')}</MSetEyebrow>
<MSetHint className="mb-2 mt-0">{t('settings.webPush.hint')}</MSetHint>
{push.stateText && (
<p role="status" className="mb-2 text-[0.75rem] font-semibold text-m-ink">
{push.stateText}
</p>
)}
{push.canSwitch && (
<div className="flex flex-wrap gap-2">
<MSetButton variant={push.on ? 'ghost' : 'primary'} onClick={push.toggle} disabled={push.busy}>
{push.on ? <BellOff size={13} /> : <BellRing size={13} />}
{push.switchLabel}
</MSetButton>
<MSetButton variant="ghost" onClick={push.sendTest} disabled={!push.canTest}>
<Send size={13} />
{t('settings.notificationPreferences.sendTest')}
</MSetButton>
</div>
)}
</div>
)
}