mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 16:10:20 +00:00
Made user session store base directory lazy, logging it for easier de… (#197)
* Made user session store base directory lazy, logging it for easier debugging * Make baseDirectory private set, expose it instead of the baseDirectoryPath.
This commit is contained in:
@@ -34,7 +34,7 @@ class AuthenticationServiceProxy: AuthenticationServiceProxyProtocol {
|
|||||||
|
|
||||||
init(userSessionStore: UserSessionStoreProtocol) {
|
init(userSessionStore: UserSessionStoreProtocol) {
|
||||||
self.userSessionStore = userSessionStore
|
self.userSessionStore = userSessionStore
|
||||||
authenticationService = AuthenticationService(basePath: userSessionStore.baseDirectoryPath)
|
authenticationService = AuthenticationService(basePath: userSessionStore.baseDirectory.path)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Public
|
// MARK: - Public
|
||||||
|
|||||||
@@ -26,7 +26,22 @@ class UserSessionStore: UserSessionStoreProtocol {
|
|||||||
var hasSessions: Bool { !keychainController.restoreTokens().isEmpty }
|
var hasSessions: Bool { !keychainController.restoreTokens().isEmpty }
|
||||||
|
|
||||||
/// The base directory where all session data is stored.
|
/// 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) {
|
init(bundleIdentifier: String, backgroundTaskService: BackgroundTaskServiceProtocol) {
|
||||||
keychainController = KeychainController(identifier: bundleIdentifier)
|
keychainController = KeychainController(identifier: bundleIdentifier)
|
||||||
@@ -86,11 +101,13 @@ class UserSessionStore: UserSessionStoreProtocol {
|
|||||||
deleteSessionDirectory(for: userID)
|
deleteSessionDirectory(for: userID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - Private
|
||||||
|
|
||||||
private func restorePreviousLogin(_ credentials: KeychainCredentials) async -> Result<ClientProxyProtocol, UserSessionStoreError> {
|
private func restorePreviousLogin(_ credentials: KeychainCredentials) async -> Result<ClientProxyProtocol, UserSessionStoreError> {
|
||||||
Benchmark.startTrackingForIdentifier("Login", message: "Started restoring previous login")
|
Benchmark.startTrackingForIdentifier("Login", message: "Started restoring previous login")
|
||||||
|
|
||||||
let builder = ClientBuilder()
|
let builder = ClientBuilder()
|
||||||
.basePath(path: baseDirectoryPath)
|
.basePath(path: baseDirectory.path)
|
||||||
.username(username: credentials.userID)
|
.username(username: credentials.userID)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
@@ -125,7 +142,7 @@ class UserSessionStore: UserSessionStoreProtocol {
|
|||||||
private func deleteSessionDirectory(for userID: String) {
|
private func deleteSessionDirectory(for userID: String) {
|
||||||
// Rust sanitises the user ID replacing invalid characters with an _
|
// Rust sanitises the user ID replacing invalid characters with an _
|
||||||
let sanitisedUserID = userID.replacingOccurrences(of: ":", with: "_")
|
let sanitisedUserID = userID.replacingOccurrences(of: ":", with: "_")
|
||||||
let url = baseDirectory().appendingPathComponent(sanitisedUserID)
|
let url = baseDirectory.appendingPathComponent(sanitisedUserID)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
try FileManager.default.removeItem(at: url)
|
try FileManager.default.removeItem(at: url)
|
||||||
@@ -133,19 +150,4 @@ class UserSessionStore: UserSessionStoreProtocol {
|
|||||||
MXLog.failure("Failed deleting the session data: \(error)")
|
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ protocol UserSessionStoreProtocol {
|
|||||||
var hasSessions: Bool { get }
|
var hasSessions: Bool { get }
|
||||||
|
|
||||||
/// Returns the location to store user data for a particular username.
|
/// Returns the location to store user data for a particular username.
|
||||||
var baseDirectoryPath: String { get }
|
var baseDirectory: URL { get }
|
||||||
|
|
||||||
/// Restores an existing user session.
|
/// Restores an existing user session.
|
||||||
func restoreUserSession() async -> Result<UserSession, UserSessionStoreError>
|
func restoreUserSession() async -> Result<UserSession, UserSessionStoreError>
|
||||||
|
|||||||
Reference in New Issue
Block a user