Files
Compass/backend/api/src/report.ts
Martin Braquet ba9b3cfb06 Add pretty formatting (#29)
* Test

* Add pretty formatting

* Fix Tests

* Fix Tests

* Fix Tests

* Fix

* Add pretty formatting fix

* Fix

* Test

* Fix tests

* Clean typeckech

* Add prettier check

* Fix api tsconfig

* Fix api tsconfig

* Fix tsconfig

* Fix

* Fix

* Prettier
2026-02-20 17:32:27 +01:00

68 lines
2.2 KiB
TypeScript

import {sendDiscordMessage} from 'common/discord/core'
import {DOMAIN} from 'common/envs/constants'
import {Row} from 'common/supabase/utils'
import {tryCatch} from 'common/util/try-catch'
import {createSupabaseDirectClient} from 'shared/supabase/init'
import {insert} from 'shared/supabase/utils'
import {APIError, APIHandler} from './helpers/endpoint'
// abusable: people can report the wrong person, that didn't write the comment
// but in practice we check it manually and nothing bad happens to them automatically
export const report: APIHandler<'report'> = async (body, auth) => {
const {contentOwnerId, contentType, contentId, description, parentId, parentType} = body
const pg = createSupabaseDirectClient()
const result = await tryCatch(
insert(pg, 'reports', {
user_id: auth.uid,
content_owner_id: contentOwnerId,
content_type: contentType,
content_id: contentId,
description,
parent_id: parentId,
parent_type: parentType,
}),
)
if (result.error) {
throw new APIError(500, 'Failed to create report: ' + result.error.message)
}
const continuation = async () => {
try {
const {data: reporter, error} = await tryCatch(
pg.oneOrNone<Row<'users'>>('select * from users where id = $1', [auth.uid]),
)
if (error) {
console.error('Failed to get user for report', error)
return
}
const {data: reported, error: userError} = await tryCatch(
pg.oneOrNone<Row<'users'>>('select * from users where id = $1', [contentOwnerId]),
)
if (userError) {
console.error('Failed to get reported user for report', userError)
return
}
const message: string = `
🚨 **New Report** 🚨
**Type:** ${contentType}
**Content ID:** ${contentId}
**Reporter:** ${reporter?.name} ([@${reporter?.username}](https://www.${DOMAIN}/${reporter?.username}))
**Reported:** ${reported?.name} ([@${reported?.username}](https://www.${DOMAIN}/${reported?.username}))
`
await sendDiscordMessage(message, 'reports')
} catch (e) {
console.error('Failed to send discord reports', e)
}
}
return {
success: true,
result: {},
continue: continuation,
}
}