Build "Sort Replies" on a cast using Neynar and OpenRanks' Global Ranking API
Sort Direct Replies in a cast for your client
Last updated
Sort Direct Replies in a cast for your client
Last updated
To gather cast information, utilize Neynar by providing a URL identifier. While we're using constants here, you can also obtain these details dynamically from feeds or other sources.
This could also be done using castHashes just change the Identifier to the cast_hash
and cast type to hash
In this step, we'll establish an asynchronous function named getCastWithResponses
. This function takes three parameters: castIdentifier
, castType
, and viewerFID
, and returns the cast along with its responses in an array.
We invoke this function and store the results in a variable named castWithResponses
. Then, we define another variable called castDirectReplies
, which is an array of objects containing all the direct replies. Later, we'll utilize this castDirectReplies
array to generate and store global ranks for each reply.
In this step, we use OpenRanks APIs to determine the global rank of users who replied directly. We define two functions:
fetchGlobalRanks
: This function takes an array of FIDs and returns their corresponding global ranks. As the API can handle only up to 100 FIDs at a time, we need to batch process the FIDs.
getAllGlobalRanks
: This function takes an array of FIDs similar to fetchGlobalRanks
, but it divides the input array into chunks of 100 elements each to comply with the API requirement. It then retrieves global ranks for each chunk and aggregates them into a single array, which is returned. Thus, getAllGlobalRanks
can process an array of any number of FIDs and return the global ranks for all of them.
Now that we have the two functions defined, let's create an array of FIDs based on the authors of the replies. We'll achieve this using a simple map function and store the FIDs of all repliers in a variable called fidsOfRepliers
. Then, we'll pass this array as an argument to the getAllGlobalRanks
function and store the returned array of global ranks in a variable called usersGlobalRankResponseArray
.
In this step, we'll create a new array called castDirectRepliesWithGlobalRank
. As the name suggests, it's essentially the same array as castDirectReplies
from Step 2, but with the addition of global ranks. We'll achieve this by declaring a utility function called addGlobalRank
, which takes two arrays as parameters: the first array is castWithDirectReplies
, and the second one is usersGlobalRankResponses
. It matches the FID and adds the global rank to each object.
At the conclusion of this step, we now have a variable named castDirectRepliesWithGlobalRank
. This variable contains an array comprising all the direct replies to the focused cast. Additionally, each reply within this array includes the global rank of its respective author.
To complete the process, we need to sort the castDirectRepliesWithGlobalRank
array in ascending order based on the authors' global ranks. To achieve this, we'll define a generalized function named sortByRankAscending
. This function takes an array as input and returns a sorted array in ascending order based on the authors' global rank.
Now, you have an array sorted based on the replies' authors' global rank. This can be utilized as a method to surface relevant replies first.