Simplify author_stats relation

This commit is contained in:
Alex Gleason 2023-12-10 15:54:31 -06:00
parent 5415656b4d
commit 07dc07ab71
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 28 additions and 35 deletions

View file

@ -153,27 +153,14 @@ function getFilterQuery(filter: DittoFilter): EventQuery {
if (filter.relations?.includes('author')) { if (filter.relations?.includes('author')) {
query = query query = query
.leftJoin( .leftJoin(
(eb) => { (eb) =>
let exp: EventQuery = eb eb
.selectFrom('events') .selectFrom('events')
.selectAll() .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') .orderBy('created_at', 'desc')
.groupBy('events.pubkey') .groupBy('pubkey')
.as('authors'); .as('authors'),
},
(join) => join.onRef('authors.pubkey', '=', 'events.pubkey'), (join) => join.onRef('authors.pubkey', '=', 'events.pubkey'),
) )
.select([ .select([
@ -184,17 +171,20 @@ function getFilterQuery(filter: DittoFilter): EventQuery {
'authors.tags as author_tags', 'authors.tags as author_tags',
'authors.created_at as author_created_at', 'authors.created_at as author_created_at',
'authors.sig as author_sig', 'authors.sig as author_sig',
...(filter.relations?.includes('stats')
? [
'authors.author_stats_followers_count',
'authors.author_stats_following_count',
'authors.author_stats_notes_count',
] as const
: []),
]); ]);
} }
if (filter.relations?.includes('stats')) { if (filter.relations?.includes('author_stats')) {
query = query
.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'),
]);
}
if (filter.relations?.includes('event_stats')) {
query = query query = query
.leftJoin('event_stats', 'event_stats.event_id', 'events.id') .leftJoin('event_stats', 'event_stats.event_id', 'events.id')
.select((eb) => [ .select((eb) => [
@ -262,14 +252,14 @@ async function getFilters<K extends number>(
tags: JSON.parse(row.author_tags!), tags: JSON.parse(row.author_tags!),
sig: row.author_sig!, sig: row.author_sig!,
}; };
}
if (typeof row.author_stats_followers_count === 'number') { if (typeof row.author_stats_followers_count === 'number') {
event.author.author_stats = { event.author_stats = {
followers_count: row.author_stats_followers_count, followers_count: row.author_stats_followers_count,
following_count: row.author_stats_following_count!, following_count: row.author_stats_following_count!,
notes_count: row.author_stats_notes_count!, notes_count: row.author_stats_notes_count!,
}; };
}
} }
if (typeof row.stats_replies_count === 'number') { if (typeof row.stats_replies_count === 'number') {

View file

@ -4,7 +4,7 @@ import { type Event, type Filter, matchFilters } from '@/deps.ts';
import type { EventData } from '@/types.ts'; import type { EventData } from '@/types.ts';
/** Additional properties that may be added by Ditto to events. */ /** Additional properties that may be added by Ditto to events. */
type Relation = 'author' | 'stats'; type Relation = 'author' | 'author_stats' | 'event_stats';
/** 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> {

View file

@ -13,7 +13,10 @@ import { DittoAttachment, renderAttachment } from '@/views/mastodon/attachments.
import { renderEmojis } from '@/views/mastodon/emojis.ts'; import { renderEmojis } from '@/views/mastodon/emojis.ts';
async function renderStatus(event: eventsDB.DittoEvent<1>, viewerPubkey?: string) { async function renderStatus(event: eventsDB.DittoEvent<1>, viewerPubkey?: string) {
const account = event.author ? await renderAccount(event.author) : await accountFromPubkey(event.pubkey); const account = event.author
? await renderAccount({ ...event.author, author_stats: event.author_stats })
: await accountFromPubkey(event.pubkey);
const replyTag = findReplyTag(event); const replyTag = findReplyTag(event);
const mentionedPubkeys = [ const mentionedPubkeys = [