This article explains how to determine if a user has already reacted to an activity or to another reaction. While the Stream Feeds API does not have a built-in feature to block duplicate reactions, it provides the tools needed to implement this logic yourself.
You can determine if a user has added a reaction by fetching the feed with the "Own Reactions" boolean field set to true or using the Reaction Pagination endpoint.
Method 1: Fetch Feeds with Own Reactions
When fetching a feed using the Node.js library's get() method, you can request enriched activities that include the current user's reactions. This method is ideal for checking if the current user has reacted to an activity.
Example in node.js:
const client = new StreamAnalytics({
baseUrl: "https://eu-west-api.stream-io-api.com/api/v1.0/",
apiKey: "API_KEY",
token: "API_SECRET"
});
await client.feed("feed_type", "feed_id").get({
limit: 100,
offset: 0,
enrich: true,
withOwnReactions: true
});
Curl request example:
curl --location 'https://eu-west-api.stream-io-api.com/api/v1.0/enrich/feed/feed_type/feed_id?api_key=APIKey&limit=25&offset=0&withOwnReactions=true' \
--header 'Stream-Auth-Type: jwt' \
--header 'Authorization: JWToken' \
--header 'Content-Type: application/json' \
--data ''
Method 2: Reaction Pagination
The Reaction Pagination endpoint facilitates the combination of activity_id and user_id or reaction_id and user_id fields within the filter, which is useful for determining whether any user has reacted to a particular activity or reaction.
Example in node.js:
const client = new StreamAnalytics({
baseUrl: "https://eu-west-api.stream-io-api.com/api/v1.0/",
apiKey: "API_KEY",
token: "API_SECRET"
});
//Check if the user has added a reaction to an activity
client.reactions.filter({activity_id: 'activityId', filter_user_id: 'userId'})
//Check if the user has added a child reaction to a reaction
client.reactions.filter({reaction_id: 'reactionId', filter_user_id: 'userId'})
Note: This filter combination may not be supported across all libraries; please contact the Support team if you find any limitations.
Curl request example to check if the user has added a reaction to an activity:
curl --location 'https://eu-west-api.stream-io-api.com/api/v1.0/reaction/activity_id/YOUR_ACTIVITY_ID/?api_key=YOUR_APIKEY&filter_user_id=YOUR_USER_ID&limit=10&offset=0' \
--header 'Stream-Auth-Type: jwt' \
--header 'Authorization: YOUR_JWToken' \
--header 'Content-Type: application/json' \
--data ''
Curl request example to check if the user has added a child reaction to a reaction:
curl --location 'https://eu-west-api.stream-io-api.com/api/v1.0/reaction/reaction_id/YOUR_REACTION_ID/?api_key=YOUR_APIKEY&filter_user_id=YOUR_USER_ID&limit=10&offset=0' \
--header 'Stream-Auth-Type: jwt' \
--header 'Authorization: YOUR_JWToken' \
--header 'Content-Type: application/json' \
--data ''
Conclusion
Both methods are useful in different contexts. Fetching a feed with own reactions is great for a single API call to see if the current user has reacted to activities. The Reaction Pagination endpoint offers capabilities for backend integrations, enabling checks for any user's reactions, including nested child reactions.
Related articles:
- Generate a server auth token to access the REST API
- List of regional Base URLs / Domains - Feeds
- How can I Fetch Reactions with my own Included? - Feeds
- Can a real-time notification be triggered by a reaction such as a like or comment?
Comments
0 comments
Please sign in to leave a comment.