File size: 3,907 Bytes
6a5e4c9
10b8070
 
69c6804
10b8070
 
2e4269d
 
 
 
10b8070
6a5e4c9
 
 
10b8070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69c6804
a1a6daf
10b8070
 
 
69c6804
10b8070
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { env } from "$env/dynamic/private";
import { collections } from "$lib/server/database.js";
import { toolFromConfigs } from "$lib/server/tools/index.js";
import { ReviewStatus } from "$lib/types/Review";
import type { CommunityToolDB } from "$lib/types/Tool.js";
import { ObjectId } from "mongodb";
import { editableToolSchema } from "$lib/server/tools/index.js";
import { generateSearchTokens } from "$lib/utils/searchTokens.js";
import { error } from "@sveltejs/kit";
import { requiresUser } from "$lib/server/auth";

export async function GET({ params }) {
	if (env.COMMUNITY_TOOLS !== "true") {
		return new Response("Community tools are not enabled", { status: 403 });
	}

	const toolId = params.toolId;

	try {
		const configTool = toolFromConfigs.find((el) => el._id.toString() === toolId);
		if (configTool) {
			return Response.json({
				_id: toolId,
				displayName: configTool.displayName,
				color: configTool.color,
				icon: configTool.icon,
				createdByName: undefined,
			});
		} else {
			// try community tools
			const tool = await collections.tools
				.findOne<CommunityToolDB>({ _id: new ObjectId(toolId) })
				.then((tool) =>
					tool
						? {
								_id: tool._id.toString(),
								displayName: tool.displayName,
								color: tool.color,
								icon: tool.icon,
								createdByName: tool.createdByName,
								review: tool.review,
							}
						: undefined
				);

			if (!tool || tool.review !== ReviewStatus.APPROVED) {
				return new Response(`Tool "${toolId}" not found`, { status: 404 });
			}

			return Response.json(tool);
		}
	} catch (e) {
		return new Response(`Tool "${toolId}" not found`, { status: 404 });
	}
}

export async function PATCH({ request, params, locals }) {
	const tool = await collections.tools.findOne({
		_id: new ObjectId(params.toolId),
	});

	if (!tool) {
		error(404, "Tool not found");
	}

	if (tool.createdById.toString() !== (locals.user?._id ?? locals.sessionId).toString()) {
		error(403, "You are not the creator of this tool");
	}

	// can only create tools when logged in, IF login is setup
	if (!locals.user && requiresUser) {
		const errors = [{ field: "description", message: "Must be logged in. Unauthorized" }];
		return new Response(JSON.stringify({ error: true, errors }), { status: 400 });
	}

	const body = await request.json();

	const parse = editableToolSchema.safeParse(body);

	if (!parse.success) {
		// Loop through the errors array and create a custom errors array
		const errors = parse.error.errors.map((error) => {
			return {
				field: error.path[0],
				message: error.message,
			};
		});

		return new Response(JSON.stringify({ error: true, errors }), { status: 400 });
	}

	// modify the tool
	await collections.tools.updateOne(
		{ _id: tool._id },
		{
			$set: {
				...parse.data,
				updatedAt: new Date(),
				searchTokens: generateSearchTokens(parse.data.displayName),
			},
		}
	);

	return new Response(JSON.stringify({ toolId: tool._id.toString() }), { status: 200 });
}

export async function DELETE({ params, locals }) {
	const tool = await collections.tools.findOne({ _id: new ObjectId(params.toolId) });

	if (!tool) {
		return new Response("Tool not found", { status: 404 });
	}

	if (
		tool.createdById.toString() !== (locals.user?._id ?? locals.sessionId).toString() &&
		!locals.user?.isAdmin
	) {
		return new Response("You are not the creator of this tool", { status: 403 });
	}

	await collections.tools.deleteOne({ _id: tool._id });

	// Remove the tool from all users' settings
	await collections.settings.updateMany(
		{
			tools: { $in: [tool._id.toString()] },
		},
		{
			$pull: { tools: tool._id.toString() },
		}
	);

	// Remove the tool from all assistants
	await collections.assistants.updateMany(
		{
			tools: { $in: [tool._id.toString()] },
		},
		{
			$pull: { tools: tool._id.toString() },
		}
	);

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