2024-10-03 16:41:27 +00:00
|
|
|
//
|
|
|
|
// CDProfileRepositoryV2.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 10/1/24.
|
|
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
2024-11-12 15:42:19 +00:00
|
|
|
import CommonLibrary
|
2024-11-14 10:02:26 +00:00
|
|
|
@preconcurrency import CoreData
|
2024-10-03 16:41:27 +00:00
|
|
|
import Foundation
|
|
|
|
import PassepartoutKit
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
final class CDProfileRepositoryV2: Sendable {
|
2024-10-03 16:41:27 +00:00
|
|
|
static var model: NSManagedObjectModel {
|
|
|
|
guard let model: NSManagedObjectModel = .mergedModel(from: [.module]) else {
|
|
|
|
fatalError("Unable to build Core Data model (Profiles v2)")
|
|
|
|
}
|
|
|
|
return model
|
|
|
|
}
|
|
|
|
|
|
|
|
private let context: NSManagedObjectContext
|
|
|
|
|
|
|
|
init(context: NSManagedObjectContext) {
|
|
|
|
self.context = context
|
|
|
|
}
|
|
|
|
|
2024-11-12 15:42:19 +00:00
|
|
|
func migratableProfiles() async throws -> [MigratableProfile] {
|
|
|
|
try await fetchProfiles(
|
|
|
|
prefetch: {
|
|
|
|
$0.propertiesToFetch = ["uuid", "name", "lastUpdate"]
|
|
|
|
},
|
|
|
|
map: {
|
|
|
|
$0.compactMap {
|
|
|
|
guard $0.value.encryptedJSON ?? $0.value.json != nil else {
|
2024-11-17 14:28:31 +00:00
|
|
|
pp_log(.App.migration, .error, "ProfileV2 \($0.key) is not migratable: missing JSON")
|
2024-11-12 15:42:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return MigratableProfile(
|
|
|
|
id: $0.key,
|
|
|
|
name: $0.value.name ?? $0.key.uuidString,
|
|
|
|
lastUpdate: $0.value.lastUpdate
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
func profile(withId profileId: UUID) async throws -> ProfileV2? {
|
|
|
|
try await profiles(withIds: [profileId]).first
|
|
|
|
}
|
|
|
|
|
|
|
|
func profiles(withIds profileIds: Set<UUID>?) async throws -> [ProfileV2] {
|
2024-11-12 15:42:19 +00:00
|
|
|
let decoder = JSONDecoder()
|
2024-11-14 10:02:26 +00:00
|
|
|
let profiles: [ProfileV2] = try await fetchProfiles(
|
|
|
|
prefetch: {
|
|
|
|
if let profileIds {
|
|
|
|
$0.predicate = NSPredicate(format: "any uuid in %@", profileIds)
|
|
|
|
}
|
|
|
|
},
|
2024-11-12 15:42:19 +00:00
|
|
|
map: {
|
|
|
|
$0.compactMap {
|
|
|
|
guard let json = $0.value.encryptedJSON ?? $0.value.json else {
|
2024-11-17 14:28:31 +00:00
|
|
|
pp_log(.App.migration, .error, "ProfileV2 \($0.key) is not migratable: missing JSON")
|
2024-11-12 15:42:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
return try decoder.decode(ProfileV2.self, from: json)
|
|
|
|
} catch {
|
2024-11-17 14:28:31 +00:00
|
|
|
pp_log(.App.migration, .error, "Unable to decode ProfileV2 \($0.key): \(error)")
|
2024-11-12 15:42:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2024-11-14 10:02:26 +00:00
|
|
|
return profiles
|
2024-11-12 15:42:19 +00:00
|
|
|
}
|
2024-11-16 11:29:03 +00:00
|
|
|
|
|
|
|
func deleteProfiles(withIds profileIds: Set<UUID>) async throws {
|
|
|
|
try await context.perform { [weak self] in
|
|
|
|
guard let self else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let request = CDProfile.fetchRequest()
|
2024-11-17 14:28:31 +00:00
|
|
|
request.predicate = NSPredicate(format: "any uuid in %@", profileIds)
|
2024-11-16 11:29:03 +00:00
|
|
|
let existing = try context.fetch(request)
|
|
|
|
existing.forEach(context.delete)
|
|
|
|
try context.save()
|
|
|
|
}
|
|
|
|
}
|
2024-11-12 15:42:19 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
extension CDProfileRepositoryV2 {
|
2024-11-12 15:42:19 +00:00
|
|
|
func fetchProfiles<T>(
|
|
|
|
prefetch: ((NSFetchRequest<CDProfile>) -> Void)? = nil,
|
|
|
|
map: @escaping ([UUID: CDProfile]) -> [T]
|
|
|
|
) async throws -> [T] {
|
2024-10-03 16:41:27 +00:00
|
|
|
try await context.perform { [weak self] in
|
|
|
|
guard let self else {
|
|
|
|
return []
|
|
|
|
}
|
2024-11-12 15:42:19 +00:00
|
|
|
|
|
|
|
let request = CDProfile.fetchRequest()
|
|
|
|
request.sortDescriptors = [
|
|
|
|
.init(key: "lastUpdate", ascending: false)
|
|
|
|
]
|
|
|
|
prefetch?(request)
|
|
|
|
let existing = try context.fetch(request)
|
|
|
|
|
|
|
|
var deduped: [UUID: CDProfile] = [:]
|
|
|
|
existing.forEach {
|
|
|
|
guard let uuid = $0.uuid else {
|
|
|
|
return
|
2024-10-03 16:41:27 +00:00
|
|
|
}
|
2024-11-12 15:42:19 +00:00
|
|
|
guard !deduped.keys.contains(uuid) else {
|
2024-11-17 14:28:31 +00:00
|
|
|
pp_log(.App.migration, .info, "Skip older duplicate of ProfileV2 \(uuid)")
|
2024-11-12 15:42:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
deduped[uuid] = $0
|
2024-10-03 16:41:27 +00:00
|
|
|
}
|
2024-11-12 15:42:19 +00:00
|
|
|
|
|
|
|
return map(deduped)
|
2024-10-03 16:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|