Finish report endpoint
This commit is contained in:
23
src/index.ts
23
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),
|
||||
|
||||
87
src/routes/berrydash/chatroom/report/post.ts
Normal file
87
src/routes/berrydash/chatroom/report/post.ts
Normal file
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user