Enrichment is one of the primary critical features of Feeds. It allows the API to work quickly and efficiently by storing the data as a reference of a collection on the activities & reactions only once, and replacing it on reading time when the activity/reaction is retrieved.
Enrichment gives our Feed product the scalability that it needs. When you update the collection object, the data will be updated on every reference upon retrieval of activities & reactions instead of manually updating the activities/reactions with the new data.
There are two ways to add a user reference to an activity:
-In the first one, you need to create the collection (users' objects are also a collection) and do a reference of the collection on the activities fields. Here is an example:
// 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').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');
// And then add an activity with these references
await client.feed('user', 'john').addActivity({
actor: johnDoeRef,
verb: 'eat',
object: cheeseBurgerRef,
});
-In the second one, after creating the user object, you can use the SU:
prefix that Stream has created. SU:
prefix means Stream User reference such that when you call the enrich endpoints to read feeds, it will replace strings with the referenced user object.
// 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').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'); // And then add an activity with these references await client.feed('user', 'john').addActivity({ actor: "SU:john"
verb: 'eat',object: cheeseBurgerRef, });
Regarding enrichment of reactions, you don't need to store a reference of the user when you create a reaction. To enable enrichment of reactions, you need to contact Stream Support (https://getstream.io/contact/support/) detailing your App ID and we will enable this for you.
Note: We have a few other abbreviations similar to SU: (Stream User). Here are they -
- SA: Stream Activity
- SR: Stream Reaction
- SO: Stream Object for a collection
Comments
0 comments
Please sign in to leave a comment.