Refresh author stats: less naive way

This commit is contained in:
Alex Gleason 2024-05-17 17:50:30 -05:00
parent 17b6330193
commit 5c2e3450a9
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 29 additions and 0 deletions

View File

@ -1,10 +1,12 @@
import { NostrEvent, NStore } from '@nostrify/nostrify';
import { LRUCache } from 'lru-cache';
import { matchFilter } from 'nostr-tools';
import { DittoDB } from '@/db/DittoDB.ts';
import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
import { DittoTables } from '@/db/DittoTables.ts';
import { Conf } from '@/config.ts';
import { refreshAuthorStats } from '@/stats.ts';
interface HydrateOpts {
events: DittoEvent[];
@ -55,6 +57,8 @@ async function hydrateEvents(opts: HydrateOpts): Promise<DittoEvent[]> {
events: await gatherEventStats(cache),
};
requestMissingAuthorStats(events, stats.authors);
// Dedupe events.
const results = [...new Map(cache.map((event) => [event.id, event])).values()];
@ -266,6 +270,31 @@ async function gatherAuthorStats(events: DittoEvent[]): Promise<DittoTables['aut
}));
}
function requestMissingAuthorStats(events: NostrEvent[], stats: DittoTables['author_stats'][]) {
const pubkeys = new Set<string>(
events
.filter((event) => event.kind === 0)
.map((event) => event.pubkey),
);
const missing = pubkeys.difference(
new Set(stats.map((stat) => stat.pubkey)),
);
for (const pubkey of missing) {
refreshAuthorStatsDebounced(pubkey);
}
}
const lru = new LRUCache<string, true>({ max: 1000 });
/** Calls `refreshAuthorStats` only once per author. */
function refreshAuthorStatsDebounced(pubkey: string): void {
if (lru.get(pubkey)) return;
lru.set(pubkey, true);
refreshAuthorStats(pubkey).catch(() => {});
}
/** Collect event stats from the events. */
async function gatherEventStats(events: DittoEvent[]): Promise<DittoTables['event_stats'][]> {
const ids = new Set<string>(