Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F32583108
D11675.1767401564.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
D11675.1767401564.diff
View Options
diff --git a/lib/utils/neynar-client.js b/lib/utils/neynar-client.js
--- a/lib/utils/neynar-client.js
+++ b/lib/utils/neynar-client.js
@@ -2,6 +2,8 @@
import invariant from 'invariant';
+import { getMessageForException } from './errors.js';
+
type FarcasterUser = {
+fid: number,
+viewerContext: {
@@ -14,7 +16,7 @@
+result: {
+users: $ReadOnlyArray<FarcasterUser>,
+next: {
- +cursor: string,
+ +cursor: ?string,
},
},
};
@@ -48,35 +50,44 @@
// We're using the term "friend" for a bidirectional follow
async fetchFriendFIDs(fid: string): Promise<string[]> {
const fids = [];
- const params: { [string]: string } = {
- fid,
- viewerFid: fid,
- limit: fetchFollowerLimit.toString(),
- };
- while (true) {
+ let paginationCursor = null;
+
+ do {
+ const params: { [string]: string } = {
+ fid,
+ viewerFid: fid,
+ limit: fetchFollowerLimit.toString(),
+ ...(paginationCursor ? { cursor: paginationCursor } : null),
+ };
+
const url = getNeynarURL('1', 'followers', params);
- const response = await fetch(url, {
- method: 'GET',
- headers: {
- Accept: 'application/json',
- api_key: this.apiKey,
- },
- });
- const json: FetchFollowersResponse = await response.json();
- const { users } = json.result;
- for (const user of users) {
- if (!user.viewerContext.following) {
- continue;
+
+ try {
+ const response = await fetch(url, {
+ method: 'GET',
+ headers: {
+ Accept: 'application/json',
+ api_key: this.apiKey,
+ },
+ });
+
+ const json: FetchFollowersResponse = await response.json();
+ const { users } = json.result;
+
+ for (const user of users) {
+ if (user.viewerContext.following) {
+ fids.push(user.fid.toString());
+ }
}
- fids.push(user.fid.toString());
- }
- if (users.length < fetchFollowerLimit) {
- return fids;
+
+ paginationCursor = json.result.next.cursor;
+ } catch (error) {
+ console.log('Failed to fetch friend FIDs:', error);
+ throw new Error(getMessageForException(error) ?? 'unknown');
}
- params.cursor = json.result.next.cursor;
- }
- // eslint-disable-next-line no-unreachable
- throw new Error('should not get here');
+ } while (paginationCursor);
+
+ return fids;
}
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Jan 3, 12:52 AM (6 h, 28 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5884656
Default Alt Text
D11675.1767401564.diff (2 KB)
Attached To
Mode
D11675: [lib] refine fetchFriendFIDs function
Attached
Detach File
Event Timeline
Log In to Comment