feat(notifications): admin defaults for users, and channels blocked for everyone

Admin > Notifications gets a Defaults for users matrix: each event and channel starts On, Off (a user may still turn it on) or Blocked (off for everyone). A user's own choice is kept against the default, and a value equal to it is stored as no row, so a later change of the default reaches everyone who never chose. Blocked cells show as locked in the user settings on desktop and phone, and the server ignores any attempt to turn one on. (#1536)
This commit is contained in:
Maurice
2026-10-01 12:28:36 +02:00
parent 7acf7f5655
commit f4626c9f3e
70 changed files with 680 additions and 57 deletions
@@ -2,7 +2,7 @@ import userEvent from '@testing-library/user-event';
import { http, HttpResponse } from 'msw';
import { buildUser } from '../../../tests/helpers/factories';
import { server } from '../../../tests/helpers/msw/server';
import { render, screen, waitFor } from '../../../tests/helpers/render';
import { render, screen, waitFor, fireEvent } from '../../../tests/helpers/render';
import { resetAllStores, seedStore } from '../../../tests/helpers/store';
import { useAuthStore } from '../../store/authStore';
import { ToastContainer } from '../shared/Toast';
@@ -1229,3 +1229,23 @@ describe('NotificationsTab: Web Push', () => {
expect(screen.queryByText('Push')).not.toBeInTheDocument();
});
});
describe('NotificationsTab — cells the admin blocked (#1536)', () => {
it('FE-COMP-NOTIFICATIONS-LOCK-001: shows a lock instead of a switch, and sends nothing for it', async () => {
let puts = 0
server.use(
http.get('/api/notifications/preferences', () => HttpResponse.json({
...minimalMatrix,
preferences: { trip_invite: { inapp: true, email: false } },
locked: { trip_invite: ['email'] },
})),
http.put('/api/notifications/preferences', () => { puts += 1; return HttpResponse.json({ success: true }) }),
)
render(<NotificationsTab />)
const lock = await screen.findByRole('img', { name: 'Turned off for everyone by the admin' })
fireEvent.click(lock)
// The in-app switch for the same event is still there and still a switch.
expect(screen.getAllByRole('button', { pressed: true })).toHaveLength(1)
expect(puts).toBe(0)
})
})
@@ -8,6 +8,8 @@ import { useToast } from '../shared/Toast'
import ToggleSwitch from './ToggleSwitch'
import Section from './Section'
import WebPushCard from './WebPushCard'
import { EVENT_LABEL_KEYS, channelLabel, isLockedCell } from './notificationLabels'
import { Tooltip } from '../shared/Tooltip'
interface ChannelDescriptor {
id: string
@@ -26,30 +28,11 @@ interface PreferencesMatrix {
channels: ChannelDescriptor[]
event_types: string[]
implemented_combos: Record<string, string[]>
/** Cells the admin switched off for everyone (#1536). */
locked?: Record<string, string[]>
defaults?: { ntfyServer: string | null }
}
/** Plugin channels have no i18n — the server sends their display name outright. */
function channelLabel(ch: ChannelDescriptor, t: (k: string) => string): string {
if (ch.labelKey) return t(ch.labelKey) || ch.id
return ch.label || ch.id
}
const EVENT_LABEL_KEYS: Record<string, string> = {
trip_invite: 'settings.notifyTripInvite',
booking_change: 'settings.notifyBookingChange',
trip_reminder: 'settings.notifyTripReminder',
todo_due: 'settings.notifyTodoDue',
vacay_invite: 'settings.notifyVacayInvite',
vacay_share: 'settings.notifyVacayShare',
collection_invite: 'settings.notifyCollectionInvite',
synology_session_cleared: 'settings.notifySynologySessionCleared',
plugin_notification: 'settings.notifyPluginNotification',
photos_shared: 'settings.notifyPhotosShared',
collab_message: 'settings.notifyCollabMessage',
packing_tagged: 'settings.notifyPackingTagged',
version_available: 'settings.notifyVersionAvailable',
}
export default function NotificationsTab(): React.ReactElement {
const { t } = useTranslation()
@@ -119,7 +102,7 @@ export default function NotificationsTab(): React.ReactElement {
}
const toggle = async (eventType: string, channel: string) => {
if (!matrix) return
if (!matrix || isLockedCell(matrix.locked, eventType, channel)) return
const current = matrix.preferences[eventType]?.[channel] ?? true
setMatrix(m => m ? {
...m,
@@ -383,6 +366,20 @@ export default function NotificationsTab(): React.ReactElement {
return <span key={ch.id} style={{ textAlign: 'center', color: 'var(--text-faint)', fontSize: 'calc(14px * var(--fs-scale-body, 1))' }}>—</span>
}
const isOn = matrix.preferences[eventType]?.[ch.id] ?? true
// Switched off for everyone by the admin (#1536): the same footprint as
// the switch, so the grid keeps its columns, and a lock that says why.
if (isLockedCell(matrix.locked, eventType, ch.id)) {
return (
<div key={ch.id} style={{ display: 'flex', justifyContent: 'center' }}>
<Tooltip label={t('settings.notificationPreferences.lockedByAdmin')}>
<span role="img" aria-label={t('settings.notificationPreferences.lockedByAdmin')} data-locked
className="inline-flex h-6 w-11 items-center justify-center rounded-full bg-surface-tertiary text-content-faint">
<Lock size={12} />
</span>
</Tooltip>
</div>
)
}
return (
<div key={ch.id} style={{ display: 'flex', justifyContent: 'center' }}>
<ToggleSwitch on={isOn} onToggle={() => toggle(eventType, ch.id)} />
@@ -0,0 +1,42 @@
/**
* Labels for the notification matrix, shared by the desktop and phone settings
* and the admin's defaults for users (#1536), so the three can never name an
* event differently.
*/
export interface NotificationChannelDescriptor {
id: string
source?: 'builtin' | 'plugin'
labelKey?: string
label?: string
settingsPath?: string
active: boolean
configured: boolean
}
export const EVENT_LABEL_KEYS: Record<string, string> = {
trip_invite: 'settings.notifyTripInvite',
booking_change: 'settings.notifyBookingChange',
trip_reminder: 'settings.notifyTripReminder',
todo_due: 'settings.notifyTodoDue',
vacay_invite: 'settings.notifyVacayInvite',
vacay_share: 'settings.notifyVacayShare',
collection_invite: 'settings.notifyCollectionInvite',
synology_session_cleared: 'settings.notifySynologySessionCleared',
plugin_notification: 'settings.notifyPluginNotification',
photos_shared: 'settings.notifyPhotosShared',
collab_message: 'settings.notifyCollabMessage',
packing_tagged: 'settings.notifyPackingTagged',
version_available: 'settings.notifyVersionAvailable',
}
/** Plugin channels have no i18n; the server sends their display name outright. */
export function channelLabel(ch: { id: string; labelKey?: string; label?: string }, t: (k: string) => string): string {
if (ch.labelKey) return t(ch.labelKey) || ch.id
return ch.label || ch.id
}
/** True when the admin switched this cell off for everyone (#1536). */
export function isLockedCell(locked: Record<string, string[]> | undefined, eventType: string, channel: string): boolean {
return !!locked?.[eventType]?.includes(channel)
}