Page MenuHomePhabricator

D13099.id44446.diff
No OneTemporary

D13099.id44446.diff

diff --git a/keyserver/src/utils/fc-cache.js b/keyserver/src/utils/fc-cache.js
--- a/keyserver/src/utils/fc-cache.js
+++ b/keyserver/src/utils/fc-cache.js
@@ -4,7 +4,7 @@
import {
getFCNames as baseGetFCNames,
type GetFCNames,
- type BaseFCInfo,
+ type BaseFCNamesInfo,
} from 'lib/utils/farcaster-helpers.js';
import { FCCache } from 'lib/utils/fc-cache.js';
import { NeynarClient } from 'lib/utils/neynar-client.js';
@@ -24,7 +24,7 @@
}
neynarClient = new NeynarClient(neynarKey);
const fcCache = new FCCache(neynarClient);
- getFCNames = <T: ?BaseFCInfo>(users: $ReadOnlyArray<T>): Promise<T[]> =>
+ getFCNames = <T: ?BaseFCNamesInfo>(users: $ReadOnlyArray<T>): Promise<T[]> =>
baseGetFCNames(fcCache, users);
}
diff --git a/lib/components/neynar-client-provider.react.js b/lib/components/neynar-client-provider.react.js
--- a/lib/components/neynar-client-provider.react.js
+++ b/lib/components/neynar-client-provider.react.js
@@ -4,8 +4,11 @@
import {
getFCNames as baseGetFCNames,
- type BaseFCInfo,
+ getFCAvatarURLs as baseGetFCAvatarURLs,
+ type BaseFCNamesInfo,
+ type BaseFCAvatarInfo,
type GetFCNames,
+ type GetFCAvatarURLs,
} from '../utils/farcaster-helpers.js';
import { FCCache } from '../utils/fc-cache.js';
import { NeynarClient } from '../utils/neynar-client.js';
@@ -14,6 +17,7 @@
+client: NeynarClient,
+fcCache: FCCache,
+getFCNames: GetFCNames,
+ +getFCAvatarURLs: GetFCAvatarURLs,
};
const NeynarClientContext: React.Context<?NeynarClientContextType> =
@@ -38,13 +42,17 @@
return null;
}
const fcCache = new FCCache(neynarClient);
- const getFCNames: GetFCNames = <T: ?BaseFCInfo>(
+ const getFCNames: GetFCNames = <T: ?BaseFCNamesInfo>(
users: $ReadOnlyArray<T>,
): Promise<T[]> => baseGetFCNames(fcCache, users);
+ const getFCAvatarURLs: GetFCAvatarURLs = (
+ fids: $ReadOnlyArray<string>,
+ ): Promise<BaseFCAvatarInfo[]> => baseGetFCAvatarURLs(fcCache, fids);
return {
client: neynarClient,
fcCache,
getFCNames,
+ getFCAvatarURLs,
};
}, [neynarClient]);
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
@@ -2,16 +2,24 @@
import { FCCache } from './fc-cache.js';
-export type BaseFCInfo = {
+export type BaseFCNamesInfo = {
+fid?: ?string,
+farcasterUsername?: ?string,
...
};
-export type GetFCNames = <T: ?BaseFCInfo>(
+export type BaseFCAvatarInfo = {
+ +fid: string,
+ +pfpURL: ?string,
+};
+
+export type GetFCNames = <T: ?BaseFCNamesInfo>(
users: $ReadOnlyArray<T>,
) => Promise<T[]>;
+export type GetFCAvatarURLs = (
+ fids: $ReadOnlyArray<string>,
+) => Promise<BaseFCAvatarInfo[]>;
-async function getFCNames<T: ?BaseFCInfo>(
+async function getFCNames<T: ?BaseFCNamesInfo>(
fcCache: FCCache,
users: $ReadOnlyArray<T>,
): Promise<T[]> {
@@ -76,4 +84,57 @@
});
}
-export { getFCNames };
+async function getFCAvatarURLs(
+ fcCache: FCCache,
+ fids: $ReadOnlyArray<string>,
+): Promise<BaseFCAvatarInfo[]> {
+ const info = fids.map(fid => {
+ const cachedResult = fcCache.getCachedFarcasterUserForFID(fid)?.pfpURL;
+ return {
+ fid,
+ cachedResult,
+ };
+ });
+
+ const needFetch = info
+ .map(user => {
+ if (!user) {
+ return null;
+ }
+ const { fid, cachedResult } = user;
+ if (cachedResult) {
+ return null;
+ }
+ return fid;
+ })
+ .filter(Boolean);
+
+ const pfpURLs = new Map<string, string>();
+ if (needFetch.length > 0) {
+ const results = await fcCache.getFarcasterUsersForFIDs(needFetch);
+ for (let i = 0; i < needFetch.length; i++) {
+ const fid = needFetch[i];
+ const result = results[i];
+ if (result) {
+ pfpURLs.set(fid, result.pfpURL);
+ }
+ }
+ }
+
+ return info.map(user => {
+ if (!user) {
+ return user;
+ }
+ const { fid, cachedResult } = user;
+ if (cachedResult) {
+ return { fid, pfpURL: cachedResult };
+ }
+ const pfpURL = pfpURLs.get(fid);
+ if (pfpURL) {
+ return { fid, pfpURL };
+ }
+ return { fid, pfpURL: null };
+ });
+}
+
+export { getFCNames, getFCAvatarURLs };

File Metadata

Mime Type
text/plain
Expires
Wed, Nov 27, 2:24 AM (20 h, 7 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2587365
Default Alt Text
D13099.id44446.diff (4 KB)

Event Timeline