nsarrazin HF staff commited on
Commit
84d0edd
1 Parent(s): 7ede405

Final fix to assistants display logic (#1056)

Browse files
src/routes/api/assistants/+server.ts CHANGED
@@ -14,11 +14,6 @@ export async function GET({ url, locals }) {
14
  const query = url.searchParams.get("q")?.trim() ?? null;
15
  const createdByCurrentUser = locals.user?.username && locals.user.username === username;
16
 
17
- const shouldBeFeatured =
18
- REQUIRE_FEATURED_ASSISTANTS === "true" && !createdByCurrentUser
19
- ? { featured: true, userCount: { $gt: 1 } }
20
- : {};
21
-
22
  let user: Pick<User, "_id"> | null = null;
23
  if (username) {
24
  user = await collections.users.findOne<Pick<User, "_id">>(
@@ -30,12 +25,23 @@ export async function GET({ url, locals }) {
30
  }
31
  }
32
 
 
 
 
 
 
 
 
 
 
 
33
  // 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
34
  const filter: Filter<Assistant> = {
35
  ...(modelId && { modelId }),
36
  ...(user && { createdById: user._id }),
37
  ...(query && { searchTokens: { $all: generateQueryTokens(query) } }),
38
  ...shouldBeFeatured,
 
39
  };
40
  const assistants = await collections.assistants
41
  .find(filter)
 
14
  const query = url.searchParams.get("q")?.trim() ?? null;
15
  const createdByCurrentUser = locals.user?.username && locals.user.username === username;
16
 
 
 
 
 
 
17
  let user: Pick<User, "_id"> | null = null;
18
  if (username) {
19
  user = await collections.users.findOne<Pick<User, "_id">>(
 
25
  }
26
  }
27
 
28
+ // if there is no user, we show community assistants, so only show featured assistants
29
+ const shouldBeFeatured =
30
+ REQUIRE_FEATURED_ASSISTANTS === "true" && !user ? { featured: true } : {};
31
+
32
+ // if the user queried is not the current user, only show "public" assistants that have been shared before
33
+ const shouldHaveBeenShared =
34
+ REQUIRE_FEATURED_ASSISTANTS === "true" && !createdByCurrentUser
35
+ ? { userCount: { $gt: 1 } }
36
+ : {};
37
+
38
  // 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
39
  const filter: Filter<Assistant> = {
40
  ...(modelId && { modelId }),
41
  ...(user && { createdById: user._id }),
42
  ...(query && { searchTokens: { $all: generateQueryTokens(query) } }),
43
  ...shouldBeFeatured,
44
+ ...shouldHaveBeenShared,
45
  };
46
  const assistants = await collections.assistants
47
  .find(filter)
src/routes/assistants/+page.server.ts CHANGED
@@ -21,11 +21,6 @@ export const load = async ({ url, locals }) => {
21
  const sort = url.searchParams.get("sort")?.trim() ?? SortKey.POPULAR;
22
  const createdByCurrentUser = locals.user?.username && locals.user.username === username;
23
 
24
- const shouldBeFeatured =
25
- REQUIRE_FEATURED_ASSISTANTS === "true" && !createdByCurrentUser
26
- ? { featured: true, userCount: { $gt: 1 } }
27
- : {};
28
-
29
  let user: Pick<User, "_id"> | null = null;
30
  if (username) {
31
  user = await collections.users.findOne<Pick<User, "_id">>(
@@ -37,12 +32,23 @@ export const load = async ({ url, locals }) => {
37
  }
38
  }
39
 
 
 
 
 
 
 
 
 
 
 
40
  // 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
41
  const filter: Filter<Assistant> = {
42
  ...(modelId && { modelId }),
43
  ...(user && { createdById: user._id }),
44
  ...(query && { searchTokens: { $all: generateQueryTokens(query) } }),
45
  ...shouldBeFeatured,
 
46
  };
47
  const assistants = await collections.assistants
48
  .find(filter)
 
21
  const sort = url.searchParams.get("sort")?.trim() ?? SortKey.POPULAR;
22
  const createdByCurrentUser = locals.user?.username && locals.user.username === username;
23
 
 
 
 
 
 
24
  let user: Pick<User, "_id"> | null = null;
25
  if (username) {
26
  user = await collections.users.findOne<Pick<User, "_id">>(
 
32
  }
33
  }
34
 
35
+ // if there is no user, we show community assistants, so only show featured assistants
36
+ const shouldBeFeatured =
37
+ REQUIRE_FEATURED_ASSISTANTS === "true" && !user ? { featured: true } : {};
38
+
39
+ // if the user queried is not the current user, only show "public" assistants that have been shared before
40
+ const shouldHaveBeenShared =
41
+ REQUIRE_FEATURED_ASSISTANTS === "true" && !createdByCurrentUser
42
+ ? { userCount: { $gt: 1 } }
43
+ : {};
44
+
45
  // 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
46
  const filter: Filter<Assistant> = {
47
  ...(modelId && { modelId }),
48
  ...(user && { createdById: user._id }),
49
  ...(query && { searchTokens: { $all: generateQueryTokens(query) } }),
50
  ...shouldBeFeatured,
51
+ ...shouldHaveBeenShared,
52
  };
53
  const assistants = await collections.assistants
54
  .find(filter)