2019-10-15 09:31:24 +02:00
|
|
|
//
|
|
|
|
// FeedlySyncStarredArticlesOperation.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Kiel Gillard on 15/10/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import os.log
|
2019-10-16 13:12:37 +02:00
|
|
|
import RSParser
|
2019-10-15 09:31:24 +02:00
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
final class FeedlySyncStarredArticlesOperation: FeedlyOperation, FeedlyOperationDelegate, FeedlyGetStreamContentsOperationDelegate, FeedlyCheckpointOperationDelegate {
|
2019-10-15 09:31:24 +02:00
|
|
|
private let account: Account
|
|
|
|
private let operationQueue: OperationQueue
|
2019-10-18 23:21:02 +02:00
|
|
|
private let service: FeedlyGetStreamContentsService
|
2019-10-15 09:31:24 +02:00
|
|
|
private let log: OSLog
|
|
|
|
|
2019-10-16 13:12:37 +02:00
|
|
|
private let setStatuses: FeedlySetStarredArticlesOperation
|
2019-10-18 23:21:02 +02:00
|
|
|
private let finishOperation: FeedlyCheckpointOperation
|
2019-10-16 13:12:37 +02:00
|
|
|
|
|
|
|
/// Buffers every starred/saved entry from every page.
|
2019-10-18 23:21:02 +02:00
|
|
|
private class StarredEntryProvider: FeedlyEntryProviding, FeedlyStarredEntryIdProviding, FeedlyParsedItemProviding {
|
2019-10-16 13:12:37 +02:00
|
|
|
var resource: FeedlyResourceId
|
|
|
|
|
|
|
|
private(set) var parsedEntries = Set<ParsedItem>()
|
|
|
|
private(set) var entries = [FeedlyEntry]()
|
|
|
|
|
|
|
|
init(resource: FeedlyResourceId) {
|
|
|
|
self.resource = resource
|
|
|
|
}
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
func addEntries(from provider: FeedlyEntryProviding & FeedlyParsedItemProviding) {
|
2019-10-16 13:12:37 +02:00
|
|
|
entries.append(contentsOf: provider.entries)
|
|
|
|
parsedEntries.formUnion(provider.parsedEntries)
|
|
|
|
}
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
var entryIds: Set<String> {
|
|
|
|
return Set(entries.map { $0.id })
|
|
|
|
}
|
2019-10-16 13:12:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private let entryProvider: StarredEntryProvider
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
convenience init(account: Account, credentials: Credentials, service: FeedlyGetStreamContentsService, log: OSLog) {
|
|
|
|
let saved = FeedlyTagResourceId.Global.saved(for: credentials.username)
|
|
|
|
self.init(account: account, resource: saved, service: service, log: log)
|
|
|
|
}
|
|
|
|
|
|
|
|
init(account: Account, resource: FeedlyResourceId, service: FeedlyGetStreamContentsService, log: OSLog) {
|
2019-10-15 09:31:24 +02:00
|
|
|
self.account = account
|
2019-10-18 23:21:02 +02:00
|
|
|
self.service = service
|
2019-10-15 09:31:24 +02:00
|
|
|
self.operationQueue = OperationQueue()
|
2019-10-16 13:12:37 +02:00
|
|
|
self.operationQueue.isSuspended = true
|
2019-10-18 23:21:02 +02:00
|
|
|
self.finishOperation = FeedlyCheckpointOperation()
|
2019-10-15 09:31:24 +02:00
|
|
|
self.log = log
|
2019-10-16 13:12:37 +02:00
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
let provider = StarredEntryProvider(resource: resource)
|
2019-10-16 13:12:37 +02:00
|
|
|
self.entryProvider = provider
|
|
|
|
self.setStatuses = FeedlySetStarredArticlesOperation(account: account,
|
2019-10-18 23:21:02 +02:00
|
|
|
allStarredEntryIdsProvider: provider,
|
2019-10-16 13:12:37 +02:00
|
|
|
log: log)
|
|
|
|
|
|
|
|
super.init()
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
let getFirstPage = FeedlyGetStreamContentsOperation(account: account,
|
|
|
|
resource: resource,
|
|
|
|
service: service,
|
2019-11-27 09:04:55 +01:00
|
|
|
newerThan: nil,
|
|
|
|
log: log)
|
2019-10-16 13:12:37 +02:00
|
|
|
|
|
|
|
let organiseByFeed = FeedlyOrganiseParsedItemsByFeedOperation(account: account,
|
2019-10-18 23:21:02 +02:00
|
|
|
parsedItemProvider: provider,
|
2019-10-16 13:12:37 +02:00
|
|
|
log: log)
|
|
|
|
|
|
|
|
let updateAccount = FeedlyUpdateAccountFeedsWithItemsOperation(account: account,
|
|
|
|
organisedItemsProvider: organiseByFeed,
|
|
|
|
log: log)
|
|
|
|
|
|
|
|
getFirstPage.delegate = self
|
|
|
|
getFirstPage.streamDelegate = self
|
|
|
|
|
|
|
|
setStatuses.addDependency(getFirstPage)
|
|
|
|
setStatuses.delegate = self
|
|
|
|
|
|
|
|
organiseByFeed.addDependency(setStatuses)
|
|
|
|
organiseByFeed.delegate = self
|
|
|
|
|
|
|
|
updateAccount.addDependency(organiseByFeed)
|
|
|
|
updateAccount.delegate = self
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
finishOperation.checkpointDelegate = self
|
2019-10-16 13:12:37 +02:00
|
|
|
finishOperation.addDependency(updateAccount)
|
|
|
|
|
|
|
|
let operations = [getFirstPage, setStatuses, organiseByFeed, updateAccount, finishOperation]
|
|
|
|
operationQueue.addOperations(operations, waitUntilFinished: false)
|
2019-10-15 09:31:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override func cancel() {
|
2019-11-14 21:59:44 +01:00
|
|
|
os_log(.debug, log: log, "Canceling sync starred articles")
|
2019-10-15 09:31:24 +02:00
|
|
|
operationQueue.cancelAllOperations()
|
|
|
|
super.cancel()
|
2019-11-14 21:59:44 +01:00
|
|
|
didFinish()
|
2019-10-15 09:31:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override func main() {
|
|
|
|
guard !isCancelled else {
|
2019-11-14 21:59:44 +01:00
|
|
|
// override of cancel calls didFinish().
|
2019-10-15 09:31:24 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-16 13:12:37 +02:00
|
|
|
operationQueue.isSuspended = false
|
|
|
|
}
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
func feedlyGetStreamContentsOperation(_ operation: FeedlyGetStreamContentsOperation, didGetContentsOf stream: FeedlyStream) {
|
2019-11-14 21:59:44 +01:00
|
|
|
guard !isCancelled else {
|
|
|
|
os_log(.debug, log: log, "Cancelled starred stream contents for %@", stream.id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-16 13:12:37 +02:00
|
|
|
entryProvider.addEntries(from: operation)
|
|
|
|
os_log(.debug, log: log, "Collecting %i items from %@", stream.items.count, stream.id)
|
2019-10-15 09:31:24 +02:00
|
|
|
|
2019-10-16 13:12:37 +02:00
|
|
|
guard let continuation = stream.continuation else {
|
|
|
|
return
|
2019-10-15 09:31:24 +02:00
|
|
|
}
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
let nextPageOperation = FeedlyGetStreamContentsOperation(account: operation.account,
|
2019-10-16 13:12:37 +02:00
|
|
|
resource: operation.resource,
|
2019-10-18 23:21:02 +02:00
|
|
|
service: operation.service,
|
2019-10-16 13:12:37 +02:00
|
|
|
continuation: continuation,
|
2019-11-27 09:04:55 +01:00
|
|
|
newerThan: operation.newerThan,
|
|
|
|
log: log)
|
2019-10-16 13:12:37 +02:00
|
|
|
nextPageOperation.delegate = self
|
|
|
|
nextPageOperation.streamDelegate = self
|
2019-10-15 09:31:24 +02:00
|
|
|
|
2019-10-16 13:12:37 +02:00
|
|
|
setStatuses.addDependency(nextPageOperation)
|
|
|
|
operationQueue.addOperation(nextPageOperation)
|
2019-10-15 09:31:24 +02:00
|
|
|
}
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
func feedlyCheckpointOperationDidReachCheckpoint(_ operation: FeedlyCheckpointOperation) {
|
|
|
|
didFinish()
|
|
|
|
}
|
|
|
|
|
2019-10-16 13:12:37 +02:00
|
|
|
func feedlyOperation(_ operation: FeedlyOperation, didFailWith error: Error) {
|
2019-10-18 23:21:02 +02:00
|
|
|
os_log(.debug, log: log, "%{public}@ failing and cancelling other operations because %{public}@.", operation, error.localizedDescription)
|
2019-10-16 13:12:37 +02:00
|
|
|
operationQueue.cancelAllOperations()
|
|
|
|
didFinish(error)
|
|
|
|
}
|
2019-10-15 09:31:24 +02:00
|
|
|
}
|