From af884d9710dd42c462437c02a53948b09d5d396f Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 23 Jan 2025 12:22:42 -0800 Subject: follows --- lib/nostr.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'lib/nostr.ts') diff --git a/lib/nostr.ts b/lib/nostr.ts index 21c254e3..9f31f6ad 100644 --- a/lib/nostr.ts +++ b/lib/nostr.ts @@ -151,12 +151,21 @@ export const getProfile = async (pubkey) => { export const getCount = async (pubkey) => { try { - const result = await scan({ authors: [pubkey], kinds: [3] }); - const follows = result.length - ? result[0].tags.filter((t) => t[0] === "p").length - : 0; + let follows = await g(`${pubkey}:follows:n`); + + if (!follows) { + const result = await get({ authors: [pubkey], kinds: [3] }); + follows = result ? result.tags.filter((t) => t[0] === "p").length : 0; + await db.set(`${pubkey}:follows:n`, follows, { EX: 300 }); + } + + let followers = await g(`${pubkey}:followers:n`); + + if (!followers) { + [followers] = await count({ "#p": [pubkey], kinds: [3] }); + await db.set(`${pubkey}:followers:n`, followers, { EX: 300 }); + } - const [followers] = await count({ "#p": [pubkey], kinds: [3] }); return { follows, followers }; } catch (e) { console.log(e); -- cgit v1.2.3