From e3d5b2ac4a908be700acffe4aa35ffe181f27d05 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 5 Dec 2023 17:15:08 -0600 Subject: [PATCH] Move with_authors to GetFilterOpts instead of DittoFilter --- src/db/events.ts | 49 ++++++++++++++++++++++++------------------------ src/filter.ts | 3 ++- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/db/events.ts b/src/db/events.ts index be46a1a..f8583a9 100644 --- a/src/db/events.ts +++ b/src/db/events.ts @@ -144,31 +144,6 @@ function getFilterQuery(filter: DittoFilter): EventQuery { : query.leftJoin('users', 'users.pubkey', 'events.pubkey').where('users.pubkey', 'is', null) as typeof query; } - if (filter.with_authors) { - // get kind 0 event associated with the `pubkey` field of the event - query = query - .leftJoin( - (eb) => - eb - .selectFrom('events') - .selectAll() - .where('kind', '=', 0) - .orderBy('created_at', 'desc') - .groupBy('pubkey') - .as('authors'), - (join) => join.onRef('authors.pubkey', '=', 'events.pubkey'), - ) - .select([ - 'authors.id as author_id', - 'authors.kind as author_kind', - 'authors.pubkey as author_pubkey', - 'authors.content as author_content', - 'authors.tags as author_tags', - 'authors.created_at as author_created_at', - 'authors.sig as author_sig', - ]); - } - if (filter.search) { query = query .innerJoin('events_fts', 'events_fts.id', 'events.id') @@ -197,6 +172,30 @@ async function getFilters( if (!filters.length) return Promise.resolve([]); let query = getFiltersQuery(filters); + if (opts.with_authors) { + query = query + .leftJoin( + (eb) => + eb + .selectFrom('events') + .selectAll() + .where('kind', '=', 0) + .orderBy('created_at', 'desc') + .groupBy('pubkey') + .as('authors'), + (join) => join.onRef('authors.pubkey', '=', 'events.pubkey'), + ) + .select([ + 'authors.id as author_id', + 'authors.kind as author_kind', + 'authors.pubkey as author_pubkey', + 'authors.content as author_content', + 'authors.tags as author_tags', + 'authors.created_at as author_created_at', + 'authors.sig as author_sig', + ]) as typeof query; + } + if (typeof opts.limit === 'number') { query = query.limit(opts.limit); } diff --git a/src/filter.ts b/src/filter.ts index 82c9be9..9146186 100644 --- a/src/filter.ts +++ b/src/filter.ts @@ -6,7 +6,6 @@ import type { EventData } from '@/types.ts'; /** Custom filter interface that extends Nostr filters with extra options for Ditto. */ interface DittoFilter extends Filter { local?: boolean; - with_authors?: boolean; } /** Additional options to apply to the whole subscription. */ @@ -15,6 +14,8 @@ interface GetFiltersOpts { timeout?: number; /** Event limit for the whole subscription. */ limit?: number; + /** Whether to include a corresponding kind 0 event in the `authors` key of each event. */ + with_authors?: boolean; } function matchDittoFilter(filter: DittoFilter, event: Event, data: EventData): boolean {