feat(cameras): ‹ › through older/newer snapshots in the large view

On the Snapshot side the large view steps through that camera's snapshots (‹ older,
› newer, also ← →), muted at either end; the caption carries the date and always names
the picture on screen (the previous one stays while the next loads; a pruned still says
"unavailable"). The tile stays on the newest; closing resets. New read-only route
GET /api/cameras/:deviceId/snapshots/:snapshotId/neighbors (session:read), ordered by
(capturedAt, id) like latest-snapshot, bounded to seek the device/time index.

Also carries the user's own label edits in en.ts / sq.ts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RZgP5oMncSaVmKRKk1UvN4
This commit is contained in:
2026-11-17 17:31:58 +01:00
parent bccef3875b
commit cb43ad78ca
8 changed files with 267 additions and 43 deletions
+49
View File
@@ -60,6 +60,55 @@ describe("GET /api/cameras/:id/latest-snapshot", () => {
});
});
describe("GET /api/cameras/:id/snapshots/:snapshotId/neighbors", () => {
const url = (cam: string, id: string) => `/api/cameras/${cam}/snapshots/${id}/neighbors`;
it("steps through one camera's snapshots by time, ties broken by id", async () => {
await seedUser(db, { username: "admin", password: "pw" });
const { cookie } = await login(app, "admin", "pw");
shot("a", "cam-dahua", "2026-11-17T10:00:00.000Z");
shot("b", "cam-dahua", "2026-11-17T11:00:00.000Z");
shot("c", "cam-dahua", "2026-11-17T11:00:00.000Z"); // same instant as b
shot("d", "cam-dahua", "2026-11-17T12:00:00.000Z");
shot("x", "other", "2026-11-17T11:30:00.000Z"); // another camera: never a neighbour
const get = async (id: string) => (await app.inject({ url: url("cam-dahua", id), headers: { cookie } })).json();
const ids = (r: { older: { id: string } | null; newer: { id: string } | null }) => [r.older?.id ?? null, r.newer?.id ?? null];
expect(ids(await get("a"))).toEqual([null, "b"]);
expect(ids(await get("b"))).toEqual(["a", "c"]);
expect(ids(await get("c"))).toEqual(["b", "d"]);
expect(ids(await get("d"))).toEqual(["c", null]);
expect(await get("b")).toMatchObject({ older: { id: "a", direction: "entry", capturedAt: "2026-11-17T10:00:00.000Z" } });
// latest-snapshot uses the same tie-break, so "newer" is never offered from the latest
const latest = await app.inject({ url: "/api/cameras/cam-dahua/latest-snapshot", headers: { cookie } });
expect(latest.json().id).toBe("d");
});
it("a tie at the newest end: latest-snapshot is the one with no newer", async () => {
await seedUser(db, { username: "admin", password: "pw" });
const { cookie } = await login(app, "admin", "pw");
shot("p", "cam-dahua", "2026-11-17T10:00:00.000Z");
shot("q", "cam-dahua", "2026-11-17T12:00:00.000Z");
shot("r", "cam-dahua", "2026-11-17T12:00:00.000Z");
const latest = (await app.inject({ url: "/api/cameras/cam-dahua/latest-snapshot", headers: { cookie } })).json();
const n = (await app.inject({ url: url("cam-dahua", latest.id), headers: { cookie } })).json();
expect(latest.id).toBe("r");
expect(n).toMatchObject({ older: { id: "q" }, newer: null });
});
it("404s another camera's snapshot, 403s without session:read", async () => {
await seedUser(db, { username: "admin", password: "pw" });
const admin = await login(app, "admin", "pw");
shot("x", "other", "2026-11-17T11:30:00.000Z");
const foreign = await app.inject({ url: url("cam-dahua", "x"), headers: { cookie: admin.cookie } });
expect(foreign.statusCode).toBe(404);
await seedUser(db, { username: "dev", password: "pw", roleId: "devonly", permissions: ["device:read"] });
const dev = await login(app, "dev", "pw");
const res = await app.inject({ url: url("other", "x"), headers: { cookie: dev.cookie } });
expect(res.statusCode).toBe(403);
});
});
describe("WS /api/cameras/:id/live", () => {
const origin = "http://booth.local:8080";
+50 -2
View File
@@ -1,5 +1,5 @@
import type { FastifyInstance } from "fastify";
import { desc, devices, eq, snapshots, type Db } from "@parking/db";
import { and, asc, desc, devices, eq, gt, gte, lt, lte, or, snapshots, type Db } from "@parking/db";
import { isLiveStreamDevice } from "@parking/devices";
import { requirePermission, roleHasPermissions } from "../auth.js";
import { buildCamera } from "../snapshot.js";
@@ -10,6 +10,9 @@ import { authenticateWsHandshake } from "./ws.js";
// - GET /api/cameras/:deviceId/latest-snapshot — the tile's default picture: the newest
// entry/exit evidence still this camera captured (metadata; bytes stay at
// /api/snapshots/:id). Evidence → `session:read`, like every snapshot read.
// - GET /api/cameras/:deviceId/snapshots/:snapshotId/neighbors — the large view's ‹ ›:
// this camera's next older and next newer snapshot ({ older, newer }, each meta or
// null). Same permission. Order is (capturedAt, id) everywhere so ties are stable.
// - WS /api/cameras/:deviceId/live — live video while the camera sees a vehicle.
// Same handshake auth as /api/ws (Origin allowlist, then desktop ticket or cookie),
// then `device:read` (the footer's permission). Server → client: text JSON
@@ -42,7 +45,7 @@ export async function cameraRoutes(app: FastifyInstance, db: Db, hub: CameraLive
.select({ id: snapshots.id, direction: snapshots.direction, capturedAt: snapshots.capturedAt })
.from(snapshots)
.where(eq(snapshots.deviceId, req.params.deviceId))
.orderBy(desc(snapshots.capturedAt))
.orderBy(desc(snapshots.capturedAt), desc(snapshots.id))
.limit(1)
.get();
if (!row) return reply.code(404).send({ error: "no snapshot for this camera yet" });
@@ -50,6 +53,51 @@ export async function cameraRoutes(app: FastifyInstance, db: Db, hub: CameraLive
},
);
app.get<{ Params: { deviceId: string; snapshotId: string } }>(
"/api/cameras/:deviceId/snapshots/:snapshotId/neighbors",
{ preHandler: requirePermission("session:read") },
async (req, reply) => {
const { deviceId, snapshotId } = req.params;
const at = db
.select({ capturedAt: snapshots.capturedAt })
.from(snapshots)
.where(and(eq(snapshots.id, snapshotId), eq(snapshots.deviceId, deviceId)))
.get();
if (!at) return reply.code(404).send({ error: "no such snapshot for this camera" });
const meta = { id: snapshots.id, direction: snapshots.direction, capturedAt: snapshots.capturedAt };
// capturedAt is always toISOString() (UTC, fixed width), so string order = time order.
// The plain lte/gte bound next to the OR lets SQLite seek snapshots_device_time_idx
// from this snapshot instead of walking the camera's index from one end.
const older = db
.select(meta)
.from(snapshots)
.where(
and(
eq(snapshots.deviceId, deviceId),
lte(snapshots.capturedAt, at.capturedAt),
or(lt(snapshots.capturedAt, at.capturedAt), and(eq(snapshots.capturedAt, at.capturedAt), lt(snapshots.id, snapshotId))),
),
)
.orderBy(desc(snapshots.capturedAt), desc(snapshots.id))
.limit(1)
.get();
const newer = db
.select(meta)
.from(snapshots)
.where(
and(
eq(snapshots.deviceId, deviceId),
gte(snapshots.capturedAt, at.capturedAt),
or(gt(snapshots.capturedAt, at.capturedAt), and(eq(snapshots.capturedAt, at.capturedAt), gt(snapshots.id, snapshotId))),
),
)
.orderBy(asc(snapshots.capturedAt), asc(snapshots.id))
.limit(1)
.get();
return { older: older ?? null, newer: newer ?? null };
},
);
app.get<{ Params: { deviceId: string } }>(
"/api/cameras/:deviceId/live",
{