Skip to content

Fix passing outdated Channel data for CidEvents to the ChatEventHandler.handleChatEvent#6381

Merged
VelikovPetar merged 1 commit intov6from
bug/fix_passing_outdated_data_to_chat_event_handler
Apr 23, 2026
Merged

Fix passing outdated Channel data for CidEvents to the ChatEventHandler.handleChatEvent#6381
VelikovPetar merged 1 commit intov6from
bug/fix_passing_outdated_data_to_chat_event_handler

Conversation

@VelikovPetar
Copy link
Copy Markdown
Contributor

@VelikovPetar VelikovPetar commented Apr 22, 2026

Goal

Currently we always read the Channel data from the DB before passing it to the ChatEventHandler.handleChatEvent. This has two issues:

  1. If OfflinePlugin is not applied, the Channel will always be null, even though we could have it in-memory
  2. The channel read from the DB will be outdated - because the DB is updated after ChatEventHandler.handleChatEvent I processed. But the in-memory channel will always be up-to-date, because the event is handled state-side before calling ChatEventHandler.handleChatEvent.

This PR changes this logic so that we always prioritise the in-memory channel, and fallback to DB if the Channel is not found in-memory.

Implementation

Prioritise the in-memory channel state instead of offline state.

🎨 UI Changes

Before After
reveal_channel_before.mov
reveal_channel_after.mov

Testing

  1. Disable offline storage (remove OfflinePlugin)
  2. Hide a channel
  3. From a different user, send a message in the channel
  4. The channel should again be revealed automatically, without the need to refresh the channels

Summary by CodeRabbit

  • Bug Fixes

    • Improved channel resolution logic for incoming chat events to prioritize in-memory channel state, reducing unnecessary database lookups and enhancing performance.
  • Tests

    • Added comprehensive unit tests to validate channel state resolution and event handling behavior.

@VelikovPetar VelikovPetar added the pr:bug Bug fix label Apr 22, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 22, 2026

PR checklist ✅

All required conditions are satisfied:

  • Title length is OK (or ignored by label).
  • At least one pr: label exists.
  • Sections ### Goal, ### Implementation, and ### Testing are filled.

🎉 Great job! This PR is ready for review.

@github-actions
Copy link
Copy Markdown
Contributor

SDK Size Comparison 📏

SDK Before After Difference Status
stream-chat-android-client 5.26 MB 5.26 MB 0.00 MB 🟢
stream-chat-android-offline 5.49 MB 5.49 MB 0.00 MB 🟢
stream-chat-android-ui-components 10.64 MB 10.64 MB 0.00 MB 🟢
stream-chat-android-compose 12.87 MB 12.87 MB 0.00 MB 🟢

@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
70.8% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@VelikovPetar VelikovPetar marked this pull request as ready for review April 22, 2026 11:03
@VelikovPetar VelikovPetar requested a review from a team as a code owner April 22, 2026 11:03
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 22, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: c6acce3b-2886-42b2-859f-99681b847e43

📥 Commits

Reviewing files that changed from the base of the PR and between aa5a191 and ab1efa3.

📒 Files selected for processing (4)
  • stream-chat-android-state/src/main/java/io/getstream/chat/android/state/plugin/logic/querychannels/internal/QueryChannelsLogic.kt
  • stream-chat-android-state/src/main/java/io/getstream/chat/android/state/plugin/logic/querychannels/internal/QueryChannelsStateLogic.kt
  • stream-chat-android-state/src/test/java/io/getstream/chat/android/state/plugin/logic/querychannels/internal/QueryChannelsLogicTest.kt
  • stream-chat-android-state/src/test/java/io/getstream/chat/android/state/plugin/logic/querychannels/internal/QueryChannelsStateLogicTest.kt

Walkthrough

The PR optimizes channel resolution in event handling by implementing a hybrid lookup strategy. Instead of always fetching all channels from the database upfront, the code now checks in-memory active channel state first via a new getActiveChannelState method, then fetches only remaining channels from the database, and merges the results before event processing.

Changes

Cohort / File(s) Summary
Channel Resolution Logic
stream-chat-android-state/src/main/java/io/getstream/chat/android/state/plugin/logic/querychannels/internal/QueryChannelsLogic.kt, stream-chat-android-state/src/main/java/io/getstream/chat/android/state/plugin/logic/querychannels/internal/QueryChannelsStateLogic.kt
QueryChannelsLogic.parseChatEventResults now uses a two-step channel resolution: checks in-memory active state first, then fetches only missing channels from database. New getActiveChannelState(cid) method added to QueryChannelsStateLogic to retrieve snapshot of active channel state from the registry.
Test Coverage
stream-chat-android-state/src/test/java/io/getstream/chat/android/state/plugin/logic/querychannels/internal/QueryChannelsLogicTest.kt, stream-chat-android-state/src/test/java/io/getstream/chat/android/state/plugin/logic/querychannels/internal/QueryChannelsStateLogicTest.kt
Added 5 new unit tests: 3 tests for parseChatEventResults covering in-memory resolution, database fallback, and mixed scenarios; 2 tests for getActiveChannelState verifying correct returns for active and inactive channels.

Sequence Diagram

sequenceDiagram
    participant Event as Chat Event
    participant Logic as QueryChannelsLogic
    participant StateReg as StateRegistry<br/>(In-Memory)
    participant DB as Database
    participant Handler as Event Handler

    Event->>Logic: parseChatEventResults(events)
    
    loop For each event's target CID
        Logic->>StateReg: getActiveChannelState(cid)
        alt Channel is Active
            StateReg-->>Logic: Channel snapshot
        else Channel Not Active
            StateReg-->>Logic: null
        end
    end
    
    Note over Logic: Collect CIDs missing<br/>from in-memory state
    Logic->>DB: selectChannels(missingCids)
    DB-->>Logic: Channel data
    
    Note over Logic: Merge in-memory entries<br/>with DB results
    Logic->>Handler: handleChatEvent(event, resolvedChannel)
    Handler-->>Logic: EventHandlingResult
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~35 minutes

Poem

🐰 Channel states, oh what a sight!
First in-memory, shiny and bright,
Then to the database we hop with care,
Merging results with rabbit-like flair!
Efficient lookups, no more delay,
Optimized channels lead the way! 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: fixing the issue of passing outdated Channel data for CidEvents to ChatEventHandler.handleChatEvent.
Description check ✅ Passed The description covers the required Goal, Implementation, UI Changes, and Testing sections. However, the Contributor and Reviewer checklists are not filled out.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bug/fix_passing_outdated_data_to_chat_event_handler

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 and usage tips.

@VelikovPetar VelikovPetar merged commit 1b7caac into v6 Apr 23, 2026
20 of 23 checks passed
@VelikovPetar VelikovPetar deleted the bug/fix_passing_outdated_data_to_chat_event_handler branch April 23, 2026 08:58
@stream-public-bot stream-public-bot added the released Included in a release label Apr 24, 2026
@stream-public-bot
Copy link
Copy Markdown
Contributor

🚀 Available in v6.37.3

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

Labels

pr:bug Bug fix released Included in a release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants