/** * What a settings update actually changed, in a form fit for the audit log. * * The audit log can be read by operators, but Settings can't — so values only go in for sections an operator could * already see in the app. The notification channels (Gotify, ntfy, SMTP, webhook) hold credentials, and even their * addresses can act as one (a webhook URL carries its own token; a public ntfy topic is the only thing protecting * it), so for those only the names of the fields that changed are recorded, never what they changed to or from. */ const NAMES_ONLY_SECTIONS = new Set(["gotify", "ntfy", "smtp", "webhook"]); /** Anything longer than this isn't a useful thing to read in a table cell. */ const MAX_VALUE_JSON = 300; export type SettingsChange = { from: unknown; to: unknown } | "(changed)"; function same(a: unknown, b: unknown): boolean { return JSON.stringify(a) === JSON.stringify(b); } function capture(value: unknown): unknown { return value !== undefined && JSON.stringify(value)?.length > MAX_VALUE_JSON ? "(too long to show)" : value; } /** * Compares each section in `patch` with what was stored before. Only fields that really differ are listed, and a * section where nothing differed is left out entirely. */ export function describeSettingsChanges(before: object, patch: object): Record> { const out: Record> = {}; for (const [section, incoming] of Object.entries(patch)) { if (incoming === null || typeof incoming !== "object") continue; const previous = ((before as Record)[section] ?? {}) as Record; const changes: Record = {}; for (const [key, value] of Object.entries(incoming as Record)) { if (same(previous[key], value)) continue; changes[key] = NAMES_ONLY_SECTIONS.has(section) ? "(changed)" : { from: capture(previous[key]), to: capture(value) }; } if (Object.keys(changes).length > 0) out[section] = changes; } return out; }