Merge branch 'policy-plugin' into 'main'

Load a custom policy from data/policy.ts

See merge request soapbox-pub/ditto!176
This commit is contained in:
Alex Gleason 2024-04-23 06:14:42 +00:00
commit e5dd4c587c
2 changed files with 19 additions and 1 deletions

View File

@ -13,7 +13,7 @@
"exclude": ["./public"], "exclude": ["./public"],
"imports": { "imports": {
"@/": "./src/", "@/": "./src/",
"@nostrify/nostrify": "jsr:@nostrify/nostrify@^0.9.7", "@nostrify/nostrify": "jsr:@nostrify/nostrify@^0.12.1",
"~/fixtures/": "./fixtures/", "~/fixtures/": "./fixtures/",
"hono": "https://deno.land/x/hono@v3.10.1/mod.ts", "hono": "https://deno.land/x/hono@v3.10.1/mod.ts",
"hono/middleware": "https://deno.land/x/hono@v3.10.1/middleware.ts", "hono/middleware": "https://deno.land/x/hono@v3.10.1/middleware.ts",

View File

@ -22,6 +22,16 @@ import { nip05Cache } from '@/utils/nip05.ts';
const debug = Debug('ditto:pipeline'); const debug = Debug('ditto:pipeline');
let UserPolicy: any;
try {
UserPolicy = (await import('../data/policy.ts')).default;
debug('policy loaded from data/policy.ts');
} catch (_e) {
// do nothing
debug('policy not found');
}
/** /**
* Common pipeline function to process (and maybe store) events. * Common pipeline function to process (and maybe store) events.
* It is idempotent, so it can be called multiple times for the same event. * It is idempotent, so it can be called multiple times for the same event.
@ -32,6 +42,14 @@ async function handleEvent(event: DittoEvent, signal: AbortSignal): Promise<void
debug(`NostrEvent<${event.kind}> ${event.id}`); debug(`NostrEvent<${event.kind}> ${event.id}`);
await hydrateEvent(event, signal); await hydrateEvent(event, signal);
if (UserPolicy) {
const [_, _eventId, ok, reason] = await new UserPolicy().call(event, signal);
if (!ok) {
const [prefix, ...rest] = reason.split(': ');
throw new RelayError(prefix, rest.join(': '));
}
}
await Promise.all([ await Promise.all([
storeEvent(event, signal), storeEvent(event, signal),
parseMetadata(event, signal), parseMetadata(event, signal),