Skip to content

Conversation

xsahil03x
Copy link
Member

@xsahil03x xsahil03x commented Jul 3, 2025

Submit a pull request

Fixes: #2298

Summary by CodeRabbit

  • New Features
    • Introduced a method to retrieve unread message counts for the current user, including detailed information by channel, thread, and channel type.
    • Added new data models to represent unread counts across channels, threads, and channel types.
  • Documentation
    • Updated the changelog to reflect the new unread counts feature.
  • Tests
    • Added comprehensive tests to validate unread counts retrieval, state updates, and data model serialization.

Copy link
Contributor

coderabbitai bot commented Jul 3, 2025

Walkthrough

This update introduces support for the getUnreadCount API in the stream_chat package. It adds a new client method, underlying API call, response models, and corresponding unit tests. The changelog and public exports are updated to reflect the new feature, ensuring users can query detailed unread message counts and related metadata.

Changes

Files/Paths Change Summary
packages/stream_chat/CHANGELOG.md Changelog updated to document new client.getUnreadCount() feature.
packages/stream_chat/lib/src/client/client.dart
packages/stream_chat/lib/src/core/api/user_api.dart
Added getUnreadCount method to StreamChatClient and UserApi for fetching unread counts.
packages/stream_chat/lib/src/core/api/responses.dart
packages/stream_chat/lib/src/core/api/responses.g.dart
Introduced GetUnreadCountResponse model and its JSON deserialization logic.
packages/stream_chat/lib/src/core/models/unread_counts.dart
packages/stream_chat/lib/src/core/models/unread_counts.g.dart
Added models for unread counts (channel, thread, channel type) and their JSON (de)serialization.
packages/stream_chat/lib/stream_chat.dart Exported unread counts models to public API.
packages/stream_chat/test/src/client/client_test.dart
packages/stream_chat/test/src/core/api/user_api_test.dart
packages/stream_chat/test/src/core/models/unread_counts_test.dart
Added unit tests for unread count API, models, and client state updates.

Sequence Diagram(s)

sequenceDiagram
    participant ClientApp
    participant StreamChatClient
    participant UserApi
    participant API_Server

    ClientApp->>StreamChatClient: getUnreadCount()
    StreamChatClient->>UserApi: getUnreadCount()
    UserApi->>API_Server: GET /unread
    API_Server-->>UserApi: GetUnreadCountResponse JSON
    UserApi-->>StreamChatClient: GetUnreadCountResponse
    StreamChatClient-->>ClientApp: GetUnreadCountResponse
Loading

Assessment against linked issues

