From 3bdc2e23602d09a6c93954bf0360c3b3750fe507 Mon Sep 17 00:00:00 2001 From: Lncvrt Date: Sun, 25 Jan 2026 17:08:52 -0700 Subject: [PATCH] Finish report endpoint --- src/index.ts | 23 +++++- src/routes/berrydash/chatroom/report/post.ts | 87 ++++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 src/routes/berrydash/chatroom/report/post.ts diff --git a/src/index.ts b/src/index.ts index 4b653d8..6c8bcaf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,7 @@ import dotenv from 'dotenv' import swagger from '@elysiajs/swagger' import { berryDashChats, berryDashUserData, users } from './lib/tables' import { and, desc, eq } from 'drizzle-orm' +import { checkAuthorization } from './lib/bd/auth' import { handler as getVerifyCodeHandler } from './routes/get-verify-code' @@ -41,7 +42,8 @@ import { handler as berryDashAccountChangeUsernamePostHandler } from './routes/b import { handler as berryDashAccountChangePasswordPostHandler } from './routes/berrydash/account/change-password/post' import { handler as berryDashAccountSaveGetHandler } from './routes/berrydash/account/save/get' import { handler as berryDashAccountSavePostHandler } from './routes/berrydash/account/save/post' -import { checkAuthorization } from './lib/bd/auth' + +import { handler as berryDashChatroomReportPostHandler } from './routes/berrydash/chatroom/report/post' dotenv.config({ quiet: true }) @@ -839,6 +841,25 @@ app.get( }) } ) +app.post( + '/berrydash/chatroom/report', + context => berryDashChatroomReportPostHandler(context), + { + detail: { + description: 'The endpoint for getting a specific icon marketplace icon.', + tags: ['Berry Dash', 'Chatroom'] + }, + body: t.Object({ + id: t.String(), + reason: t.String() + }), + headers: t.Object({ + authorization: t.String({ + description: 'This is your Berry Dash session token' + }) + }) + } +) app.post( '/berrydash/account/login', context => berryDashAccountLoginPostHandler(context), diff --git a/src/routes/berrydash/chatroom/report/post.ts b/src/routes/berrydash/chatroom/report/post.ts new file mode 100644 index 0000000..e122da2 --- /dev/null +++ b/src/routes/berrydash/chatroom/report/post.ts @@ -0,0 +1,87 @@ +import { Context } from 'elysia' +import { + getClientIp, + getDatabaseConnection, + jsonResponse +} from '../../../../lib/util' +import { berryDashChatroomReports } from '../../../../lib/tables' +import { checkAuthorization } from '../../../../lib/bd/auth' +import { and, eq } from 'drizzle-orm' + +type Body = { + id: string + reason: string +} + +export async function handler (context: Context) { + const dbInfo0 = getDatabaseConnection(0) + const dbInfo1 = getDatabaseConnection(1) + + if (!dbInfo0 || !dbInfo1) + return jsonResponse( + { success: false, message: 'Failed to connect to database' }, + 500 + ) + const { connection: connection0, db: db0 } = dbInfo1 + const { connection: connection1, db: db1 } = dbInfo1 + + const ip = getClientIp(context) + const authorizationToken = context.headers.authorization + const authResult = await checkAuthorization( + authorizationToken as string, + db1, + db0, + ip + ) + if (!authResult.valid) { + connection0.end() + connection1.end() + return jsonResponse({ success: false, message: 'Unauthorized' }, 401) + } + const userId = authResult.id + + const body = context.body as Body + const id = parseInt(body.id, 10) + if (isNaN(id)) { + connection0.end() + connection1.end() + return jsonResponse( + { success: false, message: 'No valid ID provided' }, + 400 + ) + } + + const result = await db1 + .select() + .from(berryDashChatroomReports) + .where( + and( + eq(berryDashChatroomReports.chatId, id), + eq(berryDashChatroomReports.userId, userId) + ) + ) + .execute() + if (result[0]) { + connection0.end() + connection1.end() + return jsonResponse( + { success: false, message: 'You already reported this message' }, + 400 + ) + } + + await db1 + .insert(berryDashChatroomReports) + .values({ + chatId: id, + userId, + reason: btoa(body.reason), + timestamp: Math.floor(Date.now() / 1000) + }) + .execute() + + connection0.end() + connection1.end() + + return jsonResponse({ success: true, message: null }, 200) +}