References

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.

// First create a collection entry with upsert api
	obj := stream.CollectionObject{
		ID: "cheese-burger",
		Data: map[string]any{
			"name": "Cheese Burger",
		},
	}
	_, err := client.Collections().Upsert(context.TODO(), "food", obj)
	if err != nil {
		panic(err)
	}

	// Then create a user
	user := stream.User{
		ID: "john-doe",
		Data: map[string]any{
			"name":    "John Doe",
			"occupation": "Software Engineer",
			"gender":   "male",
		},
	}
	_, err := client.Users().Add(context.TODO(), user, false)
	if err != nil {
		panic(err)
	}

	// 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,
	}
	_, err := userFeed.AddActivity(context.TODO(), activity)
	if err != nil {
		panic(err)
	}

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).

© Getstream.io, Inc. All Rights Reserved.