Make EventsDB not rely on Conf

This commit is contained in:
Alex Gleason
2024-09-11 13:06:20 -05:00
parent 624b6b278e
commit d2fb3fd253
14 changed files with 75 additions and 56 deletions

View File

@@ -24,7 +24,7 @@ export const policyWorker = Comlink.wrap<CustomPolicy>(
);
try {
await policyWorker.import(Conf.policy);
await policyWorker.init(Conf.policy, Conf.databaseUrl, Conf.pubkey);
console.debug(`Using custom policy: ${Conf.policy}`);
} catch (e) {
if (e.message.includes('Module not found')) {

View File

@@ -3,6 +3,9 @@ import { NostrEvent, NostrRelayOK, NPolicy } from '@nostrify/nostrify';
import { NoOpPolicy, ReadOnlyPolicy } from '@nostrify/nostrify/policies';
import * as Comlink from 'comlink';
import { DittoDB } from '@/db/DittoDB.ts';
import { EventsDB } from '@/storages/EventsDB.ts';
export class CustomPolicy implements NPolicy {
private policy: NPolicy = new ReadOnlyPolicy();
@@ -11,10 +14,18 @@ export class CustomPolicy implements NPolicy {
return this.policy.call(event);
}
async import(path: string): Promise<void> {
async init(path: string, databaseUrl: string, adminPubkey: string): Promise<void> {
const { kysely } = DittoDB.create(databaseUrl, { poolSize: 1 });
const store = new EventsDB({
kysely,
pubkey: adminPubkey,
timeout: 1_000,
});
try {
const Policy = (await import(path)).default;
this.policy = new Policy();
this.policy = new Policy({ store });
} catch (e) {
if (e.message.includes('Module not found')) {
this.policy = new NoOpPolicy();