Spaces:
Running
Running
File size: 3,006 Bytes
797e348 a8a9533 d4471e3 1b76365 0523dc6 5340dfb f66535d 0c913b6 69c6804 0c913b6 797e348 9a16516 a8a9533 06feee8 797e348 0c913b6 0523dc6 5340dfb e96b46c 5ca011a c3aee90 f66535d 0523dc6 06feee8 f66535d 797e348 55af7c5 8af99d6 55af7c5 c3aee90 55af7c5 8af99d6 c49fd55 f4ea1d0 c49fd55 0c913b6 f387d23 0f622fd 5340dfb c49fd55 0f622fd 0c913b6 c49fd55 d4471e3 1b76365 ce61e40 7701400 1b76365 5f03c37 0c913b6 797e348 d4471e3 0c913b6 9b6bf77 0c913b6 5340dfb 1b76365 5ca011a 0c913b6 797e348 |
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 |
import { base } from "$app/paths";
import { env } from "$env/dynamic/private";
import { collections } from "$lib/server/database.js";
import { SortKey, type Assistant } from "$lib/types/Assistant";
import type { User } from "$lib/types/User";
import { generateQueryTokens } from "$lib/utils/searchTokens.js";
import { error, redirect } from "@sveltejs/kit";
import type { Filter } from "mongodb";
import { ReviewStatus } from "$lib/types/Review";
const NUM_PER_PAGE = 24;
export const load = async ({ url, locals }) => {
if (!env.ENABLE_ASSISTANTS) {
redirect(302, `${base}/`);
}
const modelId = url.searchParams.get("modelId");
const pageIndex = parseInt(url.searchParams.get("p") ?? "0");
const username = url.searchParams.get("user");
const query = url.searchParams.get("q")?.trim() ?? null;
const sort = url.searchParams.get("sort")?.trim() ?? SortKey.TRENDING;
const showUnfeatured = url.searchParams.get("showUnfeatured") === "true";
const createdByCurrentUser = locals.user?.username && locals.user.username === username;
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`);
}
}
// if we require featured assistants, that we are not on a user page and we are not an admin who wants to see unfeatured assistants, we show featured assistants
let shouldBeFeatured = {};
if (env.REQUIRE_FEATURED_ASSISTANTS === "true" && !(locals.user?.isAdmin && showUnfeatured)) {
if (!user) {
// only show featured assistants on the community page
shouldBeFeatured = { review: ReviewStatus.APPROVED };
} else if (!createdByCurrentUser) {
// on a user page show assistants that have been approved or are pending
shouldBeFeatured = { review: { $in: [ReviewStatus.APPROVED, ReviewStatus.PENDING] } };
}
}
const noSpecificSearch = !user && !query;
// fetch the top assistants sorted by user count from biggest to smallest.
// filter by model too if modelId is provided or query if query is provided
// only show assistants that have been used by more than 5 users if no specific search is made
const filter: Filter<Assistant> = {
...(modelId && { modelId }),
...(user && { createdById: user._id }),
...(query && { searchTokens: { $all: generateQueryTokens(query) } }),
...(noSpecificSearch && { userCount: { $gte: 5 } }),
...shouldBeFeatured,
};
const assistants = await collections.assistants
.find(filter)
.sort({
...(sort === SortKey.TRENDING && { last24HoursCount: -1 }),
userCount: -1,
_id: 1,
})
.skip(NUM_PER_PAGE * pageIndex)
.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,
query,
sort,
showUnfeatured,
};
};
|