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
const response = await feed.addActivity({
  type: "post",
  text: "apple stock will go up",
});

console.log(response.activity);

//...or multiple feeds
const response = await client.addActivity({
  feeds: ["user:1", "stock:apple"],
  type: "post",
  text: "apple stock will go up",
});

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

Image & Video

feed.addActivity({
  type: "post",
  text: "look at NYC",
  attachments: [
    {
      type: "image",
      image_url: "https://example.com/image.png",
      custom: {},
    },
  ],
});

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.

const response = await feed.addActivity({
  type: "post",
  text: `Couldn't agree more!`,
  parent_id: activityToShare.id,
});

console.log(response.activity?.parent);

Overview of All Activity Fields

Here’s an overview of all the fields you can add when creating an activity:

FieldDescription
idEither set your own id, or let our server generate it. Setting your own ID can be convenient if your activity maps 1 to 1 to something in your database.
typeThe type of activity. Defaults to “post” if not provided.
feedsThe list of feeds (format: “group id:feed id”) to add this activity to
textThe text for this activity
attachmentsA list of attachments. Video, images, location, etc. Also supports custom attachments
customAny custom data you want to add
visibilityVisibility levels for the activity
locationSpecify an activity location. This allows for feeds to show content close to you. Format: ActivityLocation(lat: 40.014984, lng: -105.270546)
expiresAtWhen the activity expires. After this timestamp it’s only visible to the person who created this activity (or server side API calls)
mentionedUserIdsA list of users mentioned in this activity
parentIdThe parent activity. Used for replies/reshares etc
searchDataAny extra data you want to search on for this activity
filterTagsArray of strings that you can filter on when querying the feed
interestTagsEither set the interest tags manually or enable an activity processor to have AI determine the topics for this activity. Used for “for you” style feeds

Adding Many Activities

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

client.upsertActivities({
  activities: [
    {
      feeds: ["user:123"],
      id: "1",
      type: "post",
      text: "hi",
    },
    {
      feeds: ["user:456"],
      id: "2",
      type: "post",
      text: "hi",
    },
  ],
});

Updating & Deleting Activities

This example shows how to update or delete an activity:

// Update an activity
client.updateActivity({
  id: "123",
  text: "Updated text",
  custom: {
    color: "blue",
  },
});

client.deleteActivity({
  id: "123",
  hard_delete: false, // Soft delete sets deleted at but retains the data, hard delete fully removes it
});

// Batch delete activities
client.deleteActivities({
  id: ["123", "456"],
  hard_delete: 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
client.updateActivityPartial({
  id: activity.id,
  set: {
    text: "Japan has over 6,800 islands.",
    custom: {
      topic: "fun facts",
      color: "blue",
    },
  },
});

// Partially unset some fields
client.updateActivityPartial({
  id: activity.id,
  unset: ["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.

const activityWithStateUpdates =
  client.activityWithStateUpdates(activityId);
await activityWithStateUpdates.get({
  // Optionally fetch comments too
  comments: {
    limit: 10,
    depth: 2,
  },
});

// Subscribe to state updates
activityWithStateUpdates.state.subscribe((state) => {
  console.log(state.activity);
  console.log(state.comments_by_entity_id);
  // True if activity is being fetched
  console.log(state.is_loading);
});
// Comment pagination
activityWithStateUpdates.loadNextPageActivityComments;
activityWithStateUpdates.loadNextPageCommentReplies;

// Optionally start watching the feed
// If activity belongs to multiple feeds, it's up to you to choose which feed to watch
const fid = activityWithStateUpdates.currentState.activity!.feeds[0];
const [group, id] = fid.split(':');
const feed = client.feed(group, id);
let shouldWatch = false;
if (!feed.currentState.watch) {
  await feed.getOrCreate({
    watch: true,
    limit: 0,
    followers_pagination: { limit: 0 },
    following_pagination: { limit: 0 },
  });
}

// When leaving the page...
// dispose activity, this avoids refetching the activity if WebSocket reconnects
activityWithStateUpdates.dispose();
// you should stop watching the feed, unless your app has another component that watches the same feed
if (shouldWatch) {
  await feed.stopWatching();
}

// If you don't care about state updates, no need to call activityWithStateUpdates
await client.getActivity({
  id: activityId,
});
© Getstream.io, Inc. All Rights Reserved.