Batch Methods

LAST EDIT Jan 26 2021

Batch Add ActivitiesCopied!

Multiple activities can be added with a single batch operation. This is very convenient when importing data to Stream.

1
2
3
4
5
6
const activities = [ 
    {'actor': 'User:1', 'verb': 'tweet', 'object': 'Tweet:1'}, 
    {'actor': 'User:2', 'verb': 'watch', 'object': 'Movie:1'} 
]; 
 
const addActivities = await user1.addActivities(activities);
1
2
3
4
5
activities = [ 
    {:actor => 'User:1', :verb => 'tweet', :object => 'Tweet:1'}, 
    {:actor => 'User:2', :verb => 'watch', :object => 'Movie:1'} 
] 
user_feed_1.add_activities(activities)
1
2
3
4
5
activities = [ 
    {'actor': 'User:1', 'verb': 'tweet', 'object': 'Tweet:1'}, 
    {'actor': 'User:2', 'verb': 'watch', 'object': 'Movie:1'} 
] 
user_feed_1.add_activities(activities)
1
2
3
4
5
6
7
8
9
10
11
12
13
Activity[] activities = new Activity[]{ 
        Activity.builder() 
                .actor("User:1") 
                .verb("tweet") 
                .object("Tweet:1") 
                .build(), 
        Activity.builder() 
                .actor("User:2") 
                .verb("watch") 
                .object("Movie:1") 
                .build() 
}; 
userFeed.addActivities(activities).join();
1
2
3
4
5
6
7
8
activities := []stream.Activity{ 
    {Actor: "User:1", Verb: "tweet", Object: "Tweet:1"}, 
    {Actor: "User:2", Verb: "watch", Object: "Movie:1"}, 
} 
_, err := userFeed.AddActivities(activities...) 
if err != nil { 
    panic(err) 
}
1
2
3
4
5
6
$activities = [ 
    ['actor' => 'User:1', 'verb' => 'tweet', 'object' => 'Tweet:1'], 
    ['actor' => 'User:2', 'verb' => 'watch', 'object' => 'Movie:1'], 
]; 
 
$userFeed1->addActivities($activities);
1
2
3
4
5
6
var activities = new Activity[]{ 
    new Activity("User:1", "tweet", "Tweet:1"), 
    new Activity("User:2", "watch", "Movie:1") 
}; 
 
await userFeed1.AddActivities(activities);
1
2
3
4
5
6
var activities = new Activity[]{ 
    new Activity("User:1", "tweet", "Tweet:1"), 
    new Activity("User:2", "watch", "Movie:1") 
}; 
 
await userFeed1.AddActivities(activities);

ParametersCopied!

nametypedescriptiondefaultoptional
activitieslistThe list of activities to be added (as specified in Adding Activities)-

Activity IDsCopied!

The API will return a response with a list containing the activity ids.

TO field targeting is not permitted via this endpoint. Attempting to add activities with a 'to' property will result in an error response.
If you are importing your data for the first time, we suggest you create the follow-relationships after the activity import.

Batch Activity AddCopied!

This method allows you to add a single activity to multiple feeds with one API request.

Batch Activity Add has a limit of 5,000 target feeds. Requests that exceed this limit will return an error.
1
2
3
const feeds = ['timeline:1', 'timeline:2', 'timeline:3', 'timeline:4']; 
const activity = { 'actor': 'User:2', 'verb': 'pin', 'object': 'Place:42', 'target': 'Board:1' }; 
const response = await client.addToMany(activity, feeds);
1
2
3
4
# adds 1 activity to many feeds in one request 
feeds = ['timeline:1', 'timeline:2', 'timeline:3', 'timeline:4'] 
activity = {"actor": "User:2", "verb": "pin", "object": "Place:42", "target": "Board:1"} 
client.add_to_many(activity, feeds)
1
2
3
4
# adds 1 activity to many feeds in one request 
feeds = ['timeline:1', 'timeline:2', 'timeline:3', 'timeline:4'] 
activity = {:actor => "User:2", :verb => "pin", :object => "Place:42", :target => "Board:1"} 
client.add_to_many(activity, feeds)
1
2
3
4
5
6
7
8
9
10
11
// adds 1 activity to many feeds in one request 
$activity = [ 
   'actor' => 'User:2', 
   'verb' => 'pin', 
   'object' => 'Place:42', 
   'target' => 'Board:1' 
]; 
 
