2020-01-09 06:24:47 +01:00
|
|
|
//
|
|
|
|
// FeedlyIngestStarredArticleIdsOperation.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Kiel Gillard on 15/10/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import os.log
|
2020-01-13 07:51:16 +01:00
|
|
|
import SyncDatabase
|
2020-01-09 06:24:47 +01:00
|
|
|
|
2020-01-19 23:19:06 +01:00
|
|
|
/// Clone locally the remote starred article state.
|
2020-01-09 06:24:47 +01:00
|
|
|
///
|
|
|
|
/// Typically, it pages through the article ids of the global.saved stream.
|
|
|
|
/// When all the article ids are collected, a status is created for each.
|
|
|
|
/// The article ids previously marked as starred but not collected become unstarred.
|
|
|
|
/// So this operation has side effects *for the entire account* it operates on.
|
|
|
|
final class FeedlyIngestStarredArticleIdsOperation: FeedlyOperation {
|
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
|
2020-01-13 07:51:16 +01:00
|
|
|
private let database: SyncDatabase
|
|
|
|
private var remoteEntryIds = Set<String>()
|
2020-01-09 06:24:47 +01:00
|
|
|
private let log: OSLog
|
|
|
|
|
2020-01-13 07:51:16 +01:00
|
|
|
convenience init(account: Account, credentials: Credentials, service: FeedlyGetStreamIdsService, database: SyncDatabase, newerThan: Date?, log: OSLog) {
|
2020-01-09 06:24:47 +01:00
|
|
|
let resource = FeedlyTagResourceId.Global.saved(for: credentials.username)
|
2020-01-13 07:51:16 +01:00
|
|
|
self.init(account: account, resource: resource, service: service, database: database, newerThan: newerThan, log: log)
|
2020-01-09 06:24:47 +01:00
|
|
|
}
|
|
|
|
|
2020-01-13 07:51:16 +01:00
|
|
|
init(account: Account, resource: FeedlyResourceId, service: FeedlyGetStreamIdsService, database: SyncDatabase, newerThan: Date?, log: OSLog) {
|
2020-01-09 06:24:47 +01:00
|
|
|
self.account = account
|
|
|
|
self.resource = resource
|
|
|
|
self.service = service
|
2020-01-13 07:51:16 +01:00
|
|
|
self.database = database
|
2020-01-09 06:24:47 +01:00
|
|
|
self.log = log
|
|
|
|
}
|
|
|
|
|
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?) {
|
|
|
|
service.getStreamIds(for: resource, continuation: continuation, newerThan: nil, 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):
|
|
|
|
|
2020-01-13 07:51:16 +01:00
|
|
|
remoteEntryIds.formUnion(streamIds.ids)
|
2020-01-09 06:24:47 +01:00
|
|
|
|
|
|
|
guard let continuation = streamIds.continuation else {
|
2020-01-13 07:51:16 +01:00
|
|
|
removeEntryIdsWithPendingStatus()
|
2020-01-09 06:24:47 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 07:51:16 +01:00
|
|
|
/// Do not override pending statuses with the remote statuses of the same articles, otherwise an article will temporarily re-acquire the remote status before the pending status is pushed and subseqently pulled.
|
|
|
|
private func removeEntryIdsWithPendingStatus() {
|
2020-01-16 06:30:37 +01:00
|
|
|
guard !isCanceled else {
|
2020-01-13 07:51:16 +01:00
|
|
|
didFinish()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
database.selectPendingStarredStatusArticleIDs { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let pendingArticleIds):
|
|
|
|
self.remoteEntryIds.subtract(pendingArticleIds)
|
|
|
|
|
|
|
|
self.updateStarredStatuses()
|
|
|
|
|
|
|
|
case .failure(let error):
|
2020-01-19 23:19:06 +01:00
|
|
|
self.didFinish(with: error)
|
2020-01-13 07:51:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 06:24:47 +01:00
|
|
|
private func updateStarredStatuses() {
|
2020-01-16 06:30:37 +01:00
|
|
|
guard !isCanceled else {
|
2020-01-09 06:24:47 +01:00
|
|
|
didFinish()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
account.fetchStarredArticleIDs { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let localStarredArticleIDs):
|
|
|
|
self.processStarredArticleIDs(localStarredArticleIDs)
|
|
|
|
|
|
|
|
case .failure(let error):
|
2020-01-19 23:19:06 +01:00
|
|
|
self.didFinish(with: error)
|
2020-01-09 06:24:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func processStarredArticleIDs(_ localStarredArticleIDs: Set<String>) {
|
2020-01-16 06:30:37 +01:00
|
|
|
guard !isCanceled else {
|
2020-01-09 06:24:47 +01:00
|
|
|
didFinish()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-13 07:51:16 +01:00
|
|
|
let remoteStarredArticleIDs = remoteEntryIds
|
2020-01-09 06:24:47 +01:00
|
|
|
|
|
|
|
let group = DispatchGroup()
|
|
|
|
|
|
|
|
final class StarredStatusResults {
|
|
|
|
var markAsStarredError: Error?
|
|
|
|
var markAsUnstarredError: Error?
|
|
|
|
}
|
|
|
|
|
|
|
|
let results = StarredStatusResults()
|
|
|
|
|
|
|
|
group.enter()
|
|
|
|
account.markAsStarred(remoteStarredArticleIDs) { error in
|
|
|
|
results.markAsStarredError = error
|
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
|
|
|
|
let deltaUnstarredArticleIDs = localStarredArticleIDs.subtracting(remoteStarredArticleIDs)
|
|
|
|
group.enter()
|
|
|
|
account.markAsUnstarred(deltaUnstarredArticleIDs) { error in
|
|
|
|
results.markAsUnstarredError = error
|
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
|
|
|
|
group.notify(queue: .main) {
|
|
|
|
let markingError = results.markAsStarredError ?? results.markAsUnstarredError
|
|
|
|
guard let error = markingError else {
|
|
|
|
self.didFinish()
|
|
|
|
return
|
|
|
|
}
|
2020-01-19 23:19:06 +01:00
|
|
|
self.didFinish(with: error)
|
2020-01-09 06:24:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|