2019-10-18 23:21:02 +02:00
|
|
|
//
|
|
|
|
// FeedlySyncAllOperation.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Kiel Gillard on 19/9/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import os.log
|
|
|
|
import SyncDatabase
|
2019-11-25 08:19:33 +01:00
|
|
|
import RSWeb
|
2020-01-16 06:30:37 +01:00
|
|
|
import RSCore
|
2019-10-18 23:21:02 +02:00
|
|
|
|
2020-01-19 23:19:06 +01:00
|
|
|
/// Compose the operations necessary to get the entire set of articles, feeds and folders with the statuses the user expects between now and a certain date in the past.
|
2019-10-18 23:21:02 +02:00
|
|
|
final class FeedlySyncAllOperation: FeedlyOperation {
|
2020-01-19 23:19:06 +01:00
|
|
|
|
2020-01-16 06:30:37 +01:00
|
|
|
private let operationQueue = MainThreadOperationQueue()
|
2019-10-18 23:21:02 +02:00
|
|
|
private let log: OSLog
|
|
|
|
let syncUUID: UUID
|
|
|
|
|
|
|
|
var syncCompletionHandler: ((Result<Void, Error>) -> ())?
|
|
|
|
|
2020-01-09 06:24:47 +01:00
|
|
|
/// These requests to Feedly determine which articles to download:
|
|
|
|
/// 1. The set of all article ids we might need or show.
|
|
|
|
/// 2. The set of all unread article ids we might need or show (a subset of 1).
|
|
|
|
/// 3. The set of all article ids changed since the last sync (a subset of 1).
|
|
|
|
/// 4. The set of all starred article ids.
|
|
|
|
///
|
|
|
|
/// On the response for 1, create statuses for each article id.
|
|
|
|
/// On the response for 2, create unread statuses for each article id and mark as read those no longer in the response.
|
|
|
|
/// On the response for 4, create starred statuses for each article id and mark as unstarred those no longer in the response.
|
|
|
|
///
|
|
|
|
/// Download articles for statuses at the union of those statuses without its corresponding article and those included in 3 (changed since last successful sync).
|
|
|
|
///
|
|
|
|
init(account: Account, credentials: Credentials, lastSuccessfulFetchStartDate: Date?, markArticlesService: FeedlyMarkArticlesService, getUnreadService: FeedlyGetStreamIdsService, getCollectionsService: FeedlyGetCollectionsService, getStreamContentsService: FeedlyGetStreamContentsService, getStarredService: FeedlyGetStreamIdsService, getStreamIdsService: FeedlyGetStreamIdsService, getEntriesService: FeedlyGetEntriesService, database: SyncDatabase, downloadProgress: DownloadProgress, log: OSLog) {
|
2019-10-18 23:21:02 +02:00
|
|
|
self.syncUUID = UUID()
|
|
|
|
self.log = log
|
2020-01-16 06:30:37 +01:00
|
|
|
self.operationQueue.suspend()
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
super.init()
|
|
|
|
|
2019-11-25 08:19:33 +01:00
|
|
|
self.downloadProgress = downloadProgress
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
// Send any read/unread/starred article statuses to Feedly before anything else.
|
|
|
|
let sendArticleStatuses = FeedlySendArticleStatusesOperation(database: database, service: markArticlesService, log: log)
|
|
|
|
sendArticleStatuses.delegate = self
|
2019-11-25 08:19:33 +01:00
|
|
|
sendArticleStatuses.downloadProgress = downloadProgress
|
2020-02-06 06:18:29 +01:00
|
|
|
self.operationQueue.add(sendArticleStatuses)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
// Get all the Collections the user has.
|
|
|
|
let getCollections = FeedlyGetCollectionsOperation(service: getCollectionsService, log: log)
|
|
|
|
getCollections.delegate = self
|
2019-11-25 08:19:33 +01:00
|
|
|
getCollections.downloadProgress = downloadProgress
|
2020-01-19 23:19:06 +01:00
|
|
|
getCollections.addDependency(sendArticleStatuses)
|
2020-02-06 06:18:29 +01:00
|
|
|
self.operationQueue.add(getCollections)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
// Ensure a folder exists for each Collection, removing Folders without a corresponding Collection.
|
|
|
|
let mirrorCollectionsAsFolders = FeedlyMirrorCollectionsAsFoldersOperation(account: account, collectionsProvider: getCollections, log: log)
|
|
|
|
mirrorCollectionsAsFolders.delegate = self
|
2020-01-19 23:19:06 +01:00
|
|
|
mirrorCollectionsAsFolders.addDependency(getCollections)
|
2020-02-06 06:18:29 +01:00
|
|
|
self.operationQueue.add(mirrorCollectionsAsFolders)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
// Ensure feeds are created and grouped by their folders.
|
|
|
|
let createFeedsOperation = FeedlyCreateFeedsForCollectionFoldersOperation(account: account, feedsAndFoldersProvider: mirrorCollectionsAsFolders, log: log)
|
|
|
|
createFeedsOperation.delegate = self
|
2020-01-19 23:19:06 +01:00
|
|
|
createFeedsOperation.addDependency(mirrorCollectionsAsFolders)
|
2020-02-06 06:18:29 +01:00
|
|
|
self.operationQueue.add(createFeedsOperation)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
2020-01-09 06:24:47 +01:00
|
|
|
let getAllArticleIds = FeedlyIngestStreamArticleIdsOperation(account: account, credentials: credentials, service: getStreamIdsService, log: log)
|
|
|
|
getAllArticleIds.delegate = self
|
|
|
|
getAllArticleIds.downloadProgress = downloadProgress
|
2020-01-19 23:19:06 +01:00
|
|
|
getAllArticleIds.addDependency(createFeedsOperation)
|
2020-02-06 06:18:29 +01:00
|
|
|
self.operationQueue.add(getAllArticleIds)
|
2020-01-09 06:24:47 +01:00
|
|
|
|
2019-12-09 08:53:36 +01:00
|
|
|
// Get each page of unread article ids in the global.all stream for the last 31 days (nil = Feedly API default).
|
2020-01-13 07:51:16 +01:00
|
|
|
let getUnread = FeedlyIngestUnreadArticleIdsOperation(account: account, credentials: credentials, service: getUnreadService, database: database, newerThan: nil, log: log)
|
2019-12-09 08:53:36 +01:00
|
|
|
getUnread.delegate = self
|
2020-01-19 23:19:06 +01:00
|
|
|
getUnread.addDependency(getAllArticleIds)
|
2019-12-09 08:53:36 +01:00
|
|
|
getUnread.downloadProgress = downloadProgress
|
2020-02-06 06:18:29 +01:00
|
|
|
self.operationQueue.add(getUnread)
|
2019-12-09 08:53:36 +01:00
|
|
|
|
2020-01-09 06:24:47 +01:00
|
|
|
// Get each page of the article ids which have been update since the last successful fetch start date.
|
|
|
|
// If the date is nil, this operation provides an empty set (everything is new, nothing is updated).
|
|
|
|
let getUpdated = FeedlyGetUpdatedArticleIdsOperation(account: account, credentials: credentials, service: getStreamIdsService, newerThan: lastSuccessfulFetchStartDate, log: log)
|
|
|
|
getUpdated.delegate = self
|
|
|
|
getUpdated.downloadProgress = downloadProgress
|
2020-01-19 23:19:06 +01:00
|
|
|
getUpdated.addDependency(createFeedsOperation)
|
2020-02-06 06:18:29 +01:00
|
|
|
self.operationQueue.add(getUpdated)
|
2020-01-09 06:24:47 +01:00
|
|
|
|
|
|
|
// Get each page of the article ids for starred articles.
|
2020-01-13 07:51:16 +01:00
|
|
|
let getStarred = FeedlyIngestStarredArticleIdsOperation(account: account, credentials: credentials, service: getStarredService, database: database, newerThan: nil, log: log)
|
2020-01-09 06:24:47 +01:00
|
|
|
getStarred.delegate = self
|
|
|
|
getStarred.downloadProgress = downloadProgress
|
2020-01-19 23:19:06 +01:00
|
|
|
getStarred.addDependency(createFeedsOperation)
|
2020-02-06 06:18:29 +01:00
|
|
|
self.operationQueue.add(getStarred)
|
2020-01-09 06:24:47 +01:00
|
|
|
|
|
|
|
// Now all the possible article ids we need have a status, fetch the article ids for missing articles.
|
|
|
|
let getMissingIds = FeedlyFetchIdsForMissingArticlesOperation(account: account, log: log)
|
|
|
|
getMissingIds.delegate = self
|
|
|
|
getMissingIds.downloadProgress = downloadProgress
|
2020-01-19 23:19:06 +01:00
|
|
|
getMissingIds.addDependency(getAllArticleIds)
|
|
|
|
getMissingIds.addDependency(getUnread)
|
|
|
|
getMissingIds.addDependency(getStarred)
|
|
|
|
getMissingIds.addDependency(getUpdated)
|
2020-02-06 06:18:29 +01:00
|
|
|
self.operationQueue.add(getMissingIds)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
2020-01-09 06:24:47 +01:00
|
|
|
// Download all the missing and updated articles
|
|
|
|
let downloadMissingArticles = FeedlyDownloadArticlesOperation(account: account,
|
|
|
|
missingArticleEntryIdProvider: getMissingIds,
|
|
|
|
updatedArticleEntryIdProvider: getUpdated,
|
|
|
|
getEntriesService: getEntriesService,
|
|
|
|
log: log)
|
|
|
|
downloadMissingArticles.delegate = self
|
|
|
|
downloadMissingArticles.downloadProgress = downloadProgress
|
2020-01-19 23:19:06 +01:00
|
|
|
downloadMissingArticles.addDependency(getMissingIds)
|
|
|
|
downloadMissingArticles.addDependency(getUpdated)
|
2020-02-06 06:18:29 +01:00
|
|
|
self.operationQueue.add(downloadMissingArticles)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
// Once this operation's dependencies, their dependencies etc finish, we can finish.
|
|
|
|
let finishOperation = FeedlyCheckpointOperation()
|
|
|
|
finishOperation.checkpointDelegate = self
|
2019-11-25 08:19:33 +01:00
|
|
|
finishOperation.downloadProgress = downloadProgress
|
2020-01-19 23:19:06 +01:00
|
|
|
finishOperation.addDependency(downloadMissingArticles)
|
2020-02-06 06:18:29 +01:00
|
|
|
self.operationQueue.add(finishOperation)
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 08:19:33 +01:00
|
|
|
convenience init(account: Account, credentials: Credentials, caller: FeedlyAPICaller, database: SyncDatabase, lastSuccessfulFetchStartDate: Date?, downloadProgress: DownloadProgress, log: OSLog) {
|
2020-01-09 06:24:47 +01:00
|
|
|
self.init(account: account, credentials: credentials, lastSuccessfulFetchStartDate: lastSuccessfulFetchStartDate, markArticlesService: caller, getUnreadService: caller, getCollectionsService: caller, getStreamContentsService: caller, getStarredService: caller, getStreamIdsService: caller, getEntriesService: caller, database: database, downloadProgress: downloadProgress, log: log)
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
|
2020-01-16 06:30:37 +01:00
|
|
|
override func run() {
|
2019-10-18 23:21:02 +02:00
|
|
|
os_log(.debug, log: log, "Starting sync %{public}@", syncUUID.uuidString)
|
2020-01-16 06:30:37 +01:00
|
|
|
operationQueue.resume()
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
2020-01-19 23:19:06 +01:00
|
|
|
|
|
|
|
override func didCancel() {
|
|
|
|
os_log(.debug, log: log, "Cancelling sync %{public}@", syncUUID.uuidString)
|
|
|
|
self.operationQueue.cancelAllOperations()
|
|
|
|
syncCompletionHandler = nil
|
2020-01-20 01:55:39 +01:00
|
|
|
super.didCancel()
|
2020-01-19 23:19:06 +01:00
|
|
|
}
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension FeedlySyncAllOperation: FeedlyCheckpointOperationDelegate {
|
|
|
|
|
|
|
|
func feedlyCheckpointOperationDidReachCheckpoint(_ operation: FeedlyCheckpointOperation) {
|
|
|
|
assert(Thread.isMainThread)
|
|
|
|
os_log(.debug, log: self.log, "Sync completed: %{public}@", syncUUID.uuidString)
|
|
|
|
|
|
|
|
syncCompletionHandler?(.success(()))
|
|
|
|
syncCompletionHandler = nil
|
|
|
|
|
|
|
|
didFinish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension FeedlySyncAllOperation: FeedlyOperationDelegate {
|
|
|
|
|
|
|
|
func feedlyOperation(_ operation: FeedlyOperation, didFailWith error: Error) {
|
|
|
|
assert(Thread.isMainThread)
|
2020-01-21 08:31:35 +01:00
|
|
|
|
|
|
|
// Having this log is useful for debugging missing required JSON keys in the response from Feedly, for example.
|
|
|
|
os_log(.debug, log: log, "%{public}@ failed with error: %{public}@.", String(describing: operation), error as NSError)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
syncCompletionHandler?(.failure(error))
|
|
|
|
syncCompletionHandler = nil
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}
|