File size: 2,988 Bytes
6a5e4c9
0c3e3b2
d4471e3
0c3e3b2
 
69c6804
0c3e3b2
 
 
 
 
 
 
 
 
6a5e4c9
 
0c3e3b2
 
 
 
 
 
 
 
 
2adc19f
0c3e3b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2adc19f
 
69c6804
0c3e3b2
 
 
 
 
 
 
 
 
 
 
d4471e3
 
0c3e3b2
 
 
 
 
 
 
 
 
 
 
 
decd9b1
0c3e3b2
 
 
 
 
 
 
 
d4471e3
0c3e3b2
 
 
 
 
 
 
2adc19f
0c3e3b2
 
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
import { env } from "$env/dynamic/private";
import { authCondition } from "$lib/server/auth.js";
import { collections } from "$lib/server/database.js";
import { toolFromConfigs } from "$lib/server/tools/index.js";
import { SortKey } from "$lib/types/Assistant.js";
import { ReviewStatus } from "$lib/types/Review";
import type { CommunityToolDB } from "$lib/types/Tool.js";
import type { User } from "$lib/types/User.js";
import { generateQueryTokens, generateSearchTokens } from "$lib/utils/searchTokens.js";
import { error } from "@sveltejs/kit";
import { ObjectId, type Filter } from "mongodb";

const NUM_PER_PAGE = 16;

export const load = async ({ url, locals }) => {
	if (env.COMMUNITY_TOOLS !== "true") {
		error(403, "Community tools are not enabled");
	}

	const username = url.searchParams.get("user");
	const query = url.searchParams.get("q")?.trim() ?? null;

	const pageIndex = parseInt(url.searchParams.get("p") ?? "0");
	const sort = url.searchParams.get("sort")?.trim() ?? SortKey.TRENDING;
	const createdByCurrentUser = locals.user?.username && locals.user.username === username;
	const activeOnly = url.searchParams.get("active") === "true";
	const showUnfeatured = url.searchParams.get("showUnfeatured") === "true";

	let user: Pick<User, "_id"> | null = null;
	if (username) {
		user = await collections.users.findOne<Pick<User, "_id">>(
			{ username },
			{ projection: { _id: 1 } }
		);
		if (!user) {
			error(404, `User "${username}" doesn't exist`);
		}
	}

	const settings = await collections.settings.findOne(authCondition(locals));

	if (!settings && activeOnly) {
		error(404, "No user settings found");
	}

	const queryTokens = !!query && generateQueryTokens(query);

	const filter: Filter<CommunityToolDB> = {
		...(!createdByCurrentUser &&
			!activeOnly &&
			!(locals.user?.isAdmin && showUnfeatured) && { review: ReviewStatus.APPROVED }),
		...(user && { createdById: user._id }),
		...(queryTokens && { searchTokens: { $all: queryTokens } }),
		...(activeOnly && {
			_id: {
				$in: (settings?.tools ?? []).map((key) => {
					return new ObjectId(key);
				}),
			},
		}),
	};

	const communityTools = await collections.tools
		.find(filter)
		.skip(NUM_PER_PAGE * pageIndex)
		.sort({
			...(sort === SortKey.TRENDING && { last24HoursUseCount: -1 }),
			useCount: -1,
		})
		.limit(NUM_PER_PAGE)
		.toArray();

	const configTools = toolFromConfigs
		.filter((tool) => !tool?.isHidden)
		.filter((tool) => {
			if (queryTokens) {
				return generateSearchTokens(tool.displayName).some((token) =>
					queryTokens.some((queryToken) => queryToken.test(token))
				);
			}
			return true;
		});

	const tools = [...(pageIndex == 0 && !username ? configTools : []), ...communityTools];

	const numTotalItems = (await collections.tools.countDocuments(filter)) + toolFromConfigs.length;

	return {
		tools: JSON.parse(JSON.stringify(tools)) as CommunityToolDB[],
		numTotalItems,
		numItemsPerPage: NUM_PER_PAGE,
		query,
		sort,
		showUnfeatured,
	};
};