2019-10-18 23:21:02 +02:00
|
|
|
//
|
|
|
|
// FeedlySyncStreamContentsOperation.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Kiel Gillard on 17/10/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import os.log
|
|
|
|
import RSParser
|
2020-01-16 06:30:37 +01:00
|
|
|
import RSCore
|
2020-01-19 23:19:06 +01:00
|
|
|
import RSWeb
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
final class FeedlySyncStreamContentsOperation: FeedlyOperation, FeedlyOperationDelegate, FeedlyGetStreamContentsOperationDelegate, FeedlyCheckpointOperationDelegate {
|
2020-01-19 23:19:06 +01:00
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
private let account: Account
|
|
|
|
private let resource: FeedlyResourceId
|
2020-01-16 06:30:37 +01:00
|
|
|
private let operationQueue = MainThreadOperationQueue()
|
2019-10-18 23:21:02 +02:00
|
|
|
private let service: FeedlyGetStreamContentsService
|
|
|
|
private let newerThan: Date?
|
2020-01-17 07:16:52 +01:00
|
|
|
private let isPagingEnabled: Bool
|
2019-10-18 23:21:02 +02:00
|
|
|
private let log: OSLog
|
|
|
|
private let finishOperation: FeedlyCheckpointOperation
|
|
|
|
|
2020-01-17 07:16:52 +01:00
|
|
|
init(account: Account, resource: FeedlyResourceId, service: FeedlyGetStreamContentsService, isPagingEnabled: Bool, newerThan: Date?, log: OSLog) {
|
2019-10-18 23:21:02 +02:00
|
|
|
self.account = account
|
|
|
|
self.resource = resource
|
|
|
|
self.service = service
|
2020-01-17 07:16:52 +01:00
|
|
|
self.isPagingEnabled = isPagingEnabled
|
2020-01-16 06:30:37 +01:00
|
|
|
self.operationQueue.suspend()
|
2019-10-18 23:21:02 +02:00
|
|
|
self.newerThan = newerThan
|
|
|
|
self.log = log
|
|
|
|
self.finishOperation = FeedlyCheckpointOperation()
|
|
|
|
|
|
|
|
super.init()
|
|
|
|
|
2020-02-06 06:18:29 +01:00
|
|
|
self.operationQueue.add(self.finishOperation)
|
2019-10-18 23:21:02 +02:00
|
|
|
self.finishOperation.checkpointDelegate = self
|
|
|
|
enqueueOperations(for: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
convenience init(account: Account, credentials: Credentials, service: FeedlyGetStreamContentsService, newerThan: Date?, log: OSLog) {
|
|
|
|
let all = FeedlyCategoryResourceId.Global.all(for: credentials.username)
|
2020-01-17 07:16:52 +01:00
|
|
|
self.init(account: account, resource: all, service: service, isPagingEnabled: true, newerThan: newerThan, log: log)
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
|
2020-01-16 06:30:37 +01:00
|
|
|
override func run() {
|
|
|
|
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, "Canceling sync stream contents")
|
|
|
|
operationQueue.cancelAllOperations()
|
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
|
|
|
func enqueueOperations(for continuation: String?) {
|
|
|
|
os_log(.debug, log: log, "Requesting page for %@", resource.id)
|
|
|
|
let operations = pageOperations(for: continuation)
|
2020-01-16 06:30:37 +01:00
|
|
|
operationQueue.addOperations(operations)
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
|
2020-01-16 06:30:37 +01:00
|
|
|
func pageOperations(for continuation: String?) -> [MainThreadOperation] {
|
2019-10-18 23:21:02 +02:00
|
|
|
let getPage = FeedlyGetStreamContentsOperation(account: account,
|
|
|
|
resource: resource,
|
|
|
|
service: service,
|
|
|
|
continuation: continuation,
|
2019-11-27 09:04:55 +01:00
|
|
|
newerThan: newerThan,
|
|
|
|
log: log)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
|
2020-01-19 23:19:06 +01:00
|
|
|
let organiseByFeed = FeedlyOrganiseParsedItemsByFeedOperation(account: account, parsedItemProvider: getPage, log: log)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
2020-01-19 23:19:06 +01:00
|
|
|
let updateAccount = FeedlyUpdateAccountFeedsWithItemsOperation(account: account, organisedItemsProvider: organiseByFeed, log: log)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
getPage.delegate = self
|
|
|
|
getPage.streamDelegate = self
|
2020-01-16 06:30:37 +01:00
|
|
|
|
2020-01-19 23:19:06 +01:00
|
|
|
organiseByFeed.addDependency(getPage)
|
2019-10-18 23:21:02 +02:00
|
|
|
organiseByFeed.delegate = self
|
2020-01-16 06:30:37 +01:00
|
|
|
|
2020-01-19 23:19:06 +01:00
|
|
|
updateAccount.addDependency(organiseByFeed)
|
2019-10-18 23:21:02 +02:00
|
|
|
updateAccount.delegate = self
|
2020-01-16 06:30:37 +01:00
|
|
|
|
2020-01-19 23:19:06 +01:00
|
|
|
finishOperation.addDependency(updateAccount)
|
2020-01-16 06:30:37 +01:00
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
return [getPage, organiseByFeed, updateAccount]
|
|
|
|
}
|
|
|
|
|
|
|
|
func feedlyGetStreamContentsOperation(_ operation: FeedlyGetStreamContentsOperation, didGetContentsOf stream: FeedlyStream) {
|
2020-01-16 06:30:37 +01:00
|
|
|
guard !isCanceled else {
|
2019-11-14 21:59:44 +01:00
|
|
|
os_log(.debug, log: log, "Cancelled requesting page for %@", resource.id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
os_log(.debug, log: log, "Ingesting %i items from %@", stream.items.count, stream.id)
|
|
|
|
|
2020-01-17 07:16:52 +01:00
|
|
|
guard isPagingEnabled, let continuation = stream.continuation else {
|
2019-10-18 23:21:02 +02:00
|
|
|
os_log(.debug, log: log, "Reached end of stream for %@", stream.id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
enqueueOperations(for: continuation)
|
|
|
|
}
|
|
|
|
|
|
|
|
func feedlyCheckpointOperationDidReachCheckpoint(_ operation: FeedlyCheckpointOperation) {
|
|
|
|
os_log(.debug, log: log, "Completed ingesting items from %@", resource.id)
|
|
|
|
didFinish()
|
|
|
|
}
|
|
|
|
|
|
|
|
func feedlyOperation(_ operation: FeedlyOperation, didFailWith error: Error) {
|
|
|
|
operationQueue.cancelAllOperations()
|
2020-01-19 23:19:06 +01:00
|
|
|
didFinish(with: error)
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
}
|