Chrysolites Tech Academy

Short-Video App with React Native and Supabase · Course book

Chapter 15: The Activity Feed

Pairs with Lesson 15: An Activity Feed for Likes, Comments and Follows

Creators want to know when people respond to them. An activity feed collects those moments in one place: who liked your videos, who commented and who started following you, newest first, each with a picture and a link to the person or the video. In the short-video app it lives in the Inbox tab, alongside chats. This chapter explains how to build it: what counts as an activity, how to fetch likes, comments and follows that concern the current user, how to merge them into one timeline, worked through by hand, how to group repeated activity so it stays readable, how routing connects each row to the right screen, and how the inbox is structured around activity and chat.

What counts as activity

Activity is anything another person did that concerns you. In this app there are three kinds, each already stored in its own table by earlier chapters.

ActivitySource tableFilter for "concerns me"Row showsTapping opens
Someone liked my videoLikevideo_user_id = me, user_id ≠ me"@maya liked your video"The video
Someone commentedCommentvideo_user_id = me, user_id ≠ me"@leo commented: Love it!"The video's comments
Someone followed meFolloweruser_id = me"@eli started following you"Their profile

This is where storing the video owner's id on likes and comments pays off. Without it, finding "likes on my videos" would mean first fetching all my videos and then the likes for each, or writing a join. With it, each kind of activity is one filtered query on one table. Excluding the user's own actions, such as liking your own video, keeps the feed about other people.

There are two broad ways to build an activity feed. The approach in this chapter derives it from the existing tables at read time. The alternative keeps a separate notifications table, into which a row is written whenever something notable happens, often by database triggers, with fields for the type, the actor, the recipient, the related video and whether it has been read. Deriving is simpler to start with; a notifications table becomes worthwhile when the app needs read and unread states, push notifications or many more kinds of event.

Fetching the three streams

The activity screen runs three queries, one per table, each filtered to the current user, ordered newest first, limited to a reasonable number such as the latest 50, and each fetching the actor's username and picture through the related User row in the same request. Running them together with Promise.all means the screen waits for the slowest query, not for all three in turn.

const [likes, comments, follows] = await Promise.all([
  supabase.from("Like").select("*, User(username, profile_photo)")
    .eq("video_user_id", me).neq("user_id", me)
    .order("created_at", { ascending: false }).limit(50),
  supabase.from("Comment").select("*, User(username, profile_photo)")
    .eq("video_user_id", me).neq("user_id", me)
    .order("created_at", { ascending: false }).limit(50),
  supabase.from("Follower").select("*, User:follower_user_id(username, profile_photo)")
    .eq("user_id", me)
    .order("created_at", { ascending: false }).limit(50),
]);

Each result is then turned into a common shape, such as { type, actor, videoId, text, created_at }, so that one row component can display any kind of activity. Indexes on video_user_id in the Like and Comment tables and on user_id in the Follower table keep these queries fast as activity accumulates.

Merging into one timeline

The three lists are each sorted newest first, and the screen needs one combined list in the same order. The simplest method concatenates them and sorts by created_at, descending. Because each list is already sorted, a merge is more efficient: repeatedly compare the first remaining item of each list and take the newest, the same idea used in merge sort.

Worked example. Likes arrived at 14:50 and 11:05; comments at 14:20 and 09:40; a follow at 12:30. Merging newest first: compare 14:50, 14:20 and 12:30, take the like at 14:50; next compare 11:05, 14:20 and 12:30, take the comment at 14:20; then 11:05, 09:40 and 12:30, take the follow at 12:30; then 11:05 against 09:40, take the like at 11:05; finally the comment at 09:40. The timeline is like, comment, follow, like, comment. Each step compares at most three items, which takes at most two comparisons, so merging n items costs at most about 2n comparisons, rather than the roughly n log2 n of sorting them from scratch. For 150 items that is about 300 comparisons against roughly 1,080, a difference that matters little here but is the right habit for larger feeds.
LikesCommentsFollows 14:5011:0514:2009:4012:30 Timeline, newest first 14:50 @maya liked14:20 @leo commented12:30 @eli followed you11:05 @sam liked09:40 @ana commented
Figure 15.1 — Merging three sorted streams into one timeline by repeatedly taking the newest remaining item.

