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 @@ -21,9 +21,23 @@ }, }; +type FarcasterChannel = { + +id: string, + +name: string, + ... +}; + +type FetchFollowedFarcasterChannelsResponse = { + +channels: $ReadOnlyArray, + +next: { + +cursor: ?string, + }, +}; + const neynarBaseURL = 'https://api.neynar.com/'; const neynarURLs = { '1': `${neynarBaseURL}v1/farcaster/`, + '2': `${neynarBaseURL}v2/farcaster/`, }; function getNeynarURL( apiVersion: string, @@ -39,6 +53,7 @@ } const fetchFollowerLimit = 150; +const fetchFollowedChannelsLimit = 100; class NeynarClient { apiKey: string; @@ -89,6 +104,49 @@ return fids; } + + async fetchFollowedFarcasterChannels( + fid: string, + ): Promise { + const farcasterChannels = []; + let paginationCursor = null; + + do { + const params: { [string]: string } = { + fid, + limit: fetchFollowedChannelsLimit.toString(), + ...(paginationCursor ? { cursor: paginationCursor } : null), + }; + + const url = getNeynarURL('2', 'user/channels', params); + + try { + const response = await fetch(url, { + method: 'GET', + headers: { + Accept: 'application/json', + api_key: this.apiKey, + }, + }); + + const json: FetchFollowedFarcasterChannelsResponse = + await response.json(); + + const { channels, next } = json; + + channels.forEach(channel => { + farcasterChannels.push(channel); + }); + + paginationCursor = next.cursor; + } catch (error) { + console.log('Failed to fetch followed Farcaster channels:', error); + throw new Error(getMessageForException(error) ?? 'unknown'); + } + } while (paginationCursor); + + return farcasterChannels; + } } export { NeynarClient };