Mishig coyotte508 HF staff commited on
Commit
cd9f246
1 Parent(s): c8e89ef

[Mongo] optimizations (#859)

Browse files

* [Mongo] remove duplicated assistants query

* Update src/routes/+layout.server.ts

Co-authored-by: Eliott C. <coyotte508@gmail.com>

* fix avatarHash problem

* limit convo to last 300

---------

Co-authored-by: Eliott C. <coyotte508@gmail.com>

src/routes/+layout.server.ts CHANGED
@@ -73,11 +73,13 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
73
  createdAt: 1,
74
  assistantId: 1,
75
  })
 
76
  .toArray();
77
 
78
- const assistantIds = conversations
79
- .map((conv) => conv.assistantId)
80
- .filter((el) => !!el) as ObjectId[];
 
81
 
82
  const assistants = await collections.assistants.find({ _id: { $in: assistantIds } }).toArray();
83
 
@@ -160,6 +162,12 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
160
  unlisted: model.unlisted,
161
  })),
162
  oldModels,
 
 
 
 
 
 
163
  user: locals.user && {
164
  id: locals.user._id.toString(),
165
  username: locals.user.username,
 
73
  createdAt: 1,
74
  assistantId: 1,
75
  })
76
+ .limit(300)
77
  .toArray();
78
 
79
+ const assistantIds = [
80
+ ...(settings?.assistants?.map((assistantId) => assistantId) ?? []),
81
+ ...(conversations.map((conv) => conv.assistantId).filter((el) => !!el) as ObjectId[]),
82
+ ];
83
 
84
  const assistants = await collections.assistants.find({ _id: { $in: assistantIds } }).toArray();
85
 
 
162
  unlisted: model.unlisted,
163
  })),
164
  oldModels,
165
+ assistants: assistants.map((el) => ({
166
+ ...el,
167
+ _id: el._id.toString(),
168
+ createdById: undefined,
169
+ createdByMe: el.createdById.toString() === (locals.user?._id ?? locals.sessionId).toString(),
170
+ })),
171
  user: locals.user && {
172
  id: locals.user._id.toString(),
173
  username: locals.user.username,
src/routes/settings/+layout.server.ts CHANGED
@@ -1,17 +1,9 @@
1
  import { collections } from "$lib/server/database";
2
- import { ObjectId } from "mongodb";
3
  import type { LayoutServerLoad } from "./$types";
4
  import type { Report } from "$lib/types/Report";
5
 
6
  export const load = (async ({ locals, parent }) => {
7
- const { settings } = await parent();
8
-
9
- // find assistants matching the settings assistants
10
- const assistants = await collections.assistants
11
- .find({
12
- _id: { $in: settings.assistants.map((el) => new ObjectId(el)) },
13
- })
14
- .toArray();
15
 
16
  let reportsByUser: string[] = [];
17
  const createdBy = locals.user?._id ?? locals.sessionId;
@@ -25,10 +17,7 @@ export const load = (async ({ locals, parent }) => {
25
  return {
26
  assistants: assistants.map((el) => ({
27
  ...el,
28
- _id: el._id.toString(),
29
- createdById: undefined,
30
- createdByMe: el.createdById.toString() === (locals.user?._id ?? locals.sessionId).toString(),
31
- reported: reportsByUser.includes(el._id.toString()),
32
  })),
33
  };
34
  }) satisfies LayoutServerLoad;
 
1
  import { collections } from "$lib/server/database";
 
2
  import type { LayoutServerLoad } from "./$types";
3
  import type { Report } from "$lib/types/Report";
4
 
5
  export const load = (async ({ locals, parent }) => {
6
+ const { assistants } = await parent();
 
 
 
 
 
 
 
7
 
8
  let reportsByUser: string[] = [];
9
  const createdBy = locals.user?._id ?? locals.sessionId;
 
17
  return {
18
  assistants: assistants.map((el) => ({
19
  ...el,
20
+ reported: reportsByUser.includes(el._id),
 
 
 
21
  })),
22
  };
23
  }) satisfies LayoutServerLoad;