push to maple-street
This commit is contained in:
@@ -0,0 +1,279 @@
|
||||
/**
|
||||
* Event video queries and access checks.
|
||||
*
|
||||
* Video bytes are never stored in Postgres: `originalStoredName` points into the
|
||||
* configured NFS archive and `webStoredName` / `posterStoredName` point into
|
||||
* `UPLOAD_DIR`. Every access check reuses the event access model, so a video is
|
||||
* private unless its visibility says otherwise.
|
||||
*/
|
||||
|
||||
import { and, asc, desc, eq, inArray } from "drizzle-orm";
|
||||
import { db } from "@/db";
|
||||
import {
|
||||
eventVideos,
|
||||
events,
|
||||
type EventVideoStatus,
|
||||
type EventVideoVisibility,
|
||||
} from "@/db/schema";
|
||||
import { getEventAccess, type EventSummaryRow } from "@/db/events";
|
||||
import type { SystemRole } from "@/auth/roles";
|
||||
import { uuidv7 } from "@/lib/uuid";
|
||||
|
||||
export type EventVideoRow = typeof eventVideos.$inferSelect;
|
||||
|
||||
export interface EventVideoWithEvent extends EventVideoRow {
|
||||
eventTitle: string;
|
||||
eventStartsAt: Date;
|
||||
}
|
||||
|
||||
export interface EventVideoAccess {
|
||||
video: EventVideoRow;
|
||||
event: EventSummaryRow;
|
||||
canManage: boolean;
|
||||
canContribute: boolean;
|
||||
}
|
||||
|
||||
export interface EventVideoPatch {
|
||||
title?: string;
|
||||
description?: string | null;
|
||||
visibility?: EventVideoVisibility;
|
||||
durationMs?: number | null;
|
||||
width?: number | null;
|
||||
height?: number | null;
|
||||
trimStartMs?: number;
|
||||
trimEndMs?: number | null;
|
||||
webStoredName?: string | null;
|
||||
posterStoredName?: string | null;
|
||||
status?: EventVideoStatus;
|
||||
errorMessage?: string | null;
|
||||
processedAt?: Date | null;
|
||||
updatedAt?: Date;
|
||||
}
|
||||
|
||||
function withEvent(row: {
|
||||
video: EventVideoRow;
|
||||
eventTitle: string;
|
||||
eventStartsAt: Date;
|
||||
}): EventVideoWithEvent {
|
||||
return {
|
||||
...row.video,
|
||||
eventTitle: row.eventTitle,
|
||||
eventStartsAt: row.eventStartsAt,
|
||||
};
|
||||
}
|
||||
|
||||
export async function listEventVideos(
|
||||
eventId: string,
|
||||
): Promise<EventVideoRow[]> {
|
||||
return db
|
||||
.select()
|
||||
.from(eventVideos)
|
||||
.where(eq(eventVideos.eventId, eventId))
|
||||
.orderBy(desc(eventVideos.createdAt));
|
||||
}
|
||||
|
||||
export async function listEventVideosForEvents(
|
||||
eventIds: readonly string[],
|
||||
): Promise<EventVideoWithEvent[]> {
|
||||
if (eventIds.length === 0) return [];
|
||||
const rows = await db
|
||||
.select({
|
||||
video: eventVideos,
|
||||
eventTitle: events.title,
|
||||
eventStartsAt: events.startsAt,
|
||||
})
|
||||
.from(eventVideos)
|
||||
.innerJoin(events, eq(events.id, eventVideos.eventId))
|
||||
.where(inArray(eventVideos.eventId, [...eventIds]))
|
||||
.orderBy(desc(eventVideos.createdAt));
|
||||
return rows.map(withEvent);
|
||||
}
|
||||
|
||||
export async function getEventVideoRow(
|
||||
videoId: string,
|
||||
): Promise<EventVideoRow | null> {
|
||||
const [row] = await db
|
||||
.select()
|
||||
.from(eventVideos)
|
||||
.where(eq(eventVideos.id, videoId))
|
||||
.limit(1);
|
||||
return row ?? null;
|
||||
}
|
||||
|
||||
export async function getEventVideoAccess(
|
||||
videoId: string,
|
||||
userId: string,
|
||||
role: SystemRole | null | undefined,
|
||||
): Promise<EventVideoAccess | null> {
|
||||
const [row] = await db
|
||||
.select({ video: eventVideos, eventId: events.id })
|
||||
.from(eventVideos)
|
||||
.innerJoin(events, eq(events.id, eventVideos.eventId))
|
||||
.where(eq(eventVideos.id, videoId))
|
||||
.limit(1);
|
||||
if (!row) return null;
|
||||
|
||||
const access = await getEventAccess(row.video.eventId, userId, role);
|
||||
if (!access) return null;
|
||||
return {
|
||||
video: row.video,
|
||||
event: access.event,
|
||||
canManage: access.canManage,
|
||||
canContribute: access.canContribute,
|
||||
};
|
||||
}
|
||||
|
||||
/** Ready, public/unlisted video with its event; used by watch and embed routes. */
|
||||
export async function getPublicEventVideo(
|
||||
videoId: string,
|
||||
): Promise<EventVideoWithEvent | null> {
|
||||
const [row] = await db
|
||||
.select({
|
||||
video: eventVideos,
|
||||
eventTitle: events.title,
|
||||
eventStartsAt: events.startsAt,
|
||||
})
|
||||
.from(eventVideos)
|
||||
.innerJoin(events, eq(events.id, eventVideos.eventId))
|
||||
.where(
|
||||
and(
|
||||
eq(eventVideos.id, videoId),
|
||||
eq(eventVideos.status, "ready"),
|
||||
inArray(eventVideos.visibility, ["public", "unlisted"]),
|
||||
),
|
||||
)
|
||||
.limit(1);
|
||||
return row ? withEvent(row) : null;
|
||||
}
|
||||
|
||||
export async function findEventVideoByChecksum(
|
||||
eventId: string,
|
||||
checksumSha256: string,
|
||||
): Promise<EventVideoRow | null> {
|
||||
const [row] = await db
|
||||
.select()
|
||||
.from(eventVideos)
|
||||
.where(
|
||||
and(
|
||||
eq(eventVideos.eventId, eventId),
|
||||
eq(eventVideos.checksumSha256, checksumSha256),
|
||||
),
|
||||
)
|
||||
.limit(1);
|
||||
return row ?? null;
|
||||
}
|
||||
|
||||
export async function addEventVideo(input: {
|
||||
id?: string;
|
||||
eventId: string;
|
||||
uploaderId: string;
|
||||
title: string;
|
||||
description: string | null;
|
||||
filename: string;
|
||||
mimeType: string;
|
||||
sizeBytes: number;
|
||||
checksumSha256: string;
|
||||
originalStoredName: string;
|
||||
archiveFolder: string;
|
||||
visibility: EventVideoVisibility;
|
||||
}): Promise<EventVideoRow> {
|
||||
const [row] = await db
|
||||
.insert(eventVideos)
|
||||
.values({
|
||||
// The upload route reserves this id for the NFS path, so reuse it here.
|
||||
id: input.id ?? uuidv7(),
|
||||
eventId: input.eventId,
|
||||
uploaderId: input.uploaderId,
|
||||
title: input.title,
|
||||
description: input.description,
|
||||
filename: input.filename,
|
||||
mimeType: input.mimeType,
|
||||
sizeBytes: input.sizeBytes,
|
||||
checksumSha256: input.checksumSha256,
|
||||
originalStoredName: input.originalStoredName,
|
||||
archiveFolder: input.archiveFolder,
|
||||
visibility: input.visibility,
|
||||
status: "processing",
|
||||
trimStartMs: 0,
|
||||
trimEndMs: null,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.returning();
|
||||
return row;
|
||||
}
|
||||
|
||||
export async function updateEventVideo(
|
||||
videoId: string,
|
||||
patch: EventVideoPatch,
|
||||
): Promise<EventVideoRow | null> {
|
||||
const [row] = await db
|
||||
.update(eventVideos)
|
||||
.set({ ...patch, updatedAt: patch.updatedAt ?? new Date() })
|
||||
.where(eq(eventVideos.id, videoId))
|
||||
.returning();
|
||||
return row ?? null;
|
||||
}
|
||||
|
||||
export async function markVideoProcessing(
|
||||
videoId: string,
|
||||
): Promise<EventVideoRow | null> {
|
||||
return updateEventVideo(videoId, {
|
||||
status: "processing",
|
||||
errorMessage: null,
|
||||
});
|
||||
}
|
||||
|
||||
export async function markVideoReady(input: {
|
||||
videoId: string;
|
||||
durationMs: number;
|
||||
width: number;
|
||||
height: number;
|
||||
trimStartMs: number;
|
||||
trimEndMs: number;
|
||||
webStoredName: string;
|
||||
posterStoredName: string;
|
||||
}): Promise<EventVideoRow | null> {
|
||||
return updateEventVideo(input.videoId, {
|
||||
status: "ready",
|
||||
durationMs: input.durationMs,
|
||||
width: input.width,
|
||||
height: input.height,
|
||||
trimStartMs: input.trimStartMs,
|
||||
trimEndMs: input.trimEndMs,
|
||||
webStoredName: input.webStoredName,
|
||||
posterStoredName: input.posterStoredName,
|
||||
errorMessage: null,
|
||||
processedAt: new Date(),
|
||||
});
|
||||
}
|
||||
|
||||
export async function markVideoFailed(
|
||||
videoId: string,
|
||||
message: string,
|
||||
): Promise<EventVideoRow | null> {
|
||||
return updateEventVideo(videoId, {
|
||||
status: "failed",
|
||||
errorMessage: message.slice(0, 1000),
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteEventVideo(
|
||||
videoId: string,
|
||||
): Promise<EventVideoRow | null> {
|
||||
const [row] = await db
|
||||
.delete(eventVideos)
|
||||
.where(eq(eventVideos.id, videoId))
|
||||
.returning();
|
||||
return row ?? null;
|
||||
}
|
||||
|
||||
export async function listVideosForEventOrdered(
|
||||
eventId: string,
|
||||
): Promise<EventVideoRow[]> {
|
||||
return db
|
||||
.select()
|
||||
.from(eventVideos)
|
||||
.where(eq(eventVideos.eventId, eventId))
|
||||
.orderBy(asc(eventVideos.createdAt));
|
||||
}
|
||||
Reference in New Issue
Block a user