Skip to content

Commit 55f2d02

Browse files
authored
Merge branch 'main' into dev
2 parents 683352e + ff4270f commit 55f2d02

File tree

13 files changed

+1330
-237
lines changed

13 files changed

+1330
-237
lines changed

app/api/cron/check-renders/route.ts

Lines changed: 579 additions & 0 deletions
Large diffs are not rendered by default.

app/api/cron/ingest/route.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,13 @@ async function createSanityDocuments(
292292
_type: "reference",
293293
_ref: contentIdea._id,
294294
},
295-
script: script.script,
295+
script: {
296+
...script.script,
297+
scenes: script.script.scenes.map((scene, i) => ({
298+
...scene,
299+
_key: `scene-${i + 1}`,
300+
})),
301+
},
296302
scriptQualityScore: criticResult.score,
297303
status: isFlagged ? "flagged" : "script_ready",
298304
...(isFlagged && {

app/api/webhooks/sanity-content/route.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NextResponse } from 'next/server';
1+
import { NextResponse, after } from 'next/server';
22
import { isValidSignature, SIGNATURE_HEADER_NAME } from '@sanity/webhook';
33
import { processVideoProduction } from '@/lib/services/video-pipeline';
44

@@ -82,10 +82,16 @@ export async function POST(request: Request) {
8282
);
8383
}
8484

85-
// Fire and forget — trigger pipeline in background, return 200 immediately
85+
// Use after() to run the pipeline after the response is sent.
86+
// On Vercel, serverless functions terminate after the response — fire-and-forget
87+
// (promise.catch() without await) silently dies. after() keeps the function alive.
8688
console.log(`[WEBHOOK] Triggering video production for document: ${body._id}`);
87-
processVideoProduction(body._id).catch((error) => {
88-
console.log(`[WEBHOOK] Background processing error for ${body._id}:`, error);
89+
after(async () => {
90+
try {
91+
await processVideoProduction(body._id);
92+
} catch (error) {
93+
console.error(`[WEBHOOK] Background processing error for ${body._id}:`, error);
94+
}
8995
});
9096

9197
return NextResponse.json({ triggered: true }, { status: 200 });

app/api/webhooks/sanity-distribute/route.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,12 @@
11
import { type NextRequest, NextResponse } from "next/server";
2-
import * as crypto from "node:crypto";
2+
import { isValidSignature, SIGNATURE_HEADER_NAME } from "@sanity/webhook";
33
import { writeClient } from "@/lib/sanity-write-client";
44
import { generateWithGemini } from "@/lib/gemini";
55
import { uploadVideo, uploadShort, generateShortsMetadata } from "@/lib/youtube-upload";
66
import { notifySubscribers } from "@/lib/resend-notify";
77
import { postVideoAnnouncement } from "@/lib/x-social";
88

9-
// ---------------------------------------------------------------------------
10-
// Webhook signature validation
11-
// ---------------------------------------------------------------------------
12-
13-
function isValidSignature(body: string, signature: string | null): boolean {
14-
const secret = process.env.SANITY_WEBHOOK_SECRET;
15-
if (!secret) {
16-
console.warn("[sanity-distribute] SANITY_WEBHOOK_SECRET not set");
17-
return true;
18-
}
19-
if (!signature) return false;
20-
const hmac = crypto.createHmac("sha256", secret);
21-
hmac.update(body);
22-
const digest = hmac.digest("base64");
23-
try {
24-
return crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(digest));
25-
} catch {
26-
return false;
27-
}
28-
}
9+
const WEBHOOK_SECRET = process.env.SANITY_WEBHOOK_SECRET;
2910

3011
// ---------------------------------------------------------------------------
3112
// Types
@@ -122,8 +103,15 @@ async function updateStatus(docId: string, status: string, extra: Record<string,
122103

123104
export async function POST(req: NextRequest): Promise<NextResponse> {
124105
const rawBody = await req.text();
125-
const signature = req.headers.get("sanity-webhook-signature");
126-
if (!isValidSignature(rawBody, signature)) {
106+
const signature = req.headers.get(SIGNATURE_HEADER_NAME);
107+
108+
if (!WEBHOOK_SECRET) {
109+
console.error("[sanity-distribute] Missing SANITY_WEBHOOK_SECRET");
110+
return NextResponse.json({ error: "Server misconfigured" }, { status: 500 });
111+
}
112+
113+
if (!signature || !(await isValidSignature(rawBody, signature, WEBHOOK_SECRET))) {
114+
console.log("[sanity-distribute] Invalid signature");
127115
return NextResponse.json({ error: "Invalid signature" }, { status: 401 });
128116
}
129117

0 commit comments

Comments
 (0)