2020-01-09 06:24:47 +01:00
|
|
|
//
|
|
|
|
// FeedlyGetUpdatedArticleIdsOperation.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Kiel Gillard on 11/1/20.
|
|
|
|
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import os.log
|
|
|
|
|
|
|
|
/// Single responsibility is to identify articles that have changed since a particular date.
|
|
|
|
///
|
|
|
|
/// Typically, it pages through the article ids of the global.all stream.
|
|
|
|
/// When all the article ids are collected, it is the responsibility of another operation to download them when appropriate.
|
|
|
|
class FeedlyGetUpdatedArticleIdsOperation: FeedlyOperation, FeedlyEntryIdentifierProviding {
|
2020-01-19 23:19:06 +01:00
|
|
|
|
2020-01-09 06:24:47 +01:00
|
|
|
private let account: Account
|
|
|
|
private let resource: FeedlyResourceId
|
|
|
|
private let service: FeedlyGetStreamIdsService
|
|
|
|
private let newerThan: Date?
|
|
|
|
private let log: OSLog
|
|
|
|
|
|
|
|
init(account: Account, resource: FeedlyResourceId, service: FeedlyGetStreamIdsService, newerThan: Date?, log: OSLog) {
|
|
|
|
self.account = account
|
|
|
|
self.resource = resource
|
|
|
|
self.service = service
|
|
|
|
self.newerThan = newerThan
|
|
|
|
self.log = log
|
|
|
|
}
|
|
|
|
|
|
|
|
convenience init(account: Account, credentials: Credentials, service: FeedlyGetStreamIdsService, newerThan: Date?, log: OSLog) {
|
|
|
|
let all = FeedlyCategoryResourceId.Global.all(for: credentials.username)
|
|
|
|
self.init(account: account, resource: all, service: service, newerThan: newerThan, log: log)
|
|
|
|
}
|
|
|
|
|
|
|
|
var entryIds: Set<String> {
|
|
|
|
return storedUpdatedArticleIds
|
|
|
|
}
|
|
|
|
|
|
|
|
private var storedUpdatedArticleIds = Set<String>()
|
|
|
|
|
2020-01-16 06:30:37 +01:00
|
|
|
override func run() {
|
2020-01-09 06:24:47 +01:00
|
|
|
getStreamIds(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func getStreamIds(_ continuation: String?) {
|
|
|
|
guard let date = newerThan else {
|
|
|
|
os_log(.debug, log: log, "No date provided so everything must be new (nothing is updated).")
|
|
|
|
didFinish()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
service.getStreamIds(for: resource, continuation: continuation, newerThan: date, unreadOnly: nil, completion: didGetStreamIds(_:))
|
|
|
|
}
|
|
|
|
|
|
|
|
private func didGetStreamIds(_ result: Result<FeedlyStreamIds, Error>) {
|
2020-01-16 06:30:37 +01:00
|
|
|
guard !isCanceled else {
|
2020-01-09 06:24:47 +01:00
|
|
|
didFinish()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let streamIds):
|
|
|
|
storedUpdatedArticleIds.formUnion(streamIds.ids)
|
|
|
|
|
|
|
|
guard let continuation = streamIds.continuation else {
|
|
|
|
os_log(.debug, log: log, "%{public}i articles updated since last successful sync start date.", storedUpdatedArticleIds.count)
|
|
|
|
didFinish()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
getStreamIds(continuation)
|
|
|
|
|
|
|
|
case .failure(let error):
|
2020-01-19 23:19:06 +01:00
|
|
|
didFinish(with: error)
|
2020-01-09 06:24:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|