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

View File

@ -8,7 +8,7 @@ import { hydrateEvents } from '@/storages/hydrate.ts';
import { renderAdminReport } from '@/views/mastodon/reports.ts'; import { renderAdminReport } from '@/views/mastodon/reports.ts';
import { renderReport } from '@/views/mastodon/reports.ts'; import { renderReport } from '@/views/mastodon/reports.ts';
const reportsSchema = z.object({ const reportSchema = z.object({
account_id: n.id(), account_id: n.id(),
status_ids: n.id().array().default([]), status_ids: n.id().array().default([]),
comment: z.string().max(1000).default(''), comment: z.string().max(1000).default(''),
@ -17,10 +17,10 @@ const reportsSchema = z.object({
}); });
/** https://docs.joinmastodon.org/methods/reports/#post */ /** https://docs.joinmastodon.org/methods/reports/#post */
const reportsController: AppController = async (c) => { const reportController: AppController = async (c) => {
const store = c.get('store'); const store = c.get('store');
const body = await parseBody(c.req.raw); const body = await parseBody(c.req.raw);
const result = reportsSchema.safeParse(body); const result = reportSchema.safeParse(body);
if (!result.success) { if (!result.success) {
return c.json(result.error, 422); return c.json(result.error, 422);
@ -33,11 +33,6 @@ const reportsController: AppController = async (c) => {
category, category,
} = result.data; } = result.data;
const [profile] = await store.query([{ kinds: [0], authors: [account_id] }]);
if (profile) {
await hydrateEvents({ events: [profile], storage: store });
}
const tags = [ const tags = [
['p', account_id, category], ['p', account_id, category],
['P', Conf.pubkey], ['P', Conf.pubkey],
@ -53,7 +48,8 @@ const reportsController: AppController = async (c) => {
tags, tags,
}, c); }, 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 */ /** https://docs.joinmastodon.org/methods/admin/reports/#get */
@ -113,7 +109,7 @@ const adminReportResolveController: AppController = async (c) => {
content: 'Report closed.', content: 'Report closed.',
}, c); }, 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'; import { renderStatus } from '@/views/mastodon/statuses.ts';
/** Expects a `reportEvent` of kind 1984 and a `profile` of kind 0 of the person being reported */ /** 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 // 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 category = event.tags.find(([name]) => name === 'p')?.[2];
const statusIds = event.tags.filter(([name]) => name === 'e').map((tag) => tag[1]) ?? [];
const statusIds = reportEvent.tags.filter(([name]) => name === 'e').map((tag) => tag[1]) ?? []; const reportedPubkey = event.tags.find(([name]) => name === 'p')?.[1];
const reportedPubkey = reportEvent.tags.find(([name]) => name === 'p')?.[1];
if (!reportedPubkey) return; if (!reportedPubkey) return;
return { return {
id: reportEvent.id, id: event.id,
action_taken: false, action_taken: false,
action_taken_at: null, action_taken_at: null,
category, category,
comment: reportEvent.content, comment: event.content,
forwarded: false, forwarded: false,
created_at: nostrDate(reportEvent.created_at).toISOString(), created_at: nostrDate(event.created_at).toISOString(),
status_ids: statusIds, status_ids: statusIds,
rules_ids: null, 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 { interface RenderAdminReportOpts {
viewerPubkey?: string; viewerPubkey?: string;
action_taken?: boolean; actionTaken?: boolean;
} }
/** Admin-level information about a filed report. /** Admin-level information about a filed report.
* Expects an event of kind 1984 fully hydrated. * Expects an event of kind 1984 fully hydrated.
* https://docs.joinmastodon.org/entities/Admin_Report */ * https://docs.joinmastodon.org/entities/Admin_Report */
async function renderAdminReport(reportEvent: DittoEvent, opts: RenderAdminReportOpts) { 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 // 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 category = reportEvent.tags.find(([name]) => name === 'p')?.[2];
@ -51,7 +51,7 @@ async function renderAdminReport(reportEvent: DittoEvent, opts: RenderAdminRepor
return { return {
id: reportEvent.id, id: reportEvent.id,
action_taken, action_taken: actionTaken,
action_taken_at: null, action_taken_at: null,
category, category,
comment: reportEvent.content, comment: reportEvent.content,