-
Notifications
You must be signed in to change notification settings - Fork 9
/
CloudBackupClient+Live.swift
366 lines (314 loc) · 13.1 KB
/
CloudBackupClient+Live.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import CloudKit
import ComposableArchitecture
import DependenciesAdditions
import os
extension CKRecord.RecordType {
static let profile = "ProfileV2"
}
extension CKRecord.FieldKey {
static let content = "content"
static let snapshotVersion = "snapshotVersion"
static let creatingDeviceID = "creatingDeviceID"
static let creatingDeviceDate = "creatingDeviceDate"
static let creatingDeviceDescription = "creatingDeviceDescription"
static let lastUsedOnDeviceID = "lastUsedOnDeviceID"
static let lastUsedOnDeviceDate = "lastUsedOnDeviceDate"
static let lastUsedOnDeviceDescription = "lastUsedOnDeviceDescription"
static let lastModified = "lastModified"
static let numberOfAccounts = "numberOfAccounts"
static let numberOfPersonas = "numberOfPersonas"
static let numberOfNetworks = "numberOfNetworks"
}
extension [CKRecord.FieldKey] {
static let header: Self = [
.snapshotVersion,
.creatingDeviceID,
.creatingDeviceDate,
.creatingDeviceDescription,
.lastUsedOnDeviceID,
.lastUsedOnDeviceDate,
.lastUsedOnDeviceDescription,
.lastModified,
.numberOfAccounts,
.numberOfPersonas,
.numberOfNetworks,
]
}
extension CloudBackupClient {
public func deleteProfileBackup() async throws {
try await deleteProfileBackup(nil)
}
}
extension CloudBackupClient {
struct IncorrectRecordTypeError: Error {}
struct NoProfileInRecordError: Error {}
struct MissingMetadataError: Error {}
struct WrongRecordTypeError: Error { let type: CKRecord.RecordType }
struct FailedToClaimProfileError: Error { let error: Error }
public static let liveValue: Self = .live()
public static func live(
profileStore: ProfileStore = .shared
) -> CloudBackupClient {
@Dependency(\.appPreferencesClient) var appPreferencesClient
@Dependency(\.overlayWindowClient) var overlayWindowClient
@Dependency(\.secureStorageClient) var secureStorageClient
@Dependency(\.userDefaults) var userDefaults
let container: CKContainer = .default()
@Sendable
func fetchProfileRecord(_ id: ProfileID) async throws -> CKRecord {
let recordID = CKRecord.ID(recordName: id.uuidString)
let record = try await container.privateCloudDatabase.record(for: recordID)
guard record.recordType == .profile else {
throw WrongRecordTypeError(type: record.recordType)
}
return record
}
@Sendable
func fetchAllProfileHeaders() async throws -> [CKRecord] {
let records = try await container.privateCloudDatabase.records(
matching: .init(recordType: .profile, predicate: .init(value: true)),
desiredKeys: .header
)
return try records.matchResults.map { try $0.1.get() }
}
@Sendable
func getProfile(_ record: CKRecord) throws -> BackedUpProfile {
guard record.recordType == .profile else {
throw IncorrectRecordTypeError()
}
guard let asset = record[.content] as? CKAsset, let fileURL = asset.fileURL else {
throw NoProfileInRecordError()
}
let json = try String(contentsOf: fileURL, encoding: .utf8)
let containsLegacyP2PLinks = Profile.checkIfProfileJsonStringContainsLegacyP2PLinks(jsonString: json)
let profile = try Profile(jsonString: json)
try FileManager.default.removeItem(at: fileURL)
return BackedUpProfile(profile: profile, containsLegacyP2PLinks: containsLegacyP2PLinks)
}
@discardableResult
@Sendable
func backupProfile(_ profile: Either<Data, String>, header: Profile.Header, existingRecord: CKRecord?) async throws -> CKRecord {
let fileManager = FileManager.default
let tempDirectoryURL = fileManager.temporaryDirectory
let fileURL = tempDirectoryURL.appendingPathComponent(UUID().uuidString)
switch profile {
case let .left(data):
try data.write(to: fileURL)
case let .right(json):
try json.write(to: fileURL, atomically: true, encoding: .utf8)
}
let id = header.id
let record = existingRecord ?? .init(recordType: .profile, recordID: .init(recordName: id.uuidString))
record[.content] = CKAsset(fileURL: fileURL)
setProfileHeader(header, on: record)
let savedRecord = try await container.privateCloudDatabase.save(record)
try fileManager.removeItem(at: fileURL)
return savedRecord
}
@Sendable
func backupProfileAndSaveResult(_ profile: Profile, existingRecord: CKRecord?) async throws {
try? userDefaults.setLastCloudBackup(.started(.now), of: profile.header)
do {
let json = profile.toJSONString()
try await backupProfile(.right(json), header: profile.header, existingRecord: existingRecord)
} catch {
if await (try? container.accountStatus()) == .restricted {
try await appPreferencesClient.setIsCloudBackupEnabled(false)
}
let failure: BackupResult.Result.Failure
switch error {
case CKError.accountTemporarilyUnavailable:
failure = .temporarilyUnavailable
case CKError.notAuthenticated:
failure = .notAuthenticated
default:
loggerGlobal.error("Cloud backup failed with error \(error)")
failure = .other
}
try? userDefaults.setLastCloudBackup(.failure(failure), of: profile.header)
throw error
}
try? userDefaults.setLastCloudBackup(.success, of: profile.header)
}
@Sendable
func performAutomaticBackup(_ profile: Profile, timeToCheckIfClaimed: Bool) async {
let existingRecord = try? await fetchProfileRecord(profile.id)
let backedUpHeader = try? existingRecord.map(getProfileHeader)
if let backedUpHeader, let backupDate = existingRecord?.modificationDate {
try? userDefaults.setLastCloudBackup(.success, of: backedUpHeader, at: backupDate)
} else {
try? userDefaults.removeLastCloudBackup(for: profile.id)
}
guard profile.appPreferences.security.isCloudProfileSyncEnabled else { return }
let isBackedUp = backedUpHeader?.saveIdentifier == profile.header.saveIdentifier
let shouldBackUp = profile.header.isNonEmpty && !isBackedUp
guard shouldBackUp || timeToCheckIfClaimed else { return }
let shouldReclaim: Bool
if let backedUpID = backedUpHeader?.lastUsedOnDevice.id, await !profileStore.isThisDevice(deviceID: backedUpID) {
let action = await overlayWindowClient.scheduleFullScreen(.init(root: .claimWallet(.init())))
switch action {
case .claimWallet(.transferBack):
shouldReclaim = true
case .claimWallet(.didClearWallet), .dismiss:
return
}
} else {
shouldReclaim = false
}
guard shouldBackUp || shouldReclaim else { return }
var profileToUpload = profile
if shouldReclaim {
// The profile will already be locally claimed, but we want to update the lastUsedOnDevice date
await profileStore.claimOwnership(of: &profileToUpload)
}
try? await backupProfileAndSaveResult(profileToUpload, existingRecord: existingRecord)
}
let retryBackupInterval: DispatchTimeInterval = .seconds(60)
let checkClaimedProfileInterval: TimeInterval = 15 * 60
return .init(
isCloudProfileSyncEnabled: {
await profileStore.appPreferencesValues()
.map(\.security.isCloudProfileSyncEnabled)
.removeDuplicates()
.eraseToAnyAsyncSequence()
},
startAutomaticBackups: {
// The active profile should not be synced to iCloud keychain
let profileID = await profileStore.profile.id
try secureStorageClient.disableCloudProfileSync(profileID)
let ticks = AsyncTimerSequence(every: retryBackupInterval)
let profiles = await profileStore.values()
var lastClaimCheck: Date = .distantPast
let iCloudAvailability = NotificationCenter.default.notifications(named: .NSUbiquityIdentityDidChange)
.map { _ in () }.prepend(())
let cloudKitStatus = NotificationCenter.default.notifications(named: .CKAccountChanged)
.map { _ in () }.prepend(())
let iCloudStatus = combineLatest(iCloudAvailability, cloudKitStatus)
.map { _, _ in () }
for try await (profile, tick, _) in combineLatest(profiles, ticks, iCloudStatus) {
guard !Task.isCancelled else { return }
if tick.timeIntervalSince(lastClaimCheck) > checkClaimedProfileInterval {
await performAutomaticBackup(profile, timeToCheckIfClaimed: true)
lastClaimCheck = .now
} else {
await performAutomaticBackup(profile, timeToCheckIfClaimed: false)
}
}
},
migrateProfilesFromKeychain: {
let activeProfile = await profileStore.profile.id
guard let headerList = try secureStorageClient.loadProfileHeaderList() else { return [] }
let previouslyMigrated = userDefaults.getMigratedKeychainProfiles
let migratable = try headerList.compactMap { header -> (Data, Profile.Header)? in
let id = header.id
guard !previouslyMigrated.contains(id), header.id != activeProfile else { return nil }
guard let profileData = try? secureStorageClient.loadProfileSnapshotData(id) else { return nil }
let profile = try Profile(jsonData: profileData)
guard !profile.networks.isEmpty else { return nil }
guard profile.appPreferences.security.isCloudProfileSyncEnabled else { return nil }
return (profileData, header)
}
let backedUpRecords = try await fetchAllProfileHeaders()
let migrated: [CKRecord] = try await migratable.asyncCompactMap { profileData, header in
let backedUpRecord = backedUpRecords.first { $0.recordID.recordName == header.id.uuidString }
if let backedUpRecord, try getProfileHeader(backedUpRecord).lastModified >= header.lastModified {
// We already have a more recent version backed up on iCloud
return nil
}
return try await backupProfile(.left(profileData), header: header, existingRecord: backedUpRecord)
}
let migratedIDs = migrated.compactMap { ProfileID(uuidString: $0.recordID.recordName) }
try userDefaults.appendMigratedKeychainProfiles(migratedIDs)
return migrated
},
deleteProfileBackup: { optionalID in
let activeProfileID = await profileStore.profile.id
let id = optionalID ?? activeProfileID
try await container.privateCloudDatabase.deleteRecord(withID: .init(recordName: id.uuidString))
try userDefaults.removeLastCloudBackup(for: id)
},
checkAccountStatus: {
try await container.accountStatus()
},
lastBackup: { id in
userDefaults.lastCloudBackupValues(for: id)
},
loadProfile: { id in
try await getProfile(fetchProfileRecord(id))
},
loadProfileHeaders: {
try await fetchAllProfileHeaders()
.map(getProfileHeader)
.filter(\.isNonEmpty)
},
claimProfileOnICloud: { profile in
let existingRecord = try? await fetchProfileRecord(profile.id)
do {
try await backupProfileAndSaveResult(profile, existingRecord: existingRecord)
} catch {
loggerGlobal.error("Failed to claim profile on iCloud \(error)")
throw FailedToClaimProfileError(error: error)
}
}
)
}
}
private extension Profile.Header {
var isNonEmpty: Bool {
contentHint.numberOfAccountsOnAllNetworksInTotal + contentHint.numberOfPersonasOnAllNetworksInTotal > 0
}
}
extension CloudBackupClient {
@Sendable
private static func getProfileHeader(_ record: CKRecord) throws -> Profile.Header {
guard let snapshotVersion = (record[.snapshotVersion] as? UInt16).flatMap(ProfileSnapshotVersion.init(rawValue:)),
let id = UUID(uuidString: record.recordID.recordName),
let creatingDeviceID = (record[.creatingDeviceID] as? String).flatMap(UUID.init),
let creatingDeviceDate = (record[.creatingDeviceDate] as? Date),
let creatingDeviceDescription = (record[.creatingDeviceDescription] as? String),
let lastUsedOnDeviceID = (record[.lastUsedOnDeviceID] as? String).flatMap(UUID.init),
let lastUsedOnDeviceDate = (record[.lastUsedOnDeviceDate] as? Date),
let lastUsedOnDeviceDescription = (record[.lastUsedOnDeviceDescription] as? String),
let lastModified = record[.lastModified] as? Date,
let numberOfAccounts = record[.numberOfAccounts] as? UInt16,
let numberOfPersonas = record[.numberOfPersonas] as? UInt16,
let numberOfNetworks = record[.numberOfNetworks] as? UInt16
else {
throw MissingMetadataError()
}
return .init(
snapshotVersion: snapshotVersion,
id: id,
creatingDevice: .init(
id: creatingDeviceID,
date: creatingDeviceDate,
description: creatingDeviceDescription
),
lastUsedOnDevice: .init(
id: lastUsedOnDeviceID,
date: lastUsedOnDeviceDate,
description: lastUsedOnDeviceDescription
),
lastModified: lastModified,
contentHint: .init(
numberOfAccountsOnAllNetworksInTotal: numberOfAccounts,
numberOfPersonasOnAllNetworksInTotal: numberOfPersonas,
numberOfNetworks: numberOfNetworks
)
)
}
@Sendable
private static func setProfileHeader(_ header: Profile.Header, on record: CKRecord) {
record[.snapshotVersion] = header.snapshotVersion.rawValue
record[.creatingDeviceID] = header.creatingDevice.id.uuidString
record[.creatingDeviceDate] = header.creatingDevice.date
record[.creatingDeviceDescription] = header.creatingDevice.description
record[.lastUsedOnDeviceID] = header.lastUsedOnDevice.id.uuidString
record[.lastUsedOnDeviceDate] = header.lastUsedOnDevice.date
record[.lastUsedOnDeviceDescription] = header.lastUsedOnDevice.description
record[.lastModified] = header.lastModified
record[.numberOfAccounts] = header.contentHint.numberOfAccountsOnAllNetworksInTotal
record[.numberOfPersonas] = header.contentHint.numberOfPersonasOnAllNetworksInTotal
record[.numberOfNetworks] = header.contentHint.numberOfNetworks
}
}