Reactions

Adding Reactions

Reactions are a special kind of data that can be used to capture user interaction with specific activities. Common examples of reactions are likes, comments, and upvotes. Reactions are automatically returned to feeds’ activities at read time when the reactions parameters are used.

Reactions are always related to activities; in order to add a reaction to an activity you need to provide its ID.

Parameters

nametypedescriptiondefaultoptional
kindstringThe type of reaction (eg. like, comment, …)-
activity_idstringThe ID of the activity the reaction refers to-
dataobjectAdditional data to attach to the reaction-
target_feedsarrayThe feeds that should receive a notification activity-
target_feeds_extra_dataobjectAdditional data to attach to the notification activities-
user_idstringId of the user adding the reaction if not own user. Required for server side.-
// add a like reaction to the activity with id activityID
	r := stream.AddReactionRequestObject{
		Kind:    "like",
		ActivityID: activityID,
	}
	like, err := client.Reactions().Add(context.TODO(), r)
	if err != nil {
		panic(err)
	}

	// adds a comment reaction to the activity with id activityId
	r = stream.AddReactionRequestObject{
		Kind:    "comment",
		ActivityID: activityID,
		UserID:   "bob",
	}
	comment, err := client.Reactions().Add(context.TODO(), r)
	if err != nil {
		panic(err)
	}

Here’s a complete example:

// first let's read current user's timeline feed and pick one activity
	feed, err := client.FlatFeed("timeline", "bob")
	if err != nil {
		panic(err)
	}

	resp, err := feed.GetActivities(context.TODO())
	if err != nil {
		panic(err)
	}
	activity := resp.Results[0]

	// then let's add a like reaction to that activity
	r := stream.AddReactionRequestObject{
		Kind:    "like",
		ActivityID: activity.ID,
		UserID:   "bob",
	}
	_, err := client.Reactions().Add(context.TODO(), r)
	if err != nil {
		panic(err)
	}

The size of a reaction can not exceed 10kB

Retrieving Reactions

You can read reactions and filter them based on their user_id or activity_id values. Further filtering can be done with the kind parameter (e.g. retrieve all likes by one user, retrieve all comments for one activity, etc.).

Reactions are returned in descending order (newest to oldest) by default and when using id_lt[e], and in ascending order (oldest to newest) when using id_gt[e].

Parameters

nametypedescriptiondefaultoptional
activity_idstringRetrieve reactions by activity_id-
user_idstringRetrieve reactions by user_id-
reaction_idstringRetrieve children reaction by reaction_id-
kindstringIf provided it will only retrieve reactions of a certain kind (e.g. “like”)-
limitintegerThe number of reactions to retrieve (Max. 25)10
id_gtestringRetrieve reactions created after the one with ID equal to the parameter (inclusive)-
id_gtstringRetrieve reactions created after the one with ID equal to the parameter.-
id_ltestringRetrieve reactions created before the one with ID equal to the parameter (inclusive)-
id_ltstringRetrieve reactions before the one with ID equal to the parameter-
with_activity_databooleanReturns activity data when paginating using activity_id-
with_own_childrenbooleanEnable returning the children reactions when filtering reactions by parent ID-
// retrieve all kind of reactions for an activity
	resp, err := client.Reactions().Filter(context.TODO(), stream.ByActivityID("ed2837a6-0a3b-4679-adc1-778a1704852d"))
	if err != nil {
		panic(err)
	}

	// retrieve first 10 likes for an activity
	resp, err := client.Reactions().Filter(
		context.TODO(),
		stream.ByActivityID("ed2837a6-0a3b-4679-adc1-778a1704852d").ByKind("like"),
		stream.WithLimit(10),
	)
	if err != nil {
		panic(err)
	}

	// retrieve the next 10 likes using the id_lt param
	filterAttribute := stream.ByActivityID("ed2837a6-0a3b-4679-adc1-778a1704852d").ByKind("like")
	pagination := stream.WithIDLT("e561de8f-00f1-11e4-b400-0cc47a024be0")
	resp, err := client.Reactions().Filter(context.TODO(), filterAttribute, pagination)
	if err != nil {
		panic(err)
	}

Retrieving Reactions by ID

resp, err := client.Reactions().Get(context.TODO(), id)
if err != nil {
  panic(err)
}

Updating Reactions

Reactions can be updated by providing reaction ID parameter. Changes to reactions are propagated to all notified feeds; if the target_feeds list is updated, notifications will be added and removed accordingly.

Parameters

nametypedescriptiondefaultoptional
reaction_idstringThe ID of the reaction-
dataobjectReaction data-
target_feedsstringThe list of feeds that should receive a copy of the reaction.-
resp, err := client.Reactions().Update(context.TODO(), reactionID, map[string]any{"text": "love it!"}, nil)
	if err != nil {
		panic(err)
	}

Removing Reactions

Reactions are easy to remove. Simply pass in their ID, like so:

_, err := client.Reactions().Delete(context.TODO(), reactionID)
	if err != nil {
		panic(err)
	}

Soft delete and restore.

This feature is currently supported only selected SDKs. If you need support for other SDKs, please contact support.

Reactions can be soft deleted and restored by ID:

await client.Reactions.DeleteAsync(reactionId, soft: true);
await client.Reactions.RestoreSoftDeletedAsync(reactionId);
© Getstream.io, Inc. All Rights Reserved.