Notification routing rules, per-browser push wishes, escalation of unacknowledged alerts

- Routing: rules pick channels by kind, severity and server tags (first match wins, none = nobody); mixed batches split per route; recoveries follow their alert; quiet-hours digest keeps each notification's audience
- Push: each subscribed browser can ask for a minimum severity and some kinds
- Escalation: up to three timed steps re-notify alerts nobody has acknowledged, through chosen channels and an optional extra email; an acknowledgement stops them
- Fix ntfy: titles with an em dash made every ntfy send fail (header not Latin-1); now RFC 2047 encoded

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-18 19:44:45 +02:00
1 parent 9caa414829
commit 43fc5e0272
21 files changed
+4769 -55

No files matched your search

+54
View File
@@ -15,6 +15,8 @@ import { scheduleQuietHoursFlush } from "../services/quietHoursScheduler.js";
import { scheduleWeeklyDigest } from "../services/weeklyDigestScheduler.js";
import { buildDigest, sendDigest } from "../services/weeklyDigest.js";
import { enabledChannelCount } from "../services/notify.js";
import { CATEGORIES, CHANNEL_IDS } from "../services/notifyRouting.js";
import { InvalidTagError, normalizeTags } from "../services/serverTags.js";
import { testGotify, testNtfy, testSmtp, testWebhook, flushQuietHoursQueue } from "../services/notify.js";
import { listQueuedNotifications } from "../services/notificationQueue.js";
import { buildExportPayload, encryptExport, decryptExport, applyImportPayload, type EncryptedExportFile } from "../services/configBackup.js";
@@ -47,7 +49,51 @@ settingsRouter.get("/display", asyncHandler(async (_req, res) => {
res.json({ display });
}));
const channelEnum = z.enum(CHANNEL_IDS);
// Routing rules: which channels hear which notifications. A rule with no channels means "tell nobody" (the Alerts page still shows it).
const routingSchema = z.object({
rules: z
.array(
z.object({
id: z.string().trim().min(1).max(40),
name: z.string().trim().min(1).max(60),
enabled: z.boolean(),
categories: z.array(z.enum(CATEGORIES)).max(10),
minSeverity: z.enum(["warning", "critical"]).nullable(),
tags: z.array(z.string().trim().min(1).max(30)).max(10),
channels: z.array(channelEnum).max(5),
}),
)
.max(30),
});
// Escalation: a few steps in time order. Each must say who to tell — a channel, or an address.
const escalationSchema = z
.object({
enabled: z.boolean(),
steps: z
.array(
z.object({
afterMinutes: z.number().int().min(5).max(10080),
minSeverity: z.enum(["warning", "critical"]),
channels: z.array(channelEnum).max(5),
extraEmail: z.string().trim().max(200).refine((v) => v === "" || /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(v), "That isn't an email address"),
evenInQuietHours: z.boolean(),
}),
)
.max(3),
})
.superRefine((v, ctx) => {
v.steps.forEach((s, i) => {
if (s.channels.length === 0 && s.extraEmail === "") ctx.addIssue({ code: "custom", path: ["steps", i], message: "Choose a channel to tell, or an address to email." });
if (i > 0 && s.afterMinutes <= v.steps[i - 1].afterMinutes) ctx.addIssue({ code: "custom", path: ["steps", i, "afterMinutes"], message: "Each step must come later than the one before." });
});
});
const updateSchema = z.object({
routing: routingSchema.optional(),
escalation: escalationSchema.optional(),
gotify: z.object({ enabled: z.boolean(), url: z.string(), token: z.string(), priority: z.number() }).partial().optional(),
ntfy: z.object({ enabled: z.boolean(), url: z.string(), topic: z.string(), token: z.string(), priority: z.number() }).partial().optional(),
smtp: z
@@ -126,6 +172,14 @@ settingsRouter.put("/", requireRole("admin"), asyncHandler(async (req, res) => {
return res.status(400).json({ error: "invalid_body", details: parsed.error.flatten() });
}
// Tags in routing rules are written the way server tags are, so they match.
try {
if (parsed.data.routing) for (const rule of parsed.data.routing.rules) rule.tags = normalizeTags(rule.tags);
} catch (err) {
if (err instanceof InvalidTagError) return res.status(400).json({ error: "invalid_tag", message: err.message });
throw err;
}
const before = await getSettings();
const updated = await updateSettings(parsed.data);