Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Properly handle multiple jobs per report
  • Loading branch information
regalijan committed Mar 24, 2024
1 parent 5546993 commit d3f5a1a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
45 changes: 45 additions & 0 deletions functions/api/coconut.ts
@@ -0,0 +1,45 @@
import { jsonError } from "../common.js";

export async function onRequestPost(context: RequestContext) {
const { searchParams } = new URL(context.request.url);
const id = searchParams.get("attachment");
const token = searchParams.get("token");

if (!id || !token) return jsonError("Invalid report id or token", 400);

const coconutData: { token: string } | null = await context.env.DATA.get(
`coconutjob_${id}`,
{ type: "json" },
);

if (!coconutData)
return jsonError("Request is stale or otherwise invalid", 400);

if (coconutData.token !== token) return jsonError("Forbidden", 403);

await context.env.DATA.delete(`coconutjob_${id}`);

const { event } = await context.request.json();

if (event === "job.failed") {
await fetch(context.env.REPORTS_WEBHOOK, {
body: JSON.stringify({
embeds: [
{
title: "Transcoding Failed",
color: 16711680,
description: `Attachment ${id} could not be transcoded, please log in to Coconut to see what went wrong.`,
},
],
}),
headers: {
"content-type": "application/json",
},
method: "POST",
});
}

return new Response(null, {
status: 204,
});
}
22 changes: 14 additions & 8 deletions functions/api/reports/complete.ts
Expand Up @@ -21,18 +21,19 @@ export async function onRequestPost(context: RequestContext) {

if (!value) return jsonError("Report is missing", 500);

const coconutData = (await context.env.DATA.get(`coconutjob_${id}`, {
const coconutData = (await context.env.DATA.get(`coconutdata_${id}`, {
type: "json",
})) as {
attachments: string[];
token: string;
} | null;

if (coconutData) {
const coconutResponsePromises = [];
const responsePromises = [];

for (const attachment of coconutData.attachments)
coconutResponsePromises.push(
for (const attachment of coconutData.attachments) {
const token = crypto.randomUUID();

responsePromises.push(
fetch("https://api.coconut.co/v2/jobs", {
body: JSON.stringify({
input: {
Expand All @@ -48,8 +49,8 @@ export async function onRequestPost(context: RequestContext) {
},
notification: {
params: {
report_id: id,
token: coconutData.token,
attachment: attachment,
token,
},
type: "http",
url: `https://${context.request.headers.get("host")}/api/coconut`,
Expand All @@ -76,9 +77,14 @@ export async function onRequestPost(context: RequestContext) {
},
method: "POST",
}),
context.env.DATA.put(`coconutjob_${attachment}`, token, {
expirationTtl: 600,
}),
);
}

await Promise.allSettled(coconutResponsePromises);
await Promise.allSettled(responsePromises);
await context.env.DATA.delete(`coconutdata_${id}`);
}

if (context.env.REPORTS_WEBHOOK) {
Expand Down
3 changes: 1 addition & 2 deletions functions/api/reports/submit.ts
Expand Up @@ -180,10 +180,9 @@ export async function onRequestPost(context: RequestContext) {
const { current_user: currentUser } = context.data;
if (filesToProcess.length)
await context.env.DATA.put(
`coconutjob_${reportId}`,
`coconutdata_${reportId}`,
JSON.stringify({
attachments: filesToProcess,
token: crypto.randomUUID(),
}),
{
expirationTtl: 1800,
Expand Down

0 comments on commit d3f5a1a

Please sign in to comment.