# X-style home timeline

This recipe builds a home timeline like the one in X (formerly Twitter): a list of posts from the people a user follows, newest first, a "Show new posts" banner instead of posts jumping in while the user reads, likes, threaded replies, reposts and quote posts, and bookmarks.

Everything here uses two built-in feed groups, so there is no feed group to set up:

- Each user posts to their own `user:<user id>` feed.
- Each user reads their `timeline:<user id>` feed, which follows the `user` feeds of the people they follow. Its `following` selector merges the posts of the followed feeds when the timeline is read, and caches the result per user, so later reads only add what's new.

## 1. Create each user's feeds

Create the user's `user` and `timeline` feeds when they sign up, by calling `getOrCreate` on `user:<user id>` and `timeline:<user id>` from your server.

## 2. Follow people

Follow from the user's timeline feed to the other user's `user` feed. On X, a user's own posts show up in their home timeline too, so also follow the user's own `user` feed from their timeline when they sign up. Use `getOrCreateFollows`: it's idempotent, so calling it for a follow that already exists does nothing, and it takes up to 1000 follows in one request.

```js label="JavaScript"
// Jack's timeline follows Maria and his own user feed
await client.getOrCreateFollows({
  follows: [
    { source: "timeline:jack", target: "user:maria" },
    { source: "timeline:jack", target: "user:jack" },
  ],
});
```

The self-follow counts like any other: it adds 1 to the user feed's `follower_count` and to the timeline's `following_count`, so subtract it when you show the counts.

