NetNewsWire/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift

866 lines
26 KiB
Swift
Raw Normal View History

2020-03-18 21:48:44 +01:00
//
// CloudKitAppDelegate.swift
// Account
//
// Created by Maurice Parker on 3/18/20.
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import CloudKit
import SystemConfiguration
import os.log
import SyncDatabase
2020-03-18 21:48:44 +01:00
import RSCore
import RSParser
import Articles
import ArticlesDatabase
2020-03-18 21:48:44 +01:00
import RSWeb
2020-04-10 04:07:56 +02:00
import Secrets
2020-03-18 21:48:44 +01:00
enum CloudKitAccountDelegateError: LocalizedError {
case invalidParameter
case unknown
var errorDescription: String? {
return NSLocalizedString("An unexpected CloudKit error occurred.", comment: "An unexpected CloudKit error occurred.")
}
2020-03-18 21:48:44 +01:00
}
final class CloudKitAccountDelegate: AccountDelegate {
private var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "CloudKit")
private let database: SyncDatabase
private let container: CKContainer = {
let orgID = Bundle.main.object(forInfoDictionaryKey: "OrganizationIdentifier") as! String
return CKContainer(identifier: "iCloud.\(orgID).NetNewsWire")
}()
private let accountZone: CloudKitAccountZone
2020-04-01 18:46:37 +02:00
private let articlesZone: CloudKitArticlesZone
private let mainThreadOperationQueue = MainThreadOperationQueue()
private lazy var refresher: LocalAccountRefresher = {
let refresher = LocalAccountRefresher()
refresher.delegate = self
return refresher
}()
weak var account: Account?
2020-03-18 21:48:44 +01:00
let behaviors: AccountBehaviors = []
let isOPMLImportInProgress = false
let server: String? = nil
var credentials: Credentials?
var accountMetadata: AccountMetadata?
var refreshProgress = DownloadProgress(numberOfTasks: 0)
init(dataFolder: String) {
accountZone = CloudKitAccountZone(container: container)
2020-04-01 18:46:37 +02:00
articlesZone = CloudKitArticlesZone(container: container)
let databaseFilePath = (dataFolder as NSString).appendingPathComponent("Sync.sqlite3")
database = SyncDatabase(databaseFilePath: databaseFilePath)
}
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any], completion: @escaping () -> Void) {
let op = CloudKitRemoteNotificationOperation(accountZone: accountZone, articlesZone: articlesZone, userInfo: userInfo)
op.completionBlock = { mainThreadOperaion in
completion()
}
mainThreadOperationQueue.add(op)
}
2020-03-18 21:48:44 +01:00
func refreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
guard refreshProgress.isComplete else {
completion(.success(()))
return
}
let reachability = SCNetworkReachabilityCreateWithName(nil, "apple.com")
var flags = SCNetworkReachabilityFlags()
guard SCNetworkReachabilityGetFlags(reachability!, &flags), flags.contains(.reachable) else {
completion(.success(()))
return
}
2020-04-13 11:59:41 +02:00
standardRefreshAll(for: account, completion: completion)
2020-03-18 21:48:44 +01:00
}
func sendArticleStatus(for account: Account, completion: @escaping ((Result<Void, Error>) -> Void)) {
sendArticleStatus(for: account, showProgress: false, completion: completion)
2020-03-18 21:48:44 +01:00
}
func refreshArticleStatus(for account: Account, completion: @escaping ((Result<Void, Error>) -> Void)) {
let op = CloudKitReceiveStatusOperation(articlesZone: articlesZone)
op.completionBlock = { mainThreadOperaion in
if mainThreadOperaion.isCanceled {
completion(.failure(CloudKitAccountDelegateError.unknown))
} else {
2020-04-01 18:46:37 +02:00
completion(.success(()))
}
}
mainThreadOperationQueue.add(op)
2020-03-18 21:48:44 +01:00
}
func importOPML(for account:Account, opmlFile: URL, completion: @escaping (Result<Void, Error>) -> Void) {
guard refreshProgress.isComplete else {
completion(.success(()))
return
}
2020-03-18 21:48:44 +01:00
var fileData: Data?
do {
fileData = try Data(contentsOf: opmlFile)
} catch {
completion(.failure(error))
return
}
guard let opmlData = fileData else {
completion(.success(()))
return
}
let parserData = ParserData(url: opmlFile.absoluteString, data: opmlData)
var opmlDocument: RSOPMLDocument?
do {
opmlDocument = try RSOPMLParser.parseOPML(with: parserData)
} catch {
completion(.failure(error))
return
}
guard let loadDocument = opmlDocument else {
completion(.success(()))
return
}
2020-04-01 01:10:35 +02:00
guard let opmlItems = loadDocument.children, let rootExternalID = account.externalID else {
2020-03-18 21:48:44 +01:00
return
}
2020-04-01 01:10:35 +02:00
let normalizedItems = OPMLNormalizer.normalize(opmlItems)
2020-03-18 21:48:44 +01:00
2020-05-02 21:39:28 +02:00
refreshProgress.addToNumberOfTasksAndRemaining(1)
self.accountZone.importOPML(rootExternalID: rootExternalID, items: normalizedItems) { _ in
2020-05-02 21:39:28 +02:00
self.refreshProgress.completeTask()
self.standardRefreshAll(for: account, completion: completion)
2020-04-05 00:35:09 +02:00
}
2020-03-18 21:48:44 +01:00
}
func createWebFeed(for account: Account, url urlString: String, name: String?, container: Container, completion: @escaping (Result<WebFeed, Error>) -> Void) {
guard let url = URL(string: urlString), let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
2020-03-18 21:48:44 +01:00
completion(.failure(LocalAccountDelegateError.invalidParameter))
return
}
let editedName = name == nil || name!.isEmpty ? nil : name
2020-03-29 10:43:20 +02:00
// Username should be part of the URL on new feed adds
if let feedProvider = FeedProviderManager.shared.best(for: urlComponents) {
createProviderWebFeed(for: account, urlComponents: urlComponents, editedName: editedName, container: container, feedProvider: feedProvider, completion: completion)
} else {
createRSSWebFeed(for: account, url: url, editedName: editedName, container: container, completion: completion)
2020-03-18 21:48:44 +01:00
}
2020-03-18 21:48:44 +01:00
}
func renameWebFeed(for account: Account, with feed: WebFeed, to name: String, completion: @escaping (Result<Void, Error>) -> Void) {
2020-03-30 00:53:11 +02:00
let editedName = name.isEmpty ? nil : name
refreshProgress.addToNumberOfTasksAndRemaining(1)
2020-03-30 00:53:11 +02:00
accountZone.renameWebFeed(feed, editedName: editedName) { result in
self.refreshProgress.completeTask()
2020-03-30 00:53:11 +02:00
switch result {
case .success:
feed.editedName = name
completion(.success(()))
case .failure(let error):
self.processAccountError(account, error)
2020-03-30 00:53:11 +02:00
completion(.failure(error))
}
}
2020-03-18 21:48:44 +01:00
}
func removeWebFeed(for account: Account, with feed: WebFeed, from container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
removeWebFeedFromCloud(for: account, with: feed, from: container) { result in
2020-03-29 15:52:59 +02:00
switch result {
case .success:
container.removeWebFeed(feed)
completion(.success(()))
2020-03-29 15:52:59 +02:00
case .failure(let error):
completion(.failure(error))
}
}
2020-03-18 21:48:44 +01:00
}
func moveWebFeed(for account: Account, with feed: WebFeed, from fromContainer: Container, to toContainer: Container, completion: @escaping (Result<Void, Error>) -> Void) {
refreshProgress.addToNumberOfTasksAndRemaining(1)
accountZone.moveWebFeed(feed, from: fromContainer, to: toContainer) { result in
self.refreshProgress.completeTask()
switch result {
case .success:
fromContainer.removeWebFeed(feed)
toContainer.addWebFeed(feed)
completion(.success(()))
case .failure(let error):
self.processAccountError(account, error)
completion(.failure(error))
}
}
2020-03-18 21:48:44 +01:00
}
func addWebFeed(for account: Account, with feed: WebFeed, to container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
refreshProgress.addToNumberOfTasksAndRemaining(1)
accountZone.addWebFeed(feed, to: container) { result in
self.refreshProgress.completeTask()
switch result {
case .success:
container.addWebFeed(feed)
completion(.success(()))
case .failure(let error):
self.processAccountError(account, error)
completion(.failure(error))
}
}
2020-03-18 21:48:44 +01:00
}
func restoreWebFeed(for account: Account, feed: WebFeed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
refreshProgress.addToNumberOfTasksAndRemaining(2)
accountZone.createWebFeed(url: feed.url, name: feed.name, editedName: feed.editedName, homePageURL: feed.homePageURL, container: container) { result in
self.refreshProgress.completeTask()
switch result {
case .success(let externalID):
feed.externalID = externalID
container.addWebFeed(feed)
account.fetchArticlesAsync(.webFeed(feed)) { result in
switch result {
case .success(let articles):
self.articlesZone.saveNewArticles(articles) { result in
self.refreshProgress.completeTask()
if case .failure(let error) = result {
os_log(.error, log: self.log, "Restore articles error: %@.", error.localizedDescription)
}
2020-04-27 09:18:49 +02:00
completion(.success(()))
}
case .failure(let error):
self.refreshProgress.clear()
completion(.failure(error))
}
}
case .failure(let error):
self.refreshProgress.clear()
self.processAccountError(account, error)
completion(.failure(error))
}
}
2020-03-18 21:48:44 +01:00
}
func createFolder(for account: Account, name: String, completion: @escaping (Result<Folder, Error>) -> Void) {
refreshProgress.addToNumberOfTasksAndRemaining(1)
2020-03-30 22:15:45 +02:00
accountZone.createFolder(name: name) { result in
self.refreshProgress.completeTask()
2020-03-30 22:15:45 +02:00
switch result {
case .success(let externalID):
if let folder = account.ensureFolder(with: name) {
folder.externalID = externalID
completion(.success(folder))
} else {
completion(.failure(FeedbinAccountDelegateError.invalidParameter))
}
case .failure(let error):
self.processAccountError(account, error)
2020-03-30 22:15:45 +02:00
completion(.failure(error))
}
2020-03-18 21:48:44 +01:00
}
}
func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result<Void, Error>) -> Void) {
refreshProgress.addToNumberOfTasksAndRemaining(1)
2020-03-30 22:15:45 +02:00
accountZone.renameFolder(folder, to: name) { result in
self.refreshProgress.completeTask()
2020-03-30 22:15:45 +02:00
switch result {
case .success:
folder.name = name
completion(.success(()))
case .failure(let error):
self.processAccountError(account, error)
2020-03-30 22:15:45 +02:00
completion(.failure(error))
}
}
2020-03-18 21:48:44 +01:00
}
func removeFolder(for account: Account, with folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
refreshProgress.addToNumberOfTasksAndRemaining(2)
accountZone.findWebFeedExternalIDs(for: folder) { result in
self.refreshProgress.completeTask()
2020-03-30 22:15:45 +02:00
switch result {
case .success(let webFeedExternalIDs):
let webFeeds = webFeedExternalIDs.compactMap { account.existingWebFeed(withExternalID: $0) }
let group = DispatchGroup()
var errorOccurred = false
for webFeed in webFeeds {
group.enter()
self.removeWebFeedFromCloud(for: account, with: webFeed, from: folder) { result in
group.leave()
if case .failure(let error) = result {
os_log(.error, log: self.log, "Remove folder, remove webfeed error: %@.", error.localizedDescription)
errorOccurred = true
}
}
}
group.notify(queue: DispatchQueue.global(qos: .background)) {
guard !errorOccurred else {
self.refreshProgress.completeTask()
completion(.failure(CloudKitAccountDelegateError.unknown))
return
}
self.accountZone.removeFolder(folder) { result in
self.refreshProgress.completeTask()
switch result {
case .success:
account.removeFolder(folder)
completion(.success(()))
case .failure(let error):
completion(.failure(error))
}
}
}
2020-03-30 22:15:45 +02:00
case .failure(let error):
self.refreshProgress.completeTask()
self.refreshProgress.completeTask()
self.processAccountError(account, error)
2020-03-30 22:15:45 +02:00
completion(.failure(error))
}
}
2020-03-18 21:48:44 +01:00
}
func restoreFolder(for account: Account, folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
2020-03-30 22:15:45 +02:00
guard let name = folder.name else {
completion(.failure(LocalAccountDelegateError.invalidParameter))
return
}
let feedsToRestore = folder.topLevelWebFeeds
refreshProgress.addToNumberOfTasksAndRemaining(1 + feedsToRestore.count)
2020-03-30 22:15:45 +02:00
accountZone.createFolder(name: name) { result in
self.refreshProgress.completeTask()
2020-03-30 22:15:45 +02:00
switch result {
case .success(let externalID):
folder.externalID = externalID
account.addFolder(folder)
let group = DispatchGroup()
for feed in feedsToRestore {
folder.topLevelWebFeeds.remove(feed)
group.enter()
self.restoreWebFeed(for: account, feed: feed, container: folder) { result in
self.refreshProgress.completeTask()
group.leave()
switch result {
case .success:
break
case .failure(let error):
os_log(.error, log: self.log, "Restore folder feed error: %@.", error.localizedDescription)
}
}
}
group.notify(queue: DispatchQueue.main) {
account.addFolder(folder)
completion(.success(()))
}
2020-03-30 22:15:45 +02:00
case .failure(let error):
self.processAccountError(account, error)
2020-03-30 22:15:45 +02:00
completion(.failure(error))
}
}
2020-03-18 21:48:44 +01:00
}
func markArticles(for account: Account, articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) -> Set<Article>? {
2020-04-01 03:56:34 +02:00
let syncStatuses = articles.map { article in
return SyncStatus(articleID: article.articleID, key: SyncStatus.Key(statusKey), flag: flag)
2020-04-01 03:56:34 +02:00
}
try? database.insertStatuses(syncStatuses)
let articles = try? account.update(articles, statusKey: statusKey, flag: flag)
2020-04-01 03:56:34 +02:00
database.selectPendingCount { result in
if let count = try? result.get(), count > 100 {
self.sendArticleStatus(for: account, showProgress: false) { _ in }
2020-04-01 03:56:34 +02:00
}
}
return articles
2020-03-18 21:48:44 +01:00
}
func accountDidInitialize(_ account: Account) {
self.account = account
2020-04-26 03:20:56 +02:00
accountZone.delegate = CloudKitAcountZoneDelegate(account: account, refreshProgress: refreshProgress, articlesZone: articlesZone)
articlesZone.delegate = CloudKitArticlesZoneDelegate(account: account, database: database, articlesZone: articlesZone)
2020-03-30 22:15:45 +02:00
database.resetAllSelectedForProcessing()
2020-04-04 20:33:49 +02:00
// Check to see if this is a new account and initialize anything we need
2020-03-30 22:15:45 +02:00
if account.externalID == nil {
accountZone.findOrCreateAccount() { result in
switch result {
case .success(let externalID):
account.externalID = externalID
2020-04-13 11:59:41 +02:00
self.initialRefreshAll(for: account) { _ in }
2020-03-30 22:15:45 +02:00
case .failure(let error):
os_log(.error, log: self.log, "Error adding account container: %@", error.localizedDescription)
}
}
accountZone.subscribeToZoneChanges()
articlesZone.subscribeToZoneChanges()
2020-03-30 22:15:45 +02:00
}
2020-03-18 21:48:44 +01:00
}
func accountWillBeDeleted(_ account: Account) {
accountZone.resetChangeToken()
articlesZone.resetChangeToken()
2020-03-18 21:48:44 +01:00
}
static func validateCredentials(transport: Transport, credentials: Credentials, endpoint: URL? = nil, completion: (Result<Credentials?, Error>) -> Void) {
return completion(.success(nil))
}
// MARK: Suspend and Resume (for iOS)
func suspendNetwork() {
refresher.suspend()
}
func suspendDatabase() {
2020-04-01 03:56:34 +02:00
database.suspend()
2020-03-18 21:48:44 +01:00
}
func resume() {
refresher.resume()
2020-04-01 03:56:34 +02:00
database.resume()
2020-03-18 21:48:44 +01:00
}
}
private extension CloudKitAccountDelegate {
2020-04-13 11:59:41 +02:00
func initialRefreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
2020-04-13 11:59:41 +02:00
func fail(_ error: Error) {
self.processAccountError(account, error)
self.refreshProgress.clear()
completion(.failure(error))
}
2020-04-29 18:07:24 +02:00
refreshProgress.addToNumberOfTasksAndRemaining(3)
accountZone.fetchChangesInZone() { result in
self.refreshProgress.completeTask()
let webFeeds = account.flattenedWebFeeds()
self.refreshProgress.addToNumberOfTasksAndRemaining(webFeeds.count)
2020-04-13 11:59:41 +02:00
switch result {
case .success:
self.refreshArticleStatus(for: account) { result in
self.refreshProgress.completeTask()
2020-04-13 11:59:41 +02:00
switch result {
case .success:
self.combinedRefresh(account, webFeeds) {
self.refreshProgress.clear()
account.metadata.lastArticleFetchEndTime = Date()
}
2020-04-13 11:59:41 +02:00
case .failure(let error):
fail(error)
}
}
case .failure(let error):
fail(error)
}
}
}
func standardRefreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
let intialWebFeedsCount = account.flattenedWebFeeds().count
refreshProgress.addToNumberOfTasksAndRemaining(3 + intialWebFeedsCount)
func fail(_ error: Error) {
self.processAccountError(account, error)
self.refreshProgress.clear()
completion(.failure(error))
}
accountZone.fetchChangesInZone() { result in
switch result {
case .success:
self.refreshProgress.completeTask()
let webFeeds = account.flattenedWebFeeds()
2020-04-13 11:59:41 +02:00
self.refreshProgress.addToNumberOfTasksAndRemaining(webFeeds.count - intialWebFeedsCount)
self.refreshArticleStatus(for: account) { result in
switch result {
case .success:
self.refreshProgress.completeTask()
self.combinedRefresh(account, webFeeds) {
self.sendArticleStatus(for: account, showProgress: true) { _ in
self.refreshProgress.clear()
account.metadata.lastArticleFetchEndTime = Date()
}
}
case .failure(let error):
fail(error)
}
}
case .failure(let error):
fail(error)
}
}
}
2020-04-26 03:20:56 +02:00
func combinedRefresh(_ account: Account, _ webFeeds: Set<WebFeed>, completion: @escaping () -> Void) {
var refresherWebFeeds = Set<WebFeed>()
let group = DispatchGroup()
2020-04-26 03:20:56 +02:00
for webFeed in webFeeds {
if let components = URLComponents(string: webFeed.url), let feedProvider = FeedProviderManager.shared.best(for: components) {
group.enter()
feedProvider.refresh(webFeed) { result in
switch result {
case .success(let parsedItems):
account.update(webFeed.webFeedID, with: parsedItems) { result in
switch result {
case .success(let articleChanges):
self.storeArticleChanges(new: articleChanges.newArticles, updated: articleChanges.updatedArticles, deleted: articleChanges.deletedArticles) {
self.refreshProgress.completeTask()
group.leave()
}
2020-04-26 03:20:56 +02:00
case .failure(let error):
os_log(.error, log: self.log, "CloudKit Feed refresh update error: %@.", error.localizedDescription)
self.refreshProgress.completeTask()
group.leave()
}
}
case .failure(let error):
os_log(.error, log: self.log, "CloudKit Feed refresh error: %@.", error.localizedDescription)
self.refreshProgress.completeTask()
group.leave()
}
}
} else {
refresherWebFeeds.insert(webFeed)
}
}
group.enter()
refresher.refreshFeeds(refresherWebFeeds) {
group.leave()
2020-04-26 03:20:56 +02:00
}
group.notify(queue: DispatchQueue.main) {
completion()
2020-04-26 03:20:56 +02:00
}
}
func createProviderWebFeed(for account: Account, urlComponents: URLComponents, editedName: String?, container: Container, feedProvider: FeedProvider, completion: @escaping (Result<WebFeed, Error>) -> Void) {
refreshProgress.addToNumberOfTasksAndRemaining(6)
feedProvider.metaData(urlComponents) { result in
self.refreshProgress.completeTask()
switch result {
case .success(let metaData):
guard let urlString = urlComponents.url?.absoluteString else {
completion(.failure(AccountError.createErrorNotFound))
return
}
self.accountZone.createWebFeed(url: urlString, name: metaData.name, editedName: editedName, homePageURL: metaData.homePageURL, container: container) { result in
self.refreshProgress.completeTask()
switch result {
case .success(let externalID):
let feed = account.createWebFeed(with: metaData.name, url: urlString, webFeedID: urlString, homePageURL: metaData.homePageURL)
feed.editedName = editedName
feed.externalID = externalID
container.addWebFeed(feed)
feedProvider.refresh(feed) { result in
self.refreshProgress.completeTask()
switch result {
case .success(let parsedItems):
account.update(urlString, with: parsedItems) { result in
switch result {
case .success:
self.sendNewArticlesToTheCloud(account, feed, completion: completion)
case .failure(let error):
self.refreshProgress.clear()
completion(.failure(error))
}
}
case .failure:
self.refreshProgress.clear()
completion(.failure(AccountError.createErrorNotFound))
}
}
case .failure(let error):
self.refreshProgress.clear()
completion(.failure(error))
}
}
case .failure:
self.refreshProgress.clear()
completion(.failure(AccountError.createErrorNotFound))
}
}
}
func createRSSWebFeed(for account: Account, url: URL, editedName: String?, container: Container, completion: @escaping (Result<WebFeed, Error>) -> Void) {
BatchUpdate.shared.start()
refreshProgress.addToNumberOfTasksAndRemaining(6)
FeedFinder.find(url: url) { result in
self.refreshProgress.completeTask()
switch result {
case .success(let feedSpecifiers):
guard let bestFeedSpecifier = FeedSpecifier.bestFeed(in: feedSpecifiers), let url = URL(string: bestFeedSpecifier.urlString) else {
BatchUpdate.shared.end()
self.refreshProgress.clear()
completion(.failure(AccountError.createErrorNotFound))
return
}
if account.hasWebFeed(withURL: bestFeedSpecifier.urlString) {
BatchUpdate.shared.end()
self.refreshProgress.clear()
completion(.failure(AccountError.createErrorAlreadySubscribed))
return
}
let feed = account.createWebFeed(with: nil, url: url.absoluteString, webFeedID: url.absoluteString, homePageURL: nil)
feed.editedName = editedName
container.addWebFeed(feed)
InitialFeedDownloader.download(url) { parsedFeed in
self.refreshProgress.completeTask()
if let parsedFeed = parsedFeed {
account.update(feed, with: parsedFeed) { result in
switch result {
case .success:
BatchUpdate.shared.end()
self.accountZone.createWebFeed(url: bestFeedSpecifier.urlString,
name: parsedFeed.title,
editedName: editedName,
homePageURL: parsedFeed.homePageURL,
container: container) { result in
self.refreshProgress.completeTask()
switch result {
case .success(let externalID):
feed.externalID = externalID
self.sendNewArticlesToTheCloud(account, feed, completion: completion)
case .failure(let error):
self.refreshProgress.clear()
completion(.failure(error))
}
}
case .failure(let error):
BatchUpdate.shared.end()
self.refreshProgress.clear()
completion(.failure(error))
}
}
} else {
self.refreshProgress.clear()
completion(.success(feed))
}
}
case .failure:
BatchUpdate.shared.end()
self.refreshProgress.clear()
completion(.failure(AccountError.createErrorNotFound))
}
}
}
func sendNewArticlesToTheCloud(_ account: Account, _ feed: WebFeed, completion: @escaping (Result<WebFeed, Error>) -> Void) {
account.fetchArticlesAsync(.webFeed(feed)) { result in
switch result {
case .success(let articles):
self.storeArticleChanges(new: articles, updated: Set<Article>(), deleted: Set<Article>()) {
self.sendArticleStatus(for: account, showProgress: true) { result in
switch result {
case .success:
self.articlesZone.fetchChangesInZone() { _ in
self.refreshProgress.clear()
completion(.success(feed))
}
case .failure(let error):
self.refreshProgress.clear()
completion(.failure(error))
}
}
}
case .failure(let error):
self.refreshProgress.clear()
completion(.failure(error))
}
}
}
func processAccountError(_ account: Account, _ error: Error) {
if case CloudKitZoneError.userDeletedZone = error {
account.removeFeeds(account.topLevelWebFeeds)
for folder in account.folders ?? Set<Folder>() {
account.removeFolder(folder)
}
}
}
func storeArticleChanges(new: Set<Article>?, updated: Set<Article>?, deleted: Set<Article>?, completion: @escaping () -> Void) {
// New records with a read status aren't really new, they just didn't have the read article stored
let group = DispatchGroup()
if let new = new {
let filteredNew = new.filter { $0.status.read == false }
group.enter()
insertSyncStatuses(articles: filteredNew, statusKey: .new, flag: true) {
group.leave()
}
}
group.enter()
insertSyncStatuses(articles: updated, statusKey: .new, flag: false) {
group.leave()
}
group.enter()
insertSyncStatuses(articles: deleted, statusKey: .deleted, flag: true) {
group.leave()
}
group.notify(queue: DispatchQueue.global(qos: .userInitiated)) {
DispatchQueue.main.async {
completion()
}
}
}
func insertSyncStatuses(articles: Set<Article>?, statusKey: SyncStatus.Key, flag: Bool, completion: @escaping () -> Void) {
guard let articles = articles, !articles.isEmpty else {
completion()
return
}
let syncStatuses = articles.map { article in
return SyncStatus(articleID: article.articleID, key: statusKey, flag: flag)
}
database.insertStatuses(syncStatuses) { _ in
completion()
}
}
func sendArticleStatus(for account: Account, showProgress: Bool, completion: @escaping ((Result<Void, Error>) -> Void)) {
let op = CloudKitSendStatusOperation(account: account,
articlesZone: articlesZone,
refreshProgress: refreshProgress,
showProgress: showProgress,
database: database)
op.completionBlock = { mainThreadOperaion in
if mainThreadOperaion.isCanceled {
completion(.failure(CloudKitAccountDelegateError.unknown))
} else {
completion(.success(()))
}
}
mainThreadOperationQueue.add(op)
}
func removeWebFeedFromCloud(for account: Account, with feed: WebFeed, from container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
refreshProgress.addToNumberOfTasksAndRemaining(2)
accountZone.removeWebFeed(feed, from: container) { result in
self.refreshProgress.completeTask()
switch result {
case .success:
guard let webFeedExternalID = feed.externalID else {
completion(.success(()))
return
}
self.articlesZone.deleteArticles(webFeedExternalID) { result in
self.refreshProgress.completeTask()
completion(result)
}
case .failure(let error):
self.refreshProgress.completeTask()
self.processAccountError(account, error)
completion(.failure(error))
}
}
}
}
extension CloudKitAccountDelegate: LocalAccountRefresherDelegate {
func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed) {
refreshProgress.completeTask()
}
func localAccountRefresher(_ refresher: LocalAccountRefresher, articleChanges: ArticleChanges, completion: @escaping () -> Void) {
self.storeArticleChanges(new: articleChanges.newArticles,
updated: articleChanges.updatedArticles,
deleted: articleChanges.deletedArticles,
completion: completion)
}
}