File size: 1,932 Bytes
2e4269d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { base } from "$app/paths";

import { collections } from "$lib/server/database";
import { error } from "@sveltejs/kit";
import { ObjectId } from "mongodb";
import { z } from "zod";

import { env } from "$env/dynamic/private";
import { env as envPublic } from "$env/dynamic/public";
import { sendSlack } from "$lib/server/sendSlack";
import type { Tool } from "$lib/types/Tool";

export async function POST({ params, request, locals, url }) {
	// is there already a report from this user for this model ?
	const report = await collections.reports.findOne({
		createdBy: locals.user?._id ?? locals.sessionId,
		object: "tool",
		contentId: new ObjectId(params.toolId),
	});

	if (report) {
		return error(400, "Already reported");
	}

	const { reason } = z.object({ reason: z.string().min(1).max(128) }).parse(await request.json());

	if (!reason) {
		return error(400, "Invalid report reason");
	}

	const { acknowledged } = await collections.reports.insertOne({
		_id: new ObjectId(),
		contentId: new ObjectId(params.toolId),
		object: "tool",
		createdBy: locals.user?._id ?? locals.sessionId,
		createdAt: new Date(),
		updatedAt: new Date(),
		reason,
	});

	if (!acknowledged) {
		return error(500, "Failed to report tool");
	}

	if (env.WEBHOOK_URL_REPORT_ASSISTANT) {
		const prefixUrl =
			envPublic.PUBLIC_SHARE_PREFIX || `${envPublic.PUBLIC_ORIGIN || url.origin}${base}`;
		const toolUrl = `${prefixUrl}/tools/${params.toolId}`;

		const tool = await collections.tools.findOne<Pick<Tool, "displayName" | "name">>(
			{ _id: new ObjectId(params.toolId) },
			{ projection: { displayName: 1, name: 1 } }
		);

		const username = locals.user?.username;

		await sendSlack(
			`🔴 Tool <${toolUrl}|${tool?.displayName ?? tool?.name}> reported by ${
				username ? `<http://hf.co/${username}|${username}>` : "non-logged in user"
			}.\n\n> ${reason}`
		);
	}

	return new Response("Tool reported", { status: 200 });
}