Handle waiting list error. (#1165)

This commit is contained in:
Doug
2023-06-26 18:26:20 +01:00
committed by GitHub
parent bc7faec44a
commit 2fce96d540
25 changed files with 589 additions and 19 deletions
@@ -14,3 +14,9 @@
"soft_logout_clear_data_submit" = "Clear all data";
"soft_logout_clear_data_dialog_title" = "Clear data";
"soft_logout_clear_data_dialog_content" = "Clear all data currently stored on this device?\nSign in again to access your account data and messages.";
"common_refreshing" = "Refreshing…";
"screen_waitlist_title" = "You're on the waitlist!";
"screen_waitlist_title_success" = "You're in!";
"screen_waitlist_message" = "There's a high demand for %1$@ on %2$@ at the moment. Come back to the app in a few days and try again.\n\nThanks for your patience!";
"screen_waitlist_message_success" = "Welcome to %1$@";
@@ -291,10 +291,10 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
displayName = name
}
let credentials = SoftLogoutScreenCredentials(userId: userSession.userID,
let credentials = SoftLogoutScreenCredentials(userID: userSession.userID,
homeserverName: userSession.homeserver,
userDisplayName: displayName,
deviceId: userSession.deviceID)
deviceID: userSession.deviceID)
let authenticationService = AuthenticationServiceProxy(userSessionStore: userSessionStore)
_ = await authenticationService.configure(for: userSession.homeserver)
@@ -10,6 +10,22 @@ import Foundation
// swiftlint:disable explicit_type_interface function_parameter_count identifier_name line_length
// swiftlint:disable nesting type_body_length type_name vertical_whitespace_opening_braces
public enum UntranslatedL10n {
/// Refreshing…
public static var commonRefreshing: String { return UntranslatedL10n.tr("Untranslated", "common_refreshing") }
/// There's a high demand for %1$@ on %2$@ at the moment. Come back to the app in a few days and try again.
///
/// Thanks for your patience!
public static func screenWaitlistMessage(_ p1: Any, _ p2: Any) -> String {
return UntranslatedL10n.tr("Untranslated", "screen_waitlist_message", String(describing: p1), String(describing: p2))
}
/// Welcome to %1$@
public static func screenWaitlistMessageSuccess(_ p1: Any) -> String {
return UntranslatedL10n.tr("Untranslated", "screen_waitlist_message_success", String(describing: p1))
}
/// You're on the waitlist!
public static var screenWaitlistTitle: String { return UntranslatedL10n.tr("Untranslated", "screen_waitlist_title") }
/// You're in!
public static var screenWaitlistTitleSuccess: String { return UntranslatedL10n.tr("Untranslated", "screen_waitlist_title_success") }
/// Clear all data currently stored on this device?
/// Sign in again to access your account data and messages.
public static var softLogoutClearDataDialogContent: String { return UntranslatedL10n.tr("Untranslated", "soft_logout_clear_data_dialog_content") }
@@ -168,12 +168,33 @@ class AuthenticationCoordinator: CoordinatorProtocol {
case .configuredForOIDC:
// Pop back to the confirmation screen for OIDC login to continue.
navigationStackCoordinator.pop(animated: false)
case .isOnWaitlist(let credentials):
showWaitlistScreen(for: credentials)
}
}
navigationStackCoordinator.push(coordinator)
}
private func showWaitlistScreen(for credentials: WaitlistScreenCredentials) {
let parameters = WaitlistScreenCoordinatorParameters(credentials: credentials,
authenticationService: authenticationService)
let coordinator = WaitlistScreenCoordinator(parameters: parameters)
coordinator.actions.sink { [weak self] action in
guard let self else { return }
switch action {
case .signedIn(let userSession):
userHasSignedIn(userSession: userSession)
case .cancel:
navigationStackCoordinator.pop()
}
}
.store(in: &cancellables)
navigationStackCoordinator.push(coordinator)
}
private func userHasSignedIn(userSession: UserSessionProtocol) {
showAnalyticsPromptIfNeeded { [weak self] in
guard let self else { return }
@@ -27,6 +27,8 @@ enum LoginScreenCoordinatorAction {
case configuredForOIDC
/// Login was successful.
case signedIn(UserSessionProtocol)
/// The user's request to login failed due to being on the proxy waitlist.
case isOnWaitlist(WaitlistScreenCredentials)
}
final class LoginScreenCoordinator: CoordinatorProtocol {
@@ -117,13 +119,22 @@ final class LoginScreenCoordinator: CoordinatorProtocol {
switch await authenticationService.login(username: username,
password: password,
initialDeviceName: UIDevice.current.initialDeviceName,
deviceId: nil) {
deviceID: nil) {
case .success(let userSession):
callback?(.signedIn(userSession))
stopLoading()
case .failure(let error):
stopLoading()
handleError(error)
switch error {
case .isOnWaitlist:
callback?(.isOnWaitlist(.init(username: username,
password: password,
initialDeviceName: UIDevice.current.initialDeviceName,
deviceID: nil,
homeserver: authenticationService.homeserver.value)))
default:
handleError(error)
}
}
}
}
@@ -31,10 +31,10 @@ enum MockSoftLogoutScreenState: String, CaseIterable {
/// Generate the view struct for the screen state.
@MainActor var viewModel: SoftLogoutScreenViewModel {
let credentials = SoftLogoutScreenCredentials(userId: "@mock:matrix.org",
let credentials = SoftLogoutScreenCredentials(userID: "@mock:matrix.org",
homeserverName: "matrix.org",
userDisplayName: "mock",
deviceId: nil)
deviceID: nil)
switch self {
case .emptyPassword:
return SoftLogoutScreenViewModel(credentials: credentials,
@@ -108,7 +108,7 @@ final class SoftLogoutScreenCoordinator: CoordinatorProtocol {
/// Login with the supplied username and password.
@MainActor private func login(withPassword password: String) {
let username = parameters.credentials.userId
let username = parameters.credentials.userID
startLoading()
@@ -116,7 +116,7 @@ final class SoftLogoutScreenCoordinator: CoordinatorProtocol {
switch await authenticationService.login(username: username,
password: password,
initialDeviceName: UIDevice.current.initialDeviceName,
deviceId: parameters.credentials.deviceId) {
deviceID: parameters.credentials.deviceID) {
case .success(let userSession):
callback?(.signedIn(userSession))
stopLoading()
@@ -17,10 +17,10 @@
import SwiftUI
struct SoftLogoutScreenCredentials {
let userId: String
let userID: String
let homeserverName: String
let userDisplayName: String
let deviceId: String?
let deviceID: String?
}
enum SoftLogoutScreenViewModelAction: CustomStringConvertible {
@@ -64,7 +64,7 @@ struct SoftLogoutScreen: View {
.foregroundColor(.compound.textPrimary)
.accessibilityIdentifier(A11yIdentifiers.softLogoutScreen.title)
Text(UntranslatedL10n.softLogoutSigninNotice(context.viewState.credentials.homeserverName, context.viewState.credentials.userDisplayName, context.viewState.credentials.userId))
Text(UntranslatedL10n.softLogoutSigninNotice(context.viewState.credentials.homeserverName, context.viewState.credentials.userDisplayName, context.viewState.credentials.userID))
.font(.compound.bodyLG)
.multilineTextAlignment(.leading)
.foregroundColor(.compound.textPrimary)
@@ -0,0 +1,106 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
struct WaitlistScreen: View {
@ObservedObject var context: WaitlistScreenViewModel.Context
var body: some View {
FullscreenDialog(topPadding: UIConstants.iconTopPaddingToNavigationBar) {
header
} bottomContent: {
buttons
}
.background()
.environment(\.backgroundStyle, AnyShapeStyle(Color.compound.bgCanvasDefault))
.navigationBarBackButtonHidden()
.toolbar { toolbar }
.toolbar(.visible, for: .navigationBar) // Layout consistency in all states.
.overlay {
EffectsView(effect: context.viewState.isWaiting ? .none : .confetti)
.ignoresSafeArea()
.allowsHitTesting(false)
}
}
/// The main content of the view to be shown in a scroll view.
var header: some View {
VStack(spacing: 8) {
AuthenticationIconImage(image: Image(systemName: context.viewState.iconSymbolName))
.fontWeight(.semibold)
.padding(.bottom, 8)
Text(context.viewState.title)
.font(.compound.headingMDBold)
.multilineTextAlignment(.center)
.foregroundColor(.compound.textPrimary)
.fixedSize(horizontal: false, vertical: true)
Text(context.viewState.message)
.font(.compound.bodyMD)
.multilineTextAlignment(.center)
.foregroundColor(.compound.textSecondary)
}
.padding(.horizontal, 16)
}
/// The action buttons shown at the bottom of the view.
@ViewBuilder
var buttons: some View {
if let userSession = context.viewState.userSession {
Button { context.send(viewAction: .continue(userSession)) } label: {
Text(L10n.actionContinue)
}
.buttonStyle(.elementAction(.xLarge))
}
}
@ToolbarContentBuilder
var toolbar: some ToolbarContent {
if context.viewState.isWaiting {
ToolbarItem(placement: .cancellationAction) {
Button(L10n.actionCancel) {
context.send(viewAction: .cancel)
}
}
}
}
}
// MARK: - Previews
struct WaitlistScreen_Previews: PreviewProvider {
static let viewModel = WaitlistScreenViewModel(homeserver: .mockMatrixDotOrg)
static let successViewModel = {
let viewModel = WaitlistScreenViewModel(homeserver: .mockMatrixDotOrg)
viewModel.update(userSession: MockUserSession(clientProxy: MockClientProxy(userID: "@alice:matrix.org"),
mediaProvider: MockMediaProvider()))
return viewModel
}()
static var previews: some View {
NavigationStack {
WaitlistScreen(context: viewModel.context)
}
.previewDisplayName("Waiting")
NavigationStack {
WaitlistScreen(context: successViewModel.context)
}
.previewDisplayName("Success")
}
}
@@ -0,0 +1,128 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Combine
import SwiftUI
struct WaitlistScreenCoordinatorParameters {
/// The credentials for the login.
let credentials: WaitlistScreenCredentials
/// The service used to authenticate the user.
let authenticationService: AuthenticationServiceProxyProtocol
/// The service locator for the screen.
var userIndicatorController: UserIndicatorControllerProtocol = ServiceLocator.shared.userIndicatorController
}
enum WaitlistScreenCoordinatorAction {
/// Login was successful after a retry attempt.
case signedIn(UserSessionProtocol)
/// The user would like to try sign in another way.
case cancel
}
final class WaitlistScreenCoordinator: CoordinatorProtocol {
private let parameters: WaitlistScreenCoordinatorParameters
private var viewModel: WaitlistScreenViewModelProtocol
private let actionsSubject: PassthroughSubject<WaitlistScreenCoordinatorAction, Never> = .init()
private var cancellables: Set<AnyCancellable> = .init()
private var refreshCancellable: AnyCancellable?
var actions: AnyPublisher<WaitlistScreenCoordinatorAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(parameters: WaitlistScreenCoordinatorParameters) {
self.parameters = parameters
viewModel = WaitlistScreenViewModel(homeserver: parameters.credentials.homeserver)
refreshCancellable = NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)
.sink { [weak self] _ in
self?.refresh()
}
}
func start() {
viewModel.actions.sink { [weak self] action in
guard let self else { return }
switch action {
case .cancel:
actionsSubject.send(.cancel)
case .continue(let userSession):
actionsSubject.send(.signedIn(userSession))
}
}
.store(in: &cancellables)
}
func toPresentable() -> AnyView {
AnyView(WaitlistScreen(context: viewModel.context))
}
// MARK: - Private
/// Refresh the screen by retrying login to see if the waitlist has opened up.
private func refresh() {
guard parameters.credentials.homeserver == parameters.authenticationService.homeserver.value else {
MXLog.warning("Homeserver configuration changed.")
actionsSubject.send(.cancel)
return
}
showRefreshIndicator()
Task {
switch await parameters.authenticationService.login(username: parameters.credentials.username,
password: parameters.credentials.password,
initialDeviceName: parameters.credentials.initialDeviceName,
deviceID: parameters.credentials.deviceID) {
case .success(let userSession):
hideRefreshIndicator()
refreshCancellable = nil
viewModel.update(userSession: userSession)
case .failure(.isOnWaitlist):
hideRefreshIndicator() // Nothing to do, still waiting for availability.
case .failure(.invalidCredentials):
hideRefreshIndicator()
actionsSubject.send(.cancel)
case .failure:
hideRefreshIndicator()
showFailureIndicator()
}
}
}
private static let refreshIndicatorID = "WaitlistCoordinatorRefresh"
private static let failureIndicatorID = "WaitlistCoordinatorFailure"
private func showRefreshIndicator() {
parameters.userIndicatorController.submitIndicator(UserIndicator(id: Self.refreshIndicatorID,
type: .modal,
title: UntranslatedL10n.commonRefreshing,
persistent: true))
}
private func hideRefreshIndicator() {
parameters.userIndicatorController.retractIndicatorWithId(Self.refreshIndicatorID)
}
private func showFailureIndicator() {
parameters.userIndicatorController.submitIndicator(UserIndicator(id: Self.failureIndicatorID,
type: .toast,
title: L10n.errorUnknown,
iconName: "xmark"))
}
}
@@ -0,0 +1,74 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
enum WaitlistScreenViewModelAction {
case cancel
case `continue`(UserSessionProtocol)
}
/// The user's credentials used to retry login and refresh the waiting list.
struct WaitlistScreenCredentials: CustomStringConvertible, CustomDebugStringConvertible {
let username: String
let password: String
let initialDeviceName: String?
let deviceID: String?
let homeserver: LoginHomeserver
var description: String { "Redacted" }
var debugDescription: String { "Redacted" }
}
struct WaitlistScreenViewState: BindableState {
/// The homeserver the user is waiting for.
let homeserver: LoginHomeserver
/// When refresh was successful, the user session that was returned by the login.
var userSession: UserSessionProtocol?
/// Whether or not the user is still waiting in the queue.
var isWaiting: Bool { userSession == nil }
var iconSymbolName: String {
if isWaiting {
return "stopwatch"
} else {
return "sparkles"
}
}
var title: String {
if isWaiting {
return UntranslatedL10n.screenWaitlistTitle
} else {
return UntranslatedL10n.screenWaitlistTitleSuccess
}
}
var message: String {
if isWaiting {
return UntranslatedL10n.screenWaitlistMessage(InfoPlistReader.main.bundleDisplayName, homeserver.address)
} else {
return UntranslatedL10n.screenWaitlistMessageSuccess(InfoPlistReader.main.bundleDisplayName)
}
}
}
enum WaitlistScreenViewAction {
case cancel
case `continue`(UserSessionProtocol)
}
@@ -0,0 +1,47 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Combine
import SwiftUI
typealias WaitlistScreenViewModelType = StateStoreViewModel<WaitlistScreenViewState, WaitlistScreenViewAction>
class WaitlistScreenViewModel: WaitlistScreenViewModelType, WaitlistScreenViewModelProtocol {
private var actionsSubject: PassthroughSubject<WaitlistScreenViewModelAction, Never> = .init()
var actions: AnyPublisher<WaitlistScreenViewModelAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(homeserver: LoginHomeserver) {
super.init(initialViewState: WaitlistScreenViewState(homeserver: homeserver))
}
// MARK: - Public
override func process(viewAction: WaitlistScreenViewAction) {
switch viewAction {
case .cancel:
actionsSubject.send(.cancel)
case .continue(let userSession):
actionsSubject.send(.continue(userSession))
}
}
func update(userSession: UserSessionProtocol) {
state.userSession = userSession
}
}
@@ -0,0 +1,26 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Combine
@MainActor
protocol WaitlistScreenViewModelProtocol {
var actions: AnyPublisher<WaitlistScreenViewModelAction, Never> { get }
var context: WaitlistScreenViewModelType.Context { get }
/// Set a user session on the screen to transition to the success state.
func update(userSession: UserSessionProtocol)
}
@@ -102,13 +102,13 @@ class AuthenticationServiceProxy: AuthenticationServiceProxyProtocol {
// }
}
func login(username: String, password: String, initialDeviceName: String?, deviceId: String?) async -> Result<UserSessionProtocol, AuthenticationServiceError> {
func login(username: String, password: String, initialDeviceName: String?, deviceID: String?) async -> Result<UserSessionProtocol, AuthenticationServiceError> {
do {
let client = try await Task.dispatch(on: .global()) {
try self.authenticationService.login(username: username,
password: password,
initialDeviceName: initialDeviceName,
deviceId: deviceId)
deviceId: deviceID)
}
return await userSession(for: client)
@@ -116,6 +116,10 @@ class AuthenticationServiceProxy: AuthenticationServiceProxyProtocol {
MXLog.error("Failed logging in with error: \(error)")
guard let error = error as? AuthenticationError else { return .failure(.failedLoggingIn) }
if error.isElementWaitlist {
return .failure(.isOnWaitlist)
}
switch error.code {
case .forbidden:
return .failure(.invalidCredentials)
@@ -34,6 +34,8 @@ enum AuthenticationServiceError: Error {
case slidingSyncNotAvailable
case accountDeactivated
case failedLoggingIn
case isOnWaitlist
}
protocol AuthenticationServiceProxyProtocol {
@@ -47,7 +49,7 @@ protocol AuthenticationServiceProxyProtocol {
/// Add docs.
func loginWithOIDCCallback(_ callbackURL: URL, data: OIDCAuthenticationDataProxy) async -> Result<UserSessionProtocol, AuthenticationServiceError>
/// Performs a password login using the current homeserver.
func login(username: String, password: String, initialDeviceName: String?, deviceId: String?) async -> Result<UserSessionProtocol, AuthenticationServiceError>
func login(username: String, password: String, initialDeviceName: String?, deviceID: String?) async -> Result<UserSessionProtocol, AuthenticationServiceError>
}
// MARK: - OIDC
@@ -56,7 +56,7 @@ class MockAuthenticationServiceProxy: AuthenticationServiceProxyProtocol {
.failure(.oidcError(.notSupported))
}
func login(username: String, password: String, initialDeviceName: String?, deviceId: String?) async -> Result<UserSessionProtocol, AuthenticationServiceError> {
func login(username: String, password: String, initialDeviceName: String?, deviceID: String?) async -> Result<UserSessionProtocol, AuthenticationServiceError> {
// Login only succeeds if the username and password match the valid credentials property
guard username == validCredentials.username, password == validCredentials.password else {
return .failure(.invalidCredentials)
@@ -46,4 +46,12 @@ extension AuthenticationError {
return first
}
/// Whether or not the error is related to the sliding sync proxy being full.
///
/// This is a temporary error whilst we scale the backend infrastructure.
var isElementWaitlist: Bool {
guard case let .Generic(message) = self else { return false }
return message.contains("IO_ELEMENT_X_WAIT_LIST")
}
}
@@ -116,13 +116,22 @@ class MockScreen: Identifiable {
navigationStackCoordinator.setRootCoordinator(coordinator)
return navigationStackCoordinator
case .softLogout:
let credentials = SoftLogoutScreenCredentials(userId: "@mock:matrix.org",
let credentials = SoftLogoutScreenCredentials(userID: "@mock:matrix.org",
homeserverName: "matrix.org",
userDisplayName: "mock",
deviceId: "ABCDEFGH")
deviceID: "ABCDEFGH")
return SoftLogoutScreenCoordinator(parameters: .init(authenticationService: MockAuthenticationServiceProxy(),
credentials: credentials,
keyBackupNeeded: false))
case .waitlist:
let credentials = WaitlistScreenCredentials(username: "alice",
password: "password",
initialDeviceName: nil,
deviceID: nil,
homeserver: .mockMatrixDotOrg)
return WaitlistScreenCoordinator(parameters: .init(credentials: credentials,
authenticationService: MockAuthenticationServiceProxy(),
userIndicatorController: UserIndicatorControllerMock.default))
case .simpleRegular:
return TemplateScreenCoordinator(parameters: .init(promptType: .regular))
case .simpleUpgrade:
@@ -24,6 +24,7 @@ enum UITestsScreenIdentifier: String {
case serverSelectionNonModal
case authenticationFlow
case softLogout
case waitlist
case analyticsPrompt
case analyticsSettingsScreen
case simpleRegular