Skip to content

Commit c755352

Browse files
author
Mishig
authored
Revert "[Mongo] Optimize reports collection query (#832)" (#833)
This reverts commit 19329ab.
1 parent 19329ab commit c755352

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

src/lib/server/database.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,4 @@ client.on("open", () => {
8686
assistants.createIndex({ featured: 1, userCount: -1 }).catch(console.error);
8787
assistants.createIndex({ modelId: 1, userCount: -1 }).catch(console.error);
8888
reports.createIndex({ assistantId: 1 }).catch(console.error);
89-
reports.createIndex({ createdBy: 1, assistantId: 1 }).catch(console.error);
9089
});

src/routes/settings/+layout.server.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { collections } from "$lib/server/database";
22
import { ObjectId } from "mongodb";
33
import type { LayoutServerLoad } from "./$types";
4-
import type { Report } from "$lib/types/Report";
54

65
export const load = (async ({ locals, parent }) => {
76
const { settings } = await parent();
@@ -13,22 +12,20 @@ export const load = (async ({ locals, parent }) => {
1312
})
1413
.toArray();
1514

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-
2515
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-
})),
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+
),
3330
};
3431
}) satisfies LayoutServerLoad;

src/routes/settings/assistants/[assistantId]/+page.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ export const actions: Actions = {
5757
report: async ({ request, params, locals, url }) => {
5858
// is there already a report from this user for this model ?
5959
const report = await collections.reports.findOne({
60-
createdBy: locals.user?._id ?? locals.sessionId,
6160
assistantId: new ObjectId(params.assistantId),
61+
createdBy: locals.user?._id ?? locals.sessionId,
6262
});
6363

6464
if (report) {

0 commit comments

Comments
 (0)