db/events: support 'stats' relation

This commit is contained in:
Alex Gleason 2023-12-10 15:02:19 -06:00
parent 21b6a02ff3
commit 6a92c5135d
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 33 additions and 7 deletions

View File

@ -140,7 +140,7 @@ function getFilterQuery(filter: DittoFilter): EventQuery {
query = query
.leftJoin('tags', 'tags.event_id', 'events.id')
.where('tags.tag', '=', tag)
.where('tags.value', 'in', value) as typeof query;
.where('tags.value', 'in', value);
}
}
@ -153,14 +153,27 @@ function getFilterQuery(filter: DittoFilter): EventQuery {
if (filter.relations?.includes('author')) {
query = query
.leftJoin(
(eb) =>
eb
(eb) => {
let exp: EventQuery = eb
.selectFrom('events')
.selectAll()
.where('kind', '=', 0)
.where('kind', '=', 0);
if (filter.relations?.includes('stats')) {
exp = exp
.leftJoin('pubkey_stats', 'pubkey_stats.pubkey', 'events.pubkey')
.select((eb) => [
eb.fn.coalesce('pubkey_stats.followers_count', eb.val(0)).as('author_stats_followers_count'),
eb.fn.coalesce('pubkey_stats.following_count', eb.val(0)).as('author_stats_following_count'),
eb.fn.coalesce('pubkey_stats.notes_count', eb.val(0)).as('author_stats_notes_count'),
]) as typeof exp;
}
return exp
.orderBy('created_at', 'desc')
.groupBy('pubkey')
.as('authors'),
.as('authors');
},
(join) => join.onRef('authors.pubkey', '=', 'events.pubkey'),
)
.select([
@ -171,7 +184,20 @@ function getFilterQuery(filter: DittoFilter): EventQuery {
'authors.tags as author_tags',
'authors.created_at as author_created_at',
'authors.sig as author_sig',
]) as typeof query;
'authors.author_stats_followers_count',
'authors.author_stats_following_count',
'authors.author_stats_notes_count',
]);
}
if (filter.relations?.includes('stats')) {
query = query
.leftJoin('event_stats', 'event_stats.event_id', 'events.id')
.select((eb) => [
eb.fn.coalesce('event_stats.replies_count', eb.val(0)).as('stats_replies_count'),
eb.fn.coalesce('event_stats.reposts_count', eb.val(0)).as('stats_reposts_count'),
eb.fn.coalesce('event_stats.reactions_count', eb.val(0)).as('stats_reactions_count'),
]);
}
if (filter.search) {

View File

@ -4,7 +4,7 @@ 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';
type Relation = 'author' | 'stats';
/** Custom filter interface that extends Nostr filters with extra options for Ditto. */
interface DittoFilter<K extends number = number> extends Filter<K> {