Load a custom policy from data/policy.ts

This commit is contained in:
Alex Gleason
2024-04-23 01:11:37 -05:00
parent ed08ac7c17
commit 69178ae523
2 changed files with 19 additions and 1 deletions

View File

@@ -22,6 +22,16 @@ import { nip05Cache } from '@/utils/nip05.ts';
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.
* 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}`);
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([
storeEvent(event, signal),
parseMetadata(event, signal),