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
	activity := stream.Activity{
		Actor: "user:Eric",
		Verb:  "tweet",
		Object: "tweet:id",
		To: []string{
			"notification:Jessica", // In production use user ids, not their usernames
		},
		Extra: map[string]any{
			"message": "@Jessica check out getstream.io it's so dang awesome.",
		},
	}
	_, err := userFeed.AddActivity(context.TODO(), activity)
	if err != nil {
		panic(err)
	}

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 send to the player, match and team feed
	activity := stream.Activity{
		Actor: "Player:Suarez",
		Verb:  "foul",
		Object: "Player:Ramos",
		To: []string{
			"team:barcelona",
			"match:1",
		},
		Extra: map[string]any{
			"match": map[string]any{
				"name": "El Clasico",
				"id":  10,
			},
		},
	}
	_, err := playerFeed.AddActivity(context.TODO(), activity)
	if err != nil {
		panic(err)
	}

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.

// Remove all old targets and set new ones (replace):
	newTargets := []string{"user:1", "user:2"}

	_, err := userFeed.UpdateToTargets(context.TODO(), activity, stream.WithToTargetsNew(newTargets...))
	if err != nil {
		panic(err)
	}

	// Add some targets and remove some others:
	add := []string{"user:1", "user:2"}
	remove := []string{"user:3", "user:4"}

	_, err := userFeed.UpdateToTargets(
		context.TODO(),
		activity,
		stream.WithToTargetsAdd(add...),
		stream.WithToTargetsRemove(remove...),
	)
	if err != nil {
		panic(err)
	}
© Getstream.io, Inc. All Rights Reserved.