mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
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:
@@ -10,6 +10,7 @@ import {
|
||||
adminAddonUpdateRequestSchema,
|
||||
adminCollabFeaturesRequestSchema,
|
||||
adminNotificationPreferencesRequestSchema,
|
||||
notificationDefaultsUpdateRequestSchema,
|
||||
adminDefaultUserSettingsRequestSchema,
|
||||
adminTestNotificationRequestSchema,
|
||||
adminTransitProviderRequestSchema,
|
||||
@@ -35,6 +36,7 @@ export class AdminOidcUpdateDto extends createZodDto(adminOidcUpdateRequestSchem
|
||||
export class AdminAddonUpdateDto extends createZodDto(adminAddonUpdateRequestSchema) {}
|
||||
export class AdminCollabFeaturesDto extends createZodDto(adminCollabFeaturesRequestSchema) {}
|
||||
export class AdminNotificationPreferencesDto extends createZodDto(adminNotificationPreferencesRequestSchema) {}
|
||||
export class NotificationDefaultsUpdateDto extends createZodDto(notificationDefaultsUpdateRequestSchema) {}
|
||||
export class AdminDefaultUserSettingsDto extends createZodDto(adminDefaultUserSettingsRequestSchema) {}
|
||||
export class AdminTestNotificationDto extends createZodDto(adminTestNotificationRequestSchema) {}
|
||||
export class AdminTransitProviderDto extends createZodDto(adminTransitProviderRequestSchema) {}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { NOTIFICATION_DEFAULTS, type NotificationDefault } from '@trek/shared';
|
||||
import { DatabaseService } from '../database/database.service';
|
||||
import { MailerService } from './mailer/mailer.service';
|
||||
import { listChannels } from './channel-registry';
|
||||
@@ -21,8 +22,20 @@ export interface PreferencesMatrix {
|
||||
event_types: NotifEventType[];
|
||||
implemented_combos: Record<string, NotifChannel[]>;
|
||||
defaults?: { ntfyServer: string | null };
|
||||
/** Cells the admin switched off for everyone (#1536), event → channels. User scope only. */
|
||||
locked?: Partial<Record<NotifEventType, NotifChannel[]>>;
|
||||
}
|
||||
|
||||
/** The admin's per-cell defaults for user notifications, with the matrix to draw them in (#1536). */
|
||||
export interface InstanceDefaultsMatrix {
|
||||
defaults: Partial<Record<NotifEventType, Partial<Record<NotifChannel, NotificationDefault>>>>;
|
||||
channels: ChannelDescriptor[];
|
||||
event_types: NotifEventType[];
|
||||
implemented_combos: Record<string, NotifChannel[]>;
|
||||
}
|
||||
|
||||
const DEFAULT_KEY = (event: string, channel: string) => `notif_default_${event}_${channel}`;
|
||||
|
||||
/**
|
||||
* Who gets which notification over which channel: the admin's channel switches,
|
||||
* the per-user event opt-outs, and the matrix the settings UI renders.
|
||||
@@ -97,11 +110,57 @@ export class NotificationPreferencesService {
|
||||
* Default (no row) = enabled. Only returns false if there's an explicit disabled row.
|
||||
*/
|
||||
isEnabledForEvent(userId: number, eventType: NotifEventType, channel: NotifChannel): boolean {
|
||||
const fallback = this.instanceDefault(eventType, channel);
|
||||
if (fallback === 'blocked') return false;
|
||||
const row = this.db.get<{ enabled: number }>(
|
||||
'SELECT enabled FROM notification_channel_preferences WHERE user_id = ? AND event_type = ? AND channel = ?',
|
||||
userId, eventType, channel,
|
||||
);
|
||||
return row === undefined || row.enabled === 1;
|
||||
return row === undefined ? fallback === 'on' : row.enabled === 1;
|
||||
}
|
||||
|
||||
// ── Instance defaults (#1536) ──────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* What a cell is for a user who never touched it. Admin-scoped events keep
|
||||
* their own global switches and are never affected; an unknown stored value
|
||||
* means the old behaviour, on.
|
||||
*/
|
||||
instanceDefault(eventType: NotifEventType, channel: NotifChannel): NotificationDefault {
|
||||
if (ADMIN_SCOPED_EVENTS.has(eventType)) return 'on';
|
||||
const raw = this.getAppSetting(DEFAULT_KEY(eventType, channel));
|
||||
return (NOTIFICATION_DEFAULTS as readonly string[]).includes(raw ?? '') ? (raw as NotificationDefault) : 'on';
|
||||
}
|
||||
|
||||
private userEventTypes(): NotifEventType[] {
|
||||
return ALL_EVENT_TYPES.filter(e => !ADMIN_SCOPED_EVENTS.has(e));
|
||||
}
|
||||
|
||||
getInstanceDefaults(adminId: number): InstanceDefaultsMatrix {
|
||||
const event_types = this.userEventTypes();
|
||||
const implemented_combos = this.allCombos();
|
||||
const defaults: InstanceDefaultsMatrix['defaults'] = {};
|
||||
for (const event of event_types) {
|
||||
defaults[event] = {};
|
||||
for (const channel of implemented_combos[event]) defaults[event]![channel] = this.instanceDefault(event, channel);
|
||||
}
|
||||
return { defaults, channels: this.describeChannels(adminId, 'user'), event_types, implemented_combos };
|
||||
}
|
||||
|
||||
/** Stores the cells given; `on` removes the row, so the table only holds what differs. Unknown cells are skipped. */
|
||||
setInstanceDefaults(defaults: Partial<Record<string, Partial<Record<string, NotificationDefault>>>>): void {
|
||||
const userEvents = new Set<string>(this.userEventTypes());
|
||||
this.db.transaction(() => {
|
||||
for (const [event, channels] of Object.entries(defaults)) {
|
||||
if (!userEvents.has(event) || !channels) continue;
|
||||
const allowed = new Set(this.combosFor(event as NotifEventType));
|
||||
for (const [channel, value] of Object.entries(channels)) {
|
||||
if (!allowed.has(channel) || !value) continue;
|
||||
if (value === 'on') this.db.run('DELETE FROM app_settings WHERE key = ?', DEFAULT_KEY(event, channel));
|
||||
else this.db.run('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)', DEFAULT_KEY(event, channel), value);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ── Preferences matrix ─────────────────────────────────────────────────────
|
||||
@@ -185,8 +244,10 @@ export class NotificationPreferencesService {
|
||||
|
||||
const implemented_combos = this.allCombos();
|
||||
|
||||
// Build the full matrix with defaults (true when no row exists)
|
||||
// Build the full matrix with defaults: the admin's instance default when no
|
||||
// row exists (on unless set otherwise), and off for a cell the admin blocked.
|
||||
const preferences: Partial<Record<NotifEventType, Partial<Record<NotifChannel, boolean>>>> = {};
|
||||
const locked: Partial<Record<NotifEventType, NotifChannel[]>> = {};
|
||||
|
||||
for (const eventType of ALL_EVENT_TYPES) {
|
||||
preferences[eventType] = {};
|
||||
@@ -194,8 +255,14 @@ export class NotificationPreferencesService {
|
||||
// Admin-scoped events use global settings for the built-in external channels
|
||||
if (scope === 'admin' && ADMIN_SCOPED_EVENTS.has(eventType) && isAdminGlobalChannel(channel)) {
|
||||
preferences[eventType]![channel] = this.getAdminGlobalPref(eventType, channel);
|
||||
continue;
|
||||
}
|
||||
const fallback = this.instanceDefault(eventType, channel);
|
||||
if (fallback === 'blocked') {
|
||||
preferences[eventType]![channel] = false;
|
||||
(locked[eventType] ??= []).push(channel);
|
||||
} else {
|
||||
preferences[eventType]![channel] = stored[eventType]?.[channel] ?? true;
|
||||
preferences[eventType]![channel] = stored[eventType]?.[channel] ?? fallback === 'on';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -210,7 +277,7 @@ export class NotificationPreferencesService {
|
||||
channels: this.describeChannels(userId, scope),
|
||||
event_types,
|
||||
implemented_combos,
|
||||
...(scope === 'user' && { defaults: { ntfyServer: this.getAppSetting('admin_ntfy_server') || null } }),
|
||||
...(scope === 'user' && { defaults: { ntfyServer: this.getAppSetting('admin_ntfy_server') || null }, locked }),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -249,12 +316,13 @@ export class NotificationPreferencesService {
|
||||
for (const [eventType, channels] of Object.entries(prefs)) {
|
||||
if (!channels) continue;
|
||||
for (const [channel, enabled] of Object.entries(channels)) {
|
||||
if (enabled) {
|
||||
// Remove explicit row — default is enabled
|
||||
del.run(userId, eventType, channel);
|
||||
} else {
|
||||
upsert.run(userId, eventType, channel, 0);
|
||||
}
|
||||
const fallback = this.instanceDefault(eventType as NotifEventType, channel);
|
||||
// A cell the admin blocked stays off whatever is sent (#1536).
|
||||
if (fallback === 'blocked') continue;
|
||||
// A value that matches the instance default is stored as no row, so a later
|
||||
// change of the default reaches everybody who never chose otherwise.
|
||||
if (enabled === (fallback === 'on')) del.run(userId, eventType, channel);
|
||||
else upsert.run(userId, eventType, channel, enabled ? 1 : 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ import {
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { AdminGuard } from '../auth/admin.guard';
|
||||
import { NotificationPreferencesService } from './notification-preferences.service';
|
||||
import { AdminNotificationPreferencesDto } from '../admin/admin.dto';
|
||||
import { AdminNotificationPreferencesDto, NotificationDefaultsUpdateDto } from '../admin/admin.dto';
|
||||
import { CurrentUser } from '../auth/current-user.decorator';
|
||||
import { ManagedForbidden } from '../common/managed';
|
||||
|
||||
@@ -229,6 +229,18 @@ export class AdminNotificationPreferencesController {
|
||||
return this.prefs.getPreferencesMatrix(user.id, user.role, 'admin');
|
||||
}
|
||||
|
||||
/** What every user's notification cells start as, and which the admin blocked (#1536). */
|
||||
@Get('defaults')
|
||||
getDefaults(@CurrentUser() user: User) {
|
||||
return this.prefs.getInstanceDefaults(user.id);
|
||||
}
|
||||
|
||||
@Put('defaults')
|
||||
setDefaults(@CurrentUser() user: User, @Body() body: NotificationDefaultsUpdateDto) {
|
||||
this.prefs.setInstanceDefaults(body.defaults);
|
||||
return this.prefs.getInstanceDefaults(user.id);
|
||||
}
|
||||
|
||||
@Put()
|
||||
set(@CurrentUser() user: User, @Body() body: AdminNotificationPreferencesDto) {
|
||||
this.prefs.setAdminPreferences(user.id, body);
|
||||
|
||||
Reference in New Issue
Block a user