Merge branch 'feat-create-reports' into 'main'

Create user reports through kind 1984

Closes #97

See merge request soapbox-pub/ditto!210
This commit is contained in:
Alex Gleason 2024-05-03 14:25:22 +00:00
commit 2ee668d562
3 changed files with 85 additions and 0 deletions

View File

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

View File

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

View File

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