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 @@ -3,19 +3,17 @@ import invariant from 'invariant'; import { getMessageForException } from './errors.js'; -import type { - NeynarChannel, - NeynarUser, - NeynarUserWithViewerContext, -} from '../types/farcaster-types.js'; - -type FetchFollowersResponse = { - +result: { - +users: $ReadOnlyArray, - +next: { - +cursor: ?string, +import type { NeynarChannel, NeynarUser } from '../types/farcaster-types.js'; + +type FetchRelevantFollowersResponse = { + +all_relevant_followers_dehydrated: $ReadOnlyArray<{ + +user: { + +fid: number, + ... }, - }, + ... + }>, + ... }; type FetchFarcasterChannelsResponse = { @@ -60,7 +58,6 @@ return `${neynarURL}${apiCall}?${new URLSearchParams(params).toString()}`; } -const fetchFollowerLimit = 150; const fetchFollowedChannelsLimit = 100; const fetchChannelsLimit = 50; @@ -73,48 +70,35 @@ // We're using the term "friend" for a bidirectional follow async fetchFriendFIDs(fid: string): Promise { - const fids = []; - let paginationCursor = null; - - do { - const params: { [string]: string } = { - fid, - viewerFid: fid, - limit: fetchFollowerLimit.toString(), - ...(paginationCursor ? { cursor: paginationCursor } : null), - }; - - const url = getNeynarURL('1', 'followers', params); - - try { - const response = await fetch(url, { - method: 'GET', - headers: { - Accept: 'application/json', - api_key: this.apiKey, - }, - }); + const params: { [string]: string } = { + target_fid: fid, + viewer_fid: fid, + }; - const json: FetchFollowersResponse = await response.json(); - const { users } = json.result; + const url = getNeynarURL('2', 'followers/relevant', params); - for (const user of users) { - if (user.viewerContext.following) { - fids.push(user.fid.toString()); - } - } + try { + const response = await fetch(url, { + method: 'GET', + headers: { + Accept: 'application/json', + api_key: this.apiKey, + }, + }); - paginationCursor = json.result.next.cursor; - } catch (error) { - console.log( - 'Failed to fetch friend FIDs:', - getMessageForException(error) ?? 'unknown', - ); - throw error; - } - } while (paginationCursor); + const json: FetchRelevantFollowersResponse = await response.json(); + const { all_relevant_followers_dehydrated } = json; - return fids; + return all_relevant_followers_dehydrated.map(follower => + follower.user.fid.toString(), + ); + } catch (error) { + console.log( + 'Failed to fetch friend FIDs:', + getMessageForException(error) ?? 'unknown', + ); + throw error; + } } async fetchFollowedFarcasterChannelsWithFilter(