From 4d96dbbc7ae8037bc263878122912f7aafee0c1c Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Mon, 30 Sep 2024 13:53:55 +0800 Subject: [PATCH] Wait for task to complete synchronously after setting up domain account rather than wrapping everything in task Signed-off-by: Claudio Cambra --- ...ileProviderExtension+ClientInterface.swift | 75 +++++++++++-------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension+ClientInterface.swift b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension+ClientInterface.swift index e1bf70541..ad9c24d03 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension+ClientInterface.swift +++ b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension+ClientInterface.swift @@ -105,6 +105,47 @@ extension FileProviderExtension: NSFileProviderServicing, ChangeNotificationInte } @objc func setupDomainAccount(user: String, serverUrl: String, password: String) { + let semaphore = DispatchSemaphore(value: 0) + var authAttemptState = AuthenticationAttemptResultState.connectionError // default + Task { + let authTestNcKit = NextcloudKit() + authTestNcKit.setup(user: user, userId: user, password: password, urlBase: serverUrl) + + // Retry a few times if we have a connection issue + for authTimeout in AuthenticationTimeouts { + authAttemptState = await authTestNcKit.tryAuthenticationAttempt() + guard authAttemptState == .connectionError else { break } + + Logger.fileProviderExtension.info( + "\(user, privacy: .public) authentication try timed out. Trying again soon." + ) + try? await Task.sleep(nanoseconds: authTimeout) + } + semaphore.signal() + } + semaphore.wait() + + switch (authAttemptState) { + case .authenticationError: + Logger.fileProviderExtension.info( + "\(user, privacy: .public) authentication failed due to bad creds, stopping" + ) + return + case .connectionError: + // Despite multiple connection attempts we are still getting connection issues, so quit. + Logger.fileProviderExtension.info( + "\(user, privacy: .public) authentication try failed, no connection." + ) + return + case .success: + Logger.fileProviderExtension.info( + """ + Authenticated! Nextcloud account set up in File Provider extension. + User: \(user, privacy: .public) at server: \(serverUrl, privacy: .public) + """ + ) + } + let newNcAccount = Account(user: user, serverUrl: serverUrl, password: password) guard newNcAccount != ncAccount else { return } ncAccount = newNcAccount @@ -122,39 +163,7 @@ extension FileProviderExtension: NSFileProviderServicing, ChangeNotificationInte remoteInterface: ncKit, changeNotificationInterface: self, domain: domain ) ncKit.setup(delegate: changeObserver) - - Task { - var authAttemptState = AuthenticationAttemptResultState.connectionError // default - for authTimeout in AuthenticationTimeouts { // Retry if we have a connection issue - authAttemptState = await ncKit.tryAuthenticationAttempt() - guard authAttemptState == .connectionError else { break } - Logger.fileProviderExtension.info( - "\(user, privacy: .public) authentication try timed out. Trying again soon." - ) - try? await Task.sleep(nanoseconds: authTimeout) - } - - switch (authAttemptState) { - case .authenticationError: - Logger.fileProviderExtension.info( - "\(user, privacy: .public) authentication failed due to bad creds, stopping" - ) - ncAccount = nil - ncKit.setup(user: "", userId: "", password: "", urlBase: "") // In case ongoing ops - case .connectionError: - Logger.fileProviderExtension.info( - "\(user, privacy: .public) authentication try failed due to internet connectivity issues." - ) - case .success: - Logger.fileProviderExtension.info( - """ - Authenticated! Nextcloud account set up in File Provider extension. - User: \(user, privacy: .public) at server: \(serverUrl, privacy: .public) - """ - ) - Task { @MainActor in signalEnumeratorAfterAccountSetup() } - } - } + signalEnumeratorAfterAccountSetup() } @objc func removeAccountConfig() { -- 2.30.2