extra --> relations, move it back to a filter option

This commit is contained in:
Alex Gleason 2023-12-05 22:06:27 -06:00
parent 22b1d730eb
commit 7d2813b214
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 30 additions and 29 deletions

View File

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

View File

@ -3,22 +3,23 @@ import { type Event, type Filter, matchFilters } from '@/deps.ts';
import type { EventData } from '@/types.ts';
/** Additional properties that may be added by Ditto to events. */
type Relation = 'author';
/** Custom filter interface that extends Nostr filters with extra options for Ditto. */
interface DittoFilter<K extends number = number> extends Filter<K> {
/** Whether the event was authored by a local user. */
local?: boolean;
/** Additional fields to add to the returned event. */
relations?: Relation[];
}
/** Additional properties that may be added by Ditto to events. */
type Extra = 'author';
/** Additional options to apply to the whole subscription. */
interface GetFiltersOpts {
/** How long to wait (in milliseconds) until aborting the request. */
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. */
extra?: Extra[];
}
function matchDittoFilter(filter: DittoFilter, event: Event, data: EventData): boolean {