File size: 1,666 Bytes
786115c 08e8583 25a7ba5 786115c 69684cd 786115c 25a7ba5 08e8583 69684cd 08e8583 786115c 25a7ba5 881070b 69684cd 25a7ba5 786115c 25a7ba5 786115c 25a7ba5 786115c 25a7ba5 2128ce0 25a7ba5 786115c |
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 |
import { base } from "$app/paths";
import { ENABLE_ASSISTANTS } from "$env/static/private";
import { collections } from "$lib/server/database.js";
import type { Assistant } from "$lib/types/Assistant";
import { error, redirect } from "@sveltejs/kit";
import type { Filter } from "mongodb";
const NUM_PER_PAGE = 24;
export const load = async ({ url, locals }) => {
if (!ENABLE_ASSISTANTS) {
throw redirect(302, `${base}/`);
}
const modelId = url.searchParams.get("modelId");
const pageIndex = parseInt(url.searchParams.get("p") ?? "0");
const createdByName = url.searchParams.get("user");
const createdByCurrentUser = locals.user?.username && locals.user.username === createdByName;
if (createdByName) {
const existingUser = await collections.users.findOne({ username: createdByName });
if (!existingUser) {
throw error(404, `User "${createdByName}" doesn't exist`);
}
}
// fetch the top assistants sorted by user count from biggest to smallest, filter out all assistants with only 1 users. filter by model too if modelId is provided
const filter: Filter<Assistant> = {
...(modelId && { modelId }),
...(!createdByCurrentUser && { userCount: { $gt: 1 } }),
...(createdByName ? { createdByName } : { featured: true }),
};
const assistants = await collections.assistants
.find(filter)
.skip(NUM_PER_PAGE * pageIndex)
.sort({ userCount: -1 })
.limit(NUM_PER_PAGE)
.toArray();
const numTotalItems = await collections.assistants.countDocuments(filter);
return {
assistants: JSON.parse(JSON.stringify(assistants)) as Array<Assistant>,
selectedModel: modelId ?? "",
numTotalItems,
numItemsPerPage: NUM_PER_PAGE,
};
};
|