Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move userid validator to ban endpoint
  • Loading branch information
regalijan committed Oct 19, 2023
1 parent debb8d9 commit 75c38e9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
22 changes: 14 additions & 8 deletions functions/api/appeals/[id]/_middleware.ts
Expand Up @@ -12,16 +12,22 @@ export async function onRequestPost(context: RequestContext) {
const { body } = context.data;
const id = context.params.id as string;

if (id.search(/^\d{16,19}$/) === -1)
return new Response('{"error":"Invalid target id"}', {
headers: {
"content-type": "application/json",
},
status: 400,
});

context.data.targetId = id;

if (!new URL(context.request.url).pathname.endsWith("/ban")) {
const keyWithMeta = await context.env.DATA.getWithMetadata(`appeal_${id}`);

if (!keyWithMeta.value)
return new Response('{"error":"No appeal with that ID exists"}', {
headers: {
"content-type": "application/json",
},
status: 404,
});

context.data.appeal = keyWithMeta;
}

if (
body.feedback &&
(typeof body.feedback !== "string" || body.feedback.length > 512)
Expand Down
8 changes: 8 additions & 0 deletions functions/api/appeals/[id]/ban.ts
@@ -1,6 +1,14 @@
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,
});

await context.env.DATA.put(
`appealban_${context.data.targetId}`,
JSON.stringify({ moderator: currentUser.id })
Expand Down

0 comments on commit 75c38e9

Please sign in to comment.