Rework reports with event sets

This commit is contained in:
Alex Gleason 2024-06-08 19:48:56 -05:00
parent a2d865d6cc
commit a14515bbe0
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 75 additions and 24 deletions

View File

@ -1,12 +1,14 @@
import { NSchema as n } from '@nostrify/nostrify'; import { NostrFilter, NSchema as n } from '@nostrify/nostrify';
import { z } from 'zod'; import { z } from 'zod';
import { type AppController } from '@/app.ts'; import { type AppController } from '@/app.ts';
import { Conf } from '@/config.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 { 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';
import { getTagSet } from '@/utils/tags.ts';
import { booleanParamSchema } from '@/schema.ts';
const reportSchema = z.object({ const reportSchema = z.object({
account_id: n.id(), account_id: n.id(),
@ -52,18 +54,60 @@ const reportController: AppController = async (c) => {
return c.json(await renderReport(event)); 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 */ /** https://docs.joinmastodon.org/methods/admin/reports/#get */
const adminReportsController: AppController = async (c) => { const adminReportsController: AppController = async (c) => {
const store = c.get('store'); const store = c.get('store');
const viewerPubkey = await c.get('signer')?.getPublicKey(); const viewerPubkey = await c.get('signer')?.getPublicKey();
const reports = await store.query([{ kinds: [1984], '#P': [Conf.pubkey] }]) const params = paginationSchema.parse(c.req.query());
.then((events) => hydrateEvents({ store, events: events, signal: c.req.raw.signal })) const { resolved, account_id, target_account_id } = adminReportsSchema.parse(c.req.query());
.then((events) =>
Promise.all( const filter: NostrFilter = {
events.map((event) => renderAdminReport(event, { viewerPubkey })), 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); return c.json(reports);
}; };
@ -82,12 +126,13 @@ const adminReportController: AppController = async (c) => {
}], { signal }); }], { signal });
if (!event) { 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 }); 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 */ /** https://docs.joinmastodon.org/methods/admin/reports/#resolve */
@ -104,18 +149,15 @@ const adminReportResolveController: AppController = async (c) => {
}], { signal }); }], { signal });
if (!event) { 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 hydrateEvents({ events: [event], store, signal });
await createAdminEvent({ const report = await renderAdminReport(event, { viewerPubkey: pubkey, actionTaken: true });
kind: 5, return c.json(report);
tags: [['e', event.id]],
content: 'Report closed.',
}, c);
return c.json(await renderAdminReport(event, { viewerPubkey: pubkey, actionTaken: true }));
}; };
export { adminReportController, adminReportResolveController, adminReportsController, reportController }; export { adminReportController, adminReportResolveController, adminReportsController, reportController };

View File

@ -29,9 +29,9 @@ class EventsDB implements NStore {
'a': ({ count }) => count < 15, 'a': ({ count }) => count < 15,
'd': ({ event, count }) => count === 0 && NKinds.parameterizedReplaceable(event.kind), 'd': ({ event, count }) => count === 0 && NKinds.parameterizedReplaceable(event.kind),
'e': ({ event, count, value }) => ((event.kind === 10003) || count < 15) && isNostrId(value), 'e': ({ event, count, value }) => ((event.kind === 10003) || count < 15) && isNostrId(value),
'k': ({ count, value }) => count === 0 && Number.isInteger(Number(value)),
'L': ({ event, count }) => event.kind === 1985 || count === 0, 'L': ({ event, count }) => event.kind === 1985 || count === 0,
'l': ({ event, count }) => event.kind === 1985 || count === 0, 'l': ({ event, count }) => event.kind === 1985 || count === 0,
'media': ({ count, value }) => (count < 4) && isURL(value),
'n': ({ count, value }) => count < 50 && value.length < 50, 'n': ({ count, value }) => count < 50 && value.length < 50,
'P': ({ count, value }) => count === 0 && isNostrId(value), 'P': ({ count, value }) => count === 0 && isNostrId(value),
'p': ({ event, count, value }) => (count < 15 || event.kind === 3) && isNostrId(value), 'p': ({ event, count, value }) => (count < 15 || event.kind === 3) && isNostrId(value),

View File

@ -107,12 +107,20 @@ async function updateAdminEvent<E extends EventStub>(
return createAdminEvent(fn(prev), c); return createAdminEvent(fn(prev), c);
} }
async function updateUser(pubkey: string, n: Record<string, boolean>, c: AppContext): Promise<NostrEvent> { function updateUser(pubkey: string, n: Record<string, boolean>, c: AppContext): Promise<NostrEvent> {
return updateNames(30382, pubkey, n, c);
}
function updateEventInfo(id: string, n: Record<string, boolean>, c: AppContext): Promise<NostrEvent> {
return updateNames(30383, id, n, c);
}
async function updateNames(k: number, d: string, n: Record<string, boolean>, c: AppContext): Promise<NostrEvent> {
const signer = new AdminSigner(); const signer = new AdminSigner();
const admin = await signer.getPublicKey(); const admin = await signer.getPublicKey();
return updateAdminEvent( return updateAdminEvent(
{ kinds: [30382], authors: [admin], '#d': [pubkey], limit: 1 }, { kinds: [k], authors: [admin], '#d': [d], limit: 1 },
(prev) => { (prev) => {
const prevNames = prev?.tags.reduce((acc, [name, value]) => { const prevNames = prev?.tags.reduce((acc, [name, value]) => {
if (name === 'n') acc[value] = true; if (name === 'n') acc[value] = true;
@ -124,10 +132,10 @@ async function updateUser(pubkey: string, n: Record<string, boolean>, c: AppCont
const other = prev?.tags.filter(([name]) => !['d', 'n'].includes(name)) ?? []; const other = prev?.tags.filter(([name]) => !['d', 'n'].includes(name)) ?? [];
return { return {
kind: 30382, kind: k,
content: prev?.content ?? '', content: prev?.content ?? '',
tags: [ tags: [
['d', pubkey], ['d', d],
...nTags, ...nTags,
...other, ...other,
], ],
@ -296,6 +304,7 @@ export {
parseBody, parseBody,
updateAdminEvent, updateAdminEvent,
updateEvent, updateEvent,
updateEventInfo,
updateListAdminEvent, updateListAdminEvent,
updateListEvent, updateListEvent,
updateUser, updateUser,