Skip to content

Commit

Permalink
Storage location change (#404)
Browse files Browse the repository at this point in the history
* Remove LegacyRestorationToken and handling
* Fixes #389 - Migrate and store session data in Application Support instead of Caches
  • Loading branch information
stefanceriu authored Jan 6, 2023
1 parent e573bc0 commit 34eb835
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 43 deletions.
33 changes: 29 additions & 4 deletions ElementX/Sources/Other/Extensions/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ extension URL {

/// The base directory where all session data is stored.
static var sessionsBaseDirectory: URL {
let url = cacheBaseDirectory
.appendingPathComponent("Sessions", isDirectory: true)
let cacheSessionsURL = cacheBaseDirectory.appendingPathComponent("Sessions", isDirectory: true)
let applicationSupportSessionsURL = applicationSupportBaseDirectory.appendingPathComponent("Sessions", isDirectory: true)

#warning("Migration from caches to application support. Remove this in a couple of releases.")
if FileManager.default.directoryExists(at: cacheSessionsURL) {
try? FileManager.default.moveItem(at: cacheSessionsURL, to: applicationSupportSessionsURL)
}

try? FileManager.default.createDirectoryIfNeeded(at: url)
try? FileManager.default.createDirectoryIfNeeded(at: applicationSupportSessionsURL)

return url
return applicationSupportSessionsURL
}

/// The base directory where all cache is stored.
Expand All @@ -53,4 +58,24 @@ extension URL {

return url
}

/// The base directory where all application support data is stored.
static var applicationSupportBaseDirectory: URL {
var url = appGroupContainerDirectory
.appendingPathComponent("Library", isDirectory: true)
.appendingPathComponent("Application Support", isDirectory: true)
.appendingPathComponent(InfoPlistReader.target.baseBundleIdentifier, isDirectory: true)

try? FileManager.default.createDirectoryIfNeeded(at: url)

do {
var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try url.setResourceValues(resourceValues)
} catch {
MXLog.error("Failed excluding Application Support from backups")
}

return url
}
}
11 changes: 0 additions & 11 deletions ElementX/Sources/Services/Keychain/KeychainController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,6 @@ class KeychainController: KeychainControllerProtocol {
return nil
}

// Handle the previous restoration token format as we don't want users to have to log in again
// It will automatically be updated to the new version after login
if let legacyRestorationToken = try? JSONDecoder().decode(LegacyRestorationToken.self, from: tokenData) {
return .init(session: .init(accessToken: legacyRestorationToken.session.accessToken,
refreshToken: nil,
userId: legacyRestorationToken.session.userId,
deviceId: legacyRestorationToken.session.deviceId,
homeserverUrl: legacyRestorationToken.homeURL,
isSoftLogout: legacyRestorationToken.isSoftLogout ?? false))
}

return try JSONDecoder().decode(RestorationToken.self, from: tokenData)
} catch {
MXLog.error("Failed retrieving user restore token")
Expand Down
28 changes: 0 additions & 28 deletions ElementX/Sources/Services/UserSession/RestorationToken.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,3 @@ extension MatrixRustSDK.Session: Codable {
case accessToken, refreshToken, userId, deviceId, homeserverUrl, isSoftLogout
}
}

#warning("Remove this in a couple of releases - sceriu 03.11.2022")
struct LegacyRestorationToken: Decodable {
let isGuest: Bool?
let isSoftLogout: Bool?
let homeURL: String
let session: Session

enum CodingKeys: String, CodingKey {
case isGuest = "is_guest"
case isSoftLogout = "is_soft_logout"
case homeURL = "homeurl"
case session
}

struct Session: Decodable {
let accessToken: String
let userId: String
let deviceId: String

// swiftlint:disable:next nesting
enum CodingKeys: String, CodingKey {
case accessToken = "access_token"
case userId = "user_id"
case deviceId = "device_id"
}
}
}
1 change: 1 addition & 0 deletions changelog.d/389.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Migrate and store session data in Application Support instead of Caches

0 comments on commit 34eb835

Please sign in to comment.