Targeting with TO Field

The TO field allows you to specify a list of feeds to which the activity should be copied. One way to think about it is as the CC functionality of email.

If initial activity is removed from the origin feed, it's also removed from all TO targeted feeds and origin feed followers.

Use Case: Mentions

Targeting is useful when you want to support @mentions and notify users. An example is shown below:

// Add the activity to Eric's feed and to Jessica's notification feed
const activity = {
  actor: "user:Eric",
  message: "@Jessica check out getstream.io it's awesome!",
  verb: "tweet",
  object: "tweet:id",
  to: ["notification:Jessica"],
};

const response = await user_feed_1.addActivity(activity);

The TO field is limited to a maximum of 100 targets per API request.

Use Case: Organizations & Topics

Another everyday use case is for teams, organizations or topics.

For example, one of our customers runs a football community. Updates about a player should also be added to their team's feed. Stream supports this by adding the activity to the player's feed, and specifying the target feeds in the TO field:

// The TO field ensures the activity is sent to the player, match and team feed
const activity = {
  actor: "Player:Suarez",
  verb: "foul",
  object: "Player:Ramos",
  match: { name: "El Clasico", id: 10 },
  to: ["team:barcelona", "match:1"],
};

const response = await playerFeed1.addActivity(activity);

By default only the user, flat, aggregated and notification feed groups are set up automatically. You'll need to go to the dashboard and create feeds called team, player and match for the above example to work.

Update to targets

To targets can be updated later on to change where the activity is copied. This API requires server side auth and requires foreign id and time.

// update the 'to' fields on an existing activity
// client.feed("user", "ken").function (foreign_id, timestamp, new_targets, added_targets, removed_targets)
// new_targets, added_targets, and removed_targets are all arrays of feed IDs
// either provide only the `new_targets` parameter (will replace all targets on the activity),
// OR provide the added_targets and removed_targets parameters
// NOTE - the updateActivityToTargets method is not intended to be used in a browser environment.
feed.updateActivityToTargets(foreignID, timestamp, ["feed:1234"]);
feed.updateActivityToTargets(foreignID, timestamp, null, ["feed:1234"]);
feed.updateActivityToTargets(foreignID, timestamp, null, null, ["feed:1234"]);