$feeds = ['timeline:1', 'timeline:2', 'timeline:3', 'timeline:4']; 
 
$batcher->addToMany($activity, $feeds);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// adds 1 activity to many feeds in one request 
activity = Activity.builder() 
        .actor("User:2") 
        .verb("pin") 
        .object("Place:42") 
        .target("Board:1") 
        .build(); 
FeedID[] feeds = new FeedID[]{ 
        new FeedID("timeline", "1"), 
        new FeedID("timeline", "2"), 
        new FeedID("timeline", "3"), 
        new FeedID("timeline", "4") 
}; 
client.batch().addToMany(activity, feeds).join();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// adds 1 activity to many feeds in one request 
feeds := []stream.Feed{ 
    client.FlatFeed("timeline", "1"), 
    client.FlatFeed("timeline", "2"), 
    client.FlatFeed("timeline", "3"), 
    client.FlatFeed("timeline", "4"), 
} 
activity := stream.Activity{ 
    Actor:  "User:1", 
    Verb:   "pin", 
    Object: "Place:42", 
    Target: "Board:1", 
} 
err := client.AddToMany(activity, feeds) 
if err != nil { 
    panic(err) 
}
1
2
3
4
5
6
7
// adds 1 activity to many feeds in one request 
var feeds = new string[] { "timeline:1", "timeline:2", "timeline:3", "timeline:4" }; 
var activity = new Activity("User:2", "pin", "Place:42") 
{ 
    Target = "Board:1" 
}; 
await client.Batch.AddToMany(activity, feeds);

ParametersCopied!

name

type

description

feeds

list

The list of a feeds e.g. ['user:1', 'timeline:2']

activity

object

The activity object (see Feed endpoint for reference)

Activities added using this method are not propagated to followers. That is, any other Feeds that follow the Feed(s) listed in the API call will not receive the new Activity.
Even if real-time is enabled, for this endpoint it is disabled by default since it's mainly for backend processing which shouldn't create noise for users. If you want to enable it, please send a request to support with your app id.

Batch get Activities by IDCopied!

Activities can be retrieved by IDs or foreign ID and time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// retrieve two activities by ID 
const response = await client.getActivities({ 
    ids: [ 
        '01b3c1dd-e7ab-4649-b5b3-b4371d8f7045', 
        'ed2837a6-0a3b-4679-adc1-778a1704852d' 
    ] 
}); 
 
// retrieve two activities by their foreign ID and time 
const response = await client.getActivities({ 
    foreignIDTimes: [ 
        { foreignID: 'like:1', time: '2018-07-08T14:09:36.000000' }, 
        { foreignID: 'post:2', time: '2018-07-09T20:30:40.000000' }, 
    ] 
});
1
2
3
4
5
6
7
8
9
10
# retrieve two activities by ID 
client.get_activities(ids=[ 
    '01b3c1dd-e7ab-4649-b5b3-b4371d8f7045', 
    'ed2837a6-0a3b-4679-adc1-778a1704852d' 
]) 
 
# retrieve an activity by foreign ID and time 
client.get_activities(foreign_id_times=[ 
    (foreign_id, activity_time), 
])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# retrieve two activities by ID 
client.get_activities( 
  ids: [ 
    '01b3c1dd-e7ab-4649-b5b3-b4371d8f7045', 
    'ed2837a6-0a3b-4679-adc1-778a1704852d' 
  ] 
) 
 
