The centre tab in our app used to be a placeholder with the logo on it. It is now a feed: announcements from the team, notification activity, and short cards for what is happening in your groups. Building the scrolling list was the easy half. The hard half was deciding what a person who installed the app today should see. An announcement timeline is easy to render and surprisingly easy to get wrong: the obvious implementation has a backlog problem that only shows up for new accounts.

This post covers the delivery model behind that timeline, the three sources merged into it, and the authorization bug the photo albums exposed.

The backlog problem hiding under a simple feed

The first design everyone reaches for is a posts table with a publish_at column: filter publish_at <= now, order by time, page. It works beautifully for the people who were already there when each post was written.

Now imagine someone registers after ten announcements. They see all ten at once, newest first. None of it is news, the oldest items are the least relevant, and the newest are buried under them. Worse, the feed has no idea what “new” means for that person: every post is equally unread, the badge has to guess, and a push has nothing to anchor to, because no record exists of a post ever reaching anyone.

A client-side rule (show only the newest post per day) is a rendering trick, not a delivery model. It does not survive a reinstall, cannot drive a push, and cannot tell you whether the fourth post was seen.

The fix is to treat delivery as data.

Delivery is a row, not a comparison

We store one row per reader per post:

feed_deliveries (
  post_id      references feed_posts(id) on delete cascade,
  user_id      references users(id)      on delete cascade,
  delivered_at timestamptz not null,
  seen_at      timestamptz null,
  primary key (post_id, user_id)
)

The row is the delivery. Its existence answers “has this person received this post?”, delivered_at answers “when?”, and seen_at answers “did they look at it?”. Four questions that were previously unanswerable become one indexed lookup.

The composite primary key gives us idempotency for free: inserting the same pair twice is a conflict, not a duplicate. That matters, because the insert is also the moment we send the push and the realtime event. If the insert cannot happen twice, the notification cannot happen twice.

The per-user gap then falls out of a single aggregate. Before delivering a post, we ask when this user was last given anything:

SELECT u.id
FROM users u
LEFT JOIN (
  SELECT user_id, MAX(delivered_at) AS last_delivered
  FROM feed_deliveries
  GROUP BY user_id
) d ON d.user_id = u.id
WHERE NOT EXISTS (
  SELECT 1 FROM feed_deliveries x
  WHERE x.post_id = :post AND x.user_id = u.id
)
AND (d.last_delivered IS NULL
     OR d.last_delivered <= :now - make_interval(hours => :gap));

Each post carries its own minimum gap (24 hours by default; zero means “deliver on the next pass”). The gap is enforced across posts, so a user never receives two posts in the same sweep no matter how many are due.

The sweeper: slow on purpose

The delivery work runs in a background loop rather than inside the feed request. A page load should never fan out posts to hundreds of accounts; it should read what already exists.

The loop wakes on an interval, takes a lock shared by all backend instances so two of them cannot send the same post, and walks due posts oldest first. For each post it selects eligible readers, checks the gap, inserts the delivery row, and only then emits the push and the realtime event. A scheduled post flips to active the first time its publish time is reached, and the drip takes over.

Two details are worth copying:

  • No second notification. A feed post does not also create an inbox row. The post is the notification, and a duplicate card for the same event in the same timeline is just noise. Only the delivery row is written.
  • The cadence is read on every wake. The loop sleeps in short slices and re-reads its configured interval, so a shortened interval is adopted without waiting out the old sleep. We use a shorter cadence while testing a batch of posts and let it settle back to the default afterwards, with no restart in between.

The default cadence is minutes, not seconds, deliberately. A post arriving within ten minutes of its slot is fine; a burst is not.

The first open still has to work

A new account should not wait for the next sweep to see anything. The welcome post is seeded once, and opening the feed lazily ensures the reader has its delivery row. So the first open shows one card immediately, and later posts trickle in.

Seen state is separate from delivery on purpose: seen_at is stamped when the card is actually rendered, not when it was delivered. That keeps the badge honest. Marking the feed as seen also marks the matching notification rows as read, so the two surfaces cannot disagree about the same event.

A timeline is three lists, not one

The feed contains three kinds of cards:

  1. Posts — announcements, with reactions and comments.
  2. Activity rows — profile views, friend requests, reactions and similar events already known to the notifications list.
  3. Group message previews — short, expandable cards for recent incoming messages in groups you belong to.

These lists have different shapes, lifetimes and pagination cursors. We merge them on the client, by time, in one place.

A server-side union would need one cursor valid for three tables with three sort keys, and every new source would make it more fragile. Instead each list is returned capped and one page widget merges and sorts them. The merge logic is client code, but each source keeps its own pagination and freshness rules, and adding a source is a local change.

Group messages are context, not a second inbox

The group cards needed rules before they could be useful:

  • Only groups the viewer belongs to, and only messages from other people.
  • Never private chats, system rows, deleted rows, or groups the viewer has hidden.
  • Albums collapse into their anchor row with a +N badge, so five photos are one card, not five.
  • The full text ships with the card, so Show more expands in place instead of firing another request.

