Greatly reduce repeated code
This commit is contained in:
@ -1,3 +1,5 @@
|
||||
import { jsonError } from "../../../common.js";
|
||||
|
||||
export async function onRequestPost(context: RequestContext) {
|
||||
const { pathname } = new URL(context.request.url);
|
||||
|
||||
@ -8,12 +10,7 @@ export async function onRequestPost(context: RequestContext) {
|
||||
const { permissions } = context.data.current_user;
|
||||
|
||||
if (!(permissions & (1 << 0)) && !(permissions & (1 << 11)))
|
||||
return new Response('{"error":"Forbidden"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 403,
|
||||
});
|
||||
return jsonError("Forbidden", 403);
|
||||
|
||||
const { body } = context.data;
|
||||
const id = context.params.id as string;
|
||||
@ -23,13 +20,7 @@ export async function onRequestPost(context: RequestContext) {
|
||||
if (!pathname.endsWith("/ban")) {
|
||||
const key = await context.env.DATA.get(`appeal_${id}`);
|
||||
|
||||
if (!key)
|
||||
return new Response('{"error":"No appeal with that ID exists"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 404,
|
||||
});
|
||||
if (!key) return jsonError("No appeal with that ID exists", 404);
|
||||
|
||||
context.data.appeal = JSON.parse(key);
|
||||
}
|
||||
@ -38,12 +29,7 @@ export async function onRequestPost(context: RequestContext) {
|
||||
body.feedback &&
|
||||
(typeof body.feedback !== "string" || body.feedback.length > 512)
|
||||
)
|
||||
return new Response('{"error":"Invalid feedback"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 400,
|
||||
});
|
||||
return jsonError("Invalid feedback", 400);
|
||||
|
||||
return await context.next();
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
import { jsonError } from "../../../common.js";
|
||||
|
||||
export async function onRequestPost(context: RequestContext) {
|
||||
const { appeal } = context.data;
|
||||
const body = new FormData();
|
||||
@ -20,12 +22,7 @@ export async function onRequestPost(context: RequestContext) {
|
||||
|
||||
if (!emailReq.ok) {
|
||||
console.log(await emailReq.json());
|
||||
return new Response('{"error":"Failed to accept appeal"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 500,
|
||||
});
|
||||
return jsonError("Failed to accept appeal", 500);
|
||||
}
|
||||
|
||||
const { current_user: currentUser } = context.data;
|
||||
|
@ -1,13 +1,10 @@
|
||||
import { jsonError } from "../../../common.js";
|
||||
|
||||
export async function onRequestPost(context: RequestContext) {
|
||||
const { current_user: currentUser } = context.data;
|
||||
|
||||
if (context.data.targetId.search(/^\d{16,19}$/) === -1)
|
||||
return new Response('{"error":"Invalid target id"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 400,
|
||||
});
|
||||
return jsonError("Invalid target id", 400);
|
||||
|
||||
await context.env.D1.prepare(
|
||||
"INSERT INTO appeal_bans (created_at, created_by, user) VALUES (?, ?, ?);",
|
||||
|
@ -1,3 +1,5 @@
|
||||
import { jsonError } from "../../../common.js";
|
||||
|
||||
export async function onRequestPost(context: RequestContext) {
|
||||
const { appeal } = context.data;
|
||||
const body = new FormData();
|
||||
@ -20,12 +22,7 @@ export async function onRequestPost(context: RequestContext) {
|
||||
|
||||
if (!emailReq.ok) {
|
||||
console.log(await emailReq.json());
|
||||
return new Response('{"error":"Failed to deny appeal"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 500,
|
||||
});
|
||||
return jsonError("Failed to deny appeal", 500);
|
||||
}
|
||||
|
||||
await context.env.D1.prepare("UPDATE appeals SET open = 0 WHERE id = ?;")
|
||||
|
@ -1,11 +1,7 @@
|
||||
import { jsonError } from "../../common.js";
|
||||
|
||||
export async function onRequest(context: RequestContext) {
|
||||
if (!context.data.current_user)
|
||||
return new Response('{"error":"Not logged in"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 401,
|
||||
});
|
||||
if (!context.data.current_user) return jsonError("Not logged in", 401);
|
||||
|
||||
return await context.next();
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
import { jsonError } from "../../common.js";
|
||||
|
||||
export async function onRequestPost(context: RequestContext) {
|
||||
const { learned, whyBanned, whyUnban } = context.data.body;
|
||||
|
||||
@ -12,25 +14,11 @@ export async function onRequestPost(context: RequestContext) {
|
||||
!whyUnban.length ||
|
||||
whyUnban.length > 2000
|
||||
)
|
||||
return new Response(
|
||||
'{"error":"One or more fields are missing or invalid"}',
|
||||
{
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 400,
|
||||
},
|
||||
);
|
||||
return jsonError("One or more fields are missing or invalid", 400);
|
||||
|
||||
const { current_user: currentUser } = context.data;
|
||||
|
||||
if (!currentUser.email)
|
||||
return new Response('{"error":"No email for this session"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 403,
|
||||
});
|
||||
if (!currentUser.email) return jsonError("No email for this session", 403);
|
||||
|
||||
const existingAppeals = await context.env.DATA.list({
|
||||
prefix: `appeal_${currentUser.id}`,
|
||||
@ -45,12 +33,7 @@ export async function onRequestPost(context: RequestContext) {
|
||||
(appeal) => (appeal.metadata as { [k: string]: any })?.open,
|
||||
)
|
||||
)
|
||||
return new Response('{"error":"Appeal already submitted"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 403,
|
||||
});
|
||||
return jsonError("Appeal already submitted", 403);
|
||||
|
||||
if (
|
||||
await context.env.D1.prepare("SELECT * FROM appeal_bans WHERE user = ?;")
|
||||
|
@ -1,22 +1,14 @@
|
||||
import { jsonError } from "../../common.js";
|
||||
|
||||
export async function onRequestPost(context: RequestContext) {
|
||||
const { active } = context.data.body;
|
||||
const { permissions } = context.data.current_user;
|
||||
|
||||
if (!(permissions & (1 << 0)) && !(permissions & (1 << 11)))
|
||||
return new Response('{"error":"Forbidden"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 403,
|
||||
});
|
||||
return jsonError("Forbidden", 403);
|
||||
|
||||
if (typeof active !== "boolean")
|
||||
return new Response('{"error":"Active property must be a boolean"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 400,
|
||||
});
|
||||
return jsonError("Active property must be a boolean", 400);
|
||||
|
||||
if (active) {
|
||||
await context.env.DATA.delete("appeal_disabled");
|
||||
|
Reference in New Issue
Block a user