Move with_authors to GetFilterOpts instead of DittoFilter

This commit is contained in:
Alex Gleason 2023-12-05 17:15:08 -06:00
parent f9d3240fa8
commit e3d5b2ac4a
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 26 additions and 26 deletions

View File

@ -144,31 +144,6 @@ function getFilterQuery(filter: DittoFilter): EventQuery {
: query.leftJoin('users', 'users.pubkey', 'events.pubkey').where('users.pubkey', 'is', null) as typeof query; : 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) { if (filter.search) {
query = query query = query
.innerJoin('events_fts', 'events_fts.id', 'events.id') .innerJoin('events_fts', 'events_fts.id', 'events.id')
@ -197,6 +172,30 @@ async function getFilters<K extends number>(
if (!filters.length) return Promise.resolve([]); if (!filters.length) return Promise.resolve([]);
let query = getFiltersQuery(filters); 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') { if (typeof opts.limit === 'number') {
query = query.limit(opts.limit); query = query.limit(opts.limit);
} }

View File

@ -6,7 +6,6 @@ import type { EventData } from '@/types.ts';
/** Custom filter interface that extends Nostr filters with extra options for Ditto. */ /** Custom filter interface that extends Nostr filters with extra options for Ditto. */
interface DittoFilter<K extends number = number> extends Filter<K> { interface DittoFilter<K extends number = number> extends Filter<K> {
local?: boolean; local?: boolean;
with_authors?: boolean;
} }
/** Additional options to apply to the whole subscription. */ /** Additional options to apply to the whole subscription. */
@ -15,6 +14,8 @@ interface GetFiltersOpts {
timeout?: number; timeout?: number;
/** Event limit for the whole subscription. */ /** Event limit for the whole subscription. */
limit?: number; 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 { function matchDittoFilter(filter: DittoFilter, event: Event, data: EventData): boolean {