From 7bd937b18dad3260150dbaf2f13afcb99986fce0 Mon Sep 17 00:00:00 2001 From: Mishig Davaadorj Date: Thu, 22 Feb 2024 17:20:40 +0100 Subject: [PATCH 1/2] [Assistants settings] Update data backend --- src/routes/settings/+layout.server.ts | 23 ----------------- .../assistants/[assistantId]/+page.server.ts | 25 +++++++++++++++++++ .../assistants/[assistantId]/+page.svelte | 2 +- .../assistants/[assistantId]/+page.ts | 14 ----------- 4 files changed, 26 insertions(+), 38 deletions(-) delete mode 100644 src/routes/settings/+layout.server.ts delete mode 100644 src/routes/settings/assistants/[assistantId]/+page.ts diff --git a/src/routes/settings/+layout.server.ts b/src/routes/settings/+layout.server.ts deleted file mode 100644 index 140b1a7e149..00000000000 --- a/src/routes/settings/+layout.server.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { collections } from "$lib/server/database"; -import type { LayoutServerLoad } from "./$types"; -import type { Report } from "$lib/types/Report"; - -export const load = (async ({ locals, parent }) => { - const { assistants } = await parent(); - - let reportsByUser: string[] = []; - const createdBy = locals.user?._id ?? locals.sessionId; - if (createdBy) { - const reports = await collections.reports - .find>({ createdBy }, { projection: { _id: 0, assistantId: 1 } }) - .toArray(); - reportsByUser = reports.map((r) => r.assistantId.toString()); - } - - return { - assistants: assistants.map((el) => ({ - ...el, - reported: reportsByUser.includes(el._id), - })), - }; -}) satisfies LayoutServerLoad; diff --git a/src/routes/settings/assistants/[assistantId]/+page.server.ts b/src/routes/settings/assistants/[assistantId]/+page.server.ts index 0734d964c39..0050ef3efa7 100644 --- a/src/routes/settings/assistants/[assistantId]/+page.server.ts +++ b/src/routes/settings/assistants/[assistantId]/+page.server.ts @@ -7,6 +7,7 @@ import { PUBLIC_ORIGIN, PUBLIC_SHARE_PREFIX } from "$env/static/public"; import { WEBHOOK_URL_REPORT_ASSISTANT } from "$env/static/private"; import { z } from "zod"; import type { Assistant } from "$lib/types/Assistant"; + async function assistantOnlyIfAuthor(locals: App.Locals, assistantId?: string) { const assistant = await collections.assistants.findOne({ _id: new ObjectId(assistantId) }); @@ -21,6 +22,30 @@ async function assistantOnlyIfAuthor(locals: App.Locals, assistantId?: string) { return assistant; } +export async function load({ parent, params, locals }) { + const data = await parent(); + + const assistantId = params.assistantId; + + const assistant = data.settings.assistants.find((id) => id === assistantId); + if (!assistant) { + throw redirect(302, `${base}/assistant/${params.assistantId}`); + } + + let isReported = false; + + const createdBy = locals.user?._id ?? locals.sessionId; + if (createdBy) { + const report = await collections.reports.findOne({ + createdBy, + assistantId: new ObjectId(assistantId), + }); + isReported = !!report; + } + + return { isReported }; +} + export const actions: Actions = { delete: async ({ params, locals }) => { let assistant; diff --git a/src/routes/settings/assistants/[assistantId]/+page.svelte b/src/routes/settings/assistants/[assistantId]/+page.svelte index 29a01da5fb0..2612fac2851 100644 --- a/src/routes/settings/assistants/[assistantId]/+page.svelte +++ b/src/routes/settings/assistants/[assistantId]/+page.svelte @@ -107,7 +107,7 @@ Duplicate - {#if !assistant?.reported} + {#if !data.isReported}