diff --git a/src/app.ts b/src/app.ts index 5fb376dc..df8919d2 100644 --- a/src/app.ts +++ b/src/app.ts @@ -43,6 +43,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, @@ -198,6 +199,8 @@ app.delete('/api/v1/pleroma/admin/statuses/:id', requireRole('admin'), pleromaAd app.get('/api/v1/admin/ditto/relays', requireRole('admin'), adminRelaysController); app.put('/api/v1/admin/ditto/relays', requireRole('admin'), adminSetRelaysController); +app.post('/api/v1/reports', requirePubkey, reportsController); + // Not (yet) implemented. app.get('/api/v1/custom_emojis', emptyArrayController); app.get('/api/v1/filters', emptyArrayController); diff --git a/src/controllers/api/reports.ts b/src/controllers/api/reports.ts new file mode 100644 index 00000000..9e1f9339 --- /dev/null +++ b/src/controllers/api/reports.ts @@ -0,0 +1,53 @@ +import { type AppController } from '@/app.ts'; +import { createEvent, parseBody } from '@/utils/api.ts'; +import { Conf } from '@/config.ts'; +import { hydrateEvents } from '@/storages/hydrate.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: n.id(), + status_ids: n.id().array().default([]), + comment: z.string().max(1000).default(''), + forward: z.boolean().default(false), + category: z.string().default('other'), + // TODO: rules_ids[] is not implemented +}); + +/** https://docs.joinmastodon.org/methods/reports/ */ +const reportsController: AppController = async (c) => { + const store = c.get('store'); + const body = await parseBody(c.req.raw); + const result = reportsSchema.safeParse(body); + + if (!result.success) { + return c.json(result.error, 422); + } + + const { + account_id, + status_ids, + comment, + forward, + category, + } = result.data; + + const [profile] = await store.query([{ kinds: [0], authors: [account_id] }]); + if (profile) { + await hydrateEvents({ events: [profile], storage: store }); + } + + const event = await createEvent({ + kind: 1984, + content: JSON.stringify({ account_id, status_ids, comment, forward, category }), + tags: [ + ['p', account_id, category], + ['P', Conf.pubkey], + ], + }, c); + + return c.json(await renderReport(event, profile)); +}; + +export { reportsController }; diff --git a/src/views/mastodon/reports.ts b/src/views/mastodon/reports.ts new file mode 100644 index 00000000..03291f7a --- /dev/null +++ b/src/views/mastodon/reports.ts @@ -0,0 +1,29 @@ +import { type DittoEvent } from '@/interfaces/DittoEvent.ts'; +import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts'; +import { nostrDate } from '@/utils.ts'; + +/** 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, + comment, + forward, + category, + } = JSON.parse(reportEvent.content); + + return { + id: account_id, + action_taken: false, + action_taken_at: null, + category, + comment, + forwarded: forward, + created_at: nostrDate(reportEvent.created_at).toISOString(), + status_ids, + rules_ids: null, + target_account: profile ? await renderAccount(profile) : await accountFromPubkey(account_id), + }; +} + +export { renderReport };