Debug custom policies with ditto:policy

This commit is contained in:
Alex Gleason 2024-05-15 18:53:30 -05:00
parent 8e68d13ff1
commit 8a672c93ec
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 9 additions and 4 deletions

View File

@ -219,7 +219,7 @@ class Conf {
static get firehoseEnabled(): boolean { static get firehoseEnabled(): boolean {
return optionalBooleanSchema.parse(Deno.env.get('FIREHOSE_ENABLED')) ?? true; return optionalBooleanSchema.parse(Deno.env.get('FIREHOSE_ENABLED')) ?? true;
} }
/** Path to the custom policy module. Supports any value Deno's `import()` accepts, including relative path, absolute path, https:, npm:, and jsr:. */ /** Path to the custom policy module. Must be an absolute path, https:, npm:, or jsr: URI. */
static get policy(): string { static get policy(): string {
return Deno.env.get('DITTO_POLICY') || new URL('../data/policy.ts', import.meta.url).toString(); return Deno.env.get('DITTO_POLICY') || new URL('../data/policy.ts', import.meta.url).toString();
} }

View File

@ -2,6 +2,7 @@ import { NKinds, NostrEvent, NPolicy, NSchema as n } from '@nostrify/nostrify';
import { LNURL } from '@nostrify/nostrify/ln'; import { LNURL } from '@nostrify/nostrify/ln';
import { PipePolicy } from '@nostrify/nostrify/policies'; import { PipePolicy } from '@nostrify/nostrify/policies';
import Debug from '@soapbox/stickynotes/debug'; import Debug from '@soapbox/stickynotes/debug';
import { Stickynotes } from '@soapbox/stickynotes';
import { sql } from 'kysely'; import { sql } from 'kysely';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
@ -55,6 +56,8 @@ async function handleEvent(event: DittoEvent, signal: AbortSignal): Promise<void
} }
async function policyFilter(event: NostrEvent): Promise<void> { async function policyFilter(event: NostrEvent): Promise<void> {
const console = new Stickynotes('ditto:policy');
const policies: NPolicy[] = [ const policies: NPolicy[] = [
new MuteListPolicy(Conf.pubkey, await Storages.admin()), new MuteListPolicy(Conf.pubkey, await Storages.admin()),
]; ];
@ -62,14 +65,16 @@ async function policyFilter(event: NostrEvent): Promise<void> {
try { try {
const CustomPolicy = (await import(Conf.policy)).default; const CustomPolicy = (await import(Conf.policy)).default;
policies.push(new CustomPolicy()); policies.push(new CustomPolicy());
} catch (_e) { console.info(`Using custom policy: ${Conf.policy}`);
debug('policy not found - https://docs.soapbox.pub/ditto/policies/'); } catch {
console.info('Custom policy not found <https://docs.soapbox.pub/ditto/policies/>');
} }
const policy = new PipePolicy(policies.reverse()); const policy = new PipePolicy(policies.reverse());
const result = await policy.call(event); const result = await policy.call(event);
debug(JSON.stringify(result)); console.debug(JSON.stringify(result));
RelayError.assert(result); RelayError.assert(result);
} }