Import the custom policy only once

This commit is contained in:
Alex Gleason 2024-05-31 12:09:14 -05:00
parent f98ebd359a
commit a2c5e824b9
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 18 additions and 18 deletions

View File

@ -1,4 +1,4 @@
import { NKinds, NostrEvent, NPolicy, NSchema as n } from '@nostrify/nostrify'; import { NKinds, NostrEvent, NSchema as n } from '@nostrify/nostrify';
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 { sql } from 'kysely'; import { sql } from 'kysely';
@ -55,24 +55,10 @@ async function handleEvent(event: DittoEvent, signal: AbortSignal): Promise<void
async function policyFilter(event: NostrEvent): Promise<void> { async function policyFilter(event: NostrEvent): Promise<void> {
const debug = Debug('ditto:policy'); const debug = Debug('ditto:policy');
const policies: NPolicy[] = [ const policy = new PipePolicy([
new MuteListPolicy(Conf.pubkey, await Storages.admin()), new MuteListPolicy(Conf.pubkey, await Storages.admin()),
]; policyWorker,
]);
try {
await policyWorker.import(Conf.policy);
policies.push(policyWorker);
debug(`Using custom policy: ${Conf.policy}`);
} catch (e) {
if (e.message.includes('Module not found')) {
debug('Custom policy not found <https://docs.soapbox.pub/ditto/policies/>');
} else {
console.error(`DITTO_POLICY (error importing policy): ${Conf.policy}`, e);
throw new RelayError('blocked', 'policy could not be loaded');
}
}
const policy = new PipePolicy(policies.reverse());
try { try {
const result = await policy.call(event); const result = await policy.call(event);

View File

@ -1,8 +1,11 @@
import { Stickynotes } from '@soapbox/stickynotes';
import * as Comlink from 'comlink'; import * as Comlink from 'comlink';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import type { CustomPolicy } from '@/workers/policy.worker.ts'; import type { CustomPolicy } from '@/workers/policy.worker.ts';
const console = new Stickynotes('ditto:policy');
export const policyWorker = Comlink.wrap<CustomPolicy>( export const policyWorker = Comlink.wrap<CustomPolicy>(
new Worker( new Worker(
new URL('./policy.worker.ts', import.meta.url), new URL('./policy.worker.ts', import.meta.url),
@ -19,3 +22,14 @@ export const policyWorker = Comlink.wrap<CustomPolicy>(
}, },
), ),
); );
try {
await policyWorker.import(Conf.policy);
console.debug(`Using custom policy: ${Conf.policy}`);
} catch (e) {
if (e.message.includes('Module not found')) {
console.debug('Custom policy not found <https://docs.soapbox.pub/ditto/policies/>');
} else {
throw new Error(`DITTO_POLICY (error importing policy): ${Conf.policy}`, e);
}
}