mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 06:55:59 +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,55 @@
|
||||
//
|
||||
// Copyright 2022-2024 New Vector Ltd.
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Please see LICENSE in the repository root for full details.
|
||||
//
|
||||
|
||||
// periphery:ignore:all - this is just a editRoomAddress remove this comment once generating the final file
|
||||
|
||||
import Combine
|
||||
import SwiftUI
|
||||
|
||||
struct EditRoomAddressScreenCoordinatorParameters {
|
||||
let roomProxy: JoinedRoomProxyProtocol
|
||||
let clientProxy: ClientProxyProtocol
|
||||
let userIndicatorController: UserIndicatorControllerProtocol
|
||||
}
|
||||
|
||||
enum EditRoomAddressScreenCoordinatorAction {
|
||||
case dismiss
|
||||
}
|
||||
|
||||
final class EditRoomAddressScreenCoordinator: CoordinatorProtocol {
|
||||
private let viewModel: EditRoomAddressScreenViewModelProtocol
|
||||
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
|
||||
private let actionsSubject: PassthroughSubject<EditRoomAddressScreenCoordinatorAction, Never> = .init()
|
||||
var actionsPublisher: AnyPublisher<EditRoomAddressScreenCoordinatorAction, Never> {
|
||||
actionsSubject.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
init(parameters: EditRoomAddressScreenCoordinatorParameters) {
|
||||
viewModel = EditRoomAddressScreenViewModel(roomProxy: parameters.roomProxy,
|
||||
clientProxy: parameters.clientProxy,
|
||||
userIndicatorController: parameters.userIndicatorController)
|
||||
}
|
||||
|
||||
func start() {
|
||||
viewModel.actionsPublisher.sink { [weak self] action in
|
||||
MXLog.info("Coordinator: received view model action: \(action)")
|
||||
|
||||
guard let self else { return }
|
||||
switch action {
|
||||
case .dismiss:
|
||||
actionsSubject.send(.dismiss)
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
func toPresentable() -> AnyView {
|
||||
AnyView(EditRoomAddressScreen(context: viewModel.context))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// Copyright 2022-2024 New Vector Ltd.
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Please see LICENSE in the repository root for full details.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
enum EditRoomAddressScreenViewModelAction {
|
||||
case dismiss
|
||||
}
|
||||
|
||||
struct EditRoomAddressScreenViewState: BindableState {
|
||||
let serverName: String
|
||||
var currentAliasLocalPart: String?
|
||||
var aliasErrors: Set<EditRoomAddressErrorState> = []
|
||||
|
||||
var canSave: Bool {
|
||||
currentAliasLocalPart != bindings.desiredAliasLocalPart &&
|
||||
aliasErrors.isEmpty &&
|
||||
!bindings.desiredAliasLocalPart.isEmpty
|
||||
}
|
||||
|
||||
var bindings: EditRoomAddressScreenViewStateBindings
|
||||
}
|
||||
|
||||
struct EditRoomAddressScreenViewStateBindings {
|
||||
var desiredAliasLocalPart: String
|
||||
}
|
||||
|
||||
enum EditRoomAddressScreenViewAction {
|
||||
case save
|
||||
case cancel
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
//
|
||||
// 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
|
||||
|
||||
@MainActor
|
||||
protocol EditRoomAddressScreenViewModelProtocol {
|
||||
var actionsPublisher: AnyPublisher<EditRoomAddressScreenViewModelAction, Never> { get }
|
||||
var context: EditRoomAddressScreenViewModelType.Context { get }
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
//
|
||||
// Copyright 2022-2024 New Vector Ltd.
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Please see LICENSE in the repository root for full details.
|
||||
//
|
||||
|
||||
import Compound
|
||||
import SwiftUI
|
||||
|
||||
struct EditRoomAddressScreen: View {
|
||||
@ObservedObject var context: EditRoomAddressScreenViewModel.Context
|
||||
|
||||
var body: some View {
|
||||
Form {
|
||||
Section {
|
||||
EditRoomAddressListRow(aliasLocalPart: $context.desiredAliasLocalPart,
|
||||
serverName: context.viewState.serverName, shouldDisplayError: context.viewState.aliasErrors.errorDescription != nil)
|
||||
.onChange(of: context.desiredAliasLocalPart) { _, newAliasLocalPart in
|
||||
context.desiredAliasLocalPart = newAliasLocalPart.lowercased()
|
||||
}
|
||||
.onSubmit {
|
||||
if context.viewState.canSave {
|
||||
context.send(viewAction: .save)
|
||||
}
|
||||
}
|
||||
} footer: {
|
||||
VStack(alignment: .leading, spacing: 12) {
|
||||
if let errorDescription = context.viewState.aliasErrors.errorDescription {
|
||||
Label(errorDescription, icon: \.error, iconSize: .xSmall, relativeTo: .compound.bodySM)
|
||||
.foregroundStyle(.compound.textCriticalPrimary)
|
||||
.font(.compound.bodySM)
|
||||
}
|
||||
Text(L10n.screenCreateRoomRoomAddressSectionFooter)
|
||||
.compoundListSectionFooter()
|
||||
.font(.compound.bodySM)
|
||||
}
|
||||
}
|
||||
}
|
||||
.compoundList()
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.navigationTitle(L10n.screenEditRoomAddressTitle)
|
||||
.toolbar { toolbar }
|
||||
}
|
||||
|
||||
@ToolbarContentBuilder
|
||||
var toolbar: some ToolbarContent {
|
||||
ToolbarItem(placement: .confirmationAction) {
|
||||
Button(L10n.actionSave) {
|
||||
context.send(viewAction: .save)
|
||||
}
|
||||
.disabled(!context.viewState.canSave)
|
||||
}
|
||||
ToolbarItem(placement: .cancellationAction) {
|
||||
Button(L10n.actionCancel) {
|
||||
context.send(viewAction: .cancel)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Previews
|
||||
|
||||
struct EditRoomAddressScreen_Previews: PreviewProvider, TestablePreview {
|
||||
static let noAliasviewModel = EditRoomAddressScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(name: "Room Name")),
|
||||
clientProxy: ClientProxyMock(.init(userIDServerName: "matrix.org")),
|
||||
userIndicatorController: UserIndicatorControllerMock())
|
||||
|
||||
static let aliasviewModel = EditRoomAddressScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(name: "Room Name", canonicalAlias: "#room-alias:matrix.org")),
|
||||
clientProxy: ClientProxyMock(.init(userIDServerName: "matrix.org")),
|
||||
userIndicatorController: UserIndicatorControllerMock())
|
||||
|
||||
static let invalidSymbolsViewModel = EditRoomAddressScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(name: "Room Name", canonicalAlias: "#room#-alias:matrix.org")),
|
||||
clientProxy: ClientProxyMock(.init(userIDServerName: "matrix.org")),
|
||||
userIndicatorController: UserIndicatorControllerMock())
|
||||
|
||||
static let alreadyExistingViewModel = {
|
||||
let clientProxy = ClientProxyMock(.init(userIDServerName: "matrix.org"))
|
||||
clientProxy.isAliasAvailableReturnValue = .success(false)
|
||||
return EditRoomAddressScreenViewModel(initialViewState: .init(serverName: "matrix.org",
|
||||
bindings: .init(desiredAliasLocalPart: "whatever")),
|
||||
roomProxy: JoinedRoomProxyMock(.init(name: "Room Name")),
|
||||
clientProxy: clientProxy,
|
||||
userIndicatorController: UserIndicatorControllerMock())
|
||||
}()
|
||||
|
||||
static var previews: some View {
|
||||
NavigationStack {
|
||||
EditRoomAddressScreen(context: noAliasviewModel.context)
|
||||
}
|
||||
.previewDisplayName("No alias")
|
||||
|
||||
NavigationStack {
|
||||
EditRoomAddressScreen(context: aliasviewModel.context)
|
||||
}
|
||||
.previewDisplayName("With alias")
|
||||
|
||||
NavigationStack {
|
||||
EditRoomAddressScreen(context: invalidSymbolsViewModel.context)
|
||||
}
|
||||
.snapshotPreferences(expect: invalidSymbolsViewModel.context.$viewState.map { state in
|
||||
!state.aliasErrors.isEmpty
|
||||
})
|
||||
.previewDisplayName("Invalid symbols")
|
||||
|
||||
NavigationStack {
|
||||
EditRoomAddressScreen(context: alreadyExistingViewModel.context)
|
||||
}
|
||||
.snapshotPreferences(expect: alreadyExistingViewModel.context.$viewState.map { state in
|
||||
!state.aliasErrors.isEmpty
|
||||
})
|
||||
.previewDisplayName("Already existing")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user