draft: attempt to fix followers count

This commit is contained in:
P. Reis 2024-04-17 21:34:29 -03:00
parent a07d018ea6
commit c43df106d8

View file

@ -22,6 +22,9 @@ async function updateStats(event: NostrEvent) {
prev = await maybeGetPrev(event); prev = await maybeGetPrev(event);
if (!prev || event.created_at >= prev.created_at) { if (!prev || event.created_at >= prev.created_at) {
queries.push(updateFollowingCountQuery(event)); queries.push(updateFollowingCountQuery(event));
const followersQuery = await updateFollowersCountQuery(event);
if (followersQuery) queries.push(followersQuery);
} }
} }
@ -179,6 +182,30 @@ function updateFollowingCountQuery({ pubkey, tags }: NostrEvent) {
); );
} }
async function updateFollowersCountQuery({ tags }: NostrEvent) {
const userThatHasFollowersPub = tags.find(([name]) => name === 'p')?.[1];
if (!userThatHasFollowersPub) return;
const result = await eventsDB.query([{ kinds: [3], '#p': [userThatHasFollowersPub] }], {
signal: AbortSignal.timeout(1000),
});
if (result.length <= 0) return;
return db.insertInto('author_stats')
.values({
pubkey: userThatHasFollowersPub,
following_count: 0,
followers_count: result.length,
notes_count: 0,
})
.onConflict((oc) =>
oc
.column('pubkey')
.doUpdateSet({ followers_count: result.length })
);
}
/** Compare the old and new follow events (if any), and return a diff array. */ /** Compare the old and new follow events (if any), and return a diff array. */
function getFollowDiff(event: NostrEvent, prev?: NostrEvent): AuthorStatDiff[] { function getFollowDiff(event: NostrEvent, prev?: NostrEvent): AuthorStatDiff[] {
const prevTags = prev?.tags ?? []; const prevTags = prev?.tags ?? [];