Add webhook to report to discord

This commit is contained in:
MartinBraquet
2025-10-16 20:59:46 +02:00
parent e22f50ecd3
commit 8ea6c406e0
4 changed files with 47 additions and 6 deletions

View File

@@ -1,7 +1,10 @@
import { APIError, APIHandler } from './helpers/endpoint'
import { createSupabaseDirectClient } from 'shared/supabase/init'
import { tryCatch } from 'common/util/try-catch'
import { insert } from 'shared/supabase/utils'
import {APIError, APIHandler} from './helpers/endpoint'
import {createSupabaseDirectClient} from 'shared/supabase/init'
import {tryCatch} from 'common/util/try-catch'
import {insert} from 'shared/supabase/utils'
import {sendDiscordMessage} from "common/discord/core";
import {Row} from "common/supabase/utils";
import {DOMAIN} from "common/envs/constants";
// 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
@@ -33,5 +36,38 @@ export const report: APIHandler<'report'> = async (body, auth) => {
throw new APIError(500, 'Failed to create report: ' + result.error.message)
}
return { success: true }
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
}
let 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,
}
}

View File

@@ -5,10 +5,13 @@ export const sendDiscordMessage = async (content: string, channel: string) => {
members: process.env.DISCORD_WEBHOOK_MEMBERS,
general: process.env.DISCORD_WEBHOOK_GENERAL,
health: process.env.DISCORD_WEBHOOK_HEALTH,
reports: process.env.DISCORD_WEBHOOK_REPORTS,
}[channel]
if (IS_DEV) webhookUrl = process.env.DISCORD_WEBHOOK_DEV
// console.log(`Discord webhook URL: ${webhookUrl}`, channel, content)
if (!webhookUrl) return
const response = await fetch(webhookUrl!, {

View File

@@ -21,6 +21,7 @@ export const secrets = (
'DISCORD_WEBHOOK_MEMBERS',
'DISCORD_WEBHOOK_GENERAL',
'DISCORD_WEBHOOK_HEALTH',
'DISCORD_WEBHOOK_REPORTS',
// Some typescript voodoo to keep the string literal types while being not readonly.
] as const
).concat()

View File

@@ -7,6 +7,7 @@ import { Button } from 'web/components/buttons/button'
import Textarea from 'react-expanding-textarea'
import { toast } from 'react-hot-toast'
import { api } from 'web/lib/api'
import {randomString} from "common/util/random";
export const ReportUser = (props: { user: User; closeModal: () => void }) => {
const { user, closeModal } = props
@@ -31,7 +32,7 @@ export const ReportUser = (props: { user: User; closeModal: () => void }) => {
.promise(
api('report', {
contentType: 'user',
contentId: user.id,
contentId: randomString(16),
contentOwnerId: user.id,
description:
'Reasons: ' + [...selectedReportTypes, otherReportType].join(', '),