Loading files
import { eq } from "drizzle-orm";
import {
hiddenLockError,
isAdminSession,
moderatorVisibilityChange,
moderatorVisibilityPatchOnly,
} from "@/lib/community/server/admin";
import { getAuth } from "@/lib/community/server/auth";
import { originBlocked, preflight, withCors } from "@/lib/community/cors";
import { communityEnabled, getDb } from "@/lib/community/server/db";
import { DECK_MAX, PUBLIC_DECKS_MAX } from "@/lib/community/deck";
import { checkDeckPattern, cleanPatternIds } from "@/lib/community/deckShare";
import {
clearNotificationsFor,
notifyDeckInclusion,
notifyVisibilityModerated,
} from "@/lib/community/server/notify";
import { countPublicDecksByUser, getDeckStub, getPatternsForDeck } from "@/lib/community/server/queries";
import { rateLimit } from "@/lib/community/ratelimit";
import { serializePerformance, validatePerformance } from "@/lib/pattern/pfst";
import { deckPatterns, decks } from "@/lib/community/server/schema";
import {
COMMENT_MAX,
cleanDescription,
cleanModerationReason,
cleanTitle,
} from "@/lib/community/validate";
import { cleanVisibility, type Visibility } from "@/lib/community/visibility";
// PATCH /api/community/decks/[id] — the owner edits their deck: details,
// visibility, or the whole running order (replaced in one move, the same way
// the working deck thinks about it).
//
// DELETE — the owner, or a moderator. Editing stays owner-only: same rule as
// patterns, removing content is moderation, rewriting it is not. The one
// PATCH a moderator may send is the softer removal — taking the deck off the
// wall, or putting back one a moderator took (lib/community/server/admin.ts).
export async function PATCH(request: Request, context: { params: Promise<{ id: string }> }) {
const blocked = originBlocked(request);
if (blocked) return blocked;
return withCors(request, await handlePatch(request, context));
}
export async function DELETE(request: Request, context: { params: Promise<{ id: string }> }) {
const blocked = originBlocked(request);
if (blocked) return blocked;
return withCors(request, await handleDelete(request, context));
}
export const OPTIONS = preflight;
async function handleDelete(request: Request, context: { params: Promise<{ id: string }> }) {
if (!communityEnabled()) {
return Response.json({ error: "Community is not enabled on this deployment." }, { status: 503 });
}
const session = await getAuth().api.getSession({ headers: request.headers });
if (!session) {
return Response.json({ error: "Sign in to delete your deck." }, { status: 401 });
}
const { id } = await context.params;
const deck = await getDeckStub(id);
if (!deck) return Response.json({ error: "Deck not found." }, { status: 404 });
if (deck.userId !== session.user.id && !isAdminSession(session)) {
return Response.json({ error: "You can only delete your own decks." }, { status: 403 });
}
// The join rows cascade; the patterns inside belong to their authors and
// are untouched.
await getDb().delete(decks).where(eq(decks.id, id));
await clearNotificationsFor({ targetType: "deck", targetId: id });
return Response.json({ ok: true });
}
async function handlePatch(request: Request, context: { params: Promise<{ id: string }> }) {
if (!communityEnabled()) {
return Response.json({ error: "Community is not enabled on this deployment." }, { status: 503 });
}
const session = await getAuth().api.getSession({ headers: request.headers });
if (!session) {
return Response.json({ error: "Sign in to edit your deck." }, { status: 401 });
}
if (!rateLimit(`deck:${session.user.id}`, 5, 60_000)) {
return Response.json({ error: "Too many deck updates — wait a minute and try again." }, { status: 429 });
}
const { id } = await context.params;
const deck = await getDeckStub(id);
if (!deck) return Response.json({ error: "Deck not found." }, { status: 404 });
const isOwner = deck.userId === session.user.id;
const moderating = !isOwner && isAdminSession(session);
if (!isOwner && !moderating) {
return Response.json({ error: "You can only edit your own decks." }, { status: 403 });
}
let body: unknown;
try {
body = await request.json();
} catch {
return Response.json({ error: "Invalid JSON body." }, { status: 400 });
}
const raw = body as Record<string, unknown>;
if (moderating) {
if (!moderatorVisibilityPatchOnly(raw)) {
return Response.json({ error: "A moderator can only make this deck private." }, { status: 403 });
}
return moderateVisibility(deck, raw, session.user.id);
}
let title = deck.title;
if (raw.title !== undefined) {
const next = cleanTitle(raw.title);
if (!next) return Response.json({ error: "Title is required (max 80 chars)." }, { status: 400 });
title = next;
}
let description: string | null | undefined;
if (raw.description !== undefined) {
description = cleanDescription(raw.description);
if (description === undefined) {
return Response.json({ error: "Description is too long (max 2000 chars)." }, { status: 400 });
}
}
let visibility = deck.visibility as Visibility;
if (raw.visibility !== undefined) {
const next = cleanVisibility(raw.visibility);
if (!next) return Response.json({ error: "Unknown visibility value." }, { status: 400 });
// Same lock as a pattern's: a moderator took it down, a moderator puts
// it back.
if (next === "public" && deck.hiddenAt) {
return Response.json({ error: hiddenLockError("deck") }, { status: 403 });
}
visibility = next;
}
// The slot check runs whenever the result is public — countPublicDecksByUser
// excludes this deck, so an already-public deck editing its title passes.
// Moderators are exempt, same as on create.
if (
visibility === "public" &&
!isAdminSession(session) &&
(await countPublicDecksByUser(session.user.id, id)) >= PUBLIC_DECKS_MAX
) {
return Response.json(
{
error: `You already have ${PUBLIC_DECKS_MAX} public decks — that is the shelf. Make one of them private to free a slot.`,
},
{ status: 400 },
);
}
let patternIds: string[] | null = null;
if (raw.patternIds !== undefined) {
patternIds = cleanPatternIds(raw.patternIds);
if (!patternIds) {
return Response.json(
{ error: `A deck is 1 to ${DECK_MAX} patterns, in order, with no duplicates.` },
{ status: 400 },
);
}
}
// Optional attached performance (Director timeline). null detaches; a
// string is validated against the same limits the device's .pfs player
// enforces, and stored canonically so the table the pack ships is stable
// across re-serializations. Serve-time zip decoration means no rebuild is
// queued either way.
let performanceJson: string | null | undefined;
if (raw.performanceJson !== undefined) {
if (raw.performanceJson === null || raw.performanceJson === "") {
performanceJson = null;
} else if (typeof raw.performanceJson !== "string") {
return Response.json({ error: "performanceJson must be a JSON string or null." }, { status: 400 });
} else if (raw.performanceJson.length > 64 * 1024) {
return Response.json({ error: "Performance JSON is too large (max 64 KB)." }, { status: 400 });
} else {
const verdict = validatePerformance(raw.performanceJson);
if (!verdict.ok) {
return Response.json({ error: `Performance: ${verdict.error}` }, { status: 400 });
}
performanceJson = JSON.stringify(serializePerformance(verdict.perf), null, 2);
}
}
const db = getDb();
// The list as it stands — the fallback when no replacement came, and the
// baseline for "which patterns are NEW here" when one did.
const currentIds = (
await db
.select({ patternId: deckPatterns.patternId })
.from(deckPatterns)
.where(eq(deckPatterns.deckId, id))
).map((row) => row.patternId);
// Whatever list the deck ends up with has to be allowed at the visibility it
// ends up at. A replaced list is checked strictly; for a visibility-only
// change the rows that still exist are checked and gaps stay gaps — a deck
// is not held hostage by a pattern its author already deleted.
const effectiveIds = patternIds ?? currentIds;
const rows = await getPatternsForDeck(effectiveIds);
const byId = new Map(rows.map((row) => [row.id, row]));
for (const patternId of effectiveIds) {
const pattern = byId.get(patternId);
if (!pattern && patternIds === null) continue; // existing gap
const check = checkDeckPattern(pattern, visibility, session.user.id);
if (!check.ok) {
return check.reason === "private"
? Response.json(
{
error: `"${check.title}" is private — make it public before putting it in a shared deck.`,
},
{ status: 400 },
)
: Response.json(
{ error: "A pattern in this deck is no longer available." },
{ status: 400 },
);
}
}
await db
.update(decks)
.set({
title,
...(description !== undefined ? { description } : {}),
...(performanceJson !== undefined ? { performanceJson } : {}),
visibility,
updatedAt: new Date(),
})
.where(eq(decks.id, id));
if (patternIds) {
await db.delete(deckPatterns).where(eq(deckPatterns.deckId, id));
await db.insert(deckPatterns).values(
patternIds.map((patternId, position) => ({
deckId: id,
patternId,
position,
titleSnapshot: byId.get(patternId)?.title ?? "",
})),
);
}
// A deck that just became public announces its whole running order to the
// pattern authors; one already public announces only what was added. A
// title edit announces nothing, and notifyDeckInclusion's unread guard
// absorbs visibility flip-flopping.
if (visibility === "public") {
const wasPublic = deck.visibility === "public";
const announceIds = !wasPublic
? effectiveIds
: patternIds
? patternIds.filter((patternId) => !currentIds.includes(patternId))
: [];
const announce = announceIds
.map((patternId) => byId.get(patternId))
.filter((pattern): pattern is NonNullable<typeof pattern> => Boolean(pattern));
if (announce.length > 0) {
await notifyDeckInclusion({
deckId: id,
deckTitle: title,
actorId: session.user.id,
patterns: announce,
});
}
}
return Response.json({ ok: true });
}
// A moderator taking somebody's deck off the wall, or putting back one a
// moderator took. Only the visibility moves.
async function moderateVisibility(
deck: NonNullable<Awaited<ReturnType<typeof getDeckStub>>>,
raw: Record<string, unknown>,
actorId: string,
): Promise<Response> {
const requested = cleanVisibility(raw.visibility);
if (!requested) return Response.json({ error: "Unknown visibility value." }, { status: 400 });
// A deck has no comments, so the reason rides in the alert alone — the
// same limit as a pattern's, where it becomes one.
const reason = cleanModerationReason(raw.reason);
if (reason === undefined) {
return Response.json(
{ error: `Keep the reason to ${COMMENT_MAX} characters at most.` },
{ status: 400 },
);
}
const change = moderatorVisibilityChange(deck, requested, "deck");
if (!change.ok) return Response.json({ error: change.error }, { status: change.status });
// Restoring skips both checks the author's own publish goes through, and
// announces nothing: it puts the deck back as if it had never come down.
// It held one of its author's public slots when it was taken down — if they
// have filled that since, the shelf is theirs to rebalance — and a pattern
// that went private in the meantime leaves the gap it would have left
// anyway. Its running order was announced when it first went public.
await getDb()
.update(decks)
.set({ visibility: change.visibility, hiddenAt: change.hiddenAt, updatedAt: new Date() })
.where(eq(decks.id, deck.id));
await notifyVisibilityModerated({
recipientId: deck.userId,
targetType: "deck",
targetId: deck.id,
targetTitle: deck.title,
hidden: change.visibility === "private",
reason,
actorId,
});
return Response.json({ ok: true, visibility: change.visibility });
}
07e931b46This file is pinned to the selected release. Download the original to open it in your design tool.