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';
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: {

View File

@ -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 };

View File

@ -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 };