Skip to content

Commit

Permalink
Add channel role sorting key and channel role and email filter keys f…
Browse files Browse the repository at this point in the history
…or ChannelMemberListQuery
  • Loading branch information
laevandus committed Dec 17, 2024
1 parent 8b9690d commit 3e195b3
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 2 deletions.
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)
}
}
}

/// 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 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,15 +16,18 @@ 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 role (`channel_role`).
case role = "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 .createdAt: value = "created_at"
case .name: value = "name"
case .role: value = "channel_role"
case .userId: value = "user_id"
}

Expand Down
2 changes: 2 additions & 0 deletions Tests/StreamChatTests/Query/MemberListFilterScope_Tests.swift
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, .role, .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 .role:
XCTAssertEqual(key.rawValue, KeyPath.string(\MemberDTO.channelRoleRaw))
case .userId:
XCTAssertEqual(key.rawValue, NSExpression(forKeyPath: \MemberDTO.user.id).keyPath)
}
Expand Down

0 comments on commit 3e195b3

Please sign in to comment.