Wall displays: a night schedule that dims or blanks the board

A named display can dim the board or blank it (so a tablet or computer may
sleep) between two times on the screen's own clock. A critical alert brings the
board back unless told not to, and a touch or key press brings it back for two
minutes. A share link that opens the display follows the same schedule.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-13 18:47:53 +01:00
1 parent 168d01dc7b
commit a69d0a611d
17 files changed
+9948 -22

No files matched your search

@@ -0,0 +1,4 @@
ALTER TABLE `wall_displays` ADD `night_from` text;--> statement-breakpoint
ALTER TABLE `wall_displays` ADD `night_to` text;--> statement-breakpoint
ALTER TABLE `wall_displays` ADD `night_mode` text;--> statement-breakpoint
ALTER TABLE `wall_displays` ADD `night_wake_critical` integer DEFAULT true NOT NULL;
File diff suppressed because it is too large. Load diff
+7
View File
@@ -687,6 +687,13 @@
"when": 1794521526082,
"tag": "0097_duplicati_followups",
"breakpoints": true
},
{
"idx": 98,
"version": "6",
"when": 1794522492798,
"tag": "0098_wall_display_night",
"breakpoints": true
}
]
}
+5
View File
@@ -857,6 +857,11 @@ export const wallDisplays = sqliteTable("wall_displays", {
slug: text("slug").notNull().unique(), // the address: /wall/<slug>
pages: text("pages").notNull().default("[]"), // JSON: page ids in order ("overview", "alerts", "group:apps" …); empty = every page the board has
rotateSeconds: integer("rotate_seconds"), // how long each page stays; null = not set (the address or the browser decides, else the board does not rotate)
// A night schedule on the screen's own clock: from this time to that (HH:MM, and past midnight when "to" is earlier), the board is dimmed or blanked. All null = none.
nightFrom: text("night_from"),
nightTo: text("night_to"),
nightMode: text("night_mode"), // dim | off
nightWakeCritical: integer("night_wake_critical", { mode: "boolean" }).notNull().default(true), // show the board again while something is critical
createdBy: text("created_by"),
createdAt: text("created_at")
.notNull()
+17 -3
View File
@@ -5,7 +5,7 @@ import { db } from "../db/client.js";
import { shareLinks, wallDisplays } from "../db/schema.js";
import { requireAuth, requireRole } from "../auth/middleware.js";
import { recordAudit } from "../services/audit.js";
import { freeSlug, listDisplays, MAX_DISPLAYS, MAX_SLUG, PAGE_ID, ROTATE_MAX, ROTATE_MIN, shapeDisplay, SLUG, slugOf } from "../services/wallDisplays.js";
import { freeSlug, listDisplays, MAX_DISPLAYS, MAX_SLUG, NIGHT_MODES, PAGE_ID, ROTATE_MAX, ROTATE_MIN, shapeDisplay, SLUG, slugOf, TIME } from "../services/wallDisplays.js";
import { asyncHandler } from "../utils/asyncHandler.js";
export const wallDisplaysRouter = Router();
@@ -20,8 +20,22 @@ const displaySchema = z.object({
// The pages in the order the board rotates through them; empty is every page it has.
pages: z.array(z.string().regex(PAGE_ID, "That is not a page of the board.")).max(20).default([]),
rotateSeconds: z.number().int().min(ROTATE_MIN).max(ROTATE_MAX).nullable().default(null),
// A night schedule on the screen's own clock, or none. From and to are times of day; "to" earlier than "from" runs past midnight.
night: z
.object({
from: z.string().regex(TIME, "Use a time like 22:00."),
to: z.string().regex(TIME, "Use a time like 07:00."),
mode: z.enum(NIGHT_MODES),
wakeOnCritical: z.boolean().default(true),
})
.refine((n) => n.from !== n.to, { message: "The night needs a start and an end that differ.", path: ["to"] })
.nullable()
.default(null),
});
/** The columns a night schedule is kept in. */
const nightColumns = (n: z.infer<typeof displaySchema>["night"]) => ({ nightFrom: n?.from ?? null, nightTo: n?.to ?? null, nightMode: n?.mode ?? null, nightWakeCritical: n?.wakeOnCritical ?? true });
const idOf = (v: string) => (/^\d+$/.test(v) ? Number(v) : NaN);
const addressOf = (slug: string) => `/wall/${slug}`;
@@ -40,7 +54,7 @@ wallDisplaysRouter.post("/", requireRole("admin"), asyncHandler(async (req, res)
const actor = req.currentUser!;
const [row] = await db
.insert(wallDisplays)
.values({ name: parsed.data.name, slug, pages: JSON.stringify([...new Set(parsed.data.pages)]), rotateSeconds: parsed.data.rotateSeconds, createdBy: actor.name ?? actor.email ?? actor.oidcSub })
.values({ name: parsed.data.name, slug, pages: JSON.stringify([...new Set(parsed.data.pages)]), rotateSeconds: parsed.data.rotateSeconds, ...nightColumns(parsed.data.night), createdBy: actor.name ?? actor.email ?? actor.oidcSub })
.returning();
await recordAudit({ actor, category: "wall_display", action: "create", targetType: "wall_display", targetId: row.id, detail: { name: row.name, address: addressOf(row.slug) } });
res.status(201).json({ display: shapeDisplay(row) });
@@ -59,7 +73,7 @@ wallDisplaysRouter.put("/:id", requireRole("admin"), asyncHandler(async (req, re
if (all.some((d) => d.id !== id && d.slug === slug)) return res.status(409).json({ error: "slug_taken", message: `Another display already has the address ${addressOf(slug)}.` });
const [row] = await db
.update(wallDisplays)
.set({ name: parsed.data.name, slug, pages: JSON.stringify([...new Set(parsed.data.pages)]), rotateSeconds: parsed.data.rotateSeconds })
.set({ name: parsed.data.name, slug, pages: JSON.stringify([...new Set(parsed.data.pages)]), rotateSeconds: parsed.data.rotateSeconds, ...nightColumns(parsed.data.night) })
.where(eq(wallDisplays.id, id))
.returning();
await recordAudit({ actor: req.currentUser!, category: "wall_display", action: "update", targetType: "wall_display", targetId: id, detail: { name: row.name, address: addressOf(row.slug) } });
+23 -3
View File
@@ -19,6 +19,26 @@ export const PAGE_ID = /^(overview|alerts|group:[a-z0-9-]{1,40})$/;
/** The address part of a display: lower case letters, digits and single hyphens. */
export const SLUG = /^[a-z0-9]+(-[a-z0-9]+)*$/;
export const MAX_SLUG = 40;
/** A time of day, 00:00 to 23:59. */
export const TIME = /^([01]\d|2[0-3]):[0-5]\d$/;
export const NIGHT_MODES = ["dim", "off"] as const;
export type NightMode = (typeof NIGHT_MODES)[number];
export interface Night {
from: string;
to: string;
mode: NightMode;
/** Show the board again, whatever the hour, while something is critical. */
wakeOnCritical: boolean;
}
/** What is stored of a night schedule, read back as one; none unless all three of its parts are there and make sense. */
export function nightOf(row: Pick<WallDisplayRow, "nightFrom" | "nightTo" | "nightMode" | "nightWakeCritical">): Night | null {
if (!row.nightFrom || !row.nightTo || !row.nightMode) return null;
if (!TIME.test(row.nightFrom) || !TIME.test(row.nightTo) || row.nightFrom === row.nightTo) return null;
if (!(NIGHT_MODES as readonly string[]).includes(row.nightMode)) return null;
return { from: row.nightFrom, to: row.nightTo, mode: row.nightMode as NightMode, wakeOnCritical: row.nightWakeCritical };
}
/** Pure: a name as an address part — "Hall TV (upstairs)" → "hall-tv-upstairs"; Swedish letters are folded (å, ä → a, ö → o) so the address is plain. */
export function slugOf(name: string): string {
@@ -56,7 +76,7 @@ export function parsePages(stored: string | null | undefined): string[] {
/** A display as everyone signed in sees it. */
export function shapeDisplay(row: WallDisplayRow) {
return { id: row.id, name: row.name, slug: row.slug, pages: parsePages(row.pages), rotateSeconds: row.rotateSeconds, createdBy: row.createdBy, createdAt: row.createdAt };
return { id: row.id, name: row.name, slug: row.slug, pages: parsePages(row.pages), rotateSeconds: row.rotateSeconds, night: nightOf(row), createdBy: row.createdBy, createdAt: row.createdAt };
}
export async function listDisplays(): Promise<WallDisplayRow[]> {
@@ -64,8 +84,8 @@ export async function listDisplays(): Promise<WallDisplayRow[]> {
}
/** The display a share link points at, as the public board needs it (name, pages, speed) — null when there is none or it was taken away. */
export async function displayForLink(displayId: number | null): Promise<{ name: string; pages: string[]; rotateSeconds: number | null } | null> {
export async function displayForLink(displayId: number | null): Promise<{ name: string; pages: string[]; rotateSeconds: number | null; night: Night | null } | null> {
if (displayId === null) return null;
const [row] = await db.select().from(wallDisplays).where(eq(wallDisplays.id, displayId)).limit(1);
return row ? { name: row.name, pages: parsePages(row.pages), rotateSeconds: row.rotateSeconds } : null;
return row ? { name: row.name, pages: parsePages(row.pages), rotateSeconds: row.rotateSeconds, night: nightOf(row) } : null;
}
+71 -1
View File
@@ -96,6 +96,49 @@ describe("what a display shows, and how fast", () => {
});
});
describe("the night", () => {
const at = (hh: number, mm = 0) => new Date(2026, 10, 10, hh, mm, 0);
const night = { from: "22:00", to: "07:00", mode: "dim" as const, wakeOnCritical: true };
test("a night across midnight is from the start up to, not including, the end; one within a day is plain", () => {
assert.equal(P.inNight(at(21, 59), night), false);
assert.equal(P.inNight(at(22, 0), night), true);
assert.equal(P.inNight(at(23, 59), night), true);
assert.equal(P.inNight(at(0, 0), night), true);
assert.equal(P.inNight(at(6, 59), night), true);
assert.equal(P.inNight(at(7, 0), night), false, "the end is the morning");
assert.equal(P.inNight(at(12, 0), night), false);
const nap = { from: "13:00", to: "15:30" };
assert.deepEqual([at(12, 59), at(13, 0), at(15, 29), at(15, 30)].map((d) => P.inNight(d, nap)), [false, true, true, false]);
assert.equal(P.inNight(at(3), { from: "05:00", to: "05:00" }), false, "no length, no night");
});
test("at night it is dimmed or blank; by day, with no schedule, or when something is critical (if told to) it is the board", () => {
const quiet = { critical: false, wokenUntil: null };
assert.equal(P.nightState(at(12), night, quiet), "day");
assert.equal(P.nightState(at(23), night, quiet), "dim");
assert.equal(P.nightState(at(23), { ...night, mode: "off" }, quiet), "off");
assert.equal(P.nightState(at(23), null, quiet), "day");
assert.equal(P.nightState(at(23), undefined, quiet), "day");
assert.equal(P.nightState(at(23), night, { critical: true, wokenUntil: null }), "day", "something is critical");
assert.equal(P.nightState(at(23), { ...night, mode: "off", wakeOnCritical: false }, { critical: true, wokenUntil: null }), "off", "told not to");
});
test("a touch brings the board back for two minutes, then the night returns", () => {
const now = at(23);
const touched = now.getTime() + P.WAKE_FOR_MS;
assert.equal(P.WAKE_FOR_MS, 120_000);
assert.equal(P.nightState(now, night, { critical: false, wokenUntil: touched }), "day");
assert.equal(P.nightState(new Date(touched + 1), night, { critical: false, wokenUntil: touched }), "dim");
});
test("what is stored is a night only when all of it is there and makes sense", () => {
const row = { nightFrom: "22:00", nightTo: "07:00", nightMode: "off", nightWakeCritical: false };
assert.deepEqual(W.nightOf(row), { from: "22:00", to: "07:00", mode: "off", wakeOnCritical: false });
for (const bad of [{ nightFrom: null }, { nightTo: null }, { nightMode: null }, { nightFrom: "7:00" }, { nightTo: "24:00" }, { nightMode: "sleep" }, { nightTo: "22:00" }]) assert.equal(W.nightOf({ ...row, ...bad }), null, JSON.stringify(bad));
});
});
describe("the routes", () => {
let app: Server;
let base = "";
@@ -184,7 +227,7 @@ describe("the routes", () => {
assert.equal(link.displayId, id);
const board = await (await call("GET", `/public/wall/${token}`)).json();
assert.deepEqual(board.displayConfig, { name: "Hall TV", pages: ["overview", "group:apps"], rotateSeconds: 30 });
assert.deepEqual(board.displayConfig, { name: "Hall TV", pages: ["overview", "group:apps"], rotateSeconds: 30, night: null });
// a link with no display says none
const plain = (await (await call("POST", "/share-links", { label: "Whole board", kind: "wall", expiresInDays: null })).json()).token;
@@ -216,6 +259,33 @@ describe("the routes", () => {
assert.equal(describeChange("wall_display", "delete", { name: "Hall TV" }).title, "Wall display removed: Hall TV");
});
test("a display can have a night schedule; it is read back, given to the public board, and taken away by saving without one", async () => {
const made = await call("POST", "/wall-displays", { name: "Bedroom", night: { from: "23:00", to: "06:30", mode: "off", wakeOnCritical: false } });
assert.equal(made.status, 201);
const display = (await made.json()).display;
assert.deepEqual(display.night, { from: "23:00", to: "06:30", mode: "off", wakeOnCritical: false });
const link = await (await call("POST", "/share-links", { label: "Bedroom screen", kind: "wall", displayId: display.id, expiresInDays: null })).json();
const board = await (await call("GET", `/public/wall/${link.token}`)).json();
assert.deepEqual(board.displayConfig.night, { from: "23:00", to: "06:30", mode: "off", wakeOnCritical: false });
const without = (await (await call("PUT", `/wall-displays/${display.id}`, { name: "Bedroom" })).json()).display;
assert.equal(without.night, null);
assert.equal((await (await call("GET", `/public/wall/${link.token}`)).json()).displayConfig.night, null);
const defaults = (await (await call("PUT", `/wall-displays/${display.id}`, { name: "Bedroom", night: { from: "22:00", to: "07:00", mode: "dim" } })).json()).display;
assert.equal(defaults.night.wakeOnCritical, true, "a critical alert brings it back unless it is told not to");
await call("DELETE", `/wall-displays/${display.id}`);
});
test("a night needs real times, two that differ, and a way", async () => {
const bad = (night: unknown) => call("POST", "/wall-displays", { name: "Nope", night });
for (const n of [{ from: "7:00", to: "22:00", mode: "dim" }, { from: "22:00", to: "25:00", mode: "dim" }, { from: "22:00", to: "22:00", mode: "dim" }, { from: "22:00", to: "07:00", mode: "sleep" }, { from: "22:00", to: "07:00" }, { from: "22:00" }, "night"]) {
assert.equal((await bad(n)).status, 400, JSON.stringify(n));
}
assert.match((await (await bad({ from: "22:00", to: "22:00", mode: "dim" })).json()).message, /start and an end that differ/);
});
test("no more than twenty", async () => {
for (let i = 0; i < 25; i++) {
const r = await call("POST", "/wall-displays", { name: `Screen ${i}` });