Activity Feeds V3 is in closed alpha — do not use it in production (just yet).

Stories

Stories are activities with an expiration date, usually used to share information that is available for a limited amount of time. Most often there is a dedicated feed for users to post expiring activities, and there is a separate timeline for reading stories.

Built-in story groups

There are two built-in story groups:

  • story for users to post their stories to
  • stories for users to follow others’ stories

It’s possible to update the built-in story groups or create custom ones.

Creating story groups

If you prefer to create your own story feed groups, you can do so with custom feed groups.

Creating or updating feed groups is only possible server-side.

The following example defines two feed groups:

  • my-story for users to post their stories to
  • my-stories for users to follow others’ stories

There are two settings specific to story feed groups:

  • You can enable tracking watched activities: this makes it possible to track which stories a user has seen or not (please note that this is different from notification configurations, typically you don’t need that for stories)
  • You can decide if watched stories should be removed from the feed. It’s false for the built-in story groups
$feedsClient->createFeedGroup(
    new GeneratedModels\CreateFeedGroupRequest(
        id: 'my-story',
        stories: new GeneratedModels\StoriesConfig(
            skipWatched: false,
            trackWatched: true
        )
    )
);

$feedsClient->createFeedGroup(
    new GeneratedModels\CreateFeedGroupRequest(
        id: 'my-stories',
        stories: new GeneratedModels\StoriesConfig(
            skipWatched: false,
            trackWatched: true
        ),
        aggregation: new GeneratedModels\AggregationConfig(
            format: '{{ user_id }}'
        ),
        activitySelectors: [
            ['type' => 'following']
        ]
    )
);

Posting stories

Usually stories are activities containing a single image or video. But you can implement them any way you want.

$johnStoryFeed = $feedsClient->feed('story', 'john');
$johnStoryFeed->getOrCreateFeed(
    new GeneratedModels\GetOrCreateFeedRequest(userID: 'john')
);

$tomorrow = new DateTime();
$tomorrow->modify('+1 day');

$feedsClient->addActivity(
    new GeneratedModels\AddActivityRequest(
        type: 'post',
        feeds: ['story:john'],
        attachments: [
            [
                'image_url' => 'https://example.com/image.jpg',
                'custom' => (object)[]
            ]
        ],
        expiresAt: $tomorrow->format('c'),
        userID: 'john'
    )
);

Activities on story feeds are ordered cronologically.

Following story feeds

Following someone’s story feed is the same as following any other feed:

$saraStoryTimeline = $feedsClient->feed('stories', 'sara');
$saraStoryTimeline->getOrCreateFeed(
    new GeneratedModels\GetOrCreateFeedRequest(userID: 'sara')
);

$feedsClient->follow(
    new GeneratedModels\FollowRequest(
        source: 'stories:sara',
        target: 'story:john'
    )
);

Reading story timeline and marking activities as watched

$saraStoryTimeline = $feedsClient->feed('stories', 'sara');
$response = $saraStoryTimeline->getOrCreateFeed(
    new GeneratedModels\GetOrCreateFeedRequest(userID: 'sara')
);

// Since story timeline is aggregated by user id, we read aggregated activities
$johnStories = $response->getData()->aggregatedActivities[0];
// True if we watched all active stories of a user
echo $johnStories['is_watched'] ? 'true' : 'false';

// Display all of John's active stories
foreach ($johnStories['activities'] as $activity) {
    // True if we watched a given story
    echo $activity['is_watched'] ? 'true' : 'false';
}

// Mark a story as read
$saraStoryTimeline->markActivity(
    new GeneratedModels\MarkActivityRequest(
        markWatched: [$johnStories['activities'][0]['id']],
        userID: 'sara'
    )
);

Story groups in story timelines are sorted by unread count. Watched story groups are returned last (unless the feed group is configured not to return watched stories).

When marking an activity as watched, feeds.stories_feed.updated WebSocket event is dispatched to clients of the user who marked the activity (Sara in this example). There are no WebSocket events sent when a user adds to their story, you need to reread the feed to get updates.

While there is no limit of how many active stories a user can have, aggregated_activities return the latest 100 activities for each group.

Pagination for story groups work the same way as for any other feeds. This is how you can load the next page of story groups:

$feed = $feedsClient->feed('user', 'jack');

$feedResponse1 = $feed->getOrCreateFeed(
    new GeneratedModels\GetOrCreateFeedRequest(userID: "jack", limit: 10)
);

$feedResponse2 = $feed->getOrCreateFeed(
    new GeneratedModels\GetOrCreateFeedRequest(userID: "jack", limit: 10, next: $feedResponse1->getData()->next)
);

Reading story feed

Following someone’s story feed is one way to read stories. However, it’s also possible to read someone’s story feed directly:

// Sara reads John's story feed
$johnStoryFeed = $feedsClient->feed('story', 'john');

$response = $johnStoryFeed->getOrCreateFeed(
    new GeneratedModels\GetOrCreateFeedRequest(
        userID: 'john',
        limit: 100
    )
);

$johnStories = $response->getData()->activities;

// Display all of John's active stories
foreach ($johnStories as $activity) {
    // True if we watched a given story
    echo $activity['is_watched'] ? 'true' : 'false';
}

// Mark a story as watched
$johnStoryFeed->markActivity(
    new GeneratedModels\MarkActivityRequest(
        markWatched: [$johnStories[0]['id']],
        userID: 'sara'
    )
);

When marking an activity as watched, feeds.stories_feed.updated WebSocket event is dispatched to clients of the user who marked the activity (Sara in this example). If a new story is added to the feed, a feeds.activity.added event is dispatched.

Reading expired stories

Users can list the expired stories that they created, but it’s not possible to read other users’ expired stories:

$now = new DateTime();
$result = $feedsClient->queryActivities(
    new GeneratedModels\QueryActivitiesRequest(
        filter: (object)[
            'expires_at' => ['$lte' => $now->format('c')],
            'user_id' => $john->id
        ],
        sort: [
            ['field' => 'created_at', 'direction' => -1]
        ]
    )
);
© Getstream.io, Inc. All Rights Reserved.