mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 12:07:55 +00:00
Kick/ban room members (#2501)
* Add a temporary membershipChangePublisher on the timeline. * Kick/Ban users from a room. * Unit tests. * Disable autocorrection on the members list search field.
This commit is contained in:
@@ -17,7 +17,7 @@
|
||||
import Foundation
|
||||
import MatrixRustSDK
|
||||
|
||||
struct RoomMemberDetails: Identifiable, Equatable {
|
||||
struct RoomMemberDetails: Identifiable, Hashable {
|
||||
let id: String
|
||||
let name: String?
|
||||
let avatarURL: URL?
|
||||
|
||||
@@ -46,6 +46,7 @@ final class RoomMemberProxy: RoomMemberProxyProtocol {
|
||||
lazy var powerLevel = Int(member.powerLevel())
|
||||
lazy var role = member.suggestedRoleForPowerLevel()
|
||||
lazy var canInviteUsers = member.canInvite()
|
||||
lazy var canKickUsers = member.canKick()
|
||||
lazy var canBanUsers = member.canBan()
|
||||
|
||||
func canSendStateEvent(type: StateEventType) -> Bool {
|
||||
|
||||
@@ -34,6 +34,7 @@ protocol RoomMemberProxyProtocol: AnyObject {
|
||||
var powerLevel: Int { get }
|
||||
var role: RoomMemberRole { get }
|
||||
var canInviteUsers: Bool { get }
|
||||
var canKickUsers: Bool { get }
|
||||
var canBanUsers: Bool { get }
|
||||
|
||||
func ignoreUser() async -> Result<Void, RoomMemberProxyError>
|
||||
|
||||
@@ -416,6 +416,38 @@ class RoomProxy: RoomProxyProtocol {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Moderation
|
||||
|
||||
func kickUser(_ userID: String) async -> Result<Void, RoomProxyError> {
|
||||
do {
|
||||
try await room.kickUser(userId: userID, reason: nil)
|
||||
return .success(())
|
||||
} catch {
|
||||
MXLog.error("Failed kicking \(userID) with error: \(error)")
|
||||
return .failure(.failedModeration)
|
||||
}
|
||||
}
|
||||
|
||||
func banUser(_ userID: String) async -> Result<Void, RoomProxyError> {
|
||||
do {
|
||||
try await room.banUser(userId: userID, reason: nil)
|
||||
return .success(())
|
||||
} catch {
|
||||
MXLog.error("Failed banning \(userID) with error: \(error)")
|
||||
return .failure(.failedModeration)
|
||||
}
|
||||
}
|
||||
|
||||
func unbanUser(_ userID: String) async -> Result<Void, RoomProxyError> {
|
||||
do {
|
||||
try await room.unbanUser(userId: userID, reason: nil)
|
||||
return .success(())
|
||||
} catch {
|
||||
MXLog.error("Failed unbanning \(userID) with error: \(error)")
|
||||
return .failure(.failedModeration)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Element Call
|
||||
|
||||
func canUserJoinCall(userID: String) async -> Result<Bool, RoomProxyError> {
|
||||
|
||||
@@ -36,6 +36,7 @@ enum RoomProxyError: Error, Equatable {
|
||||
case failedMarkingAsRead
|
||||
case failedSendingTypingNotice
|
||||
case failedFlaggingAsFavourite
|
||||
case failedModeration
|
||||
}
|
||||
|
||||
enum RoomProxyAction {
|
||||
@@ -120,6 +121,12 @@ protocol RoomProxyProtocol {
|
||||
|
||||
func flagAsFavourite(_ isFavourite: Bool) async -> Result<Void, RoomProxyError>
|
||||
|
||||
// MARK: - Moderation
|
||||
|
||||
func kickUser(_ userID: String) async -> Result<Void, RoomProxyError>
|
||||
func banUser(_ userID: String) async -> Result<Void, RoomProxyError>
|
||||
func unbanUser(_ userID: String) async -> Result<Void, RoomProxyError>
|
||||
|
||||
// MARK: - Element Call
|
||||
|
||||
func canUserJoinCall(userID: String) async -> Result<Bool, RoomProxyError>
|
||||
|
||||
@@ -38,6 +38,12 @@ class RoomTimelineProvider: RoomTimelineProviderProtocol {
|
||||
.map { _, _ in () }
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
private let membershipChangeSubject = PassthroughSubject<Void, Never>()
|
||||
var membershipChangePublisher: AnyPublisher<Void, Never> {
|
||||
membershipChangeSubject
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
init(currentItems: [TimelineItem],
|
||||
updatePublisher: AnyPublisher<[TimelineDiff], Never>,
|
||||
@@ -99,7 +105,13 @@ class RoomTimelineProvider: RoomTimelineProviderProtocol {
|
||||
|
||||
MXLog.verbose("Append \(items.map(\.debugIdentifier))")
|
||||
for (index, item) in items.enumerated() {
|
||||
changes.append(.insert(offset: Int(itemProxies.count) + index, element: TimelineItemProxy(item: item), associatedWith: nil))
|
||||
let itemProxy = TimelineItemProxy(item: item)
|
||||
|
||||
if itemProxy.isMembershipChange {
|
||||
membershipChangeSubject.send(())
|
||||
}
|
||||
|
||||
changes.append(.insert(offset: Int(itemProxies.count) + index, element: itemProxy, associatedWith: nil))
|
||||
}
|
||||
case .clear:
|
||||
MXLog.verbose("Clear all items")
|
||||
@@ -129,6 +141,11 @@ class RoomTimelineProvider: RoomTimelineProviderProtocol {
|
||||
|
||||
MXLog.verbose("Push Back \(item.debugIdentifier)")
|
||||
let itemProxy = TimelineItemProxy(item: item)
|
||||
|
||||
if itemProxy.isMembershipChange {
|
||||
membershipChangeSubject.send(())
|
||||
}
|
||||
|
||||
changes.append(.insert(offset: Int(itemProxies.count), element: itemProxy, associatedWith: nil))
|
||||
case .pushFront:
|
||||
guard let item = diff.pushFront() else { fatalError() }
|
||||
@@ -197,6 +214,20 @@ private extension TimelineItemProxy {
|
||||
return .unknown(timelineID: String(item.uniqueId()))
|
||||
}
|
||||
}
|
||||
|
||||
var isMembershipChange: Bool {
|
||||
switch self {
|
||||
case .event(let eventTimelineItemProxy):
|
||||
switch eventTimelineItemProxy.content.kind() {
|
||||
case .roomMembership:
|
||||
true
|
||||
default:
|
||||
false
|
||||
}
|
||||
case .virtual, .unknown:
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private extension VirtualTimelineItem {
|
||||
|
||||
@@ -22,7 +22,14 @@ import MatrixRustSDK
|
||||
@MainActor
|
||||
// sourcery: AutoMockable
|
||||
protocol RoomTimelineProviderProtocol {
|
||||
/// A publisher that signals when ``itemProxies`` or ``backPaginationState`` are changed.
|
||||
var updatePublisher: AnyPublisher<Void, Never> { get }
|
||||
/// The current set of items in the timeline.
|
||||
var itemProxies: [TimelineItemProxy] { get }
|
||||
/// Whether the timeline is back paginating or not (or has reached the start of the room).
|
||||
var backPaginationState: BackPaginationStatus { get }
|
||||
/// A publisher that signals when changes to the room's membership have occurred through `/sync`.
|
||||
///
|
||||
/// This is temporary and will be replace by a subscription on the room itself.
|
||||
var membershipChangePublisher: AnyPublisher<Void, Never> { get }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user