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.

Info:

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
// Create my-story feed group
var storyRequest = new CreateFeedGroupRequest
{
    ID = "my-story",
    Stories = new StoriesConfig
    {
        SkipWatched = false,
        TrackWatched = true
    }
};
await _feedsV3Client.CreateFeedGroupAsync(storyRequest);

// Create my-stories feed group
var storiesRequest = new CreateFeedGroupRequest
{
    ID = "my-stories",
    Stories = new StoriesConfig
    {
        SkipWatched = false,
        TrackWatched = true
    },
    Aggregation = new AggregationConfig
    {
        Format = "{{ user_id }}"
    },
    ActivitySelectors = new List<ActivitySelectorConfig>
    {
        new() { Type = "following" }
    }
};
await _feedsV3Client.CreateFeedGroupAsync(storiesRequest);

Posting stories

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

await _feedsV3Client.GetOrCreateFeedAsync("story", "john", new GetOrCreateFeedRequest
{
    UserID = "john",
});

var tomorrow = DateTime.UtcNow.AddDays(1);

await _feedsV3Client.AddActivityAsync(new AddActivityRequest
{
    Type = "post",
    Feeds = new List<string> { "story:john" },
    Attachments = new List<Attachment>
    {
        new()
        {
            ImageUrl = "https://example.com/image.jpg",
            Custom = new Dictionary<string, object>(),
        },
    },
    ExpiresAt = tomorrow.ToString("o"),
    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:

await _feedsV3Client.FollowAsync(new FollowRequest { Source = "stories:sara", Target = "story:john" });

Reading story timeline and marking activities as watched

var saraStoryTimeline = _feedsV3Client.Feed("stories", "sara");
var response = await saraStoryTimeline.GetOrCreateFeedAsync(new GetOrCreateFeedRequest { UserID = "sara" });

// Since story timeline is aggregated by user id, we read aggregated activities
var johnStories = response.Data.AggregatedActivities[0];
// True if we watched all active stories of a user
Console.WriteLine(johnStories.IsWatched);

// Display all of John's active stories
foreach (var story in johnStories.Activities)
{
    // True if we watched a given story
    Console.WriteLine(story.IsWatched);
}

// Mark a story as read
await saraStoryTimeline.MarkActivityAsync(new MarkActivityRequest
{
    MarkWatched = new List<string> { 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:

var firstPage = await _feedsV3Client.GetOrCreateFeedAsync(
    feedGroupID: "user",
    feedID: "jack",
    request: new GetOrCreateFeedRequest { Limit = 10, UserID = "user_id" }
);

var nextPage = await _feedsV3Client.GetOrCreateFeedAsync(
    feedGroupID: "user",
    feedID: "jack",
    request: new GetOrCreateFeedRequest { Next = firstPage.Data.Next, Limit = 10, UserID = "user_id" }
);

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:

var johnStoryFeed = _feedsV3Client.Feed("story", "john");

var response = await johnStoryFeed.GetOrCreateFeedAsync(new GetOrCreateFeedRequest
{
    Limit = 100,
    UserID = "john",
});

var johnStories = response.Data.Activities;

foreach (var story in johnStories)
{
    // True if we watched a given story
    Console.WriteLine(story.IsWatched);
}

// Mark a story as watched
await johnStoryFeed.MarkActivityAsync(new MarkActivityRequest
{
    MarkWatched = new List<string> { 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:

var result = await _feedsV3Client.QueryActivitiesAsync(new QueryActivitiesRequest
{
    Filter = new Dictionary<string, object>
    {
        ["expires_at"] = new Dictionary<string, object> { ["$lte"] = DateTime.UtcNow.ToString("o") },
        ["user_id"] = "john",
    },
    Sort = new List<SortParamRequest>
    {
        new() { Field = "created_at", Direction = -1 },
    },
});