2024-10-03 16:41:27 +00:00
|
|
|
//
|
2024-11-14 10:02:26 +00:00
|
|
|
// ProfileV2MigrationStrategy.swift
|
2024-10-03 16:41:27 +00:00
|
|
|
// 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-02 09:11:59 +00:00
|
|
|
import CommonUtils
|
2024-10-03 16:41:27 +00:00
|
|
|
import Foundation
|
|
|
|
import PassepartoutKit
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
public final class ProfileV2MigrationStrategy: ProfileMigrationStrategy, Sendable {
|
2024-11-23 12:33:02 +00:00
|
|
|
public struct Container {
|
|
|
|
public let name: String
|
|
|
|
|
|
|
|
public let cloudKitIdentifier: String?
|
|
|
|
|
|
|
|
public init(_ name: String, _ cloudKitIdentifier: String?) {
|
|
|
|
self.name = name
|
|
|
|
self.cloudKitIdentifier = cloudKitIdentifier
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-03 16:41:27 +00:00
|
|
|
private let profilesRepository: CDProfileRepositoryV2
|
|
|
|
|
2024-11-23 12:33:02 +00:00
|
|
|
private let tvProfilesRepository: CDProfileRepositoryV2
|
2024-10-03 16:41:27 +00:00
|
|
|
|
|
|
|
public init(
|
2024-11-12 15:42:19 +00:00
|
|
|
coreDataLogger: CoreDataPersistentStoreLogger?,
|
|
|
|
baseURL: URL? = nil,
|
2024-11-23 12:33:02 +00:00
|
|
|
profilesContainer: Container,
|
|
|
|
tvProfilesContainer: Container
|
2024-10-03 16:41:27 +00:00
|
|
|
) {
|
|
|
|
let store = CoreDataPersistentStore(
|
|
|
|
logger: coreDataLogger,
|
2024-11-23 12:33:02 +00:00
|
|
|
containerName: profilesContainer.name,
|
|
|
|
baseURL: baseURL,
|
|
|
|
model: CDProfileRepositoryV2.model,
|
|
|
|
cloudKitIdentifier: profilesContainer.cloudKitIdentifier,
|
|
|
|
author: nil
|
|
|
|
)
|
|
|
|
let tvStore = CoreDataPersistentStore(
|
|
|
|
logger: coreDataLogger,
|
|
|
|
containerName: tvProfilesContainer.name,
|
2024-11-12 15:42:19 +00:00
|
|
|
baseURL: baseURL,
|
2024-10-03 16:41:27 +00:00
|
|
|
model: CDProfileRepositoryV2.model,
|
2024-11-23 12:33:02 +00:00
|
|
|
cloudKitIdentifier: tvProfilesContainer.cloudKitIdentifier,
|
2024-10-03 16:41:27 +00:00
|
|
|
author: nil
|
|
|
|
)
|
2024-12-09 07:44:13 +00:00
|
|
|
profilesRepository = CDProfileRepositoryV2(context: store.backgroundContext())
|
|
|
|
tvProfilesRepository = CDProfileRepositoryV2(context: tvStore.backgroundContext())
|
2024-10-03 16:41:27 +00:00
|
|
|
}
|
2024-11-12 15:42:19 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
// MARK: - ProfileMigrationStrategy
|
2024-11-12 15:42:19 +00:00
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
extension ProfileV2MigrationStrategy {
|
2024-11-12 15:42:19 +00:00
|
|
|
public func fetchMigratableProfiles() async throws -> [MigratableProfile] {
|
|
|
|
try await profilesRepository.migratableProfiles()
|
|
|
|
}
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
public func fetchProfile(withId profileId: UUID) async throws -> Profile? {
|
2024-11-12 15:42:19 +00:00
|
|
|
let mapper = MapperV2()
|
2024-11-14 10:02:26 +00:00
|
|
|
do {
|
|
|
|
guard let profile = try await profilesRepository.profile(withId: profileId) else {
|
|
|
|
return nil
|
2024-11-12 15:42:19 +00:00
|
|
|
}
|
2024-11-23 12:33:02 +00:00
|
|
|
let tvProfile = try? await tvProfilesRepository.profile(withId: profileId)
|
|
|
|
return try mapper.toProfileV3(profile, isTV: tvProfile != nil)
|
2024-11-14 10:02:26 +00:00
|
|
|
} catch {
|
2024-11-17 14:28:31 +00:00
|
|
|
pp_log(.App.migration, .error, "Unable to fetch and map migratable profile \(profileId): \(error)")
|
2024-11-14 10:02:26 +00:00
|
|
|
return nil
|
2024-11-12 15:42:19 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-16 11:29:03 +00:00
|
|
|
|
|
|
|
public func deleteProfiles(withIds profileIds: Set<UUID>) async throws {
|
|
|
|
try await profilesRepository.deleteProfiles(withIds: profileIds)
|
|
|
|
}
|
2024-11-12 15:42:19 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
// MARK: - Internal
|
2024-10-03 16:41:27 +00:00
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
extension ProfileV2MigrationStrategy {
|
2024-11-12 15:42:19 +00:00
|
|
|
func fetchProfilesV2() async throws -> [ProfileV2] {
|
2024-11-14 10:02:26 +00:00
|
|
|
try await profilesRepository.profiles(withIds: nil)
|
2024-10-03 16:41:27 +00:00
|
|
|
}
|
|
|
|
}
|