Activity Feeds v3 is in beta — try it out!

Activities

Creating Activities

The example below shows how to create an activity and add it to a feed.

// Add an activity to 1 feed
val activity: Result<ActivityData> = feed.addActivity(
    request = FeedAddActivityRequest(
        text = "hello world",
        type = "post"
    )
)

// Add an activity to multiple feeds
val multiFeedActivity: Result<ActivityData> = client.addActivity(
    request = AddActivityRequest(
        fids = listOf("user:1", "stock:apple"),
        text = "apple stock will go up",
        type = "post"
    )
)

The above example was quite simple. Here are a few more examples:

Image & Video

val imageActivity: Result<ActivityData> = feed.addActivity(
    request = FeedAddActivityRequest(
        attachments = listOf(
            Attachment(
                imageUrl = "https://example.com/image.jpg",
                type = "image"
            )
        ),
        text = "look at NYC",
        type = "post"
    )
)

Sharing activities

When creating an activity it’s possible to set parent_id, setting this field will increase the share_count of the parent activity. This feature lets you implement “retweets”.

When reading an activity with parent id set, you can access the parent activity with activity.parent.

val activity: Result<ActivityData> = feed.addActivity(
    request = FeedAddActivityRequest(
        text = "Couldn't agree more",
        type = "post",
        parentId = "<activity to share>"
    )
)

Reading Activities with Enrichment

When you read activities from feeds, they are automatically enriched with additional data:

  • Comments: The latest 5 top-level comments (replies not included by default)
  • Reactions: Recent reactions and reaction counts
  • User data: Information about the activity author

To load more comments, replies, or additional data, use the dedicated loading methods described in the Comments and Reactions sections.

Overview of All Activity Fields

ActivityResponse

NameTypeDescriptionConstraints
attachmentsAttachment[]Media attachments for the activityRequired
bookmark_countintegerNumber of bookmarks on the activityRequired
collectionsobjectEnriched collection data referenced by this activityRequired
comment_countintegerNumber of comments on the activityRequired
commentsCommentResponse[]Comments on this activityRequired
created_atnumberWhen the activity was createdRequired
current_feedFeedResponseFeed context for this activity view. If an activity is added only to one feed, it's always set. If an activity is added to multiple feeds, it's only set when calling the GetOrCreateFeed endpoint.-
customobjectCustom data for the activityRequired
deleted_atnumberWhen the activity was deleted-
edited_atnumberWhen the activity was last edited-
expires_atnumberWhen the activity will expire-
feedsstring[]List of feed IDs containing this activityRequired
filter_tagsstring[]Tags for filteringRequired
hiddenbooleanIf this activity is hidden by this user (using activity feedback)Required
idstringUnique identifier for the activityRequired
interest_tagsstring[]Tags for user interestsRequired
is_watchedboolean--
latest_reactionsFeedsReactionResponse[]Recent reactions to the activityRequired
locationActivityLocationGeographic location related to the activity-
mentioned_usersUserResponse[]Users mentioned in the activityRequired
moderationModerationV2ResponseModeration information-
moderation_actionstring--
notification_contextNotificationContextNotification context data for the activity (if this is a reaction, comment, follow, etc.)-
own_bookmarksBookmarkResponse[]Current user's bookmarks for this activityRequired
own_reactionsFeedsReactionResponse[]Current user's reactions to this activityRequired
parentActivityResponseParent activity (if this is a reply/comment)-
pollPollResponseDataPoll attached to this activity-
popularityintegerPopularity score of the activityRequired
previewbooleanIf this activity is obfuscated for this user. For premium content where you want to show a previewRequired
reaction_countintegerNumber of reactions to the activityRequired
reaction_groupsobjectGrouped reactions by typeRequired
restrict_repliesstringControls who can reply to this activity. Values: everyone, people_i_follow, nobodyRequired
scorenumberRanking score for this activityRequired
search_dataobjectData for search indexingRequired
share_countintegerNumber of times the activity was sharedRequired
textstringText content of the activity-
typestringType of activityRequired
updated_atnumberWhen the activity was last updatedRequired
userUserResponseUser who created the activityRequired
visibilitystring (public, private, tag)Visibility setting for the activityRequired
visibility_tagstringIf visibility is 'tag', this is the tag name-

Adding Many Activities

You can also batch add activities. Here’s an example:

val activities = listOf(
    ActivityRequest(
        feeds = listOf("user:123"),
        id = "1",
        text = "hi",
        type = "post"
    ),
    ActivityRequest(
        feeds = listOf("user:456"),
        id = "2",
        text = "hi",
        type = "post"
    )
)
val upsertedActivities: Result<List<ActivityData>> = client.upsertActivities(activities)

Updating & Deleting Activities

This example shows how to update or delete an activity:

// Update an activity
val updatedActivity: Result<ActivityData> = feed.updateActivity(
    id = "123",
    request = UpdateActivityRequest(
        custom = mapOf("custom" to "custom"),
        text = "Updated text"
    )
)

// Delete an activity
val hardDelete = false // Soft delete sets deleted at but retains the data, hard delete fully removes it
feed.deleteActivity(id = "123", hardDelete = hardDelete)

// Batch delete activities
client.deleteActivities(
    request = DeleteActivitiesRequest(
        ids = listOf("123", "456"),
        hardDelete = false
    )
)

Partial activity updates

A partial update can be used to set and unset specific fields when it is necessary to retain additional custom data fields on the object. AKA a patch style update. Both set and unset can be used in the same request. The dotted-notation is also available for both set and unset for the custom field.

// Partially set some fields
val updatedActivity: Result<ActivityData> = feed.updateActivityPartial(
    id = "123",
    request = UpdateActivityPartialRequest(
        set = mapOf(
            "text" to "Japan has over 6,800 islands.",
            "custom" to mapOf(
                "topic" to "fun facts",
                "color" to "blue",
            ),
        )
    )
)

// Partially unset some fields
val updatedActivity: Result<ActivityData> = feed.updateActivityPartial(
    id = "123",
    request = UpdateActivityPartialRequest(
        unset = listOf("custom.color")
    )
)

Get activity

Fetching a single activity.

When implementing an activity details page on the client-side it may be important to receive updates for the activity. The activity state is automatically updated on HTTP requests initiated from the client. To receive real-time updates (to receive updates from other users, for example someone else liked this post), you have to watch the feed the activity belongs to (or one of the feeds, in case it belongs to multiple feeds).

When fetching an activity that belongs to multiple feeds activity.current_feed will be empty. If you wish to display feed information alongside the activity, you have to fetch any of the containing feeds with a separate API call.

// Create an activity instance
val activity = client.activity(
    activityId = "activity123",
    fid = FeedId(group = "user", id = "john")
)

// Get the latest data
val result: Result<ActivityData> = activity.get()

// Optionally query comments
activity.queryComments()

// Access the state for reactive updates
val activityState = activity.state
// Observe activity changes
activityState.activity.collect { activity ->
    // Handle activity updates
}
// Observe comments
activityState.comments.collect { comments ->
    // Handle comments updates
}

// Comment pagination
activity.queryMoreComments(limit = 10)

// Optionally start watching the feed for real-time updates
// If activity belongs to multiple feeds, it's up to you to choose which feed to watch
val feed = client.feed(
    query = FeedQuery(
        fid = FeedId(group = "user", id = "john"),
        watch = true,
        activityLimit = 0,
        followerLimit = 0,
        followingLimit = 0
    )
)
feed.getOrCreate()
© Getstream.io, Inc. All Rights Reserved.