Import the custom policy only once
This commit is contained in:
parent
f98ebd359a
commit
a2c5e824b9
|
@ -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 Debug from '@soapbox/stickynotes/debug';
|
||||
import { sql } from 'kysely';
|
||||
|
@ -55,24 +55,10 @@ async function handleEvent(event: DittoEvent, signal: AbortSignal): Promise<void
|
|||
async function policyFilter(event: NostrEvent): Promise<void> {
|
||||
const debug = Debug('ditto:policy');
|
||||
|
||||
const policies: NPolicy[] = [
|
||||
const policy = new PipePolicy([
|
||||
new MuteListPolicy(Conf.pubkey, await Storages.admin()),
|
||||
];
|
||||
|
||||
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());
|
||||
policyWorker,
|
||||
]);
|
||||
|
||||
try {
|
||||
const result = await policy.call(event);
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
import { Stickynotes } from '@soapbox/stickynotes';
|
||||
import * as Comlink from 'comlink';
|
||||
|
||||
import { Conf } from '@/config.ts';
|
||||
import type { CustomPolicy } from '@/workers/policy.worker.ts';
|
||||
|
||||
const console = new Stickynotes('ditto:policy');
|
||||
|
||||
export const policyWorker = Comlink.wrap<CustomPolicy>(
|
||||
new Worker(
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue