Merge branch 'report-nits' into 'main'

Small reportController refactoring

See merge request soapbox-pub/ditto!238
This commit is contained in:
Alex Gleason 2024-05-08 22:29:41 +00:00
commit e29fb28d78
3 changed files with 22 additions and 26 deletions

View File

@ -48,7 +48,7 @@ import {
adminReportController,
adminReportResolveController,
adminReportsController,
reportsController,
reportController,
} from '@/controllers/api/reports.ts';
import { searchController } from '@/controllers/api/search.ts';
import {
@ -212,7 +212,7 @@ 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);
app.post('/api/v1/reports', requirePubkey, reportController);
app.get('/api/v1/admin/reports', requirePubkey, requireRole('admin'), adminReportsController);
app.get('/api/v1/admin/reports/:id{[0-9a-f]{64}}', requirePubkey, requireRole('admin'), adminReportController);
app.post(

View File

@ -8,7 +8,7 @@ import { hydrateEvents } from '@/storages/hydrate.ts';
import { renderAdminReport } from '@/views/mastodon/reports.ts';
import { renderReport } from '@/views/mastodon/reports.ts';
const reportsSchema = z.object({
const reportSchema = z.object({
account_id: n.id(),
status_ids: n.id().array().default([]),
comment: z.string().max(1000).default(''),
@ -17,10 +17,10 @@ const reportsSchema = z.object({
});
/** https://docs.joinmastodon.org/methods/reports/#post */
const reportsController: AppController = async (c) => {
const reportController: AppController = async (c) => {
const store = c.get('store');
const body = await parseBody(c.req.raw);
const result = reportsSchema.safeParse(body);
const result = reportSchema.safeParse(body);
if (!result.success) {
return c.json(result.error, 422);
@ -33,11 +33,6 @@ const reportsController: AppController = async (c) => {
category,
} = result.data;
const [profile] = await store.query([{ kinds: [0], authors: [account_id] }]);
if (profile) {
await hydrateEvents({ events: [profile], storage: store });
}
const tags = [
['p', account_id, category],
['P', Conf.pubkey],
@ -53,7 +48,8 @@ const reportsController: AppController = async (c) => {
tags,
}, c);
return c.json(await renderReport(event, profile));
await hydrateEvents({ events: [event], storage: store });
return c.json(await renderReport(event));
};
/** https://docs.joinmastodon.org/methods/admin/reports/#get */
@ -113,7 +109,7 @@ const adminReportResolveController: AppController = async (c) => {
content: 'Report closed.',
}, c);
return c.json(await renderAdminReport(event, { viewerPubkey: pubkey, action_taken: true }));
return c.json(await renderAdminReport(event, { viewerPubkey: pubkey, actionTaken: true }));
};
export { adminReportController, adminReportResolveController, adminReportsController, reportsController };
export { adminReportController, adminReportResolveController, adminReportsController, reportController };

View File

@ -5,39 +5,39 @@ import { renderAdminAccount } from '@/views/mastodon/admin-accounts.ts';
import { renderStatus } from '@/views/mastodon/statuses.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) {
async function renderReport(event: DittoEvent) {
// The category is present in both the 'e' and 'p' tag, however, it is possible to report a user without reporting a note, so it's better to get the category from the 'p' tag
const category = reportEvent.tags.find(([name]) => name === 'p')?.[2];
const statusIds = reportEvent.tags.filter(([name]) => name === 'e').map((tag) => tag[1]) ?? [];
const reportedPubkey = reportEvent.tags.find(([name]) => name === 'p')?.[1];
const category = event.tags.find(([name]) => name === 'p')?.[2];
const statusIds = event.tags.filter(([name]) => name === 'e').map((tag) => tag[1]) ?? [];
const reportedPubkey = event.tags.find(([name]) => name === 'p')?.[1];
if (!reportedPubkey) return;
return {
id: reportEvent.id,
id: event.id,
action_taken: false,
action_taken_at: null,
category,
comment: reportEvent.content,
comment: event.content,
forwarded: false,
created_at: nostrDate(reportEvent.created_at).toISOString(),
created_at: nostrDate(event.created_at).toISOString(),
status_ids: statusIds,
rules_ids: null,
target_account: profile ? await renderAccount(profile) : await accountFromPubkey(reportedPubkey),
target_account: event.reported_profile
? await renderAccount(event.reported_profile)
: await accountFromPubkey(reportedPubkey),
};
}
interface RenderAdminReportOpts {
viewerPubkey?: string;
action_taken?: boolean;
actionTaken?: boolean;
}
/** Admin-level information about a filed report.
* Expects an event of kind 1984 fully hydrated.
* https://docs.joinmastodon.org/entities/Admin_Report */
async function renderAdminReport(reportEvent: DittoEvent, opts: RenderAdminReportOpts) {
const { viewerPubkey, action_taken = false } = opts;
const { viewerPubkey, actionTaken = false } = opts;
// The category is present in both the 'e' and 'p' tag, however, it is possible to report a user without reporting a note, so it's better to get the category from the 'p' tag
const category = reportEvent.tags.find(([name]) => name === 'p')?.[2];
@ -51,7 +51,7 @@ async function renderAdminReport(reportEvent: DittoEvent, opts: RenderAdminRepor
return {
id: reportEvent.id,
action_taken,
action_taken: actionTaken,
action_taken_at: null,
category,
comment: reportEvent.content,