Freshness uses three mechanisms, deliberately layered:

  • Real-time group events now carry a flag saying they belong to a group, which the feed uses to schedule a debounced refetch rather than refetching once per message in a busy conversation.
  • The feed also refreshes when the tab becomes visible again and when the app resumes.
  • A cheap 30-second poll compares the newest group-message id as a backstop.

That last one is the part I would defend hardest. Realtime is an optimization, not a guarantee: sockets drop, a device sleeps, an instance restarts. A timeline that only updates on events will eventually look stale, with no way to tell whether it is broken or quiet.

Albums are the chat upload pipeline in a different hat

Post images reuse the existing chat upload path: pick from the gallery or camera, compress, upload. A single pick can go through the crop-and-draw editor first; a multi-pick uploads as chosen.

On the card, an album renders as a one, two or three-column grid depending on the count, so a two-photo post does not leave a lonely tile. Up to ten images are allowed. The first image is mirrored into the older single-image column, so a client that predates albums still renders something.

The bug albums exposed: authorization followed the uploader, not the audience

The first build shipped, and a post with photos looked correct to the person who created it. For everybody else the image area was blank.

The cause was not the grid. Our authenticated media route decides whether an account may read a stored file by checking the relationships that grant access: ownership, an avatar reference, a profile photo, or membership in a conversation that uses the file. A feed post had no membership edge, so the check fell through. The uploader matched on ownership; everyone else matched nothing.

The fix is a rule, not an exception: a feed image is readable by every reader the post was delivered to. The authorization query joins deliveries for the requesting user and matches the requested URL against the single-image column or the album list. We compare the album as text with proper escaping instead of scanning a JSON column row by row, which keeps the check portable to the in-memory database our tests use.

Two lessons worth writing down:

  • A high-entropy key is not an authorization model. It protects against guessing, not against a forwarded link or a logged URL. The check must name the relationship that permits access.
  • Every new surface that references media must be added to the reader check. The old check already knew about chats, avatars and profile photos. The feed was new, so it was invisible to a rule written before it existed. When you add a feature that points at stored files, the authorization test is part of the feature, not a follow-up.

The regression test compares two accounts: the uploader and a second reader the post was delivered to. Both must succeed. A test with one account would have passed against the bug.

One translation switch for the whole timeline

The feed has a single translate toggle in the top bar, and it applies to every card on screen. Incoming text is translated by the app’s AI engine first, with the on-device translator as the fallback when the network or the provider is unavailable.

Two choices make the toggle usable while scrolling: requests are serialized through one queue, so a fast scroll does not launch a request per visible card, and results are cached per card, so toggling back and forth does not re-translate the same text.

When the engine that actually served a translation differs from the one requested, the card says so. A quiet substitution is how translation features lose trust.

Live edits without losing your place

When a post is edited or deleted, the change is broadcast to every reader who received it. The client refetches that one card in place, or removes it, without rebuilding the timeline or jumping the scroll position.

Reactions are optimistic: the chip updates immediately and reconciles with the server response. Totals show as emoji chips plus a small overlapping stack of recent reactors, newest first and capped at five. Tapping an avatar opens that profile; +N opens the full list. An older backend that omits the stack falls back to a plain “who reacted” button instead of breaking.

The invariants

If you are building anything similar, these are the rules I would keep:

  1. Delivery is a row, not a comparison. If a product question is “did this person receive or see this?”, store the answer at the moment it happens.
  2. Requests read; workers write. Fan-out inside a page load turns one slow read into an incident.
  3. Merge heterogeneous timelines on the client when their cursors differ, and keep the merge in exactly one place.
  4. Authorization follows the audience of the content, not the uploader of the file. Every new surface needs its own grant relationship.
  5. Realtime is an optimization. Keep a cheap poll on a cursor as the backstop, so a quiet timeline is distinguishable from a broken one.

None of this is Flutter-specific, but all of it lands in the widget tree: a badge that must not lie, a list that must not jump, and a scroll that must not fire a request per card.

FAQ

How do you stop new users from receiving an old backlog?

Do not broadcast on a publish timestamp. Store a delivery row per reader, enforce a minimum gap per reader across posts, and let a background sweep hand out the next due post. A new account then starts with the oldest due post and receives the rest one at a time.

Should feed delivery run inside the request or in a background worker?

In a worker. The request should only read what was already delivered, because fan-out inside a page load makes one slow request affect every reader at once. Guard the worker with a lock shared by all instances and make the insert idempotent so two of them cannot double-send.

How should a feed count unread items next to chat badges?

Delivered-but-unseen posts plus unread activity rows, and nothing else. Group message cards are context, not a second inbox, so they keep their own group and chat badges. Opening the feed marks its items seen, exactly like opening a chat.

Why keep albums in one column instead of a join table?

An album is a small, ordered, fixed-size list attached to one post, and it never reorders after publishing. A JSON list keeps the post’s read path to one row; a join table would buy editing and reordering we do not have yet. The first image is mirrored into the single-image column for older clients.

The feed now arrives at the pace a person can read it, and every card knows whether it has actually been seen. That is a much better foundation for the next feature than a list that merely looks finished.