feat(editor): bridge the advanced photo editor into the event workflow
CI / build (push) Has been cancelled
CI / build (push) Has been cancelled
Event photos can now be opened in Lumenroom, a WebGL2 develop module, from the photo editor's Advanced button. The dashboard signs a short-lived token scoped to one user and event; Lumenroom exchanges it, imports the event's photo list and proxies bytes on demand, so nothing is copied and the temporary catalog expires with the bridge session. Saving in Lumenroom renders the photo and writes it back as the event's edited JPEG, with the advanced recipe kept in event_photos.advanced_edit_json. Lumenroom is vendored under lumenroom/ and built as a second compose service, so one clone and `docker compose up -d --build` deploys both apps. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
@@ -0,0 +1,242 @@
|
||||
/**
|
||||
* Bridge sessions between an event and the advanced editor.
|
||||
*
|
||||
* A session is a short-lived authorization to read one event's photos and to
|
||||
* write rendered edits back. The row is the revocation point: deleting it
|
||||
* invalidates every token issued for it.
|
||||
*/
|
||||
|
||||
import { and, asc, eq, gt, lt } from "drizzle-orm";
|
||||
import { db } from "@/db";
|
||||
import {
|
||||
bridgeSessions,
|
||||
eventFolders,
|
||||
eventPhotos,
|
||||
events,
|
||||
users,
|
||||
} from "@/db/schema";
|
||||
import {
|
||||
BRIDGE_SESSION_TTL_SECONDS,
|
||||
verifyBridgeToken,
|
||||
} from "@/lib/bridge";
|
||||
import { uuidv7 } from "@/lib/uuid";
|
||||
|
||||
export interface BridgeSessionRow {
|
||||
id: string;
|
||||
userId: string;
|
||||
eventId: string;
|
||||
expiresAt: Date;
|
||||
}
|
||||
|
||||
export async function createBridgeSession(input: {
|
||||
userId: string;
|
||||
eventId: string;
|
||||
}): Promise<BridgeSessionRow> {
|
||||
const [row] = await db
|
||||
.insert(bridgeSessions)
|
||||
.values({
|
||||
id: uuidv7(),
|
||||
userId: input.userId,
|
||||
eventId: input.eventId,
|
||||
expiresAt: new Date(Date.now() + BRIDGE_SESSION_TTL_SECONDS * 1000),
|
||||
})
|
||||
.returning({
|
||||
id: bridgeSessions.id,
|
||||
userId: bridgeSessions.userId,
|
||||
eventId: bridgeSessions.eventId,
|
||||
expiresAt: bridgeSessions.expiresAt,
|
||||
});
|
||||
return row;
|
||||
}
|
||||
|
||||
export async function getBridgeSession(
|
||||
id: string,
|
||||
): Promise<BridgeSessionRow | null> {
|
||||
const [row] = await db
|
||||
.select({
|
||||
id: bridgeSessions.id,
|
||||
userId: bridgeSessions.userId,
|
||||
eventId: bridgeSessions.eventId,
|
||||
expiresAt: bridgeSessions.expiresAt,
|
||||
})
|
||||
.from(bridgeSessions)
|
||||
.where(and(eq(bridgeSessions.id, id), gt(bridgeSessions.expiresAt, new Date())))
|
||||
.limit(1);
|
||||
return row ?? null;
|
||||
}
|
||||
|
||||
export async function deleteBridgeSession(id: string): Promise<void> {
|
||||
await db.delete(bridgeSessions).where(eq(bridgeSessions.id, id));
|
||||
}
|
||||
|
||||
export async function deleteExpiredBridgeSessions(): Promise<void> {
|
||||
await db
|
||||
.delete(bridgeSessions)
|
||||
.where(lt(bridgeSessions.expiresAt, new Date()));
|
||||
}
|
||||
|
||||
export interface BridgeEventSummary {
|
||||
id: string;
|
||||
title: string;
|
||||
eventType: string;
|
||||
startsAt: Date;
|
||||
endsAt: Date | null;
|
||||
location: string | null;
|
||||
description: string | null;
|
||||
}
|
||||
|
||||
export async function getBridgeEventSummary(
|
||||
eventId: string,
|
||||
): Promise<BridgeEventSummary | null> {
|
||||
const [row] = await db
|
||||
.select({
|
||||
id: events.id,
|
||||
title: events.title,
|
||||
eventType: events.eventType,
|
||||
startsAt: events.startsAt,
|
||||
endsAt: events.endsAt,
|
||||
location: events.location,
|
||||
description: events.description,
|
||||
})
|
||||
.from(events)
|
||||
.where(eq(events.id, eventId))
|
||||
.limit(1);
|
||||
return row ?? null;
|
||||
}
|
||||
|
||||
export interface BridgePhotoRow {
|
||||
id: string;
|
||||
filename: string;
|
||||
mimeType: string;
|
||||
kind: "raw" | "image";
|
||||
sizeBytes: number;
|
||||
storedName: string;
|
||||
editedStoredName: string | null;
|
||||
createdAt: Date;
|
||||
folderName: string;
|
||||
}
|
||||
|
||||
export async function listBridgePhotos(
|
||||
eventId: string,
|
||||
): Promise<BridgePhotoRow[]> {
|
||||
return db
|
||||
.select({
|
||||
id: eventPhotos.id,
|
||||
filename: eventPhotos.filename,
|
||||
mimeType: eventPhotos.mimeType,
|
||||
kind: eventPhotos.kind,
|
||||
sizeBytes: eventPhotos.sizeBytes,
|
||||
storedName: eventPhotos.storedName,
|
||||
editedStoredName: eventPhotos.editedStoredName,
|
||||
createdAt: eventPhotos.createdAt,
|
||||
folderName: eventFolders.name,
|
||||
})
|
||||
.from(eventPhotos)
|
||||
.innerJoin(eventFolders, eq(eventFolders.id, eventPhotos.folderId))
|
||||
.where(eq(eventFolders.eventId, eventId))
|
||||
.orderBy(asc(eventFolders.createdAt), asc(eventPhotos.createdAt));
|
||||
}
|
||||
|
||||
export async function getBridgePhoto(
|
||||
eventId: string,
|
||||
photoId: string,
|
||||
): Promise<BridgePhotoRow | null> {
|
||||
const [row] = await db
|
||||
.select({
|
||||
id: eventPhotos.id,
|
||||
filename: eventPhotos.filename,
|
||||
mimeType: eventPhotos.mimeType,
|
||||
kind: eventPhotos.kind,
|
||||
sizeBytes: eventPhotos.sizeBytes,
|
||||
storedName: eventPhotos.storedName,
|
||||
editedStoredName: eventPhotos.editedStoredName,
|
||||
createdAt: eventPhotos.createdAt,
|
||||
folderName: eventFolders.name,
|
||||
})
|
||||
.from(eventPhotos)
|
||||
.innerJoin(eventFolders, eq(eventFolders.id, eventPhotos.folderId))
|
||||
.where(and(eq(eventPhotos.id, photoId), eq(eventFolders.eventId, eventId)))
|
||||
.limit(1);
|
||||
return row ?? null;
|
||||
}
|
||||
|
||||
/** Record a JPEG rendered by the advanced editor beside the original. */
|
||||
export async function saveBridgeRender(input: {
|
||||
photoId: string;
|
||||
editedStoredName: string;
|
||||
editedSizeBytes: number;
|
||||
advancedEditJson: string;
|
||||
editedBy: string;
|
||||
}): Promise<Date | null> {
|
||||
const [row] = await db
|
||||
.update(eventPhotos)
|
||||
.set({
|
||||
editedStoredName: input.editedStoredName,
|
||||
editedSizeBytes: input.editedSizeBytes,
|
||||
advancedEditJson: input.advancedEditJson,
|
||||
editedAt: new Date(),
|
||||
editedBy: input.editedBy,
|
||||
})
|
||||
.where(eq(eventPhotos.id, input.photoId))
|
||||
.returning({ editedAt: eventPhotos.editedAt });
|
||||
return row?.editedAt ?? null;
|
||||
}
|
||||
|
||||
/** Name and email of the user a bridge token was issued for. */
|
||||
export async function bridgeUser(
|
||||
userId: string,
|
||||
): Promise<{ id: string; name: string; email: string } | null> {
|
||||
const [row] = await db
|
||||
.select({
|
||||
id: users.id,
|
||||
email: users.email,
|
||||
displayName: users.displayName,
|
||||
firstName: users.firstName,
|
||||
lastName: users.lastName,
|
||||
})
|
||||
.from(users)
|
||||
.where(eq(users.id, userId))
|
||||
.limit(1);
|
||||
if (!row) return null;
|
||||
const name =
|
||||
row.displayName?.trim() ||
|
||||
`${row.firstName ?? ""} ${row.lastName ?? ""}`.trim() ||
|
||||
row.email;
|
||||
return { id: row.id, name, email: row.email };
|
||||
}
|
||||
|
||||
export interface AuthenticatedBridge {
|
||||
payload: NonNullable<ReturnType<typeof verifyBridgeToken>>;
|
||||
session: BridgeSessionRow;
|
||||
user: { id: string; name: string; email: string };
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the Authorization bearer token and that it still matches a live
|
||||
* session row for the same user and event.
|
||||
*/
|
||||
export async function authenticateBridge(
|
||||
req: Request,
|
||||
sessionId: string,
|
||||
kind: "login" | "service" = "service",
|
||||
): Promise<AuthenticatedBridge | null> {
|
||||
const header = req.headers.get("authorization") ?? "";
|
||||
const token = header.startsWith("Bearer ") ? header.slice(7).trim() : "";
|
||||
const payload = verifyBridgeToken(token);
|
||||
if (!payload || payload.sid !== sessionId || payload.kind !== kind) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const session = await getBridgeSession(sessionId);
|
||||
if (
|
||||
!session ||
|
||||
session.userId !== payload.uid ||
|
||||
session.eventId !== payload.eid
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const user = await bridgeUser(payload.uid);
|
||||
if (!user) return null;
|
||||
return { payload, session, user };
|
||||
}
|
||||
@@ -1039,6 +1039,8 @@ export const eventPhotos = pgTable(
|
||||
kind: text("kind", { enum: EVENT_PHOTO_KINDS }).notNull(),
|
||||
/** Last saved adjustment recipe (JSON); the original is never overwritten. */
|
||||
editJson: text("edit_json"),
|
||||
/** Recipe from the advanced (Lumenroom) editor, kept apart from the simple one. */
|
||||
advancedEditJson: text("advanced_edit_json"),
|
||||
/** Generated full-resolution JPEG. Null until the first edit is saved. */
|
||||
editedStoredName: text("edited_stored_name"),
|
||||
editedSizeBytes: integer("edited_size_bytes"),
|
||||
@@ -1059,6 +1061,31 @@ export const eventPhotos = pgTable(
|
||||
],
|
||||
);
|
||||
|
||||
/**
|
||||
* Short-lived hand-off to the advanced editor (Lumenroom). A row exists only
|
||||
* while the editor is open; the signed token the editor holds is scoped to it.
|
||||
*/
|
||||
export const bridgeSessions = pgTable(
|
||||
"bridge_sessions",
|
||||
{
|
||||
id: text("id").primaryKey(),
|
||||
userId: text("user_id")
|
||||
.notNull()
|
||||
.references(() => users.id, { onDelete: "cascade" }),
|
||||
eventId: text("event_id")
|
||||
.notNull()
|
||||
.references(() => events.id, { onDelete: "cascade" }),
|
||||
expiresAt: timestamp("expires_at", {
|
||||
mode: "date",
|
||||
withTimezone: true,
|
||||
}).notNull(),
|
||||
createdAt: timestamp("created_at", { mode: "date", withTimezone: true })
|
||||
.notNull()
|
||||
.defaultNow(),
|
||||
},
|
||||
(table) => [index("bridge_sessions_event_idx").on(table.eventId)],
|
||||
);
|
||||
|
||||
/* ------------------------------------------------------- event videos */
|
||||
|
||||
/** Processing state for an uploaded event video. */
|
||||
|
||||
Reference in New Issue
Block a user