Mishig commited on
Commit
c755352
1 Parent(s): 19329ab

Revert "[Mongo] Optimize `reports` collection query (#832)" (#833)

Browse files

This reverts commit 19329abf32a4f49ff048585d0f6df5887cea8465.

src/lib/server/database.ts CHANGED
@@ -86,5 +86,4 @@ client.on("open", () => {
86
  assistants.createIndex({ featured: 1, userCount: -1 }).catch(console.error);
87
  assistants.createIndex({ modelId: 1, userCount: -1 }).catch(console.error);
88
  reports.createIndex({ assistantId: 1 }).catch(console.error);
89
- reports.createIndex({ createdBy: 1, assistantId: 1 }).catch(console.error);
90
  });
 
86
  assistants.createIndex({ featured: 1, userCount: -1 }).catch(console.error);
87
  assistants.createIndex({ modelId: 1, userCount: -1 }).catch(console.error);
88
  reports.createIndex({ assistantId: 1 }).catch(console.error);
 
89
  });
src/routes/settings/+layout.server.ts CHANGED
@@ -1,7 +1,6 @@
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();
@@ -13,22 +12,20 @@ export const load = (async ({ locals, parent }) => {
13
  })
14
  .toArray();
15
 
16
- let reportsByUser: ObjectId[] = [];
17
- const createdBy = locals.user?._id ?? locals.sessionId;
18
- if (createdBy) {
19
- const reports = await collections.reports
20
- .find<Pick<Report, "assistantId">>({ createdBy }, { projection: { _id: 0, assistantId: 1 } })
21
- .toArray();
22
- reportsByUser = reports.map((r) => r.assistantId);
23
- }
24
-
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),
32
- })),
 
 
 
 
 
 
 
33
  };
34
  }) satisfies LayoutServerLoad;
 
1
  import { collections } from "$lib/server/database";
2
  import { ObjectId } from "mongodb";
3
  import type { LayoutServerLoad } from "./$types";
 
4
 
5
  export const load = (async ({ locals, parent }) => {
6
  const { settings } = await parent();
 
12
  })
13
  .toArray();
14
 
 
 
 
 
 
 
 
 
 
15
  return {
16
+ assistants: await Promise.all(
17
+ assistants.map(async (el) => ({
18
+ ...el,
19
+ _id: el._id.toString(),
20
+ createdById: undefined,
21
+ createdByMe:
22
+ el.createdById.toString() === (locals.user?._id ?? locals.sessionId).toString(),
23
+ reported:
24
+ (await collections.reports.countDocuments({
25
+ assistantId: el._id,
26
+ createdBy: locals.user?._id ?? locals.sessionId,
27
+ })) > 0,
28
+ }))
29
+ ),
30
  };
31
  }) satisfies LayoutServerLoad;
src/routes/settings/assistants/[assistantId]/+page.server.ts CHANGED
@@ -57,8 +57,8 @@ export const actions: Actions = {
57
  report: async ({ request, params, locals, url }) => {
58
  // is there already a report from this user for this model ?
59
  const report = await collections.reports.findOne({
60
- createdBy: locals.user?._id ?? locals.sessionId,
61
  assistantId: new ObjectId(params.assistantId),
 
62
  });
63
 
64
  if (report) {
 
57
  report: async ({ request, params, locals, url }) => {
58
  // is there already a report from this user for this model ?
59
  const report = await collections.reports.findOne({
 
60
  assistantId: new ObjectId(params.assistantId),
61
+ createdBy: locals.user?._id ?? locals.sessionId,
62
  });
63
 
64
  if (report) {