mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-05 11:58:32 +00:00
Improve how alias settings are handled, add unit tests (#3686)
This commit is contained in:
@@ -22,11 +22,11 @@ struct EditRoomAddressScreenViewState: BindableState {
|
||||
!bindings.desiredAliasLocalPart.isEmpty
|
||||
}
|
||||
|
||||
var bindings: EditRoomAddressScreenViewStateBindings
|
||||
var bindings = EditRoomAddressScreenViewStateBindings()
|
||||
}
|
||||
|
||||
struct EditRoomAddressScreenViewStateBindings {
|
||||
var desiredAliasLocalPart: String
|
||||
var desiredAliasLocalPart = ""
|
||||
}
|
||||
|
||||
enum EditRoomAddressScreenViewAction {
|
||||
|
||||
+27
-11
@@ -31,23 +31,33 @@ class EditRoomAddressScreenViewModel: EditRoomAddressScreenViewModelType, EditRo
|
||||
self.clientProxy = clientProxy
|
||||
self.userIndicatorController = userIndicatorController
|
||||
|
||||
var aliasLocalPart = ""
|
||||
if let canonicalAlias = roomProxy.infoPublisher.value.canonicalAlias {
|
||||
aliasLocalPart = canonicalAlias.dropFirst().split(separator: ":").first.flatMap(String.init) ?? ""
|
||||
} else if let displayName = roomProxy.infoPublisher.value.displayName {
|
||||
aliasLocalPart = roomAliasNameFromRoomDisplayName(roomName: displayName)
|
||||
}
|
||||
|
||||
if let initialViewState {
|
||||
super.init(initialViewState: initialViewState)
|
||||
} else {
|
||||
super.init(initialViewState: EditRoomAddressScreenViewState(serverName: clientProxy.userIDServerName ?? "",
|
||||
currentAliasLocalPart: aliasLocalPart,
|
||||
bindings: .init(desiredAliasLocalPart: aliasLocalPart)))
|
||||
super.init(initialViewState: EditRoomAddressScreenViewState(serverName: clientProxy.userIDServerName ?? ""))
|
||||
|
||||
state.currentAliasLocalPart = localPartForMatchingAlias(computeFromDisplayName: false)
|
||||
state.bindings.desiredAliasLocalPart = localPartForMatchingAlias(computeFromDisplayName: true) ?? ""
|
||||
}
|
||||
|
||||
setupSubscriptions()
|
||||
}
|
||||
|
||||
/// Give priority to aliases from the current user's homeserver as remote ones
|
||||
/// cannot be edited. If none match then don't fallback and show an empty alias
|
||||
/// instead so that the user can add one sepecific to this homeserver.
|
||||
private func localPartForMatchingAlias(computeFromDisplayName: Bool) -> String? {
|
||||
if let matchingAlias = roomProxy.infoPublisher.value.firstAliasMatching(serverName: clientProxy.userIDServerName, useFallback: false) {
|
||||
return matchingAlias.aliasLocalPart
|
||||
}
|
||||
|
||||
guard computeFromDisplayName, let displayName = roomProxy.infoPublisher.value.displayName else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return roomAliasNameFromRoomDisplayName(roomName: displayName)
|
||||
}
|
||||
|
||||
// MARK: - Public
|
||||
|
||||
override func process(viewAction: EditRoomAddressScreenViewAction) {
|
||||
@@ -136,7 +146,7 @@ class EditRoomAddressScreenViewModel: EditRoomAddressScreenViewModelType, EditRo
|
||||
return
|
||||
}
|
||||
|
||||
let oldAlias = roomProxy.infoPublisher.value.canonicalAlias
|
||||
let oldAlias = roomProxy.infoPublisher.value.firstAliasMatching(serverName: clientProxy.userIDServerName, useFallback: false)
|
||||
|
||||
// First publish the new alias
|
||||
if case .failure = await roomProxy.publishRoomAliasInRoomDirectory(canonicalAlias) {
|
||||
@@ -172,3 +182,9 @@ class EditRoomAddressScreenViewModel: EditRoomAddressScreenViewModelType, EditRo
|
||||
userIndicatorController.retractIndicatorWithId(Self.loadingIndicatorIdentifier)
|
||||
}
|
||||
}
|
||||
|
||||
private extension String {
|
||||
var aliasLocalPart: String {
|
||||
dropFirst().split(separator: ":").first.flatMap(String.init) ?? ""
|
||||
}
|
||||
}
|
||||
|
||||
+11
-1
@@ -79,8 +79,18 @@ class SecurityAndPrivacyScreenViewModel: SecurityAndPrivacyScreenViewModelType,
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
let userIDServerName = clientProxy.userIDServerName
|
||||
|
||||
roomProxy.infoPublisher
|
||||
.map(\.canonicalAlias)
|
||||
.compactMap { roomInfo in
|
||||
guard let userIDServerName else {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Give priority to aliases from the current user's homeserver as remote ones
|
||||
// cannot be edited.
|
||||
return roomInfo.firstAliasMatching(serverName: userIDServerName, useFallback: true)
|
||||
}
|
||||
.removeDuplicates()
|
||||
.receive(on: DispatchQueue.main)
|
||||
.weakAssign(to: \.state.canonicalAlias, on: self)
|
||||
|
||||
Reference in New Issue
Block a user