To unfollow, call `getOrCreateUnfollows` with the same source and target, up to 100 in one request. See [Follows](https://getstream.io/activity-feeds/docs/javascript/follows/).

## 3. Post

Add the post to the author's `user` feed. It shows up in the timelines of everyone who follows that feed. To limit who can reply, as X does, set `restrict_replies` to `everyone` (the default), `people_i_follow` or `nobody`. The author can always reply to their own post.

```js label="JavaScript"
const response = await feed.addActivity({
  type: "post",
  text: "apple stock will go up",
  restrict_replies: "people_i_follow", // Options: "everyone", "people_i_follow", "nobody"
});
```

To mention other users, pass their IDs in `mentioned_user_ids`. See [User mentions](https://getstream.io/activity-feeds/docs/javascript/user-mentions/).

## 4. Show the timeline

Read the user's timeline and load the next page as they scroll. Three choices keep the read fast:

- **Use a page size close to one screen.** 20 posts is plenty for the first page.
- **Skip what the list doesn't show.** Skip comments enrichment and load comments when the user opens a post (step 7). Every activity still has its `comment_count` for the reply counter. Reactions, bookmarks and the parent of reposts stay on, since every post in the list shows them.
- **Watch the feed.** `watch: true` subscribes the client to real-time events, so likes, replies and new posts arrive without reading the feed again (step 5).

```js label="JavaScript"
const timeline = client.feed("timeline", "jack");
const readOptions = {
  limit: 20,
  watch: true,
  enrichment_options: {
    skip_activity_comments: true,
    // Only needed to hide the reply button on restricted posts (step 7)
    enrich_own_followings: true,
  },
};

await timeline.getOrCreate(readOptions);
console.log(timeline.state.getLatestValue().activities);

// Load the next page when the user scrolls to the end
await timeline.getNextPage();
```

The merged timeline is cached per user, so it's cheap to read, but avoid reading the first page again every time the app comes to the foreground. Keep the activities you already have and let real-time events update them.

## 5. Show a "Show new posts" banner

When someone the user follows posts while they read, X doesn't push the list down; it shows a "Show N posts" banner. The client SDK does the same by default: on a watched feed, only the user's own new posts are added to the list, and other users' new posts are left out of it.

To show a banner, pass an `onNewActivity` callback when you create the feed. Add the user's own posts to the top and count everyone else's. When the user taps the banner, read the first page again with the same options as in step 4, which brings in the new posts with all their enrichment. A timeline read is cached for 10 seconds, so a tap within 10 seconds of the previous read can return the page without the new posts.

```js label="JavaScript"
let newPostCount = 0;

const timeline = client.feed("timeline", "jack", {
  onNewActivity: ({ activity, currentUser }) => {
    // Show the user's own posts right away
    if (activity.user.id === currentUser?.id) return "add-to-start";
    // Count everyone else's for the "Show N posts" banner
    newPostCount += 1;
    return "ignore";
  },
});

// When the user taps the banner
newPostCount = 0;
await timeline.getOrCreate(readOptions);
```

To get the events yourself, for example to show the new posts' authors' avatars on the banner, subscribe with `timeline.on("feeds.activity.added", handler)`. See [Activity list controls](https://getstream.io/activity-feeds/docs/javascript/activities/#activity-list-controls---client-side-sdks) and [Events](https://getstream.io/activity-feeds/docs/javascript/events/).

## 6. Like and unlike

A like is a reaction of type `like`. A user holds at most one reaction of each type on a post, so liking a post again doesn't add a second like. Each activity has `own_reactions` to show whether the user liked it, and `reaction_count` and `reaction_groups` for the counter. On a watched timeline, the client updates them when anyone likes or unlikes the post, so you don't need to read the feed again.

```js label="JavaScript"
// Add a reaction to an activity
await client.addActivityReaction({
  activity_id: "activity_123",
  type: "like",
});

// Remove a reaction
await client.deleteActivityReaction({
  activity_id: "activity_123",
  type: "like",
});
```

## 7. Reply

A reply is a comment on the activity. A reply to a reply is a comment with `parent_id` set to the comment it answers. If the post restricts replies, a user who isn't allowed to reply gets an error, so hide the reply button for them. With `nobody`, only the author can reply. With `people_i_follow`, a user can reply if the author follows them: read the timeline with `enrich_own_followings: true` (step 4), and show the button when the activity's `current_feed.own_followings` isn't empty.

```js label="JavaScript"
// Adding a comment to an activity
await client.addComment({
  comment: "So great!",
  object_id: "activity_123",
  object_type: "activity",
});

// Adding a reply to a comment
await client.addComment({
  comment: "I agree!",
  parent_id: "comment_456",
});
```

Load the replies only when the user opens a post, and load the replies to a reply when the user expands it. `loadNextPageActivityComments` and `loadNextPageCommentReplies` store the comments in the feed's state, so they update in real time like the rest of the timeline.

```js label="JavaScript"
// When the user opens a post, load its first page of replies
await timeline.loadNextPageActivityComments(activity, { limit: 20 });
const replies =
  timeline.state.getLatestValue().comments_by_entity_id[activity.id]?.comments;

// When the user expands a reply, load the replies to it
await timeline.loadNextPageCommentReplies(comment);
const repliesToComment =
  timeline.state.getLatestValue().comments_by_entity_id[comment.id]?.comments;
```

See [Comments](https://getstream.io/activity-feeds/docs/javascript/comments/) for sorting, editing and deleting comments.

## 8. Repost and quote

A repost and a quote post are both new activities in the reposting user's `user` feed with `parent_id` set to the original. Stream increments the original's `share_count`, and timeline reads include the original as `activity.parent`, so the card needs no extra request. For a repost, use a type of your own such as `repost` and leave out `text`:

```js label="JavaScript"
const response = await feed.addActivity({
  type: "repost",
  parent_id: activityToShare.id,
});

console.log(response.activity?.parent);
```

For a quote post, post as in step 3 with the user's text and the same `parent_id`. To undo a repost, delete the repost activity; the original's `share_count` goes down by 1.

## 9. Bookmark

Bookmarks are private to the user. `own_bookmarks` on each activity shows whether the user bookmarked it.

```js label="JavaScript"
await client.addBookmark({
  activity_id: "activity_123",
});
```

To remove a bookmark, call `deleteBookmark` with the same activity ID. To show the user's bookmarks page, use `queryBookmarks`. See [Bookmarks](https://getstream.io/activity-feeds/docs/javascript/bookmarks/) for both, and for bookmark folders.

## Going further

- **Add a For You tab.** X's second tab is a ranked feed that mixes in posts from accounts the user doesn't follow. See the [For You feed recipe](https://getstream.io/activity-feeds/docs/javascript/for-you-feed-recipe/).
- **Notify authors.** Pass `create_notification_activity: true` when a user follows, likes, replies or mentions someone. See the [notification inbox recipe](https://getstream.io/activity-feeds/docs/javascript/notification-inbox-recipe/).
- **Pin a post to a profile.** See [Pins](https://getstream.io/activity-feeds/docs/javascript/pins/).
- **Add polls.** See [Polls](https://getstream.io/activity-feeds/docs/javascript/polls/).
- **Show a profile page.** Read the author's `user` feed with the same enrichment options. Its `follower_count` and `following_count` are on the feed itself, so you don't need the followers and following lists to show the counts.

---

For the most recent version of this documentation, visit [https://getstream.io/activity-feeds/docs/javascript/timeline-recipe/](https://getstream.io/activity-feeds/docs/javascript/timeline-recipe/).