Improve mention suggestion search for large channels - #4213
Conversation
When a channel has more members than lastActiveMembers can hold, mention providers now query members via MemberList instead of only filtering the local member list.
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughMention providers now use shared search logic. Large channels use remote member searches for non-empty queries. Search APIs and controllers add configurable debouncing, cancellation, and result clearing. ChangesMention suggestion search
Estimated code review effort: 4 (Complex) | ~45 minutes Mergeability Score: ⚪ Minimal · up to The PR improves large-channel mention search and stale-result clearing without any identified merge-blocking risk; it is merge-ready after normal checks and review. Sequence Diagram(s)sequenceDiagram
participant MentionSuggestionsProvider
participant MentionSuggestionsSearch
participant MemberSearch
participant ChannelMemberListUpdater
MentionSuggestionsProvider->>MentionSuggestionsSearch: fetchUsers request
MentionSuggestionsSearch->>MemberSearch: search remote channel members
MemberSearch->>ChannelMemberListUpdater: load member query
ChannelMemberListUpdater-->>MemberSearch: return members
MemberSearch-->>MentionSuggestionsProvider: return filtered suggestions
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 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.
Inline comments:
In `@Sources/StreamChat/MentionSuggestions/MentionSuggestionsSearch.swift`:
- Around line 85-87: Update the mentionAllAppUsers branch in
MentionSuggestionsSearch to filter the result of userSearch.search(query:) and
exclude the user whose ID matches currentUserId before returning it. Preserve
all other app-wide results, and add coverage for an app-wide response containing
the current user.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: f04d3e39-3f6d-436e-b2a9-977910970a1d
📒 Files selected for processing (6)
Sources/StreamChat/MentionSuggestions/DefaultMentionSuggestionsProvider.swiftSources/StreamChat/MentionSuggestions/EnhancedMentionSuggestionsProvider.swiftSources/StreamChat/MentionSuggestions/MentionSuggestionsSearch.swiftTests/StreamChatTests/MentionSuggestions/DefaultMentionSuggestionsProvider_Tests.swiftTests/StreamChatTests/MentionSuggestions/EnhancedMentionSuggestionsProvider_Tests.swiftTests/StreamChatTests/MentionSuggestions/MentionSuggestionsSearch_Tests.swift
| public let mentionAllAppUsers: Bool | ||
|
|
||
| private let userSearch: UserSearch | ||
| private let makeMemberList: @Sendable (ChannelMemberListQuery) -> MemberList |
There was a problem hiding this comment.
why's it a closure and not a member list?
There was a problem hiding this comment.
This is because we need to create a MemberList object per query. At the moment we do not have a MemberSearch object, like now we do with ChannelSearch. It could make sense to do it now 🤔 WDYT? Or do it in a seperate PR
There was a problem hiding this comment.
What about just adding state later version? We should start moving towards async-await more.
There was a problem hiding this comment.
Then we have nice set of these. Channel, messages, member. Also the add members view in contact info vould benefit from debounce supported MemberSearch.
There was a problem hiding this comment.
This is the state layer; the provider only uses the state layer, actually. But yeah, at the moment we don't have MemberSearch. But if we want to add it in this PR, I can do it
There was a problem hiding this comment.
@laevandus @martinmitrevski I've updated the PR to include MemberSearch. Also, I added debouncing support for the other providers (Role Search and Group Search). While I was at it, I also added the missing GroupSearchCOntroller that was missing. (Why not)
Introduce a reusable state-layer search API matching UserSearch and ChannelSearch, and route large-channel mention suggestions through it instead of creating a MemberList per keystroke.
Match UserSearch/ChannelSearch so role mention typing cancels in-flight work and debounces short queries instead of hitting the API each keystroke.
Introduce a delegate-based search controller parallel to RoleSearchController, and apply the same dynamic debounce/cancel behavior to UserGroupSearch for group mention typing.
Cancel in-flight searches and clear cached results when a mention query ends, so superseded responses cannot resurface stale suggestions.
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 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.
Inline comments:
In
`@Sources/StreamChat/Controllers/RoleSearchController/RoleSearchController.swift`:
- Around line 100-107: In RoleSearchController.swift
(Sources/StreamChat/Controllers/RoleSearchController/RoleSearchController.swift#L100-L107),
add a request generation before scheduling each search, invalidate it in
clearResults(), and gate fetch results so only the current generation may update
roles, notify the delegate, or invoke completion; apply the same guard in
UserGroupSearchController.swift
(Sources/StreamChat/Controllers/UserGroupSearchController/UserGroupSearchController.swift#L91-L98).
Add controlled stale-completion tests in RoleSearchController_Tests.swift
(Tests/StreamChatTests/Controllers/RoleSearchController/RoleSearchController_Tests.swift#L117-L130)
and UserGroupSearchController_Tests.swift
(Tests/StreamChatTests/Controllers/UserGroupSearchController/UserGroupSearchController_Tests.swift#L129-L144)
covering an old request completing after a newer search and after
clearResults().
In `@Sources/StreamChat/StateLayer/MemberSearchState.swift`:
- Around line 42-44: Require a non-nil current query before MemberSearchState
applies completed results, update
Sources/StreamChat/StateLayer/MemberSearch.swift:97-101 so clearResults
invalidates or cancels in-flight pagination, and update
Sources/StreamChat/StateLayer/UserSearchState.swift:27-31 to reject responses
received after clearing; add tests covering pagination followed by clearResults
and verify both states remain empty.
In `@StreamChat.xcodeproj/project.pbxproj`:
- Line 869: Remove both UserGroupSearchController_Tests.swift and
MemberSearch_Tests.swift from the membershipExceptions entries in
StreamChat.xcodeproj/project.pbxproj: update the entries at lines 869-869 and
969-969 so neither test file is excluded from the StreamChatTests target.
In
`@TestTools/StreamChatTestTools/Mocks/StreamChat/Workers/ChannelMemberListUpdater_Mock.swift`:
- Around line 17-23: Update cleanUp() in the mock to reset load_query_called to
the default no-op closure { _ in } alongside the other load callback state,
ensuring subsequent tests cannot invoke a callback captured by an earlier
expectation.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: b9474660-a51a-4387-b968-f06a763a911f
📒 Files selected for processing (24)
CHANGELOG.mdSources/StreamChat/Controllers/RoleSearchController/RoleSearchController.swiftSources/StreamChat/Controllers/UserGroupSearchController/UserGroupSearchController.swiftSources/StreamChat/MentionSuggestions/DefaultMentionSuggestionsProvider.swiftSources/StreamChat/MentionSuggestions/EnhancedMentionSuggestionsProvider.swiftSources/StreamChat/MentionSuggestions/MentionSuggestionsProvider.swiftSources/StreamChat/MentionSuggestions/MentionSuggestionsSearch.swiftSources/StreamChat/StateLayer/ChatClient+Factory.swiftSources/StreamChat/StateLayer/MemberSearch.swiftSources/StreamChat/StateLayer/MemberSearchState.swiftSources/StreamChat/StateLayer/RoleSearch.swiftSources/StreamChat/StateLayer/RoleSearchState.swiftSources/StreamChat/StateLayer/UserGroupSearch.swiftSources/StreamChat/StateLayer/UserGroupSearchState.swiftSources/StreamChat/StateLayer/UserSearch.swiftSources/StreamChat/StateLayer/UserSearchState.swiftStreamChat.xcodeproj/project.pbxprojTestTools/StreamChatTestTools/Mocks/StreamChat/Workers/ChannelMemberListUpdater_Mock.swiftTests/StreamChatTests/Controllers/RoleSearchController/RoleSearchController_Tests.swiftTests/StreamChatTests/Controllers/UserGroupSearchController/UserGroupSearchController_Tests.swiftTests/StreamChatTests/StateLayer/MemberSearch_Tests.swiftTests/StreamChatTests/StateLayer/RoleSearch_Tests.swiftTests/StreamChatTests/StateLayer/UserGroupSearch_Tests.swiftTests/StreamChatTests/StateLayer/UserSearch_Tests.swift
🚧 Files skipped from review as they are similar to previous changes (2)
- Sources/StreamChat/MentionSuggestions/MentionSuggestionsSearch.swift
- Sources/StreamChat/MentionSuggestions/EnhancedMentionSuggestionsProvider.swift
Generated by 🚫 Danger |
Clear retained load completions from the mock so MemberController and MemberListController can deallocate after synchronize completes.
laevandus
left a comment
There was a problem hiding this comment.
✅ Verified that mentioning users not in the last active members works now. I am OK adding the additional controller as well.
Apply dynamic search debouncing to various search controllers.
Public Interface+ public class MemberSearch: @unchecked Sendable
+
+ @MainActor public var state: MemberSearchState
+
+
+ @discardableResult public func search(text: String,in cid: ChannelId)async throws -> [ChatChannelMember]
+ @discardableResult public func search(query: ChannelMemberListQuery)async throws -> [ChatChannelMember]
+ @discardableResult public func loadMoreMembers(limit: Int? = nil)async throws -> [ChatChannelMember]
+ public func clearResults()async
+ public extension UserGroupSearchControllerDelegate
+ @MainActor public final class MemberSearchState: ObservableObject
+
+ @Published public internal var query: ChannelMemberListQuery?
+ @Published public internal var members: [ChatChannelMember]
+ public class UserGroupSearchController: DataController, DelegateCallable, DataStoreProvider, @unchecked Sendable
+
+ public let client: ChatClient
+ public private var userGroups: [UserGroup]
+ public weak var delegate: UserGroupSearchControllerDelegate?
+
+
+ public func searchUserGroups(text: String,teamId: String? = nil,completion: (@MainActor (Result<[UserGroup], Error>) -> Void)? = nil)
+ public func searchUserGroups(query: UserGroupSearchQuery,completion: (@MainActor (Result<[UserGroup], Error>) -> Void)? = nil)
+ public func clearResults()
+ public protocol UserGroupSearchControllerDelegate: DataControllerStateDelegate
public final class DefaultMentionSuggestionsProvider: MentionSuggestionsProvider, Sendable
+ public func clearResults()async
public final class EnhancedMentionSuggestionsProvider: MentionSuggestionsProvider, Sendable
+ public func clearResults()async
public class UserSearch: @unchecked Sendable
+ public func clearResults()async
public class RoleSearch: @unchecked Sendable
+ public func clearResults()async
public class RoleSearchController: DataController, DelegateCallable, DataStoreProvider, @unchecked Sendable
+ public func clearResults()
public class UserGroupSearch: @unchecked Sendable
+ public func clearResults()async |
SDK Size
|
StreamChat XCSize
Show 21 more objects
|
StreamChatUI XCSize
|
|



🔗 Issue Links
🎯 Goal
Fix mention suggestions in large group chats where the target member is not in
ChatChannel.lastActiveMembers, and make mention search debounce/clear reliably so typing and deleting@…does not surface stale results.📝 Summary
memberCount > lastActiveMembers.countMemberSearch/MemberSearchStateand use it from mention suggestion providersRoleSearch/RoleSearchControllerandUserGroupSearch/UserGroupSearchControllerclearResults()across user, member, role, and user group search APIs (and mention providers) to cancel in-flight work and clear cached results@from loading the full remote member list on large channels🛠 Implementation
Large-channel member mentions
When
channel.memberCount > channel.lastActiveMembers.count, mention providers route user suggestions through remote member search instead of only filteringlastActiveMembers/lastActiveWatchers. Shared logic lives inMentionSuggestionsSearch. Smaller channels keep the existing local search path.Debounced search state
Mention providers now reuse long-lived search objects:
MemberSearchfor channel membersUserSearchfor app-wide usersRoleSearch/UserGroupSearch(plus matching controllers) for enhanced mentionsClearing results
clearResults()cancels pending debounced work and clears search state.MentionSuggestionsProvider.clearResults()forwards that to the underlying search objects so UI layers can drop stale suggestion state when the mention query ends.🧪 Manual Testing Notes
@followed by a member name who is not in the first 100 cached members@to an empty composer and confirm suggestions disappear (no stale resurfacing)☑️ Contributor Checklist
docs-contentrepoSummary by CodeRabbit
New Features
Bug Fixes
Tests