Fixes #2486 - Implement blocked users management in the app settings

This commit is contained in:
Stefan Ceriu
2024-02-28 13:21:54 +02:00
committed by GitHub
parent a0b510cb6b
commit 7615e97c7f
46 changed files with 624 additions and 98 deletions
@@ -38,6 +38,9 @@ class ClientProxy: ClientProxyProtocol {
// periphery: ignore - only for retain
private var syncServiceStateUpdateTaskHandle: TaskHandle?
// periphery:ignore - required for instance retention in the rust codebase
private var ignoredUsersListenerTaskHandle: TaskHandle?
private var delegateHandle: TaskHandle?
// These following summary providers both operate on the same allRooms() list but
@@ -69,15 +72,21 @@ class ClientProxy: ClientProxyProtocol {
private var loadCachedAvatarURLTask: Task<Void, Never>?
private let userAvatarURLSubject = CurrentValueSubject<URL?, Never>(nil)
var userAvatarURL: CurrentValuePublisher<URL?, Never> {
var userAvatarURLPublisher: CurrentValuePublisher<URL?, Never> {
userAvatarURLSubject.asCurrentValuePublisher()
}
private let userDisplayNameSubject = CurrentValueSubject<String?, Never>(nil)
var userDisplayName: CurrentValuePublisher<String?, Never> {
var userDisplayNamePublisher: CurrentValuePublisher<String?, Never> {
userDisplayNameSubject.asCurrentValuePublisher()
}
private let ignoredUsersSubject = CurrentValueSubject<[String]?, Never>(nil)
var ignoredUsersPublisher: CurrentValuePublisher<[String]?, Never> {
ignoredUsersSubject
.asCurrentValuePublisher()
}
private var cancellables = Set<AnyCancellable>()
/// Will be `true` whilst the app cleans up and forces a logout. Prevents the sync service from restarting
@@ -92,7 +101,10 @@ class ClientProxy: ClientProxyProtocol {
}
}
let callbacks = PassthroughSubject<ClientProxyCallback, Never>()
private let actionsSubject = PassthroughSubject<ClientProxyAction, Never>()
var actionsPublisher: AnyPublisher<ClientProxyAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
private let loadingStateSubject = CurrentValueSubject<ClientProxyLoadingState, Never>(.notLoading)
var loadingStatePublisher: CurrentValuePublisher<ClientProxyLoadingState, Never> {
@@ -119,7 +131,7 @@ class ClientProxy: ClientProxyProtocol {
delegateHandle = client.setDelegate(delegate: ClientDelegateWrapper { [weak self] isSoftLogout in
self?.hasEncounteredAuthError = true
self?.callbacks.send(.receivedAuthError(isSoftLogout: isSoftLogout))
self?.actionsSubject.send(.receivedAuthError(isSoftLogout: isSoftLogout))
})
networkMonitor.reachabilityPublisher
@@ -135,6 +147,10 @@ class ClientProxy: ClientProxyProtocol {
await configureAppService()
loadUserAvatarURLFromCache()
ignoredUsersListenerTaskHandle = client.subscribeToIgnoredUsers(listener: IgnoredUsersListenerProxy { [weak self] ignoredUsers in
self?.ignoredUsersSubject.send(ignoredUsers)
})
}
var userID: String {
@@ -472,6 +488,28 @@ class ClientProxy: ClientProxyProtocol {
}
}
// MARK: - Ignored users
func ignoreUser(_ userID: String) async -> Result<Void, ClientProxyError> {
do {
try await client.ignoreUser(userId: userID)
return .success(())
} catch {
MXLog.error("Failed ignoring user with error: \(error)")
return .failure(.failedIgnoringUser)
}
}
func unignoreUser(_ userID: String) async -> Result<Void, ClientProxyError> {
do {
try await client.unignoreUser(userId: userID)
return .success(())
} catch {
MXLog.error("Failed unignoring user with error: \(error)")
return .failure(.failedUnignoringUser)
}
}
// MARK: Private
private func loadUserAvatarURLFromCache() {
@@ -570,7 +608,11 @@ class ClientProxy: ClientProxyProtocol {
}
// Hide the sync spinner as soon as we get any update back
callbacks.send(.receivedSyncUpdate)
actionsSubject.send(.receivedSyncUpdate)
if ignoredUsersSubject.value == nil {
updateIgnoredUsers()
}
})
}
@@ -620,6 +662,17 @@ class ClientProxy: ClientProxyProtocol {
return (nil, nil)
}
}
private func updateIgnoredUsers() {
Task {
do {
let ignoredUsers = try await client.ignoredUsers()
ignoredUsersSubject.send(ignoredUsers)
} catch {
MXLog.error("Failed fetching ignored users with error: \(error)")
}
}
}
}
extension ClientProxy: MediaLoaderProtocol {
@@ -690,3 +743,15 @@ private class ClientDelegateWrapper: ClientDelegate {
MXLog.info("Delegating session updates to the ClientSessionDelegate.")
}
}
private class IgnoredUsersListenerProxy: IgnoredUsersListener {
private let onUpdateClosure: ([String]) -> Void
init(onUpdateClosure: @escaping ([String]) -> Void) {
self.onUpdateClosure = onUpdateClosure
}
func call(ignoredUserIds: [String]) {
onUpdateClosure(ignoredUserIds)
}
}
@@ -18,7 +18,7 @@ import Combine
import Foundation
import MatrixRustSDK
enum ClientProxyCallback {
enum ClientProxyAction {
case receivedSyncUpdate
case receivedAuthError(isSoftLogout: Bool)
@@ -50,6 +50,8 @@ enum ClientProxyError: Error {
case failedGettingUserProfile
case failedSettingUserAvatar
case failedCheckingIsLastDevice(Error?)
case failedIgnoringUser
case failedUnignoringUser
}
enum SlidingSyncConstants {
@@ -70,7 +72,7 @@ struct PusherConfiguration {
}
protocol ClientProxyProtocol: AnyObject, MediaLoaderProtocol {
var callbacks: PassthroughSubject<ClientProxyCallback, Never> { get }
var actionsPublisher: AnyPublisher<ClientProxyAction, Never> { get }
var loadingStatePublisher: CurrentValuePublisher<ClientProxyLoadingState, Never> { get }
@@ -80,9 +82,12 @@ protocol ClientProxyProtocol: AnyObject, MediaLoaderProtocol {
var homeserver: String { get }
var userDisplayName: CurrentValuePublisher<String?, Never> { get }
var userDisplayNamePublisher: CurrentValuePublisher<String?, Never> { get }
var userAvatarURL: CurrentValuePublisher<URL?, Never> { get }
var userAvatarURLPublisher: CurrentValuePublisher<URL?, Never> { get }
/// We delay fetching this until after the first sync. Nil until then
var ignoredUsersPublisher: CurrentValuePublisher<[String]?, Never> { get }
var pusherNotificationClientIdentifier: String? { get }
@@ -134,4 +139,10 @@ protocol ClientProxyProtocol: AnyObject, MediaLoaderProtocol {
func searchUsers(searchTerm: String, limit: UInt) async -> Result<SearchUsersResultsProxy, ClientProxyError>
func profile(for userID: String) async -> Result<UserProfileProxy, ClientProxyError>
// MARK: - Ignored users
func ignoreUser(_ userID: String) async -> Result<Void, ClientProxyError>
func unignoreUser(_ userID: String) async -> Result<Void, ClientProxyError>
}
@@ -19,7 +19,10 @@ import Foundation
import MatrixRustSDK
class MockClientProxy: ClientProxyProtocol {
let callbacks = PassthroughSubject<ClientProxyCallback, Never>()
let actionsSubject = PassthroughSubject<ClientProxyAction, Never>()
var actionsPublisher: AnyPublisher<ClientProxyAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
let loadingStatePublisher = CurrentValuePublisher<ClientProxyLoadingState, Never>(.notLoading)
@@ -34,9 +37,14 @@ class MockClientProxy: ClientProxyProtocol {
var inviteSummaryProvider: RoomSummaryProviderProtocol? = MockRoomSummaryProvider()
var userAvatarURL: CurrentValuePublisher<URL?, Never> { CurrentValueSubject<URL?, Never>(nil).asCurrentValuePublisher() }
var userAvatarURLPublisher: CurrentValuePublisher<URL?, Never> { CurrentValueSubject<URL?, Never>(nil).asCurrentValuePublisher() }
var userDisplayName: CurrentValuePublisher<String?, Never> { CurrentValueSubject<String?, Never>("User display name").asCurrentValuePublisher() }
var userDisplayNamePublisher: CurrentValuePublisher<String?, Never> { CurrentValueSubject<String?, Never>("User display name").asCurrentValuePublisher() }
var ignoredUsersPublisher: CurrentValuePublisher<[String]?, Never> {
let ignoredUsers = [RoomMemberProxyMock].allMembers.map(\.userID)
return CurrentValueSubject<[String]?, Never>(ignoredUsers).asCurrentValuePublisher()
}
var notificationSettings: NotificationSettingsProxyProtocol = NotificationSettingsProxyMock(with: .init())
@@ -166,4 +174,12 @@ class MockClientProxy: ClientProxyProtocol {
getProfileCalled = true
return getProfileResult
}
func ignoreUser(_ userID: String) async -> Result<Void, ClientProxyError> {
.failure(.failedIgnoringUser)
}
func unignoreUser(_ userID: String) async -> Result<Void, ClientProxyError> {
.failure(.failedUnignoringUser)
}
}