hydrateEvents: handle hydrating users

This commit is contained in:
Alex Gleason 2024-03-30 23:39:06 -05:00
parent 21bec6b131
commit 957a82ce51
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 21 additions and 0 deletions

View File

@ -1,3 +1,4 @@
import { Conf } from '@/config.ts';
import { db } from '@/db.ts'; import { db } from '@/db.ts';
import { type NostrEvent, type NStore } from '@/deps.ts'; import { type NostrEvent, type NStore } from '@/deps.ts';
import { type DittoEvent } from '@/interfaces/DittoEvent.ts'; import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
@ -29,6 +30,9 @@ async function hydrateEvents(opts: HydrateEventOpts): Promise<DittoEvent[]> {
case 'event_stats': case 'event_stats':
await hydrateEventStats(events); await hydrateEventStats(events);
break; break;
case 'user':
await hydrateUsers({ events, storage, signal });
break;
} }
} }
@ -48,6 +52,23 @@ async function hydrateAuthors(opts: Omit<HydrateEventOpts, 'relations'>): Promis
return events; return events;
} }
async function hydrateUsers(opts: Omit<HydrateEventOpts, 'relations'>): Promise<DittoEvent[]> {
const { events, storage, signal } = opts;
const pubkeys = new Set([...events].map((event) => event.pubkey));
const users = await storage.query(
[{ kinds: [30361], authors: [Conf.pubkey], '#d': [...pubkeys], limit: pubkeys.size }],
{ signal },
);
for (const event of events) {
event.user = users.find((user) => user.tags.find(([name]) => name === 'd')?.[1] === event.pubkey);
}
return events;
}
async function hydrateAuthorStats(events: DittoEvent[]): Promise<DittoEvent[]> { async function hydrateAuthorStats(events: DittoEvent[]): Promise<DittoEvent[]> {
const results = await db const results = await db
.selectFrom('author_stats') .selectFrom('author_stats')