Away mode, and critical alerts through quiet hours

For a one-person homelab, "on call" is when you can be reached. Settings ->
Notifications -> When -> Away holds notifications while you are away (a banner on
every page says so) and sends one digest when it ends, at its time or when you say
you are back. Critical alerts can still come through. Quiet hours get the same
"still send critical alerts" option. Escalation steps marked to send in quiet hours
are sent while away too. What is held for one reason is not released while the
other still holds it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-22 19:14:29 +02:00
1 parent 3b31b16796
commit 16b24f5436
21 files changed
+737 -50

No files matched your search

+76
View File
@@ -0,0 +1,76 @@
import { Router } from "express";
import { z } from "zod";
import { requireAuth, requireRole } from "../auth/middleware.js";
import { recordAudit } from "../services/audit.js";
import { awayState } from "../services/availability.js";
import { endAway } from "../services/awayMonitor.js";
import { zonedTime } from "../services/maintenanceSchedules.js";
import { listQueuedNotifications } from "../services/notificationQueue.js";
import { getSettings, updateSettings } from "../services/settingsStore.js";
import { asyncHandler } from "../utils/asyncHandler.js";
export const awayRouter = Router();
awayRouter.use(requireAuth);
export const MAX_AWAY_DAYS = 120;
const DAY_MS = 86_400_000;
/** A wall-clock time ("2026-10-25T18:00") is on the clock of the notification time zone, like every other time in the settings; a full ISO moment is itself. */
export function momentOf(text: string, timezone: string): Date | null {
if (/Z$|[+-]\d{2}:?\d{2}$/.test(text)) {
const t = Date.parse(text);
return Number.isFinite(t) ? new Date(t) : null;
}
const m = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})$/.exec(text);
if (!m) return null;
return zonedTime(Number(m[1]), Number(m[2]), Number(m[3]), Number(m[4]), Number(m[5]), timezone);
}
const bodySchema = z.object({
until: z.string().min(1),
from: z.string().nullable().optional(),
criticalThrough: z.boolean().optional(),
note: z.string().trim().max(200).optional(),
});
async function view() {
const { away, notifications } = await getSettings();
const now = new Date();
return { away: { ...away, state: awayState(away, now) }, held: (await listQueuedNotifications()).length, timezone: notifications.timezone || "UTC" };
}
// Whether somebody is away, for the banner everyone sees; and how many notifications are being held.
awayRouter.get("/", asyncHandler(async (_req, res) => {
res.json(await view());
}));
// Starts, plans or changes an away stretch: administrators, since it decides who is told what.
awayRouter.put("/", requireRole("admin"), asyncHandler(async (req, res) => {
const parsed = bodySchema.safeParse(req.body);
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: "Say until when." });
const settings = await getSettings();
const tz = settings.notifications.timezone || "UTC";
const now = new Date();
const until = momentOf(parsed.data.until, tz);
const from = parsed.data.from ? momentOf(parsed.data.from, tz) : null;
if (!until) return res.status(400).json({ error: "invalid_until", message: "That isn't a time." });
if (parsed.data.from && !from) return res.status(400).json({ error: "invalid_from", message: "That isn't a start time." });
if (until.getTime() <= now.getTime()) return res.status(400).json({ error: "in_the_past", message: "That time has already passed." });
if (until.getTime() > now.getTime() + MAX_AWAY_DAYS * DAY_MS) return res.status(400).json({ error: "too_long", message: `At most ${MAX_AWAY_DAYS} days ahead.` });
if (from && from.getTime() >= until.getTime()) return res.status(400).json({ error: "bad_range", message: "It has to start before it ends." });
const criticalThrough = parsed.data.criticalThrough ?? settings.away.criticalThrough;
const note = parsed.data.note ?? "";
// A start that has already come is simply "now".
const startIso = from && from.getTime() > now.getTime() ? from.toISOString() : null;
await updateSettings({ away: { from: startIso, until: until.toISOString(), criticalThrough, note } });
await recordAudit({ actor: req.currentUser!, category: "away", action: "start", detail: { from: startIso, until: until.toISOString(), criticalThrough, ...(note ? { note } : {}) } });
res.json(await view());
}));
// "I'm back": ends it now (or cancels one planned), and sends what was held as one digest.
awayRouter.delete("/", requireRole("admin"), asyncHandler(async (req, res) => {
const { away } = await getSettings();
if (!away.until) return res.status(404).json({ error: "not_away", message: "Nobody is away." });
const sent = await endAway({ automatic: false, actor: req.currentUser! });
res.json({ ...(await view()), sent });
}));
+2 -2
View File
@@ -148,7 +148,7 @@ const updateSchema = z.object({
.partial()
.optional(),
quietHours: z
.object({ enabled: z.boolean(), start: z.string().regex(/^\d{2}:\d{2}$/), end: z.string().regex(/^\d{2}:\d{2}$/) })
.object({ enabled: z.boolean(), start: z.string().regex(/^\d{2}:\d{2}$/), end: z.string().regex(/^\d{2}:\d{2}$/), criticalThrough: z.boolean() })
.partial()
.optional(),
signIn: z
@@ -312,7 +312,7 @@ settingsRouter.get("/quiet-hours-queue", requireRole("admin"), asyncHandler(asyn
}));
settingsRouter.post("/flush-quiet-hours", requireRole("admin"), asyncHandler(async (req, res) => {
const flushed = await flushQuietHoursQueue();
const flushed = await flushQuietHoursQueue({ force: true });
await recordAudit({ actor: req.currentUser!, category: "settings", action: "flush_quiet_hours", detail: { flushed } });
res.json({ flushed });
}));