From ca5433bcc78985c055c7d9b7e29fb7500c4557e7 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Thu, 2 May 2024 20:21:58 -0300 Subject: [PATCH] refactor(reports): update code according to code review in MR 210 --- src/app.ts | 2 +- src/controllers/api/reports.ts | 19 +++++++++---------- src/views/mastodon/reports.ts | 14 +++++--------- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/app.ts b/src/app.ts index 4c0e657..ce53251 100644 --- a/src/app.ts +++ b/src/app.ts @@ -42,6 +42,7 @@ import { } from '@/controllers/api/pleroma.ts'; import { preferencesController } from '@/controllers/api/preferences.ts'; import { relayController } from '@/controllers/nostr/relay.ts'; +import { reportsController } from '@/controllers/api/reports.ts'; import { searchController } from '@/controllers/api/search.ts'; import { bookmarkController, @@ -77,7 +78,6 @@ import { cache } from '@/middleware/cache.ts'; import { csp } from '@/middleware/csp.ts'; import { adminRelaysController, adminSetRelaysController } from '@/controllers/api/ditto.ts'; import { storeMiddleware } from '@/middleware/store.ts'; -import { reportsController } from '@/controllers/api/reports.ts'; interface AppEnv extends HonoEnv { Variables: { diff --git a/src/controllers/api/reports.ts b/src/controllers/api/reports.ts index 8008eae..9e1f933 100644 --- a/src/controllers/api/reports.ts +++ b/src/controllers/api/reports.ts @@ -1,13 +1,14 @@ import { type AppController } from '@/app.ts'; -import { z } from 'zod'; import { createEvent, parseBody } from '@/utils/api.ts'; import { Conf } from '@/config.ts'; import { hydrateEvents } from '@/storages/hydrate.ts'; -import { renderReports } from '@/views/mastodon/reports.ts'; +import { NSchema as n } from '@nostrify/nostrify'; +import { renderReport } from '@/views/mastodon/reports.ts'; +import { z } from 'zod'; const reportsSchema = z.object({ - account_id: z.string(), - status_ids: z.string().array().default([]), + account_id: n.id(), + status_ids: n.id().array().default([]), comment: z.string().max(1000).default(''), forward: z.boolean().default(false), category: z.string().default('other'), @@ -32,13 +33,11 @@ const reportsController: AppController = async (c) => { category, } = result.data; - const [personBeingReported] = await store.query([{ kinds: [0], authors: [account_id] }]); - if (!personBeingReported) { - return c.json({ error: 'Record not found' }, 404); + const [profile] = await store.query([{ kinds: [0], authors: [account_id] }]); + if (profile) { + await hydrateEvents({ events: [profile], storage: store }); } - await hydrateEvents({ events: [personBeingReported], storage: store }); - const event = await createEvent({ kind: 1984, content: JSON.stringify({ account_id, status_ids, comment, forward, category }), @@ -48,7 +47,7 @@ const reportsController: AppController = async (c) => { ], }, c); - return c.json(await renderReports(event, personBeingReported, {})); + return c.json(await renderReport(event, profile)); }; export { reportsController }; diff --git a/src/views/mastodon/reports.ts b/src/views/mastodon/reports.ts index 67f9f6d..03291f7 100644 --- a/src/views/mastodon/reports.ts +++ b/src/views/mastodon/reports.ts @@ -1,13 +1,9 @@ import { type DittoEvent } from '@/interfaces/DittoEvent.ts'; -import { renderAccount } from '@/views/mastodon/accounts.ts'; +import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts'; import { nostrDate } from '@/utils.ts'; -interface reportsOpts { - viewerPubkey?: string; -} - -/** Expects a `reportEvent` of kind 1984 and a `targetAccout` of kind 0 of the person being reported */ -async function renderReports(reportEvent: DittoEvent, targetAccout: DittoEvent, _opts: reportsOpts) { +/** Expects a `reportEvent` of kind 1984 and a `profile` of kind 0 of the person being reported */ +async function renderReport(reportEvent: DittoEvent, profile: DittoEvent) { const { account_id, status_ids, @@ -26,8 +22,8 @@ async function renderReports(reportEvent: DittoEvent, targetAccout: DittoEvent, created_at: nostrDate(reportEvent.created_at).toISOString(), status_ids, rules_ids: null, - target_account: await renderAccount(targetAccout), + target_account: profile ? await renderAccount(profile) : await accountFromPubkey(account_id), }; } -export { renderReports }; +export { renderReport };