NetNewsWire/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift

533 lines
16 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 os.log
import SyncDatabase
2020-03-18 21:48:44 +01:00
import RSCore
import RSParser
import Articles
import RSWeb
public enum CloudKitAccountDelegateError: String, Error {
case invalidParameter = "An invalid parameter was used."
}
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")
}()
2020-04-01 18:46:37 +02:00
private lazy var zones: [CloudKitZone] = [accountZone, articlesZone]
private let accountZone: CloudKitAccountZone
2020-04-01 18:46:37 +02:00
private let articlesZone: CloudKitArticlesZone
private let refresher = LocalAccountRefresher()
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)
2020-03-18 21:48:44 +01:00
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) {
os_log(.debug, log: log, "Processing remote notification...")
let group = DispatchGroup()
zones.forEach { zone in
group.enter()
zone.receiveRemoteNotification(userInfo: userInfo) {
group.leave()
}
}
group.notify(queue: DispatchQueue.main) {
os_log(.debug, log: self.log, "Done processing remote notification...")
completion()
}
}
2020-03-18 21:48:44 +01:00
func refreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
refreshAll(for: account, downloadFeeds: true, completion: completion)
2020-03-18 21:48:44 +01:00
}
func sendArticleStatus(for account: Account, completion: @escaping ((Result<Void, Error>) -> Void)) {
2020-04-01 18:46:37 +02:00
os_log(.debug, log: log, "Sending article statuses...")
database.selectForProcessing { result in
func processStatuses(_ syncStatuses: [SyncStatus]) {
2020-04-01 21:10:07 +02:00
guard syncStatuses.count > 0 else {
completion(.success(()))
return
}
2020-04-03 01:06:47 +02:00
let starredArticleIDs = syncStatuses.filter({ $0.key == .starred && $0.flag == true }).map({ $0.articleID })
account.fetchArticlesAsync(.articleIDs(Set(starredArticleIDs))) { result in
func processWithArticles(_ starredArticles: Set<Article>) {
self.articlesZone.sendArticleStatus(syncStatuses, starredArticles: starredArticles) { result in
switch result {
case .success:
self.database.deleteSelectedForProcessing(syncStatuses.map({ $0.articleID }) )
os_log(.debug, log: self.log, "Done sending article statuses.")
completion(.success(()))
case .failure(let error):
self.database.resetSelectedForProcessing(syncStatuses.map({ $0.articleID }) )
completion(.failure(error))
}
}
}
2020-04-01 18:46:37 +02:00
switch result {
2020-04-03 01:06:47 +02:00
case .success(let starredArticles):
processWithArticles(starredArticles)
case .failure(let databaseError):
completion(.failure(databaseError))
2020-04-01 18:46:37 +02:00
}
2020-04-03 01:06:47 +02:00
2020-04-01 18:46:37 +02:00
}
2020-04-03 01:06:47 +02:00
2020-04-01 18:46:37 +02:00
}
switch result {
case .success(let syncStatuses):
processStatuses(syncStatuses)
case .failure(let databaseError):
completion(.failure(databaseError))
}
}
2020-03-18 21:48:44 +01:00
}
2020-04-01 18:46:37 +02:00
2020-03-18 21:48:44 +01:00
func refreshArticleStatus(for account: Account, completion: @escaping ((Result<Void, Error>) -> Void)) {
2020-04-01 18:46:37 +02:00
os_log(.debug, log: log, "Refreshing article statuses...")
articlesZone.fetchChangesInZone() { result in
os_log(.debug, log: self.log, "Done refreshing article statuses.")
switch result {
case .success:
completion(.success(()))
case .failure(let error):
completion(.failure(error))
}
}
2020-03-18 21:48:44 +01:00
}
func importOPML(for account:Account, opmlFile: URL, completion: @escaping (Result<Void, Error>) -> Void) {
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-04-01 01:10:35 +02:00
accountZone.importOPML(rootExternalID: rootExternalID, items: normalizedItems, completion: completion)
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) else {
completion(.failure(LocalAccountDelegateError.invalidParameter))
return
}
refreshProgress.addToNumberOfTasksAndRemaining(3)
2020-03-18 21:48:44 +01:00
FeedFinder.find(url: url) { result in
self.refreshProgress.completeTask()
2020-03-18 21:48:44 +01:00
switch result {
case .success(let feedSpecifiers):
guard let bestFeedSpecifier = FeedSpecifier.bestFeed(in: feedSpecifiers), let url = URL(string: bestFeedSpecifier.urlString) else {
self.refreshProgress.completeTask()
completion(.failure(AccountError.createErrorNotFound))
return
2020-03-18 21:48:44 +01:00
}
if account.hasWebFeed(withURL: bestFeedSpecifier.urlString) {
self.refreshProgress.completeTask()
completion(.failure(AccountError.createErrorAlreadySubscribed))
return
}
2020-03-31 04:11:57 +02:00
self.accountZone.createWebFeed(url: bestFeedSpecifier.urlString, editedName: name, container: container) { result in
self.refreshProgress.completeTask()
2020-03-29 10:43:20 +02:00
switch result {
case .success(let externalID):
2020-03-29 10:43:20 +02:00
let feed = account.createWebFeed(with: nil, url: url.absoluteString, webFeedID: url.absoluteString, homePageURL: nil)
2020-04-01 01:10:35 +02:00
feed.editedName = name
feed.externalID = externalID
container.addWebFeed(feed)
2020-03-29 10:43:20 +02:00
InitialFeedDownloader.download(url) { parsedFeed in
self.refreshProgress.completeTask()
2020-03-18 21:48:44 +01:00
2020-03-29 10:43:20 +02:00
if let parsedFeed = parsedFeed {
account.update(feed, with: parsedFeed, {_ in
completion(.success(feed))
})
}
}
case .failure(let error):
completion(.failure(error))
2020-03-18 21:48:44 +01:00
}
}
2020-03-29 10:43:20 +02:00
2020-03-18 21:48:44 +01:00
case .failure:
self.refreshProgress.clear()
2020-03-18 21:48:44 +01:00
completion(.failure(AccountError.createErrorNotFound))
}
}
}
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):
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) {
refreshProgress.addToNumberOfTasksAndRemaining(1)
accountZone.removeWebFeed(feed, from: container) { result in
self.refreshProgress.completeTask()
2020-03-29 15:52:59 +02:00
switch result {
case .success:
container.removeWebFeed(feed)
completion(.success(()))
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):
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):
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(1)
accountZone.createWebFeed(url: feed.url, editedName: feed.editedName, container: container) { result in
self.refreshProgress.completeTask()
switch result {
case .success(let externalID):
feed.externalID = externalID
container.addWebFeed(feed)
completion(.success(()))
case .failure(let 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):
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):
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(1)
2020-03-30 22:15:45 +02:00
accountZone.removeFolder(folder) { result in
self.refreshProgress.completeTask()
2020-03-30 22:15:45 +02:00
switch result {
case .success:
account.removeFolder(folder)
completion(.success(()))
case .failure(let error):
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):
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: statusKey, flag: flag)
}
database.insertStatuses(syncStatuses)
database.selectPendingCount { result in
if let count = try? result.get(), count > 100 {
self.sendArticleStatus(for: account) { _ in }
}
}
2020-03-18 21:48:44 +01:00
return try? account.update(articles, statusKey: statusKey, flag: flag)
}
func accountDidInitialize(_ account: Account) {
accountZone.delegate = CloudKitAcountZoneDelegate(account: account, refreshProgress: refreshProgress)
articlesZone.delegate = CloudKitArticlesZoneDelegate(account: account, database: database, articlesZone: articlesZone)
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
self.refreshAll(for: account, downloadFeeds: false) { result in
if case .failure(let error) = result {
os_log(.error, log: self.log, "Error while doing intial refresh: %@", error.localizedDescription)
}
}
2020-03-30 22:15:45 +02:00
case .failure(let error):
os_log(.error, log: self.log, "Error adding account container: %@", error.localizedDescription)
}
}
zones.forEach { zone in
zone.subscribe()
}
2020-03-30 22:15:45 +02:00
}
2020-03-18 21:48:44 +01:00
}
func accountWillBeDeleted(_ account: Account) {
zones.forEach { zone in
zone.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 {
func refreshAll(for account: Account, downloadFeeds: Bool, completion: @escaping (Result<Void, Error>) -> Void) {
let intialWebFeedsCount = downloadFeeds ? account.flattenedWebFeeds().count : 0
refreshProgress.addToNumberOfTasksAndRemaining(3 + intialWebFeedsCount)
BatchUpdate.shared.start()
accountZone.fetchChangesInZone() { result in
BatchUpdate.shared.end()
switch result {
case .success:
let webFeeds = account.flattenedWebFeeds()
if downloadFeeds {
self.refreshProgress.addToNumberOfTasksAndRemaining(webFeeds.count - intialWebFeedsCount)
}
self.refreshProgress.completeTask()
self.sendArticleStatus(for: account) { result in
switch result {
case .success:
self.refreshProgress.completeTask()
self.refreshArticleStatus(for: account) { result in
switch result {
case .success:
self.refreshProgress.completeTask()
guard downloadFeeds else {
completion(.success(()))
return
}
self.refresher.refreshFeeds(webFeeds, feedCompletionBlock: { _ in self.refreshProgress.completeTask() }) {
account.metadata.lastArticleFetchEndTime = Date()
self.refreshProgress.clear()
completion(.success(()))
}
case .failure(let error):
completion(.failure(error))
}
}
case .failure(let error):
completion(.failure(error))
}
}
case .failure(let error):
self.refreshProgress.clear()
completion(.failure(error))
}
}
}
}