Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional sorting filter keys for member list query #3535

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Add `ChannelListSortingKey.pinnedAt`
- Add `ChatChannel.membership.pinnedAt`
- Add `ChatChannel.isPinned`
- Add member list filtering keys: `FilterKey.channelRole` and `FilterKey.email` [#3535](https://github.com/GetStream/stream-chat-swift/pull/3535)
- Add member list sorting key: `ChannelMemberListSortingKey.channelRole` [#3535](https://github.com/GetStream/stream-chat-swift/pull/3535)
### 🐞 Fixed
- End background task before starting a new one [#3528](https://github.com/GetStream/stream-chat-swift/pull/3528)

Expand Down
12 changes: 12 additions & 0 deletions Sources/StreamChat/Models/Member.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ public extension MemberRole {
self = MemberRole(rawValue: value)
}
}

func encode(to encoder: any Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .member:
try container.encode("channel_member")
case .moderator:
try container.encode("channel_moderator")
default:
try container.encode(rawValue)
}
}
Comment on lines +163 to +173
Copy link
Contributor Author

Choose a reason for hiding this comment

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

MemberRole must be encoded like this when using it as member list filter or as sorting key. Note that MemberRole.member is used for member and channel_member values (same with moderation).

}

/// The member information when adding a member to a channel.
Expand Down
8 changes: 8 additions & 0 deletions Sources/StreamChat/Query/ChannelMemberListQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public extension FilterKey where Scope: AnyMemberListFilterScope {
/// Filter key matching the name of the user
/// Supported operators: `equal`, `notEqual`, `in`, `notIn`, `autocomplete`, `query`
static var name: FilterKey<Scope, String> { "name" }

/// Filter key matching the email of the user
/// Supported operators: `equal`, `in`, `autocomplete`
static var email: FilterKey<Scope, String> { "user.email" }

/// Filter key matching the channel role of the user
/// Supported operators: `equal`
static var channelRole: FilterKey<Scope, MemberRole> { "channel_role" }

/// Filter key matching the banned status
/// Supported operators: `equal`
Expand Down
1 change: 1 addition & 0 deletions Sources/StreamChat/Query/Filter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ extension Filter: FilterValue {}

extension ChannelId: FilterValue {}
extension ChannelType: FilterValue {}
extension MemberRole: FilterValue {}
extension UserRole: FilterValue {}
extension AttachmentType: FilterValue {}
extension Optional: FilterValue where Wrapped == TeamId {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ public enum ChannelMemberListSortingKey: String, SortingKey {
///
/// - Warning: This option is heavy for the backend and can slow down API requests' response time. If there's no explicit requirement for this sorting option consider using a different one.
case name = "user.name"

/// Sort channel members by their channel role.
case channelRole = "channelRoleRaw"

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
let value: String

switch self {
/// Sort channel members by date they were created.
case .channelRole: value = "channel_role"
case .createdAt: value = "created_at"
case .name: value = "name"
case .userId: value = "user_id"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ final class MemberListFilterScope_Tests: XCTestCase {
func test_filterKeys_matchChannelCodingKeys() {
// Member specific coding keys
XCTAssertEqual(Key<Bool>.isModerator.rawValue, "is_moderator")
XCTAssertEqual(Key<String>.email.rawValue, "user.email")
XCTAssertEqual(Key<MemberRole>.channelRole.rawValue, "channel_role")

// User-related coding keys
XCTAssertEqual(Key<UserId>.id.rawValue, UserPayloadsCodingKeys.id.rawValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class ChannelMemberListSortingKey_Tests: XCTestCase {
func test_sortDescriptor_keyPaths_areValid() throws {
// Put all `ChannelMemberListSortingKey`s in an array
// We don't use `CaseIterable` since we only need this for tests
let sortingKeys: [ChannelMemberListSortingKey] = [.createdAt, .name, .userId]
let sortingKeys: [ChannelMemberListSortingKey] = [.createdAt, .name, .channelRole, .userId]

// Iterate over keys...
for key in sortingKeys {
Expand All @@ -24,6 +24,8 @@ final class ChannelMemberListSortingKey_Tests: XCTestCase {
ChannelMemberListSortingKey.name.rawValue,
NSExpression(forKeyPath: \MemberDTO.user.name).keyPath
)
case .channelRole:
XCTAssertEqual(key.rawValue, KeyPath.string(\MemberDTO.channelRoleRaw))
case .userId:
XCTAssertEqual(key.rawValue, NSExpression(forKeyPath: \MemberDTO.user.id).keyPath)
}
Expand Down
Loading