diff --git a/lib/hooks/fc-cache.js b/lib/hooks/fc-cache.js --- a/lib/hooks/fc-cache.js +++ b/lib/hooks/fc-cache.js @@ -32,7 +32,7 @@ if (farcasterUsername) { cachedResult = farcasterUsername; } else if (fid && fcCache) { - cachedResult = fcCache.getCachedFarcasterUsernameForFID(fid); + cachedResult = fcCache.getCachedFarcasterUserForFID(fid); } return { input: user, @@ -106,13 +106,13 @@ for (const fid of needFetchFIDs) { void (async () => { - const [result] = await fcCache.getFarcasterUsernamesForFIDs([fid]); + const [result] = await fcCache.getFarcasterUsersForFIDs([fid]); if (!result) { return; } setFarcasterUsernames(oldFarcasterUsernames => { const newFarcasterUsernames = new Map(oldFarcasterUsernames); - newFarcasterUsernames.set(fid, result); + newFarcasterUsernames.set(fid, result.username); return newFarcasterUsernames; }); })(); diff --git a/lib/utils/farcaster-helpers.js b/lib/utils/farcaster-helpers.js --- a/lib/utils/farcaster-helpers.js +++ b/lib/utils/farcaster-helpers.js @@ -24,7 +24,7 @@ if (farcasterUsername) { cachedResult = farcasterUsername; } else if (fid) { - cachedResult = fcCache.getCachedFarcasterUsernameForFID(fid); + cachedResult = fcCache.getCachedFarcasterUserForFID(fid)?.username; } return { input: user, @@ -48,12 +48,12 @@ const farcasterUsernames = new Map(); if (needFetch.length > 0) { - const results = await fcCache.getFarcasterUsernamesForFIDs(needFetch); + const results = await fcCache.getFarcasterUsersForFIDs(needFetch); for (let i = 0; i < needFetch.length; i++) { const fid = needFetch[i]; const result = results[i]; if (result) { - farcasterUsernames.set(fid, result); + farcasterUsernames.set(fid, result.username); } } } diff --git a/lib/utils/fc-cache.js b/lib/utils/fc-cache.js --- a/lib/utils/fc-cache.js +++ b/lib/utils/fc-cache.js @@ -1,6 +1,6 @@ // @flow -import { NeynarClient } from './neynar-client.js'; +import { NeynarClient, type FarcasterUser } from './neynar-client.js'; import sleep from './sleep.js'; const cacheTimeout = 24 * 60 * 60 * 1000; // one day @@ -15,13 +15,13 @@ type FarcasterUsernameQueryCacheEntry = { +fid: string, +expirationTime: number, - +farcasterUsername: ?string | Promise, + +farcasterUser: ?FarcasterUser | Promise, }; class FCCache { client: NeynarClient; - // Maps from FIDs to a cache entry for its Farcaster username + // Maps from FIDs to a cache entry for its Farcaster user farcasterUsernameQueryCache: Map = new Map(); @@ -29,15 +29,15 @@ this.client = client; } - getFarcasterUsernamesForFIDs( + getFarcasterUsersForFIDs( fids: $ReadOnlyArray, - ): Promise> { + ): Promise> { const cacheMatches = fids.map(fid => - this.getCachedFarcasterUsernameEntryForFID(fid), + this.getCachedFarcasterUserEntryForFID(fid), ); const cacheResultsPromise = Promise.all( cacheMatches.map(match => - Promise.resolve(match ? match.farcasterUsername : match), + Promise.resolve(match ? match.farcasterUser : match), ), ); if (cacheMatches.every(Boolean)) { @@ -53,57 +53,54 @@ } } - const fetchFarcasterUsernamesPromise = (async () => { - let farcasterUsernames: $ReadOnlyArray; + const fetchFarcasterUsersPromise = (async () => { + let farcasterUsers: $ReadOnlyArray; try { - const farcasterUsers = await Promise.race([ + farcasterUsers = await Promise.race([ this.client.getFarcasterUsers(needFetch), - throwOnTimeout(`usernames for ${JSON.stringify(needFetch)}`), + throwOnTimeout(`users for ${JSON.stringify(needFetch)}`), ]); - farcasterUsernames = farcasterUsers.map( - farcasterUser => farcasterUser?.username, - ); } catch (e) { console.log(e); - farcasterUsernames = new Array(needFetch.length).fill(null); + farcasterUsers = new Array(needFetch.length).fill(null); } - const resultMap = new Map(); + const resultMap = new Map(); for (let i = 0; i < needFetch.length; i++) { const fid = needFetch[i]; - const farcasterUsername = farcasterUsernames[i]; - resultMap.set(fid, farcasterUsername); + const farcasterUser = farcasterUsers[i]; + resultMap.set(fid, farcasterUser); } return resultMap; })(); for (let i = 0; i < needFetch.length; i++) { const fid = needFetch[i]; - const fetchFarcasterUsernamePromise = (async () => { - const resultMap = await fetchFarcasterUsernamesPromise; + const fetchFarcasterUserPromise = (async () => { + const resultMap = await fetchFarcasterUsersPromise; return resultMap.get(fid) ?? null; })(); this.farcasterUsernameQueryCache.set(fid, { fid, expirationTime: Date.now() + queryTimeout * 2, - farcasterUsername: fetchFarcasterUsernamePromise, + farcasterUser: fetchFarcasterUserPromise, }); } return (async () => { const [resultMap, cacheResults] = await Promise.all([ - fetchFarcasterUsernamesPromise, + fetchFarcasterUsersPromise, cacheResultsPromise, ]); for (let i = 0; i < needFetch.length; i++) { const fid = needFetch[i]; - const farcasterUsername = resultMap.get(fid); + const farcasterUser = resultMap.get(fid); const timeout = - farcasterUsername === null ? failedQueryCacheTimeout : cacheTimeout; + farcasterUser === null ? failedQueryCacheTimeout : cacheTimeout; this.farcasterUsernameQueryCache.set(fid, { fid, expirationTime: Date.now() + timeout, - farcasterUsername, + farcasterUser, }); } @@ -120,7 +117,7 @@ })(); } - getCachedFarcasterUsernameEntryForFID( + getCachedFarcasterUserEntryForFID( fid: string, ): ?FarcasterUsernameQueryCacheEntry { const cacheResult = this.farcasterUsernameQueryCache.get(fid); @@ -137,18 +134,18 @@ return cacheResult; } - getCachedFarcasterUsernameForFID(fid: string): ?string { - const cacheResult = this.getCachedFarcasterUsernameEntryForFID(fid); + getCachedFarcasterUserForFID(fid: string): ?FarcasterUser { + const cacheResult = this.getCachedFarcasterUserEntryForFID(fid); if (!cacheResult) { return undefined; } - const { farcasterUsername } = cacheResult; - if (typeof farcasterUsername !== 'string') { + const { farcasterUser } = cacheResult; + if (typeof farcasterUser !== 'object' || farcasterUser instanceof Promise) { return undefined; } - return farcasterUsername; + return farcasterUser; } }