Skip to content

fix(core): Fix backwards pagination not working if channel was never opened#2789

Open
VelikovPetar wants to merge 4 commits into
masterfrom
bug/FLU-547_fix_pagination_on_unread_channels
Open

fix(core): Fix backwards pagination not working if channel was never opened#2789
VelikovPetar wants to merge 4 commits into
masterfrom
bug/FLU-547_fix_pagination_on_unread_channels

Conversation

@VelikovPetar

@VelikovPetar VelikovPetar commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Submit a pull request

Linear: FLU-563

Github Issue: #

CLA

  • I have signed the Stream CLA (required).
  • The code changes follow best practices
  • Code changes are tested (add some information if not applicable)

Description of the pull request

When a user enters a channel with unread_count > 0 but the server-side read state has last_read_message_id = null and last_read = 0001-01-01T00:00:00Z (Go's time.Time{} zero value — the "never explicitly read" sentinel), StreamChannel._maybeInitChannel was unconditionally calling loadChannelAtTimestamp(currentUserRead.lastRead).

The backend silently ignores zero-time created_at_around (via IsZero()) and returns the channel tail. The client then mis-infers boundaries via _inferBoundariesFromAnchorTimestamp: because year-1 is before every loaded message, it concludes endOfPrependReached: true, sets _topPaginationEnded = true, and backwards pagination is permanently dead on the channel.

The fix guards the timestamp branch on lastRead.toUtc().isAfter(DateTime.utc(1970, 1, 1)). When it's the zero sentinel, the code falls through to the existing catch-all if (channel.state?.isUpToDate == false) loadChannelAtMessage(null) — i.e. stay on cached latest if up-to-date, otherwise reload latest.

Adds a _maybeInitChannel test group with three regression guards: idAround for lastReadMessageId, createdAtAround for real timestamps, and no query for the Go zero-time case.

Screenshots / Videos

Before After
pagin-before.mp4
pagin-after.mp4

Summary by CodeRabbit

Summary by CodeRabbit

  • Bug Fixes
    • Fixed backwards pagination for channels that have never been opened.
    • Improved StreamChannel initialization so “never read” states skip timestamp-based loading and load the latest messages instead.
    • Addressed a StreamChannelListController issue where deleted-channel notifications weren’t handled correctly.
  • Tests
    • Updated StreamChannel widget tests to use a consistent pumping helper and expanded pagination initialization coverage.
  • Documentation
    • Updated the core changelog under Upcoming → Fixed with the latest fixes.

@coderabbitai

coderabbitai Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d40f0405-f434-4017-9e27-c31110b05f33

📥 Commits

Reviewing files that changed from the base of the PR and between a57bb1f and bd9d2f4.

📒 Files selected for processing (1)
  • packages/stream_chat_flutter_core/CHANGELOG.md
✅ Files skipped from review due to trivial changes (1)
  • packages/stream_chat_flutter_core/CHANGELOG.md

📝 Walkthrough

Walkthrough

Adds a zero-read epoch sentinel check in StreamChannelState._maybeInitChannel so never-opened channels fall through to load-latest instead of loading at lastRead. The widget tests now pump an explicit channel and cover the updated pagination paths.

Changes

Backwards pagination fix and tests

Layer / File(s) Summary
_maybeInitChannel epoch sentinel guard
packages/stream_chat_flutter_core/lib/src/stream_channel.dart, packages/stream_chat_flutter_core/CHANGELOG.md
Adds a DateTime.utc(1970, 1, 1) cutoff for currentUserRead.lastRead; when the sentinel is detected, loadChannelAtTimestamp is skipped and the load-latest path is used. The changelog records the fix.
Test helper refactor and pagination coverage
packages/stream_chat_flutter_core/test/stream_channel_test.dart
Introduces _pumpStreamChannel(tester, channel) and updates getFirstUnreadMessage, pruneOldest, _maybeInitChannel, and reloadChannel tests to use an explicit channel and assert the revised pagination anchors and latest-page behavior.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • renefloor

Poem

🐇 I hopped through the channel, where epochs can hide,
A zero-read whisper said “latest, not wide.”
Now pages fall backward the proper way through,
And tests hop along to confirm the fix too.
Hooray for the bunny, the code, and the view!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main fix: backwards pagination when a channel was never opened.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bug/FLU-547_fix_pagination_on_unread_channels

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@VelikovPetar VelikovPetar marked this pull request as ready for review June 30, 2026 07:22

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
packages/stream_chat_flutter_core/test/stream_channel_test.dart (1)

838-862: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add the stale-channel zero-sentinel case.

This only locks down the isUpToDate == true path. The production fix also depends on reloading the latest page when the zero-time sentinel arrives on a stale channel, so add a companion case with state.isUpToDate = false and assert a latest-page query happens with both around-anchors still null.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/stream_chat_flutter_core/test/stream_channel_test.dart` around lines
838 - 862, The current test only covers the zero-time sentinel case when the
channel is already up to date; add a companion widget test in
stream_channel_test that sets mockChannel.state.isUpToDate to false and keeps
lastReadMessageId and the around-anchor inputs null while
currentUserRead.lastRead is DateTime.utc(1, 1, 1). In
_pumpStreamChannel/_maybeInitChannel coverage, assert that a latest-page query
is issued for the stale-channel path, using mockChannel.query to verify the
reload behavior rather than verifyNever.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/stream_chat_flutter_core/test/stream_channel_test.dart`:
- Around line 838-862: The current test only covers the zero-time sentinel case
when the channel is already up to date; add a companion widget test in
stream_channel_test that sets mockChannel.state.isUpToDate to false and keeps
lastReadMessageId and the around-anchor inputs null while
currentUserRead.lastRead is DateTime.utc(1, 1, 1). In
_pumpStreamChannel/_maybeInitChannel coverage, assert that a latest-page query
is issued for the stale-channel path, using mockChannel.query to verify the
reload behavior rather than verifyNever.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a1f671f2-011a-48d1-b78e-028786108896

📥 Commits

Reviewing files that changed from the base of the PR and between eb0a0ec and 7608989.

📒 Files selected for processing (3)
  • packages/stream_chat_flutter_core/CHANGELOG.md
  • packages/stream_chat_flutter_core/lib/src/stream_channel.dart
  • packages/stream_chat_flutter_core/test/stream_channel_test.dart

@codecov

codecov Bot commented Jun 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.09%. Comparing base (023687f) to head (bd9d2f4).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2789      +/-   ##
==========================================
+ Coverage   70.04%   70.09%   +0.05%     
==========================================
  Files         426      426              
  Lines       25680    25682       +2     
==========================================
+ Hits        17987    18003      +16     
+ Misses       7693     7679      -14     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

// channel tail, after which `_inferBoundariesFromAnchorTimestamp`
// would mis-conclude `_topPaginationEnded = true`. Skip in that case
// and fall through to the catch-all "load latest" below.
if (currentUserRead.lastRead.toUtc().isAfter(DateTime.utc(1970, 1, 1))) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the comparison works without converting them to utc. Also maybe good to extract the default somewhere in static or const value in the class.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants