Greatly reduce repeated code

This commit is contained in:
2023-10-19 16:50:48 -04:00
parent 47e639be43
commit dd2d9f2672
34 changed files with 196 additions and 481 deletions

View File

@ -1,13 +1,5 @@
import { GenerateUploadURL } from "../../gcloud.js";
function errorResponse(error: string, status: number): Response {
return new Response(JSON.stringify({ error }), {
headers: {
"content-type": "application/json",
},
status,
});
}
import { jsonError, jsonResponse } from "../../common.js";
export async function onRequestPost(context: RequestContext) {
const { actions, bypass, description, files, turnstileResponse, usernames } =
@ -15,7 +7,7 @@ export async function onRequestPost(context: RequestContext) {
if (!context.data.current_user) {
if (typeof turnstileResponse !== "string")
return errorResponse("You must complete the captcha", 401);
return jsonError("You must complete the captcha", 401);
const turnstileAPIResponse = await fetch(
"https://challenges.cloudflare.com/turnstile/v0/siteverify",
@ -34,26 +26,26 @@ export async function onRequestPost(context: RequestContext) {
const { success }: { success: boolean } = await turnstileAPIResponse.json();
if (!success) return errorResponse("Captcha test failed", 403);
if (!success) return jsonError("Captcha test failed", 403);
}
const origin = context.request.headers.get("Origin");
if (!origin) return errorResponse("No origin header", 400);
if (!origin) return jsonError("No origin header", 400);
if (bypass && !(context.data.current_user?.permissions & (1 << 5)))
return errorResponse("Bypass directive cannot be used", 403);
return jsonError("Bypass directive cannot be used", 403);
if (typeof bypass !== "boolean")
return errorResponse("Bypass must be a boolean", 400);
return jsonError("Bypass must be a boolean", 400);
if (!Array.isArray(usernames))
return errorResponse("Usernames must be type of array", 400);
return jsonError("Usernames must be type of array", 400);
if (
!["string", "undefined"].includes(typeof description) ||
description?.length > 512
)
return errorResponse("Invalid description", 400);
return jsonError("Invalid description", 400);
if (
!Array.isArray(files) ||
@ -63,7 +55,7 @@ export async function onRequestPost(context: RequestContext) {
return !keys.includes("name") || !keys.includes("size");
})
)
return errorResponse("File list missing name(s) and/or size(s)", 400);
return jsonError("File list missing name(s) and/or size(s)", 400);
if (
files.find(
@ -74,13 +66,10 @@ export async function onRequestPost(context: RequestContext) {
file.size > 536870912,
)
)
return errorResponse(
"One or more files contain an invalid name or size",
400,
);
return jsonError("One or more files contain an invalid name or size", 400);
if (!usernames.length || usernames.length > 20)
return errorResponse(
return jsonError(
"Number of usernames provided must be between 1 and 20",
400,
);
@ -91,7 +80,7 @@ export async function onRequestPost(context: RequestContext) {
username.length > 20 ||
username.match(/_/g)?.length > 1
)
return errorResponse(`Username "${username}" is invalid`, 400);
return jsonError(`Username "${username}" is invalid`, 400);
}
const rbxSearchReq = await fetch(
@ -109,7 +98,7 @@ export async function onRequestPost(context: RequestContext) {
);
if (!rbxSearchReq.ok)
return errorResponse(
return jsonError(
"Failed to locate Roblox users due to upstream error",
500,
);
@ -125,7 +114,7 @@ export async function onRequestPost(context: RequestContext) {
missingUsers.push(userData.requestedUsername);
}
return errorResponse(
return jsonError(
`The following users do not exist or are banned from Roblox: ${missingUsers.toString()}`,
400,
);
@ -165,7 +154,7 @@ export async function onRequestPost(context: RequestContext) {
"webp",
].includes(fileExten.toLowerCase())
)
return errorResponse(
return jsonError(
`File ${file.name} cannot be uploaded as it is unsupported`,
415,
);
@ -202,7 +191,7 @@ export async function onRequestPost(context: RequestContext) {
);
if (uploadUrlResults.find((uploadUrl) => uploadUrl.status === "rejected"))
return errorResponse("Failed to generate upload url", 500);
return jsonError("Failed to generate upload url", 500);
const attachments: string[] = [];
const uploadUrls: string[] = [];
@ -251,12 +240,7 @@ export async function onRequestPost(context: RequestContext) {
.run();
} catch {}
return new Response(
return jsonResponse(
JSON.stringify({ id: reportId, upload_urls: uploadUrls }),
{
headers: {
"content-type": "application/json",
},
},
);
}