From cef32637229dd2afcf895667105f47366e056c1a Mon Sep 17 00:00:00 2001 From: regalijan Date: Thu, 19 Oct 2023 16:49:40 -0400 Subject: [PATCH] Create GME add endpoint --- functions/api/gme/add.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 functions/api/gme/add.ts diff --git a/functions/api/gme/add.ts b/functions/api/gme/add.ts new file mode 100644 index 0000000..ac42c46 --- /dev/null +++ b/functions/api/gme/add.ts @@ -0,0 +1,40 @@ +function makeResponse(body: string, status: number): Response { + return new Response(body, { + headers: { + "content-type": "application/json", + }, + status, + }); +} + +export async function onRequestPost(context: RequestContext) { + const { user } = context.data.body; + + if (!user) return makeResponse('{"error":"No user provided"}', 400); + + const existingUser = await context.env.DATA.get(`gamemod_${user}`); + + if (existingUser) + return makeResponse('{"error":"Cannot add an existing user"}', 400); + + if ( + ["165594923586945025", "289372404541554689", "396347223736057866"].includes( + user + ) + ) + return new Response(null, { + status: 204, + }); + + if (!user.match(/^\d{17,19}$/)) + return makeResponse('{"error":"Invalid User ID"}', 400); + + await context.env.DATA.put( + `gamemod_${user}`, + JSON.stringify({ time: Date.now(), user: context.data.current_user.id }) + ); + + return new Response(null, { + status: 204, + }); +}