/** * When are notifications held back rather than sent? Two things hold them: **quiet hours** (a daily window) and **away** (a stretch somebody set: "I'm away until Sunday"). * A held notification is queued and delivered as one digest when the hold ends. Either can let *critical* alerts through (so a full disk at 03:00 or the lights going out while * away still reach the person who can do something about it) — or not, for those who want true silence. * * This is only the decision. Sending, queueing and the digest are in notify.ts; ending an away stretch when its time comes is awayMonitor.ts. */ import { getSettings, type AwaySettings, type QuietHoursSettings } from "./settingsStore.js"; export type HoldReason = "away" | "quiet"; /** No away stretch, one set for later, one going on now. */ export type AwayState = "none" | "planned" | "active"; /** Pure: where an away stretch stands at `now`. One whose end has passed is over (and is cleared by the monitor). */ export function awayState(away: Pick, now: Date): AwayState { if (!away.until) return "none"; const until = Date.parse(away.until); if (!Number.isFinite(until) || until <= now.getTime()) return "none"; const from = away.from ? Date.parse(away.from) : NaN; return Number.isFinite(from) && from > now.getTime() ? "planned" : "active"; } const toMinutes = (hhmm: string) => { const [h, m] = hhmm.split(":").map(Number); return (Number.isFinite(h) ? h : 0) * 60 + (Number.isFinite(m) ? m : 0); }; function clockIn(timezone: string, now: Date): number { let parts: Intl.DateTimeFormatPart[]; try { parts = new Intl.DateTimeFormat("en-GB", { timeZone: timezone, hour: "2-digit", minute: "2-digit", hour12: false }).formatToParts(now); } catch { parts = new Intl.DateTimeFormat("en-GB", { timeZone: "UTC", hour: "2-digit", minute: "2-digit", hour12: false }).formatToParts(now); } return (Number(parts.find((p) => p.type === "hour")?.value ?? "0") % 24) * 60 + Number(parts.find((p) => p.type === "minute")?.value ?? "0"); } /** Pure: is `now` inside the quiet-hours window, on the clock of the time zone? A window that wraps past midnight (22:00 → 07:00) is the usual case. */ export function inQuietWindow(quiet: Pick, timezone: string, now: Date): boolean { if (!quiet.enabled) return false; const nowMin = clockIn(timezone, now); const start = toMinutes(quiet.start); const end = toMinutes(quiet.end); if (start === end) return false; // a zero-length window is never on return start < end ? nowMin >= start && nowMin < end : nowMin >= start || nowMin < end; } /** * Pure: why a notification is held right now, or null if it goes out. Being away is checked first — it is the stronger statement. A critical one is let through when the * thing holding it says so. */ export function holdFor(settings: { away: AwaySettings; quietHours: QuietHoursSettings; timezone: string }, critical: boolean, now: Date): HoldReason | null { if (awayState(settings.away, now) === "active" && !(critical && settings.away.criticalThrough)) return "away"; if (inQuietWindow(settings.quietHours, settings.timezone, now) && !(critical && settings.quietHours.criticalThrough)) return "quiet"; return null; } export async function holdReason(critical: boolean, now: Date = new Date()): Promise { const s = await getSettings(); return holdFor({ away: s.away, quietHours: s.quietHours, timezone: s.notifications.timezone }, critical, now); }