From 6e251b7b9350b5bc1353cf236bb2378adc3e9bfa Mon Sep 17 00:00:00 2001 From: Nicolas Mauri Date: Tue, 9 May 2023 09:53:42 +0200 Subject: [PATCH 1/2] Feat: add a flag in the build settings to force the user to define a homeserver. --- Config/BuildSettings.swift | 9 +++++++-- .../AuthenticationCoordinator.swift | 14 ++++++++++++-- .../Legacy/AuthenticationViewController.m | 18 ++++++++++++++++-- .../Common/AuthenticationModels.swift | 4 ++++ ...henticationServerSelectionCoordinator.swift | 8 +++++++- changelog.d/pr-7541.change | 1 + 6 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 changelog.d/pr-7541.change diff --git a/Config/BuildSettings.swift b/Config/BuildSettings.swift index e8c129619d..1b25717083 100644 --- a/Config/BuildSettings.swift +++ b/Config/BuildSettings.swift @@ -98,10 +98,15 @@ final class BuildSettings: NSObject { // MARK: - Server configuration - // Default servers proposed on the authentication screen + // Force the user to set a homeserver instead of using the default one + static let forceHomeserverSelection = false + + // Default server proposed on the authentication screen static let serverConfigDefaultHomeserverUrlString = "https://matrix.org" - static let serverConfigDefaultIdentityServerUrlString = "https://vector.im" + // Default identity server + static let serverConfigDefaultIdentityServerUrlString = "https://vector.im" + static let serverConfigSygnalAPIUrlString = "https://matrix.org/_matrix/push/v1/notify" diff --git a/Riot/Modules/Authentication/AuthenticationCoordinator.swift b/Riot/Modules/Authentication/AuthenticationCoordinator.swift index a245147cdc..6d5250497f 100644 --- a/Riot/Modules/Authentication/AuthenticationCoordinator.swift +++ b/Riot/Modules/Authentication/AuthenticationCoordinator.swift @@ -130,9 +130,19 @@ final class AuthenticationCoordinator: NSObject, AuthenticationCoordinatorProtoc } let flow: AuthenticationFlow = initialScreen == .login ? .login : .register + + // Use the homeserver defined by a provisioningLink or by the user (if none is set, the default one will be used) + let homeserverAddress = authenticationService.provisioningLink?.homeserverUrl ?? authenticationService.state.homeserver.addressFromUser + + // Check if the user must select a server + if BuildSettings.forceHomeserverSelection, homeserverAddress == nil { + showServerSelectionScreen(for: flow) + return + } + do { - // Start the flow using the default server (or a provisioning link if set). - try await authenticationService.startFlow(flow) + // Start the flow (if homeserverAddress is nil, the default server will be used). + try await authenticationService.startFlow(flow, for: homeserverAddress) } catch { MXLog.error("[AuthenticationCoordinator] start: Failed to start, showing server selection.") showServerSelectionScreen(for: flow) diff --git a/Riot/Modules/Authentication/Legacy/AuthenticationViewController.m b/Riot/Modules/Authentication/Legacy/AuthenticationViewController.m index 3f5cfaf679..c0605d8131 100644 --- a/Riot/Modules/Authentication/Legacy/AuthenticationViewController.m +++ b/Riot/Modules/Authentication/Legacy/AuthenticationViewController.m @@ -132,7 +132,14 @@ - (void)viewDidLoad target:self action:@selector(onButtonPressed:)]; - self.defaultHomeServerUrl = RiotSettings.shared.homeserverUrlString; + if (BuildSettings.forceHomeserverSelection) + { + self.defaultHomeServerUrl = nil; + } + else + { + self.defaultHomeServerUrl = RiotSettings.shared.homeserverUrlString; + } self.defaultIdentityServerUrl = RiotSettings.shared.identityServerUrlString; @@ -1207,7 +1214,14 @@ - (void)setCustomServerFieldsVisible:(BOOL)isVisible [self saveCustomServerInputs]; // Restore default configuration - [self setHomeServerTextFieldText:self.defaultHomeServerUrl]; + if (BuildSettings.forceHomeserverSelection) + { + [self setHomeServerTextFieldText:nil]; + } + else + { + [self setHomeServerTextFieldText:self.defaultHomeServerUrl]; + } [self setIdentityServerTextFieldText:self.defaultIdentityServerUrl]; [self.customServersTickButton setImage:AssetImages.selectionUntick.image forState:UIControlStateNormal]; diff --git a/RiotSwiftUI/Modules/Authentication/Common/AuthenticationModels.swift b/RiotSwiftUI/Modules/Authentication/Common/AuthenticationModels.swift index e59aa01893..34d7adb905 100644 --- a/RiotSwiftUI/Modules/Authentication/Common/AuthenticationModels.swift +++ b/RiotSwiftUI/Modules/Authentication/Common/AuthenticationModels.swift @@ -86,6 +86,10 @@ class HomeserverAddress: NSObject { /// - Ensure the address contains a scheme, otherwise make it `https`. /// - Remove any trailing slashes. static func sanitized(_ address: String) -> String { + guard !address.isEmpty else { + // prevent prefixing an empty string with "https:" + return address + } var address = address.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() if !address.contains("://") { diff --git a/RiotSwiftUI/Modules/Authentication/ServerSelection/Coordinator/AuthenticationServerSelectionCoordinator.swift b/RiotSwiftUI/Modules/Authentication/ServerSelection/Coordinator/AuthenticationServerSelectionCoordinator.swift index c5d521701c..13308c2622 100644 --- a/RiotSwiftUI/Modules/Authentication/ServerSelection/Coordinator/AuthenticationServerSelectionCoordinator.swift +++ b/RiotSwiftUI/Modules/Authentication/ServerSelection/Coordinator/AuthenticationServerSelectionCoordinator.swift @@ -57,7 +57,13 @@ final class AuthenticationServerSelectionCoordinator: Coordinator, Presentable { self.parameters = parameters let homeserver = parameters.authenticationService.state.homeserver - let viewModel = AuthenticationServerSelectionViewModel(homeserverAddress: homeserver.displayableAddress, + let homeserverAddress: String + if BuildSettings.forceHomeserverSelection, homeserver.addressFromUser == nil { + homeserverAddress = "" + } else { + homeserverAddress = homeserver.displayableAddress + } + let viewModel = AuthenticationServerSelectionViewModel(homeserverAddress: homeserverAddress, flow: parameters.authenticationService.state.flow, hasModalPresentation: parameters.hasModalPresentation) let view = AuthenticationServerSelectionScreen(viewModel: viewModel.context) diff --git a/changelog.d/pr-7541.change b/changelog.d/pr-7541.change new file mode 100644 index 0000000000..0e0c71fa67 --- /dev/null +++ b/changelog.d/pr-7541.change @@ -0,0 +1 @@ +Add a flag in the build settings to force the user to define a homeserver instead of using the default one. From eae51b398b7fbf9ab4fdcfe8e332467c3787c5f0 Mon Sep 17 00:00:00 2001 From: Nicolas Mauri Date: Thu, 11 May 2023 09:45:04 +0200 Subject: [PATCH 2/2] Fix: apply the changes requested in the PR review --- Config/BuildSettings.swift | 6 +++--- .../Modules/Authentication/AuthenticationCoordinator.swift | 7 ++----- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Config/BuildSettings.swift b/Config/BuildSettings.swift index 1b25717083..f58f969c10 100644 --- a/Config/BuildSettings.swift +++ b/Config/BuildSettings.swift @@ -98,13 +98,13 @@ final class BuildSettings: NSObject { // MARK: - Server configuration - // Force the user to set a homeserver instead of using the default one + /// Force the user to set a homeserver instead of using the default one static let forceHomeserverSelection = false - // Default server proposed on the authentication screen + /// Default server proposed on the authentication screen static let serverConfigDefaultHomeserverUrlString = "https://matrix.org" - // Default identity server + /// Default identity server static let serverConfigDefaultIdentityServerUrlString = "https://vector.im" static let serverConfigSygnalAPIUrlString = "https://matrix.org/_matrix/push/v1/notify" diff --git a/Riot/Modules/Authentication/AuthenticationCoordinator.swift b/Riot/Modules/Authentication/AuthenticationCoordinator.swift index 6d5250497f..295a591f73 100644 --- a/Riot/Modules/Authentication/AuthenticationCoordinator.swift +++ b/Riot/Modules/Authentication/AuthenticationCoordinator.swift @@ -131,18 +131,15 @@ final class AuthenticationCoordinator: NSObject, AuthenticationCoordinatorProtoc let flow: AuthenticationFlow = initialScreen == .login ? .login : .register - // Use the homeserver defined by a provisioningLink or by the user (if none is set, the default one will be used) - let homeserverAddress = authenticationService.provisioningLink?.homeserverUrl ?? authenticationService.state.homeserver.addressFromUser - // Check if the user must select a server - if BuildSettings.forceHomeserverSelection, homeserverAddress == nil { + if BuildSettings.forceHomeserverSelection, authenticationService.provisioningLink?.homeserverUrl == nil { showServerSelectionScreen(for: flow) return } do { // Start the flow (if homeserverAddress is nil, the default server will be used). - try await authenticationService.startFlow(flow, for: homeserverAddress) + try await authenticationService.startFlow(flow) } catch { MXLog.error("[AuthenticationCoordinator] start: Failed to start, showing server selection.") showServerSelectionScreen(for: flow)