Limiting each query to 50 items raises a subtle point. If likes are far more common than follows, the 50 newest likes might all be from today while the 50th newest follow is from last month; merging them gives a timeline that is complete only back to the oldest point all three lists cover. Showing the merged list only down to the most recent of the three "oldest" timestamps, and loading more when the user scrolls, keeps the timeline honest.

Grouping and readability

A popular video can collect hundreds of likes in an hour, and listing each one would bury everything else. Activity feeds therefore group similar events: likes on the same video within a period become one row, "@maya and 37 others liked your video", with the newest actor named and a stack of small avatars. Grouping is a pass over the merged list that collects consecutive likes with the same video id into one entry with a count, keeping the time of the newest.

Each row needs a clear structure: the actor's picture on the left, a sentence in the middle with the username in bold, the relative time below, and for likes and comments a small thumbnail of the video on the right, so the creator can see at a glance which clip it was about. Comments show a short excerpt of their text, truncated to a line or two. A follow row can carry its own "follow back" button when the viewer does not yet follow that person, turning the activity feed into one of the main ways friendships form.

New activity should stand out. With a notifications table, unread rows can be highlighted and a badge on the Inbox tab can show how many there are. Without one, the app can remember on the phone when the user last opened the activity screen and highlight anything newer, a simple approach that works well until the app needs read states to sync across devices.

Keeping the screen current is the next question. Pull-to-refresh lets users ask for the latest activity whenever they like, and refetching when the screen comes into focus catches most new events without any extra machinery. For a livelier feel, the app can subscribe to new rows in the Like, Comment and Follower tables that concern the current user, using the same real-time mechanism the next chapter uses for chat, and add them to the top of the list as they arrive.

Many apps also send push notifications for activity, so that users learn about a new follower even when the app is closed. That requires the phone to register for notifications and send its push token to the back end, and a server-side process, such as a database trigger calling an edge function, to send a message through Apple's and Google's notification services when an event occurs. Users must be able to choose which kinds of notification they receive, and the app must ask permission first; sending too many is the quickest way to make people turn them off entirely.

Routing and the inbox

Every row leads somewhere, using the file-based routes built earlier. A follow opens the follower's profile at the dynamic route /user/[id]; a like opens the liked video, perhaps in a single-video player screen; a comment opens that video's comments. Because each route takes an id, the activity row only needs to know the type and the relevant id to navigate.

The Inbox tab brings activity and conversations together. A common structure has a short "Activity" entry at the top, showing the newest event and a count, which opens the full activity screen, followed by a list of chats with friends, each showing the friend's picture, name and the latest message. Building the inbox as its own stack of screens, with the inbox list as the first screen and activity and chat screens pushed on top, keeps the back button behaving as users expect. The next chapter fills in the chat side with real-time messaging.

Practice tasks

  1. Merge, newest first: likes at 16:10, 13:45 and 08:20; comments at 15:30; follows at 13:50 and 07:15.
  2. Group these consecutive likes: video A by @kim at 16:10, video A by @raj at 16:02, video A by @zoe at 15:55, video B by @raj at 15:40. What rows appear?
  3. Explain why each activity query excludes rows where the actor is the current user.
  4. Compare deriving activity from existing tables with writing a separate notifications table.
  5. Which route should each kind of activity row open, and what id does it need?
  6. Why should the merged timeline stop at the most recent of the three oldest timestamps when each query is limited?

Install Chrysolites Tech Academy

Android / Chrome / Edge: open the browser menu and choose Install app or Add to Home screen.

iPhone / iPad (Safari): tap the Share button, then Add to Home Screen.