refactor(reports): update code according to code review in MR 210

This commit is contained in:
P. Reis 2024-05-02 20:21:58 -03:00
parent 226c356646
commit ca5433bcc7
3 changed files with 15 additions and 20 deletions

View File

@ -42,6 +42,7 @@ import {
} from '@/controllers/api/pleroma.ts'; } from '@/controllers/api/pleroma.ts';
import { preferencesController } from '@/controllers/api/preferences.ts'; import { preferencesController } from '@/controllers/api/preferences.ts';
import { relayController } from '@/controllers/nostr/relay.ts'; import { relayController } from '@/controllers/nostr/relay.ts';
import { reportsController } from '@/controllers/api/reports.ts';
import { searchController } from '@/controllers/api/search.ts'; import { searchController } from '@/controllers/api/search.ts';
import { import {
bookmarkController, bookmarkController,
@ -77,7 +78,6 @@ import { cache } from '@/middleware/cache.ts';
import { csp } from '@/middleware/csp.ts'; import { csp } from '@/middleware/csp.ts';
import { adminRelaysController, adminSetRelaysController } from '@/controllers/api/ditto.ts'; import { adminRelaysController, adminSetRelaysController } from '@/controllers/api/ditto.ts';
import { storeMiddleware } from '@/middleware/store.ts'; import { storeMiddleware } from '@/middleware/store.ts';
import { reportsController } from '@/controllers/api/reports.ts';
interface AppEnv extends HonoEnv { interface AppEnv extends HonoEnv {
Variables: { Variables: {

View File

@ -1,13 +1,14 @@
import { type AppController } from '@/app.ts'; import { type AppController } from '@/app.ts';
import { z } from 'zod';
import { createEvent, parseBody } from '@/utils/api.ts'; import { createEvent, parseBody } from '@/utils/api.ts';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { hydrateEvents } from '@/storages/hydrate.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({ const reportsSchema = z.object({
account_id: z.string(), account_id: n.id(),
status_ids: z.string().array().default([]), status_ids: n.id().array().default([]),
comment: z.string().max(1000).default(''), comment: z.string().max(1000).default(''),
forward: z.boolean().default(false), forward: z.boolean().default(false),
category: z.string().default('other'), category: z.string().default('other'),
@ -32,13 +33,11 @@ const reportsController: AppController = async (c) => {
category, category,
} = result.data; } = result.data;
const [personBeingReported] = await store.query([{ kinds: [0], authors: [account_id] }]); const [profile] = await store.query([{ kinds: [0], authors: [account_id] }]);
if (!personBeingReported) { if (profile) {
return c.json({ error: 'Record not found' }, 404); await hydrateEvents({ events: [profile], storage: store });
} }
await hydrateEvents({ events: [personBeingReported], storage: store });
const event = await createEvent({ const event = await createEvent({
kind: 1984, kind: 1984,
content: JSON.stringify({ account_id, status_ids, comment, forward, category }), content: JSON.stringify({ account_id, status_ids, comment, forward, category }),
@ -48,7 +47,7 @@ const reportsController: AppController = async (c) => {
], ],
}, c); }, c);
return c.json(await renderReports(event, personBeingReported, {})); return c.json(await renderReport(event, profile));
}; };
export { reportsController }; export { reportsController };

View File

@ -1,13 +1,9 @@
import { type DittoEvent } from '@/interfaces/DittoEvent.ts'; 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'; import { nostrDate } from '@/utils.ts';
interface reportsOpts { /** Expects a `reportEvent` of kind 1984 and a `profile` of kind 0 of the person being reported */
viewerPubkey?: string; async function renderReport(reportEvent: DittoEvent, profile: DittoEvent) {
}
/** 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) {
const { const {
account_id, account_id,
status_ids, status_ids,
@ -26,8 +22,8 @@ async function renderReports(reportEvent: DittoEvent, targetAccout: DittoEvent,
created_at: nostrDate(reportEvent.created_at).toISOString(), created_at: nostrDate(reportEvent.created_at).toISOString(),
status_ids, status_ids,
rules_ids: null, rules_ids: null,
target_account: await renderAccount(targetAccout), target_account: profile ? await renderAccount(profile) : await accountFromPubkey(account_id),
}; };
} }
export { renderReports }; export { renderReport };