Objective Addressed Explanation
Add a method to StreamChatClient for fetching unread counts, returning detailed info (channels, channel types, threads, etc.) (#2298)

Suggested reviewers

  • renefloor

Poem

A bunny hopped through code so neat,
To count unread, a clever feat!
Now channels, threads, and types appear,
With numbers crisp and data clear.
The tests all pass, the docs are right—
This unread count’s a pure delight!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 850a817 and 21ef438.

📒 Files selected for processing (11)
  • packages/stream_chat/CHANGELOG.md (1 hunks)
  • packages/stream_chat/lib/src/client/client.dart (1 hunks)
  • packages/stream_chat/lib/src/core/api/responses.dart (2 hunks)
  • packages/stream_chat/lib/src/core/api/responses.g.dart (1 hunks)
  • packages/stream_chat/lib/src/core/api/user_api.dart (1 hunks)
  • packages/stream_chat/lib/src/core/models/unread_counts.dart (1 hunks)
  • packages/stream_chat/lib/src/core/models/unread_counts.g.dart (1 hunks)
  • packages/stream_chat/lib/stream_chat.dart (1 hunks)
  • packages/stream_chat/test/src/client/client_test.dart (1 hunks)
  • packages/stream_chat/test/src/core/api/user_api_test.dart (1 hunks)
  • packages/stream_chat/test/src/core/models/unread_counts_test.dart (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (11)
  • packages/stream_chat/lib/stream_chat.dart
  • packages/stream_chat/CHANGELOG.md
  • packages/stream_chat/lib/src/core/api/user_api.dart
  • packages/stream_chat/test/src/core/api/user_api_test.dart
  • packages/stream_chat/lib/src/core/api/responses.g.dart
  • packages/stream_chat/lib/src/client/client.dart
  • packages/stream_chat/lib/src/core/api/responses.dart
  • packages/stream_chat/lib/src/core/models/unread_counts.g.dart
  • packages/stream_chat/test/src/core/models/unread_counts_test.dart
  • packages/stream_chat/test/src/client/client_test.dart
  • packages/stream_chat/lib/src/core/models/unread_counts.dart
⏰ Context from checks skipped due to timeout of 90000ms (10)
  • GitHub Check: stream_chat_localizations
  • GitHub Check: stream_chat_flutter
  • GitHub Check: build (ios)
  • GitHub Check: build (android)
  • GitHub Check: test
  • GitHub Check: stream_chat
  • GitHub Check: stream_chat_persistence
  • GitHub Check: stream_chat_flutter_core
  • GitHub Check: analyze
  • GitHub Check: analyze_legacy_versions

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 367d3f8 and 8cde8c2.

📒 Files selected for processing (11)
  • packages/stream_chat/CHANGELOG.md (1 hunks)
  • packages/stream_chat/lib/src/client/client.dart (1 hunks)
  • packages/stream_chat/lib/src/core/api/responses.dart (2 hunks)
  • packages/stream_chat/lib/src/core/api/responses.g.dart (1 hunks)
  • packages/stream_chat/lib/src/core/api/user_api.dart (1 hunks)
  • packages/stream_chat/lib/src/core/models/unread_counts.dart (1 hunks)
  • packages/stream_chat/lib/src/core/models/unread_counts.g.dart (1 hunks)
  • packages/stream_chat/lib/stream_chat.dart (1 hunks)
  • packages/stream_chat/test/src/client/client_test.dart (1 hunks)
  • packages/stream_chat/test/src/core/api/user_api_test.dart (1 hunks)
  • packages/stream_chat/test/src/core/models/unread_counts_test.dart (1 hunks)
🔇 Additional comments (11)
packages/stream_chat/lib/stream_chat.dart (1)

61-61: LGTM! Clean export addition.

The export statement correctly exposes the unread counts models following the established pattern and maintains alphabetical ordering.

packages/stream_chat/lib/src/core/api/user_api.dart (1)

92-97: LGTM! Well-implemented API method.

The getUnreadCount() method follows the established patterns in the codebase:

  • Clear documentation
  • Proper async/await usage
  • Consistent endpoint pattern
  • Standard response parsing with JSON deserialization
packages/stream_chat/lib/src/client/client.dart (1)

1550-1562: LGTM! Well-implemented unread count method.

The implementation correctly:

  • Fetches unread count data from the API
  • Updates the client state with the response data
  • Uses array lengths for unreadChannels and unreadThreads counts, which aligns with the response structure
  • Follows the established pattern for async client methods

The state update approach using response.channels.length and response.threads.length is appropriate since these represent the actual unread channels and threads for the user.

packages/stream_chat/lib/src/core/models/unread_counts.g.dart (1)

1-55: Generated serialization code looks correct.

The JSON serialization/deserialization functions follow the expected json_serializable patterns:

  • Proper type conversions for numeric fields using .toInt()
  • Correct DateTime handling with DateTime.parse() and .toIso8601String()
  • Appropriate field name mappings between snake_case and camelCase

No issues with the generated code.

packages/stream_chat/test/src/core/api/user_api_test.dart (1)

186-248: Excellent comprehensive test coverage.

The test thoroughly validates the getUnreadCount functionality:

  • Mocks realistic response data with all expected fields
  • Verifies proper deserialization of complex nested structures (channels, threads, channel types)
  • Tests both simple fields (counts) and complex data (team mappings, arrays)
  • Confirms correct HTTP method (GET) and endpoint path (/unread)
  • Uses proper verification patterns to ensure no unexpected interactions

The mock data structure closely matches what would be expected from the actual API, providing good confidence in the integration.

packages/stream_chat/test/src/client/client_test.dart (2)

2634-2652: LGTM! Well-structured basic functionality test.

The test properly covers the happy path scenario for getUnreadCount() with appropriate mocking, clear assertions, and proper API interaction verification.


2654-2696: Excellent state management test coverage!

This test effectively verifies that getUnreadCount() not only returns the correct response but also properly updates the client's current user state. The comprehensive mock data with realistic UnreadCountsChannel and UnreadCountsThread objects provides thorough coverage of the state update functionality.

packages/stream_chat/lib/src/core/api/responses.g.dart (1)

471-491: Generated JSON deserialization looks correct.

The generated deserialization function properly handles the new unread count response fields. The nullable totalUnreadCountByTeam field is correctly handled with optional mapping, while required fields are directly cast as expected from the source model annotations.

packages/stream_chat/test/src/core/models/unread_counts_test.dart (1)

1-127: Excellent comprehensive test coverage!

The tests thoroughly validate JSON serialization and deserialization for all three unread count models with realistic test data. The additional test for different channel types demonstrates good edge case consideration.

packages/stream_chat/lib/src/core/api/responses.dart (1)

21-21: LGTM!

The import for unread_counts.dart is properly added to support the new response model.

packages/stream_chat/lib/src/core/models/unread_counts.dart (1)

1-95: Well-designed immutable data models!

The three unread count model classes follow excellent design principles:

  • Immutable design with final fields and const constructors
  • Comprehensive documentation with clear field descriptions
  • Consistent API patterns across all models
  • Appropriate type choices for each field

The models provide a clean abstraction for unread count data at different granularities (channel, thread, and channel type levels).

@xsahil03x xsahil03x force-pushed the feat/get-unread-count branch from 850a817 to 21ef438 Compare July 4, 2025 09:37
@xsahil03x xsahil03x requested a review from renefloor July 4, 2025 09:39
Copy link

codecov bot commented Jul 4, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 63.50%. Comparing base (367d3f8) to head (21ef438).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2300      +/-   ##
==========================================
+ Coverage   63.45%   63.50%   +0.04%     
==========================================
  Files         408      409       +1     
  Lines       25561    25584      +23     
==========================================
+ Hits        16221    16247      +26     
+ Misses       9340     9337       -3     

☔ View full report in Codecov by Sentry.
📢 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.

@xsahil03x xsahil03x merged commit 34c64f7 into master Jul 4, 2025
19 checks passed
@xsahil03x xsahil03x deleted the feat/get-unread-count branch July 4, 2025 10:20
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.

add support for getUnreadCount API
2 participants