# retrieve two activities by foreign ID and timestamp 
client.get_activities( 
  foreign_id_times: [ 
    { foreign_id: 'like:1', time: '2018-07-08T14:09:36.000000' }, 
    { foreign_id: 'post:2', time: '2018-07-09T20:30:40.000000' } 
  ] 
)
1
2
3
4
5
6
7
8
# not supported yet 
$client->getActivities([ 
    '01b3c1dd-e7ab-4649-b5b3-b4371d8f7045', 
    'ed2837a6-0a3b-4679-adc1-778a1704852d' 
]) 
 
# retrieve an activity by foreign ID and time (an array of arrays) 
$client->getActivities(null, [[$foreign_id, $activity_time]])
1
2
3
4
5
// retrieve two activities by ID 
client.batch().getActivitiesByID("01b3c1dd-e7ab-4649-b5b3-b4371d8f7045", "ed2837a6-0a3b-4679-adc1-778a1704852").join(); 
 
// retrieve two activities by foreign ID and timestamp 
client.batch().getActivitiesByForeignID(new ForeignIDTimePair("foreignID1", new Date()), new ForeignIDTimePair("foreignID2", new Date())).join();
1
2
3
4
5
6
7
8
9
10
11
// retrieve two activities by ID 
resp, err := client.GetActivitiesByID("01b3c1dd-e7ab-4649-b5b3-b4371d8f7045", "ed2837a6-0a3b-4679-adc1-778a1704852d") 
if err != nil { 
    panic(err) 
} 
 
// retrieve two activities by foreign ID and timestamp 
resp, err := client.GetActivitiesByForeignID( 
    stream.NewForeignIDTimePair("like:1", stream.Time{Time: /* ... */}), 
    stream.NewForeignIDTimePair("post:2", stream.Time{Time: /* ... */}), 
)
1
2
3
4
5
6
7
8
9
10
11
12
// retrieve two activities by ID: 
client.get(typeOf: Activity.self, activityIds: ["01b3c1dd-e7ab-4649-b5b3-b4371d8f7045", 
                                                "ed2837a6-0a3b-4679-adc1-778a1704852d"]) { result in 
    /* ... */  
} 
 
// retrieve an activity by foreign ID and time 
client.get(typeOf: Activity.self,  
           foreignIds: ["like:1", "post:2"], 
           times: ["2018-07-08T14:09:36.000000".streamDate!, "2018-07-09T20:30:40.000000".streamDate!]) { result in 
    /* ... */  
}
1
2
3
4
5
6
7
8
9
10
11
12
13
// retrieve two activities by ID 
await client.Batch.GetActivities(new string[] 
{ 
    "01b3c1dd-e7ab-4649-b5b3-b4371d8f7045", 
    "ed2837a6-0a3b-4679-adc1-778a1704852d" 
}); 
 
// retrieve two activities by foreign ID and timestamp 
await client.Batch.GetActivities(null, new ForeignIDTime[] 
{ 
    new ForeignIDTime("like:1", DateTime.Parse("2018-07-08T14:09:36.000000")), 
    new ForeignIDTime("post:1", DateTime.Parse("2018-07-09T20:30:40.000000")) 
});
Combining ID and foreign ID + time parameters is not allowed.

ParametersCopied!

nametypedescriptiondefaultoptional
idsstringThe comma-separated list of activity IDs to retrieve-
foreign_id_timeslistThe list of foreign_id and time values used to retrieve activities-
The number of activities that can be retrieved with a single request is limited to 100.

Batch FollowCopied!

Stream's Follow Many functionality gives you a fast method to follow many feeds in one go. This is convenient when importing data or on-boarding new users.

Follow Many has a limit of 2,500 follows per request.

activity_copy_limit can be specified to copy specific number of activities. By default, it's 100 such that upon follow 100 activities from the followed feed will be seen in our feed.

1
2
3
4
5
6
7
8
9
10
// Batch following many feeds 
// Let timeline:1 will follow user:1, user:2 and user:3 
 
const follows = [ 
  {'source': 'timeline:1', 'target': 'user:1'}, 
  {'source': 'timeline:1', 'target': 'user:2'}, 
  {'source': 'timeline:1', 'target': 'user:3', 'activity_copy_limit': 0} 
]; 
 
