Java

References

LAST EDIT Jan 26 2021

When you add a user or a collection object to an activity, Stream stores the unique reference and replaces it at read time. In some complex cases, you need to be able to generate a reference to an existing object and embed that inside of an activity.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// First create a collection entry with upsert api 
await client.collections.upsert('food', [ 
	{ id: 'cheese-burger', name: 'Cheese Burger' }, 
]); 
 
// Then create a user 
await client.user('john-doe').create({ 
	name: 'John Doe', 
	occupation: 'Software Engineer', 
	gender: 'male', 
}); 
 
// Since we know their IDs we can create references to both without reading from APIs 
const cheeseBurgerRef = client.collections.entry('food', 'cheese-burger'); 
const johnDoeRef = client.user('john-doe'); 
 
// And then add an activity with these references 
await client.feed('user', 'john').addActivity({ 
	actor: johnDoeRef, 
	verb: 'eat', 
	object: cheeseBurgerRef, 
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# First create a collection entry with upsert api 
client.collections.upsert( 
    "food", [{"id": "cheese-burger", "name": "Cheese Burger"}] 
) 
 
# Then create a user 
client.users.add( 
    "john-doe", 
    {"name": "John Doe", "occupation": "Software Engineer", "gender": "male"}, 
) 
 
# Since we know their IDs we can create references to both without reading from APIs 
cheese_burger_ref = client.collections.create_reference("food", "cheese-burger") 
john_doe_ref = client.users.create_reference("john-doe") 
 
# And then add an activity with these references 
client.feed("user", "john").add_activity( 
    {"actor": john_doe_ref, "verb": "eat", "object": cheese_burger_ref} 
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# First create a collection entry with upsert api 
client.collections.upsert( 
    "food", [{:id => "cheese-burger", :name => "Cheese Burger"}] 
) 
 
# Then create a user 
client.users.add( 
    "john-doe", 
    :data => {:name => "John Doe", :occupation => "Software Engineer", :gender => "male"}, 
) 
 
# Since we know their IDs we can create references to both without reading from APIs 
cheese_burger_ref = client.collections.create_reference("food", "cheese-burger") 
john_doe_ref = client.users.create_reference("john-doe") 
 
# And then add an activity with these references 
feed.add_activity( 
    {:actor => john_doe_ref, :verb => "eat", :object => cheese_burger_ref} 
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# First create a collection entry with upsert api 
$client->collections()->upsert( 
    "food", ["id" => "cheese-burger", "name" => "Cheese Burger"] 
); 
 
# Then create a user 
$client->users()->add( 
    "john-doe", 
    ["name" => "John Doe", "occupation" => "Software Engineer", "gender" => "male"], 
    true 
); 
 
# Since we know their IDs we can create references to both without reading from APIs 
$cheese_burger_ref = $client->collections()->createReference("food", "cheese-burger"); 
$john_doe_ref = $client->users()->createReference("john-doe"); 
 
# And then add an activity with these references 
$feed->addActivity( 
    ["actor" => $john_doe_ref, "verb" => "eat", "object" => $cheese_burger_ref] 
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// First create a collection entry with upsert api 
client.collections().upsert("food", new CollectionData().set("name", "Cheese Burger")).join(); 
 
// Then create a user 
client.user("john-doe").create(new Data() 
        .set("name", "John Doe") 
        .set("occupation", "Software Engineer") 
        .set("gender", "male")).join(); 
 
// Since we know their IDs we can create references to both without reading from APIs 
String cheeseBurgerRef = createCollectionReference("food", "cheese-burger"); 
String johnDoeRef = createUserReference("john-doe"); 
 
client.flatFeed("user", "john").addActivity(Activity.builder() 
        .actor(johnDoeRef) 
        .verb("eat") 
        .object(cheeseBurgerRef) 
        .build()).join();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// First create a collection entry with upsert api 
obj := stream.CollectionObject{ 
	ID: "cheese-burger", 
	Data: map[string]interface{}{ 
		"name": "Cheese Burger", 
	}, 
} 
client.Collections().Upsert("food", obj) 
 
// Then create a user 
user := stream.User{ 
	ID: "john-doe", 
	Data: map[string]interface{}{ 
		"name":       "John Doe", 
		"occupation": "Software Engineer", 
		"gender":     "male", 
	}, 
} 
client.Users().Add(user, false) 
 
// Since we know their IDs we can create references to both without reading from APIs 
cheeseBurgerRef := client.Collections().CreateReference("food", "cheese-burger") 
johnDoeRef := client.Users().CreateReference("john-doe") 
 
// And then add an activity with these references 
activity := stream.Activity{ 
	Actor:  johnDoeRef, 
	Verb:   "eat", 
	Object: cheeseBurgerRef, 
} 
client.FlatFeed("user", "john").AddActivity(activity)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// First create a collection entry with upsert api 
let cheeseBurger = Food(name: "Cheese Burger", rating: 4, id: "cheese-burger") 
 
client.add(collectionObject: cheeseBurger) { _ in 
    // Then create a user 
    let user = User(id: "john-doe") 
    client.create(user: user) { _ in 
        // Since we know their IDs we can create references to both without reading from APIs 
        // The `CollectionObjectProtocol` and `UserProtocol` conformed to the `Enrichable` protocol. 
        let cheeseBurgerRef = cheeseBurger.referenceId 
        let johnDoeRef = user.referenceId 
         
        client.flatFeed(feedSlug: "user", userId: "john") 
              .add(Activity(actor: johnDoeRef, verb: "eat", object: cheeseBurgerRef)) { result in /* ... */ } 
    } 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// First create a collection entry with upsert api 
var collectionObj = new CollectionObject("cheese-burger"); 
collectionObj.SetData("name", "Cheese Burger"); 
await client.Collections.Upsert("food", collectionObj); 
 
// Then create a user 
var userData = new Dictionary<string, object="">() 
{ 
    {"name", "John Doe"}, 
    {"occupation", "Software Engineer"}, 
    {"gender", "male"} 
}; 
var user = await client.Users.Add("john-doe", userData); 
 
// Since we know their IDs we can create references to both without reading from APIs 
var cheeseBurgerRef = Collections.Ref("food", "cheese-burger"); 
var johnDoeRef = Users.Ref("john-doe"); 
 
// And then add an activity with these references 
var activity = new Activity(johnDoeRef, "eat", cheeseBurgerRef); 
await client.Feed("user", "john").AddActivity(activity);</string,>
If you are using the APIs on web / mobile (see auth section) you must set activity.actor to the reference of the current user or otherwise you will get a permission error (see examples above).