mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 21:33:26 +00:00
Security and privacy part 2 (#3637)
* handling the history visibility flag * better logic to handle visibility * better handling of the visibility options state * added some copies, and the public room directory visibility state * completed the UI added also the preview tests * improved the handling of the directory visibility * added the space users case and improved handling of the access -> vsibility reaction. Also added a simple error handling for the public directory toggle * added the edit room address view but is missing its full implementation * implement the UI for the edit room address screen * implemented error checking when editing the address * updated preview tests and improved code * typo fix * Fix various issues after rebasing. * Fix build errors and broken snapshot tests * Adopt latest room privacy and canonical alias setting APIs * Add support for creating and editing the room's alias. * Add support for saving room privacy setting changes. * Fix room alias screen snapshot tests following recent changes. --------- Co-authored-by: Stefan Ceriu <stefanc@matrix.org>
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
//
|
||||
// Copyright 2022-2024 New Vector Ltd.
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Please see LICENSE in the repository root for full details.
|
||||
//
|
||||
|
||||
import Combine
|
||||
import MatrixRustSDK
|
||||
import SwiftUI
|
||||
|
||||
typealias EditRoomAddressScreenViewModelType = StateStoreViewModel<EditRoomAddressScreenViewState, EditRoomAddressScreenViewAction>
|
||||
|
||||
class EditRoomAddressScreenViewModel: EditRoomAddressScreenViewModelType, EditRoomAddressScreenViewModelProtocol {
|
||||
let roomProxy: JoinedRoomProxyProtocol
|
||||
let clientProxy: ClientProxyProtocol
|
||||
let userIndicatorController: UserIndicatorControllerProtocol
|
||||
|
||||
private let actionsSubject: PassthroughSubject<EditRoomAddressScreenViewModelAction, Never> = .init()
|
||||
var actionsPublisher: AnyPublisher<EditRoomAddressScreenViewModelAction, Never> {
|
||||
actionsSubject.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
@CancellableTask private var checkAliasAvailabilityTask: Task<Void, Never>?
|
||||
|
||||
init(initialViewState: EditRoomAddressScreenViewState? = nil,
|
||||
roomProxy: JoinedRoomProxyProtocol,
|
||||
clientProxy: ClientProxyProtocol,
|
||||
userIndicatorController: UserIndicatorControllerProtocol) {
|
||||
self.roomProxy = roomProxy
|
||||
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)))
|
||||
}
|
||||
setupSubscriptions()
|
||||
}
|
||||
|
||||
// MARK: - Public
|
||||
|
||||
override func process(viewAction: EditRoomAddressScreenViewAction) {
|
||||
MXLog.info("View model: received view action: \(viewAction)")
|
||||
|
||||
switch viewAction {
|
||||
case .save:
|
||||
Task { await save() }
|
||||
case .cancel:
|
||||
actionsSubject.send(.dismiss)
|
||||
}
|
||||
}
|
||||
|
||||
private func setupSubscriptions() {
|
||||
context.$viewState
|
||||
.map(\.bindings.desiredAliasLocalPart)
|
||||
.removeDuplicates()
|
||||
.debounce(for: 0.5, scheduler: DispatchQueue.main)
|
||||
.sink { [weak self] aliasLocalPart in
|
||||
guard let self else {
|
||||
return
|
||||
}
|
||||
|
||||
guard let canonicalAlias = String.makeCanonicalAlias(aliasLocalPart: aliasLocalPart,
|
||||
serverName: state.serverName) else {
|
||||
// While is empty don't display the errors, since the save button is already disabled
|
||||
state.aliasErrors.removeAll()
|
||||
return
|
||||
}
|
||||
|
||||
if !isRoomAliasFormatValid(alias: canonicalAlias) {
|
||||
state.aliasErrors.insert(.invalidSymbols)
|
||||
// If the alias is invalid we don't need to check for availability
|
||||
state.aliasErrors.remove(.alreadyExists)
|
||||
checkAliasAvailabilityTask = nil
|
||||
return
|
||||
}
|
||||
|
||||
state.aliasErrors.remove(.invalidSymbols)
|
||||
|
||||
guard aliasLocalPart != state.currentAliasLocalPart else {
|
||||
// Doesn't make sense to check the availability and display an error if the alias didn't change, the save button should also be disabled
|
||||
state.aliasErrors.remove(.alreadyExists)
|
||||
checkAliasAvailabilityTask = nil
|
||||
return
|
||||
}
|
||||
|
||||
checkAliasAvailabilityTask = Task { [weak self] in
|
||||
guard let self else {
|
||||
return
|
||||
}
|
||||
|
||||
if case .success(false) = await self.clientProxy.isAliasAvailable(canonicalAlias) {
|
||||
guard !Task.isCancelled else { return }
|
||||
state.aliasErrors.insert(.alreadyExists)
|
||||
} else {
|
||||
guard !Task.isCancelled else { return }
|
||||
state.aliasErrors.remove(.alreadyExists)
|
||||
}
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
private func save() async {
|
||||
showLoadingIndicator()
|
||||
|
||||
defer {
|
||||
hideLoadingIndicator()
|
||||
}
|
||||
|
||||
guard let canonicalAlias = String.makeCanonicalAlias(aliasLocalPart: state.bindings.desiredAliasLocalPart, serverName: state.serverName),
|
||||
isRoomAliasFormatValid(alias: canonicalAlias) else {
|
||||
state.aliasErrors = [.invalidSymbols]
|
||||
return
|
||||
}
|
||||
|
||||
switch await clientProxy.isAliasAvailable(canonicalAlias) {
|
||||
case .success(true):
|
||||
break
|
||||
case .success(false):
|
||||
state.aliasErrors = [.alreadyExists]
|
||||
return
|
||||
case .failure:
|
||||
userIndicatorController.submitIndicator(.init(title: L10n.errorUnknown))
|
||||
return
|
||||
}
|
||||
|
||||
let oldAlias = roomProxy.infoPublisher.value.canonicalAlias
|
||||
|
||||
// First publish the new alias
|
||||
if case .failure = await roomProxy.publishRoomAliasInRoomDirectory(canonicalAlias) {
|
||||
userIndicatorController.submitIndicator(.init(title: L10n.errorUnknown))
|
||||
return
|
||||
}
|
||||
|
||||
// Then set it as the main alias
|
||||
if case .failure = await roomProxy.updateCanonicalAlias(canonicalAlias, altAliases: []) {
|
||||
userIndicatorController.submitIndicator(.init(title: L10n.errorUnknown))
|
||||
return
|
||||
}
|
||||
|
||||
// And finally delete the old one
|
||||
if let oldAlias, case .failure = await roomProxy.removeRoomAliasFromRoomDirectory(oldAlias) {
|
||||
userIndicatorController.submitIndicator(.init(title: L10n.errorUnknown))
|
||||
return
|
||||
}
|
||||
|
||||
actionsSubject.send(.dismiss)
|
||||
}
|
||||
|
||||
private static let loadingIndicatorIdentifier = "\(EditRoomAddressScreenViewModel.self)-Loading"
|
||||
|
||||
private func showLoadingIndicator() {
|
||||
userIndicatorController.submitIndicator(UserIndicator(id: Self.loadingIndicatorIdentifier,
|
||||
type: .modal,
|
||||
title: L10n.commonLoading,
|
||||
persistent: true))
|
||||
}
|
||||
|
||||
private func hideLoadingIndicator() {
|
||||
userIndicatorController.retractIndicatorWithId(Self.loadingIndicatorIdentifier)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user