await client.followMany(follows);
1
2
3
4
5
6
7
8
9
10
# Batch following many feeds 
# Let timeline:1 will follow user:1, user:2 and user:3 
follows = [ 
    {'source': 'timeline:1', 'target': 'user:1'}, 
    {'source': 'timeline:1', 'target': 'user:2'}, 
    {'source': 'timeline:1', 'target': 'user:3'} 
] 
client.follow_many(follows) 
# copy only the last 10 activities from every feed 
client.follow_many(follows, activity_copy_limit=10)
1
2
3
4
5
6
7
8
# Batch following many feeds 
# Let timeline:1 will follow user:1, user:2 and user:3 
follows = [ 
    {:source => 'timeline:1', :target => 'user:1'}, 
    {:source => 'timeline:1', :target => 'user:2'}, 
    {:source => 'timeline:1', :target => 'user:3'} 
] 
client.follow_many(follows)
1
2
3
4
5
6
7
8
9
// Batch following many feeds 
// Let timeline:1 will follow user:1, user:2 and user:3 
$follows = [ 
    ['source' => 'timeline:1', 'target' => 'user:1'], 
    ['source' => 'timeline:1', 'target' => 'user:2'], 
    ['source' => 'timeline:1', 'target' => 'user:3'], 
]; 
 
$batcher->followMany($follows);
1
2
3
4
5
6
7
8
9
10
// Batch following many feeds 
// Let timeline:1 will follow user:1, user:2 and user:3 
FollowRelation[] follows = new FollowRelation[]{ 
        new FollowRelation("timeline:1", "user:1"), 
        new FollowRelation("timeline:3", "user:2"), 
        new FollowRelation("timeline:1", "user:3") 
}; 
client.batch().followMany(follows).join(); 
// copy only the last 10 activities from every feed 
client.batch().followMany(10, follows).join();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Batch following many feeds 
// Let timeline:1 will follow user:1, user:2 and user:3 
follows := []stream.FollowRelationship{ 
    stream.NewFollowRelationship(timelineFeed, userFeed1), 
    stream.NewFollowRelationship(timelineFeed, userFeed2), 
    stream.NewFollowRelationship(timelineFeed, userFeed3), 
} 
err = client.FollowMany(follows) 
if err != nil { 
    panic(err) 
} 
// copy only the last 10 activities from every feed 
err = client.FollowMany(follows, stream.WithFollowManyActivityCopyLimit(10)) 
if err != nil { 
    panic(err) 
}
1
2
3
4
5
6
7
8
9
10
11
12
// Batch following many feeds 
// Let timeline:1 follow user:1, user:2 and user:3 
var follows = new[] 
{ 
    new Follow("timeline:1", "user:1"), 
    new Follow("timeline:1", "user:2"), 
    new Follow("timeline:1", "user:3") 
}; 
await client.Batch.FollowMany(follows); 
 
// copy only the last 10 activities from every feed 
await client.Batch.FollowMany(follows, 10);

ParametersCopied!

The Batch Follow API does not return response data.

Batch UnfollowCopied!

Unfollow Many enables you to unfollow many feeds in bulk. However, its implementation is heavy. That's why its usage restricted to Enterprise accounts. While unfollowing, history can be kept by keep_history. It's false by default. When unfollow is done, any activities that come from unfollowed feed will be removed but if the flag is set, then this history will remain.

1
2
3
4
5
6
7
8
9
10
// Batch unfollowing many feeds 
// Let timeline:1 will follow user:1, user:2 and user:3 
 
const unfollows = [ 
  {'source': 'timeline:1', 'target': 'user:1'}, 
  {'source': 'timeline:1', 'target': 'user:2'}, 
  {'source': 'timeline:1', 'target': 'user:3', 'keep_history': true}   
]; 
 
await client.unfollowMany(unfollows);
The Batch Unfollow API does not return response data. An error is returned on wrong input.