From a807301746210ebe133b35a75b60079dc8338b5b Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Thu, 22 Sep 2022 13:10:23 +0300 Subject: [PATCH] =?UTF-8?q?Made=20user=20session=20store=20base=20director?= =?UTF-8?q?y=20lazy,=20logging=20it=20for=20easier=20de=E2=80=A6=20(#197)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Made user session store base directory lazy, logging it for easier debugging * Make baseDirectory private set, expose it instead of the baseDirectoryPath. --- .../AuthenticationServiceProxy.swift | 2 +- .../UserSessionStore/UserSessionStore.swift | 38 ++++++++++--------- .../UserSessionStoreProtocol.swift | 2 +- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/ElementX/Sources/Services/Authentication/AuthenticationServiceProxy.swift b/ElementX/Sources/Services/Authentication/AuthenticationServiceProxy.swift index aa47c6e7f..d88297517 100644 --- a/ElementX/Sources/Services/Authentication/AuthenticationServiceProxy.swift +++ b/ElementX/Sources/Services/Authentication/AuthenticationServiceProxy.swift @@ -34,7 +34,7 @@ class AuthenticationServiceProxy: AuthenticationServiceProxyProtocol { init(userSessionStore: UserSessionStoreProtocol) { self.userSessionStore = userSessionStore - authenticationService = AuthenticationService(basePath: userSessionStore.baseDirectoryPath) + authenticationService = AuthenticationService(basePath: userSessionStore.baseDirectory.path) } // MARK: - Public diff --git a/ElementX/Sources/Services/UserSessionStore/UserSessionStore.swift b/ElementX/Sources/Services/UserSessionStore/UserSessionStore.swift index b024f213d..f40f1b907 100644 --- a/ElementX/Sources/Services/UserSessionStore/UserSessionStore.swift +++ b/ElementX/Sources/Services/UserSessionStore/UserSessionStore.swift @@ -26,7 +26,22 @@ class UserSessionStore: UserSessionStoreProtocol { var hasSessions: Bool { !keychainController.restoreTokens().isEmpty } /// The base directory where all session data is stored. - var baseDirectoryPath: String { baseDirectory().path } + private(set) lazy var baseDirectory: URL = { + guard let appGroupContainerURL = FileManager.default.appGroupContainerURL else { + fatalError("Should always be able to retrieve the container directory") + } + + let url = appGroupContainerURL + .appendingPathComponent("Library", isDirectory: true) + .appendingPathComponent("Caches", isDirectory: true) + .appendingPathComponent("Sessions", isDirectory: true) + + try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: false, attributes: nil) + + MXLog.debug("Setup base directory at: \(url)") + + return url + }() init(bundleIdentifier: String, backgroundTaskService: BackgroundTaskServiceProtocol) { keychainController = KeychainController(identifier: bundleIdentifier) @@ -86,11 +101,13 @@ class UserSessionStore: UserSessionStoreProtocol { deleteSessionDirectory(for: userID) } + // MARK: - Private + private func restorePreviousLogin(_ credentials: KeychainCredentials) async -> Result { Benchmark.startTrackingForIdentifier("Login", message: "Started restoring previous login") let builder = ClientBuilder() - .basePath(path: baseDirectoryPath) + .basePath(path: baseDirectory.path) .username(username: credentials.userID) do { @@ -125,7 +142,7 @@ class UserSessionStore: UserSessionStoreProtocol { private func deleteSessionDirectory(for userID: String) { // Rust sanitises the user ID replacing invalid characters with an _ let sanitisedUserID = userID.replacingOccurrences(of: ":", with: "_") - let url = baseDirectory().appendingPathComponent(sanitisedUserID) + let url = baseDirectory.appendingPathComponent(sanitisedUserID) do { try FileManager.default.removeItem(at: url) @@ -133,19 +150,4 @@ class UserSessionStore: UserSessionStoreProtocol { MXLog.failure("Failed deleting the session data: \(error)") } } - - func baseDirectory() -> URL { - guard let appGroupContainerURL = FileManager.default.appGroupContainerURL else { - fatalError("Should always be able to retrieve the container directory") - } - - let url = appGroupContainerURL - .appendingPathComponent("Library", isDirectory: true) - .appendingPathComponent("Caches", isDirectory: true) - .appendingPathComponent("Sessions", isDirectory: true) - - try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: false, attributes: nil) - - return url - } } diff --git a/ElementX/Sources/Services/UserSessionStore/UserSessionStoreProtocol.swift b/ElementX/Sources/Services/UserSessionStore/UserSessionStoreProtocol.swift index 419eb745e..89b8a452a 100644 --- a/ElementX/Sources/Services/UserSessionStore/UserSessionStoreProtocol.swift +++ b/ElementX/Sources/Services/UserSessionStore/UserSessionStoreProtocol.swift @@ -30,7 +30,7 @@ protocol UserSessionStoreProtocol { var hasSessions: Bool { get } /// Returns the location to store user data for a particular username. - var baseDirectoryPath: String { get } + var baseDirectory: URL { get } /// Restores an existing user session. func restoreUserSession() async -> Result