VoiceSubmissions, ModLists and MissionFiles relied on Payload's default upload staticDir (the collection slug resolved from cwd), so prod wrote their files to /app/<slug> instead of the mounted /app/media volume and every redeploy wiped them (voice takes returned 500 on play/download). Pin all three under media/ subdirs like story Voiceovers already does, add regression guards, and fix the audio-preview fallbacks that pointed at the media collection's file route instead of the owning collection.
143 lines
4 KiB
TypeScript
143 lines
4 KiB
TypeScript
import type { User, VoiceScript, VoiceSubmission } from "@/payload-types";
|
|
import { isSuperuser } from "@/utils/access-control/hasPermission";
|
|
|
|
type PayloadType = Awaited<ReturnType<typeof import("payload").getPayload>>;
|
|
|
|
export interface VoiceoverSubmissionSummary {
|
|
id: number;
|
|
status: string;
|
|
createdAt: string;
|
|
audioUrl: string;
|
|
description: string | null;
|
|
reviewNote: string | null;
|
|
}
|
|
|
|
export interface VoiceoverScriptView {
|
|
id: number;
|
|
title: string;
|
|
description: string | null;
|
|
script: string;
|
|
xpAmount: number;
|
|
status: string;
|
|
spoiler: boolean;
|
|
createdAt: string;
|
|
/** This user's own submissions on the script, newest first. */
|
|
mySubmissions: VoiceoverSubmissionSummary[];
|
|
/**
|
|
* How many submissions OTHER users have made on this script. A number only —
|
|
* never identities (privacy requirement).
|
|
*/
|
|
otherSubmissionCount: number;
|
|
}
|
|
|
|
/** Upload docs expose `url`; fall back to this collection's own file route (files are served per collection). */
|
|
function submissionAudioUrl(submission: VoiceSubmission): string {
|
|
return (
|
|
submission.url ??
|
|
(submission.filename ? `/api/voice-submissions/file/${submission.filename}` : "")
|
|
);
|
|
}
|
|
|
|
function toSubmissionSummary(submission: VoiceSubmission): VoiceoverSubmissionSummary {
|
|
return {
|
|
id: submission.id,
|
|
status: submission.status,
|
|
createdAt: submission.createdAt,
|
|
audioUrl: submissionAudioUrl(submission),
|
|
description: submission.description ?? null,
|
|
reviewNote: submission.reviewNote ?? null,
|
|
};
|
|
}
|
|
|
|
function buildScriptView(
|
|
script: VoiceScript,
|
|
submissions: VoiceSubmission[],
|
|
userId: number,
|
|
): VoiceoverScriptView {
|
|
const mySubmissions: VoiceoverSubmissionSummary[] = [];
|
|
let otherSubmissionCount = 0;
|
|
|
|
for (const submission of submissions) {
|
|
if (submission.user === userId) {
|
|
mySubmissions.push(toSubmissionSummary(submission));
|
|
} else {
|
|
// Other users' submissions are counted, never surfaced — identities stay server-side.
|
|
otherSubmissionCount += 1;
|
|
}
|
|
}
|
|
|
|
return {
|
|
id: script.id,
|
|
title: script.title,
|
|
description: script.description ?? null,
|
|
script: script.script,
|
|
xpAmount: script.xpAmount,
|
|
status: script.status,
|
|
spoiler: script.spoiler ?? false,
|
|
createdAt: script.createdAt,
|
|
mySubmissions,
|
|
otherSubmissionCount,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* All voiceover scripts (newest first) annotated with the user's own submissions
|
|
* and an anonymous count of other users' submissions.
|
|
*
|
|
* Round-trip pattern: one query for scripts + one for all submissions, grouped
|
|
* in memory — avoids per-script count queries (N+1).
|
|
*/
|
|
export async function listScriptsForUser(
|
|
payload: PayloadType,
|
|
user: User,
|
|
): Promise<VoiceoverScriptView[]> {
|
|
const canSeeDrafts = await isSuperuser(payload, user);
|
|
const [scriptsRes, submissionsRes] = await Promise.all([
|
|
payload.find({
|
|
collection: "voice-scripts",
|
|
where: canSeeDrafts ? undefined : { status: { not_equals: "draft" } },
|
|
sort: "-createdAt",
|
|
limit: 0,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
}),
|
|
payload.find({
|
|
collection: "voice-submissions",
|
|
sort: "-createdAt",
|
|
limit: 0,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
}),
|
|
]);
|
|
|
|
return scriptsRes.docs.map((script) => buildScriptView(script, submissionsRes.docs, user.id));
|
|
}
|
|
|
|
/** A single voiceover script in the same shape as `listScriptsForUser`, or null. */
|
|
export async function getScriptForUser(
|
|
payload: PayloadType,
|
|
user: User,
|
|
scriptId: number,
|
|
): Promise<VoiceoverScriptView | null> {
|
|
const script = await payload
|
|
.findByID({
|
|
collection: "voice-scripts",
|
|
id: scriptId,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
})
|
|
.catch(() => null);
|
|
if (!script) return null;
|
|
if (script.status === "draft" && !(await isSuperuser(payload, user))) return null;
|
|
|
|
const submissionsRes = await payload.find({
|
|
collection: "voice-submissions",
|
|
where: { script: { equals: scriptId } },
|
|
sort: "-createdAt",
|
|
limit: 0,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
|
|
return buildScriptView(script, submissionsRes.docs, user.id);
|
|
}
|