mirror of
https://github.com/aljazceru/ditto.git
synced 2025-12-22 07:54:19 +01:00
Rework reports with event sets
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
import { NSchema as n } from '@nostrify/nostrify';
|
||||
import { NostrFilter, NSchema as n } from '@nostrify/nostrify';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { type AppController } from '@/app.ts';
|
||||
import { Conf } from '@/config.ts';
|
||||
import { createAdminEvent, createEvent, parseBody } from '@/utils/api.ts';
|
||||
import { createEvent, paginationSchema, parseBody, updateEventInfo } from '@/utils/api.ts';
|
||||
import { hydrateEvents } from '@/storages/hydrate.ts';
|
||||
import { renderAdminReport } from '@/views/mastodon/reports.ts';
|
||||
import { renderReport } from '@/views/mastodon/reports.ts';
|
||||
import { getTagSet } from '@/utils/tags.ts';
|
||||
import { booleanParamSchema } from '@/schema.ts';
|
||||
|
||||
const reportSchema = z.object({
|
||||
account_id: n.id(),
|
||||
@@ -52,18 +54,60 @@ const reportController: AppController = async (c) => {
|
||||
return c.json(await renderReport(event));
|
||||
};
|
||||
|
||||
const adminReportsSchema = z.object({
|
||||
resolved: booleanParamSchema.optional(),
|
||||
account_id: n.id().optional(),
|
||||
target_account_id: n.id().optional(),
|
||||
});
|
||||
|
||||
/** https://docs.joinmastodon.org/methods/admin/reports/#get */
|
||||
const adminReportsController: AppController = async (c) => {
|
||||
const store = c.get('store');
|
||||
const viewerPubkey = await c.get('signer')?.getPublicKey();
|
||||
|
||||
const reports = await store.query([{ kinds: [1984], '#P': [Conf.pubkey] }])
|
||||
.then((events) => hydrateEvents({ store, events: events, signal: c.req.raw.signal }))
|
||||
.then((events) =>
|
||||
Promise.all(
|
||||
events.map((event) => renderAdminReport(event, { viewerPubkey })),
|
||||
)
|
||||
);
|
||||
const params = paginationSchema.parse(c.req.query());
|
||||
const { resolved, account_id, target_account_id } = adminReportsSchema.parse(c.req.query());
|
||||
|
||||
const filter: NostrFilter = {
|
||||
kinds: [30383],
|
||||
authors: [Conf.pubkey],
|
||||
...params,
|
||||
};
|
||||
|
||||
if (typeof resolved === 'boolean') {
|
||||
filter['#n'] = [resolved ? 'closed' : 'open'];
|
||||
}
|
||||
if (account_id) {
|
||||
filter['#p'] = [account_id];
|
||||
}
|
||||
if (target_account_id) {
|
||||
filter['#P'] = [target_account_id];
|
||||
}
|
||||
|
||||
const orig = await store.query([filter]);
|
||||
const ids = new Set<string>();
|
||||
|
||||
for (const event of orig) {
|
||||
const d = event.tags.find(([name]) => name === 'd')?.[1];
|
||||
if (d) {
|
||||
ids.add(d);
|
||||
}
|
||||
}
|
||||
|
||||
const events = await store.query([{ kinds: [1984], ids: [...ids] }])
|
||||
.then((events) => hydrateEvents({ store, events: events, signal: c.req.raw.signal }));
|
||||
|
||||
const reports = await Promise.all(
|
||||
events.map((event) => {
|
||||
const internal = orig.find(({ tags }) => tags.some(([name, value]) => name === 'd' && value === event.id));
|
||||
const names = getTagSet(internal?.tags ?? [], 'n');
|
||||
|
||||
return renderAdminReport(event, {
|
||||
viewerPubkey,
|
||||
actionTaken: names.has('closed'),
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return c.json(reports);
|
||||
};
|
||||
@@ -82,12 +126,13 @@ const adminReportController: AppController = async (c) => {
|
||||
}], { signal });
|
||||
|
||||
if (!event) {
|
||||
return c.json({ error: 'This action is not allowed' }, 403);
|
||||
return c.json({ error: 'Not found' }, 404);
|
||||
}
|
||||
|
||||
await hydrateEvents({ events: [event], store, signal });
|
||||
|
||||
return c.json(await renderAdminReport(event, { viewerPubkey: pubkey }));
|
||||
const report = await renderAdminReport(event, { viewerPubkey: pubkey });
|
||||
return c.json(report);
|
||||
};
|
||||
|
||||
/** https://docs.joinmastodon.org/methods/admin/reports/#resolve */
|
||||
@@ -104,18 +149,15 @@ const adminReportResolveController: AppController = async (c) => {
|
||||
}], { signal });
|
||||
|
||||
if (!event) {
|
||||
return c.json({ error: 'This action is not allowed' }, 403);
|
||||
return c.json({ error: 'Not found' }, 404);
|
||||
}
|
||||
|
||||
await updateEventInfo(eventId, { open: false, closed: true }, c);
|
||||
|
||||
await hydrateEvents({ events: [event], store, signal });
|
||||
|
||||
await createAdminEvent({
|
||||
kind: 5,
|
||||
tags: [['e', event.id]],
|
||||
content: 'Report closed.',
|
||||
}, c);
|
||||
|
||||
return c.json(await renderAdminReport(event, { viewerPubkey: pubkey, actionTaken: true }));
|
||||
const report = await renderAdminReport(event, { viewerPubkey: pubkey, actionTaken: true });
|
||||
return c.json(report);
|
||||
};
|
||||
|
||||
export { adminReportController, adminReportResolveController, adminReportsController, reportController };
|
||||
|
||||
Reference in New Issue
Block a user