From c6605ece77c4c55e5879f04089ee766ad6b2c79c Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 19 Feb 2025 21:50:19 -0600 Subject: [PATCH] Fix not being able to log in for chrissakes --- packages/ditto/pipeline.ts | 29 +++++++++++---------- packages/ditto/storages/DittoPgStore.ts | 34 ++++++++++++++++++++----- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/packages/ditto/pipeline.ts b/packages/ditto/pipeline.ts index 1c49e930..d7536c91 100644 --- a/packages/ditto/pipeline.ts +++ b/packages/ditto/pipeline.ts @@ -77,21 +77,24 @@ async function handleEvent(event: DittoEvent, opts: PipelineOpts): Promise // NIP-46 events get special treatment. // They are exempt from policies and other side-effects, and should be streamed out immediately. // If streaming fails, an error should be returned. - if (event.kind !== 24133) { - // Ensure the event doesn't violate the policy. - if (event.pubkey !== Conf.pubkey) { - await policyFilter(event, opts.signal); - } + if (event.kind === 24133) { + const store = await Storages.db(); + await store.event(event, { signal: opts.signal }); + } - // Prepare the event for additional checks. - // FIXME: This is kind of hacky. Should be reorganized to fetch only what's needed for each stage. - await hydrateEvent(event, opts.signal); + // Ensure the event doesn't violate the policy. + if (event.pubkey !== Conf.pubkey) { + await policyFilter(event, opts.signal); + } - // Ensure that the author is not banned. - const n = getTagSet(event.user?.tags ?? [], 'n'); - if (n.has('disabled')) { - throw new RelayError('blocked', 'author is blocked'); - } + // Prepare the event for additional checks. + // FIXME: This is kind of hacky. Should be reorganized to fetch only what's needed for each stage. + await hydrateEvent(event, opts.signal); + + // Ensure that the author is not banned. + const n = getTagSet(event.user?.tags ?? [], 'n'); + if (n.has('disabled')) { + throw new RelayError('blocked', 'author is blocked'); } const kysely = await Storages.kysely(); diff --git a/packages/ditto/storages/DittoPgStore.ts b/packages/ditto/storages/DittoPgStore.ts index e7b88fd4..a921a309 100644 --- a/packages/ditto/storages/DittoPgStore.ts +++ b/packages/ditto/storages/DittoPgStore.ts @@ -126,10 +126,6 @@ export class DittoPgStore extends NPostgres { override async event(event: NostrEvent, opts: { signal?: AbortSignal; timeout?: number } = {}): Promise { event = purifyEvent(event); - if (this.opts.notify) { - this.encounters.set(event.id, true); - } - logi({ level: 'debug', ns: 'ditto.event', source: 'db', id: event.id, kind: event.kind }); dbEventsCounter.inc({ kind: event.kind }); @@ -137,6 +133,10 @@ export class DittoPgStore extends NPostgres { return await this.fulfill(event); } + if (this.opts.notify) { + this.encounters.set(event.id, true); + } + if (await this.isDeletedAdmin(event)) { throw new RelayError('blocked', 'event deleted by admin'); } @@ -590,8 +590,28 @@ export class DittoPgStore extends NPostgres { return filters; } - // deno-lint-ignore no-explicit-any - override async transaction(callback: (store: NPostgres, kysely: Kysely) => Promise): Promise { - return super.transaction((store, kysely) => callback(store, kysely as unknown as Kysely)); + /** Execute the callback in a new transaction, unless the Kysely instance is already a transaction. */ + private static override async trx( + db: Kysely, + callback: (trx: Kysely) => Promise, + ): Promise { + if (db.isTransaction) { + return await callback(db); + } else { + return await db.transaction().execute((trx) => callback(trx)); + } + } + + /** Execute NPostgres functions in a transaction. */ + // @ts-ignore gg + override async transaction( + callback: (store: DittoPgStore, kysely: Kysely) => Promise, + ): Promise { + const { db } = this.opts; + + await DittoPgStore.trx(db.kysely, async (trx) => { + const store = new DittoPgStore({ ...this.opts, db: { ...db, kysely: trx }, notify: false }); + await callback(store, trx); + }); } }