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,5 +1,6 @@
import { insertLogs } from "../../../gcloud.js";
import { getBanList, setBanList } from "../../../roblox-open-cloud.js";
import { insertLogs } from "../../../gcloud.js";
import { jsonError } from "../../../common.js";
export async function onRequestPost(context: RequestContext) {
const actionMap = context.data.body;
@ -13,11 +14,7 @@ export async function onRequestPost(context: RequestContext) {
action < 0 ||
action > 2
)
return new Response('{"error":"Invalid action map"}', {
headers: {
"content-type": "application/json",
},
});
return jsonError("Invalid action map", 400);
if (action === 0) continue;

View File

@ -1,13 +1,9 @@
import { jsonError } from "../../common.js";
export async function onRequestPost(context: RequestContext) {
const { id } = context.data.body;
if (!id)
return new Response('{"error":"No ID provided"}', {
headers: {
"content-type": "application/json",
},
status: 400,
});
if (!id) return jsonError("No ID provided", 400);
const user = await context.env.DATA.get(`reportprocessing_${id}`);
@ -17,24 +13,13 @@ export async function onRequestPost(context: RequestContext) {
? user !== context.data.current_user.id
: user !== context.request.headers.get("CF-Connecting-IP"))
)
return new Response('{"error":"No report with that ID is processing"}', {
headers: {
"content-type": "application/json",
},
status: 404,
});
return jsonError("No report with that ID is processing", 404);
await context.env.DATA.delete(`reportprocessing_${id}`);
const value = await context.env.DATA.get(`report_${id}`);
if (!value)
return new Response('{"error":"Report is missing"}', {
headers: {
"content-type": "application/json",
},
status: 500,
});
if (!value) return jsonError("Report is missing", 500);
if (context.env.REPORTS_WEBHOOK) {
await fetch(context.env.REPORTS_WEBHOOK, {

View File

@ -1,13 +1,9 @@
import { jsonError } from "../../common.js";
export async function onRequestPost(context: RequestContext) {
const { id } = context.data.body;
if (!id)
return new Response('{"error":"No ID provided"}', {
headers: {
"content-type": "application/json",
},
status: 400,
});
if (!id) return jsonError("No ID provided", 400);
const reportUserId = await context.env.DATA.get(`reportprocessing_${id}`);
@ -16,12 +12,7 @@ export async function onRequestPost(context: RequestContext) {
(context.data.current_user?.id !== reportUserId &&
context.request.headers.get("CF-Connecting-IP") !== reportUserId)
)
return new Response('{"error":"No processing report with that ID found"}', {
headers: {
"content-type": "application/json",
},
status: 404,
});
return jsonError("No processing report with that ID found", 404);
await context.env.DATA.delete(`report_${id}`);

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",
},
},
);
}