// Add a reaction to an activity
let reaction = try await feed.addReaction(
activityId: "activity_123",
request: .init(
custom: ["emoji": "❤️"],
type: "like"
)
)
// Remove a reaction
_ = try await feed.deleteReaction(activityId: "activity_123", type: "like")Reactions
Overview
You can react to both activities and comments. It's possible to configure any reaction types that best fit your app.
When adding reactions and the enforce_unique flag is set to true, the existing reaction of a user will be overridden with the new reaction. Use this flag if you want to ensure users have a single reaction per each comment/activity. The default value is false, in which case users can have multiple reactions.
// Add a reaction to an activity
_, err = client.Feeds().AddReaction(context.Background(), activityID, &getstream.AddReactionRequest{
Type: "like",
Custom: map[string]any{
"emoji": "❤️",
},
UserID: getstream.PtrTo("john"),
})
if err != nil {
log.Fatal("Error adding reaction:", err)
}
// Adding a reaction without triggering push notifications
_, err = client.Feeds().AddReaction(context.Background(), activityID, &getstream.AddReactionRequest{
Type: "like",
Custom: map[string]any{
"emoji": "❤️",
},
UserID: getstream.PtrTo("john"),
SkipPush: getstream.PtrTo(true),
})
if err != nil {
log.Fatal("Error adding reaction without push:", err)
}
// Add a reaction to a comment
_, err = client.Feeds().AddCommentReaction(context.Background(), commentID, &getstream.AddCommentReactionRequest{
Type: "like",
Custom: map[string]any{
"emoji": "👍",
},
UserID: getstream.PtrTo("john"),
})
if err != nil {
log.Fatal("Error adding comment reaction:", err)
}
// Adding a comment reaction without triggering push notifications
_, err = client.Feeds().AddCommentReaction(context.Background(), commentID, &getstream.AddCommentReactionRequest{
Type: "like",
Custom: map[string]any{
"emoji": "👍",
},
UserID: getstream.PtrTo("john"),
SkipPush: getstream.PtrTo(true),
})
if err != nil {
log.Fatal("Error adding comment reaction without push:", err)
}
// Delete activity reaction
_, err = client.Feeds().DeleteActivityReaction(context.Background(), activityID, "like", &getstream.DeleteActivityReactionRequest{
UserID: getstream.PtrTo("john"),
})
if err != nil {
log.Fatal("Error deleting activity reaction:", err)
}Overview of the reaction model
By default creating a reaction doesn't create an activity.
When you read a feed the reactions are included. Here's an example:
feed := client.Feeds().Feed("user", "john")
feedResponse, err := feed.GetOrCreate(context.Background(), &getstream.GetOrCreateFeedRequest{
UserID: getstream.PtrTo("john"),
})
fmt.Println(feedResponse.Data.Activities[0].OwnReactions)
fmt.Println(feedResponse.Data.Activities[0].LatestReactions)
fmt.Println(feedResponse.Data.Activities[0].ReactionGroups)Querying Reactions
You can query reactions to both activities and comments. Here are some examples:
// Query reactions to a specific activity
val activityReactionList = client.activityReactionList(
ActivityReactionsQuery(
activityId = "activity-123",
filter = ActivityReactionsFilterField.reactionType.equal("like"),
)
)
activityReactionList.get()
activityReactionList.state.reactions.collect { reactions ->
// Handle activity reactions
}
// Query reactions to a specific comment
val commentReactionList = client.commentReactionList(
CommentReactionsQuery(
commentId = "comment-456",
filter = CommentReactionsFilterField.reactionType.equal("like"),
)
)
commentReactionList.get()
commentReactionList.state.reactions.collect { reactions ->
// Handle comment reactions
}Reaction Queryable Built-In Fields
| name | type | description | supported operations | example |
|---|---|---|---|---|
reaction_type | string or list of strings | The type of reaction | $in, $eq | { reaction_type: { $in: [ 'like', 'heart', 'thumbs_up' ] } } |
user_id | string or list of strings | The ID of the user who created the reaction | $in, $eq | { user_id: { $eq: 'user_123' } } |
created_at | string, must be formatted as an RFC3339 timestamp | The time the reaction was created | $eq, $gt, $gte, $lt, $lte | { created_at: { $gte: '2023-12-04T09:30:20.45Z' } } |