2019-05-28 19:08:15 +02:00
|
|
|
//
|
2019-06-19 18:25:37 +02:00
|
|
|
// ReaderAPIAccountDelegate.swift
|
2019-05-28 19:08:15 +02:00
|
|
|
// Account
|
|
|
|
//
|
2019-06-19 13:56:25 +02:00
|
|
|
// Created by Jeremy Beker on 5/28/19.
|
2019-05-28 19:08:15 +02:00
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Articles
|
|
|
|
import RSCore
|
|
|
|
import RSParser
|
|
|
|
import RSWeb
|
|
|
|
import SyncDatabase
|
|
|
|
import os.log
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
public enum ReaderAPIAccountDelegateError: String, Error {
|
2019-05-28 19:08:15 +02:00
|
|
|
case invalidParameter = "There was an invalid parameter passed."
|
2019-06-10 22:53:35 +02:00
|
|
|
case invalidResponse = "There was an invalid response from the server."
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
final class ReaderAPIAccountDelegate: AccountDelegate {
|
2019-11-05 03:24:21 +01:00
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
private let database: SyncDatabase
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
private let caller: ReaderAPICaller
|
|
|
|
private var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "ReaderAPI")
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-09-20 18:34:31 +02:00
|
|
|
var behaviors: AccountBehaviors = [.disallowFeedInRootFolder, .disallowOPMLImports]
|
2019-06-01 12:54:10 +02:00
|
|
|
|
2019-05-29 16:54:52 +02:00
|
|
|
var server: String? {
|
|
|
|
get {
|
|
|
|
return caller.server
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 00:50:32 +02:00
|
|
|
var isOPMLImportInProgress = false
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
var credentials: Credentials? {
|
|
|
|
didSet {
|
|
|
|
caller.credentials = credentials
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
weak var accountMetadata: AccountMetadata? {
|
|
|
|
didSet {
|
|
|
|
caller.accountMetadata = accountMetadata
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
init(dataFolder: String, transport: Transport?) {
|
|
|
|
|
|
|
|
let databaseFilePath = (dataFolder as NSString).appendingPathComponent("Sync.sqlite3")
|
|
|
|
database = SyncDatabase(databaseFilePath: databaseFilePath)
|
|
|
|
|
|
|
|
if transport != nil {
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
caller = ReaderAPICaller(transport: transport!)
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
let sessionConfiguration = URLSessionConfiguration.default
|
|
|
|
sessionConfiguration.requestCachePolicy = .reloadIgnoringLocalCacheData
|
|
|
|
sessionConfiguration.timeoutIntervalForRequest = 60.0
|
|
|
|
sessionConfiguration.httpShouldSetCookies = false
|
|
|
|
sessionConfiguration.httpCookieAcceptPolicy = .never
|
|
|
|
sessionConfiguration.httpMaximumConnectionsPerHost = 1
|
|
|
|
sessionConfiguration.httpCookieStorage = nil
|
|
|
|
sessionConfiguration.urlCache = nil
|
|
|
|
|
|
|
|
if let userAgentHeaders = UserAgent.headers() {
|
|
|
|
sessionConfiguration.httpAdditionalHeaders = userAgentHeaders
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
caller = ReaderAPICaller(transport: URLSession(configuration: sessionConfiguration))
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var refreshProgress = DownloadProgress(numberOfTasks: 0)
|
|
|
|
|
|
|
|
func refreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
|
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(6)
|
|
|
|
|
|
|
|
refreshAccount(account) { result in
|
|
|
|
switch result {
|
|
|
|
case .success():
|
2019-08-30 18:47:08 +02:00
|
|
|
|
2019-11-05 03:24:21 +01:00
|
|
|
self.sendArticleStatus(for: account) { _ in
|
|
|
|
self.refreshArticleStatus(for: account) { _ in
|
2019-08-30 18:47:08 +02:00
|
|
|
self.refreshArticles(account) {
|
|
|
|
self.refreshMissingArticles(account) {
|
|
|
|
self.refreshProgress.clear()
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
completion(.success(()))
|
|
|
|
}
|
2019-06-01 23:05:55 +02:00
|
|
|
}
|
2019-06-10 22:53:35 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-01 23:05:55 +02:00
|
|
|
}
|
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
case .failure(let error):
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.refreshProgress.clear()
|
|
|
|
let wrappedError = AccountError.wrappedError(error: error, account: account)
|
|
|
|
completion(.failure(wrappedError))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-05 03:24:21 +01:00
|
|
|
func sendArticleStatus(for account: Account, completion: @escaping ((Result<Void, Error>) -> Void)) {
|
2019-05-28 19:08:15 +02:00
|
|
|
os_log(.debug, log: log, "Sending article statuses...")
|
|
|
|
|
2019-12-16 21:49:07 +01:00
|
|
|
database.selectForProcessing { result in
|
|
|
|
|
|
|
|
func processStatuses(_ syncStatuses: [SyncStatus]) {
|
|
|
|
let createUnreadStatuses = syncStatuses.filter { $0.key == ArticleStatus.Key.read && $0.flag == false }
|
|
|
|
let deleteUnreadStatuses = syncStatuses.filter { $0.key == ArticleStatus.Key.read && $0.flag == true }
|
|
|
|
let createStarredStatuses = syncStatuses.filter { $0.key == ArticleStatus.Key.starred && $0.flag == true }
|
|
|
|
let deleteStarredStatuses = syncStatuses.filter { $0.key == ArticleStatus.Key.starred && $0.flag == false }
|
|
|
|
|
|
|
|
let group = DispatchGroup()
|
|
|
|
|
|
|
|
group.enter()
|
|
|
|
self.sendArticleStatuses(createUnreadStatuses, apiCall: self.caller.createUnreadEntries) {
|
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
|
|
|
|
group.enter()
|
|
|
|
self.sendArticleStatuses(deleteUnreadStatuses, apiCall: self.caller.deleteUnreadEntries) {
|
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
|
|
|
|
group.enter()
|
|
|
|
self.sendArticleStatuses(createStarredStatuses, apiCall: self.caller.createStarredEntries) {
|
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
|
|
|
|
group.enter()
|
|
|
|
self.sendArticleStatuses(deleteStarredStatuses, apiCall: self.caller.deleteStarredEntries) {
|
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
|
|
|
|
group.notify(queue: DispatchQueue.main) {
|
|
|
|
os_log(.debug, log: self.log, "Done sending article statuses.")
|
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let syncStatuses):
|
|
|
|
processStatuses(syncStatuses)
|
|
|
|
case .failure(let databaseError):
|
|
|
|
completion(.failure(databaseError))
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 03:24:21 +01:00
|
|
|
func refreshArticleStatus(for account: Account, completion: @escaping ((Result<Void, Error>) -> Void)) {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
os_log(.debug, log: log, "Refreshing article statuses...")
|
|
|
|
|
|
|
|
let group = DispatchGroup()
|
|
|
|
|
|
|
|
group.enter()
|
|
|
|
caller.retrieveUnreadEntries() { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let articleIDs):
|
|
|
|
self.syncArticleReadState(account: account, articleIDs: articleIDs)
|
|
|
|
group.leave()
|
|
|
|
case .failure(let error):
|
|
|
|
os_log(.info, log: self.log, "Retrieving unread entries failed: %@.", error.localizedDescription)
|
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-11 21:37:21 +02:00
|
|
|
group.enter()
|
|
|
|
caller.retrieveStarredEntries() { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let articleIDs):
|
|
|
|
self.syncArticleStarredState(account: account, articleIDs: articleIDs)
|
|
|
|
group.leave()
|
|
|
|
case .failure(let error):
|
|
|
|
os_log(.info, log: self.log, "Retrieving starred entries failed: %@.", error.localizedDescription)
|
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
group.notify(queue: DispatchQueue.main) {
|
|
|
|
os_log(.debug, log: self.log, "Done refreshing article statuses.")
|
2019-11-05 03:24:21 +01:00
|
|
|
completion(.success(()))
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func importOPML(for account:Account, opmlFile: URL, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
}
|
|
|
|
|
2019-06-01 12:54:10 +02:00
|
|
|
func addFolder(for account: Account, name: String, completion: @escaping (Result<Folder, Error>) -> Void) {
|
|
|
|
if let folder = account.ensureFolder(with: name) {
|
|
|
|
completion(.success(folder))
|
|
|
|
} else {
|
|
|
|
completion(.failure(FeedbinAccountDelegateError.invalidParameter))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
|
|
|
|
caller.renameTag(oldName: folder.name ?? "", newName: name) { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
folder.name = name
|
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
case .failure(let error):
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
let wrappedError = AccountError.wrappedError(error: error, account: account)
|
|
|
|
completion(.failure(wrappedError))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-01 12:54:10 +02:00
|
|
|
func removeFolder(for account: Account, with folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
let group = DispatchGroup()
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
for feed in folder.topLevelWebFeeds {
|
2019-06-01 12:54:10 +02:00
|
|
|
group.enter()
|
2019-11-15 03:11:41 +01:00
|
|
|
removeWebFeed(for: account, with: feed, from: folder) { result in
|
2019-06-01 12:54:10 +02:00
|
|
|
group.leave()
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
break
|
|
|
|
case .failure(let error):
|
|
|
|
os_log(.error, log: self.log, "Remove feed error: %@.", error.localizedDescription)
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-01 12:54:10 +02:00
|
|
|
group.notify(queue: DispatchQueue.main) {
|
2019-06-16 19:14:23 +02:00
|
|
|
self.caller.deleteTag(name: folder.name!) { (result) in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
account.removeFolder(folder)
|
|
|
|
completion(.success(()))
|
|
|
|
case .failure(let error):
|
|
|
|
os_log(.error, log: self.log, "Remove feed error: %@.", error.localizedDescription)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-01 12:54:10 +02:00
|
|
|
}
|
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func createWebFeed(for account: Account, url: String, name: String?, container: Container, completion: @escaping (Result<WebFeed, Error>) -> Void) {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
caller.createSubscription(url: url) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let subResult):
|
|
|
|
switch subResult {
|
|
|
|
case .created(let subscription):
|
2019-06-01 12:54:10 +02:00
|
|
|
self.createFeed(account: account, subscription: subscription, name: name, container: container, completion: completion)
|
2019-05-28 19:08:15 +02:00
|
|
|
case .alreadySubscribed:
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
completion(.failure(AccountError.createErrorAlreadySubscribed))
|
|
|
|
}
|
|
|
|
case .notFound:
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
completion(.failure(AccountError.createErrorNotFound))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case .failure(let error):
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
let wrappedError = AccountError.wrappedError(error: error, account: account)
|
|
|
|
completion(.failure(wrappedError))
|
|
|
|
}
|
|
|
|
}
|
2019-06-01 12:54:10 +02:00
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-06-01 12:54:10 +02:00
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func renameWebFeed(for account: Account, with feed: WebFeed, to name: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
// This error should never happen
|
|
|
|
guard let subscriptionID = feed.subscriptionID else {
|
2019-06-01 12:54:10 +02:00
|
|
|
completion(.failure(FeedbinAccountDelegateError.invalidParameter))
|
2019-05-28 19:08:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
caller.renameSubscription(subscriptionID: subscriptionID, newName: name) { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
feed.editedName = name
|
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
case .failure(let error):
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
let wrappedError = AccountError.wrappedError(error: error, account: account)
|
|
|
|
completion(.failure(wrappedError))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-06-01 12:54:10 +02:00
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func removeWebFeed(for account: Account, with feed: WebFeed, from container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-06-01 12:54:10 +02:00
|
|
|
if feed.folderRelationship?.count ?? 0 > 1 {
|
|
|
|
deleteTagging(for: account, with: feed, from: container, completion: completion)
|
|
|
|
} else {
|
2019-11-15 03:11:41 +01:00
|
|
|
account.clearWebFeedMetadata(feed)
|
2019-06-01 12:54:10 +02:00
|
|
|
deleteSubscription(for: account, with: feed, from: container, completion: completion)
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
2019-06-01 12:54:10 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func moveWebFeed(for account: Account, with feed: WebFeed, from: Container, to: Container, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-06-01 12:54:10 +02:00
|
|
|
if from is Account {
|
2019-11-15 03:11:41 +01:00
|
|
|
addWebFeed(for: account, with: feed, to: to, completion: completion)
|
2019-06-01 12:54:10 +02:00
|
|
|
} else {
|
|
|
|
deleteTagging(for: account, with: feed, from: from) { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
2019-11-15 03:11:41 +01:00
|
|
|
self.addWebFeed(for: account, with: feed, to: to, completion: completion)
|
2019-06-01 12:54:10 +02:00
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func addWebFeed(for account: Account, with feed: WebFeed, to container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-06-16 19:14:23 +02:00
|
|
|
if let folder = container as? Folder, let feedName = feed.subscriptionID {
|
|
|
|
caller.createTagging(subscriptionID: feedName, tagName: folder.name ?? "") { result in
|
2019-05-28 19:08:15 +02:00
|
|
|
switch result {
|
2019-06-16 19:14:23 +02:00
|
|
|
case .success:
|
2019-05-28 19:08:15 +02:00
|
|
|
DispatchQueue.main.async {
|
2019-06-16 19:14:23 +02:00
|
|
|
self.saveFolderRelationship(for: feed, withFolderName: folder.name ?? "", id: feed.subscriptionID!)
|
2019-11-15 03:11:41 +01:00
|
|
|
account.removeWebFeed(feed)
|
|
|
|
folder.addWebFeed(feed)
|
2019-05-28 19:08:15 +02:00
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
case .failure(let error):
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
let wrappedError = AccountError.wrappedError(error: error, account: account)
|
|
|
|
completion(.failure(wrappedError))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DispatchQueue.main.async {
|
2019-06-01 12:54:10 +02:00
|
|
|
if let account = container as? Account {
|
|
|
|
account.addFeedIfNotInAnyFolder(feed)
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
2019-06-01 12:54:10 +02:00
|
|
|
completion(.success(()))
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func restoreWebFeed(for account: Account, feed: WebFeed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
createWebFeed(for: account, url: feed.url, name: feed.editedName, container: container) { result in
|
2019-05-28 19:08:15 +02:00
|
|
|
switch result {
|
2019-06-01 12:54:10 +02:00
|
|
|
case .success:
|
|
|
|
completion(.success(()))
|
2019-05-28 19:08:15 +02:00
|
|
|
case .failure(let error):
|
2019-06-01 12:54:10 +02:00
|
|
|
completion(.failure(error))
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func restoreFolder(for account: Account, folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
|
|
|
|
account.addFolder(folder)
|
|
|
|
let group = DispatchGroup()
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
for feed in folder.topLevelWebFeeds {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
group.enter()
|
2019-11-15 03:11:41 +01:00
|
|
|
addWebFeed(for: account, with: feed, to: folder) { result in
|
|
|
|
if account.topLevelWebFeeds.contains(feed) {
|
|
|
|
account.removeWebFeed(feed)
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
group.notify(queue: DispatchQueue.main) {
|
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func markArticles(for account: Account, articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) -> Set<Article>? {
|
|
|
|
|
|
|
|
let syncStatuses = articles.map { article in
|
|
|
|
return SyncStatus(articleID: article.articleID, key: statusKey, flag: flag)
|
|
|
|
}
|
|
|
|
database.insertStatuses(syncStatuses)
|
|
|
|
|
2019-12-16 19:50:13 +01:00
|
|
|
database.selectPendingCount { result in
|
|
|
|
if let count = try? result.get(), count > 100 {
|
|
|
|
self.sendArticleStatus(for: account) { _ in }
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
|
2019-12-17 07:45:59 +01:00
|
|
|
return try? account.update(articles, statusKey: statusKey, flag: flag)
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
}
|
2019-06-11 22:42:28 +02:00
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
func accountDidInitialize(_ account: Account) {
|
2019-09-15 17:03:47 +02:00
|
|
|
credentials = try? account.retrieveCredentials(type: .readerAPIKey)
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
|
2019-11-11 08:42:31 +01:00
|
|
|
func accountWillBeDeleted(_ account: Account) {
|
|
|
|
}
|
|
|
|
|
2019-05-29 21:16:09 +02:00
|
|
|
static func validateCredentials(transport: Transport, credentials: Credentials, endpoint: URL?, completion: @escaping (Result<Credentials?, Error>) -> Void) {
|
|
|
|
guard let endpoint = endpoint else {
|
|
|
|
completion(.failure(TransportError.noURL))
|
|
|
|
return
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
let caller = ReaderAPICaller(transport: transport)
|
2019-05-28 19:08:15 +02:00
|
|
|
caller.credentials = credentials
|
2019-05-29 21:16:09 +02:00
|
|
|
caller.validateCredentials(endpoint: endpoint) { result in
|
2019-05-28 19:08:15 +02:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
completion(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-11-30 07:57:14 +01:00
|
|
|
|
|
|
|
// MARK: Suspend and Resume (for iOS)
|
|
|
|
|
2019-12-05 01:27:39 +01:00
|
|
|
/// Suspend all network activity
|
|
|
|
func suspendNetwork() {
|
2019-12-02 21:14:35 +01:00
|
|
|
caller.cancelAll()
|
2019-12-05 01:27:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Suspend the SQLLite databases
|
|
|
|
func suspendDatabase() {
|
2019-11-30 07:57:14 +01:00
|
|
|
database.suspend()
|
|
|
|
}
|
2019-12-05 01:27:39 +01:00
|
|
|
|
|
|
|
/// Make sure no SQLite databases are open and we are ready to issue network requests.
|
2019-11-30 07:57:14 +01:00
|
|
|
func resume() {
|
|
|
|
database.resume()
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
private extension ReaderAPIAccountDelegate {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
func refreshAccount(_ account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
|
|
|
|
caller.retrieveTags { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let tags):
|
|
|
|
BatchUpdate.shared.perform {
|
|
|
|
self.syncFolders(account, tags)
|
|
|
|
}
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
self.refreshFeeds(account, completion: completion)
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
func syncFolders(_ account: Account, _ tags: [ReaderAPITag]?) {
|
2019-05-28 19:08:15 +02:00
|
|
|
guard let tags = tags else { return }
|
2019-07-13 14:47:00 +02:00
|
|
|
assert(Thread.isMainThread)
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
os_log(.debug, log: log, "Syncing folders with %ld tags.", tags.count)
|
|
|
|
|
2019-06-01 14:08:19 +02:00
|
|
|
let tagNames = tags.filter { $0.type == "folder" }.map { $0.tagID.replacingOccurrences(of: "user/-/label/", with: "") }
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
// Delete any folders not at Reader
|
2019-05-28 19:08:15 +02:00
|
|
|
if let folders = account.folders {
|
|
|
|
folders.forEach { folder in
|
|
|
|
if !tagNames.contains(folder.name ?? "") {
|
2019-11-15 03:11:41 +01:00
|
|
|
for feed in folder.topLevelWebFeeds {
|
|
|
|
account.addWebFeed(feed)
|
2019-07-13 14:47:00 +02:00
|
|
|
clearFolderRelationship(for: feed, withFolderName: folder.name ?? "")
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
2019-07-13 14:47:00 +02:00
|
|
|
account.removeFolder(folder)
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let folderNames: [String] = {
|
|
|
|
if let folders = account.folders {
|
|
|
|
return folders.map { $0.name ?? "" }
|
|
|
|
} else {
|
|
|
|
return [String]()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
// Make any folders Reader has, but we don't
|
2019-05-28 19:08:15 +02:00
|
|
|
tagNames.forEach { tagName in
|
|
|
|
if !folderNames.contains(tagName) {
|
2019-07-13 14:47:00 +02:00
|
|
|
_ = account.ensureFolder(with: tagName)
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func refreshFeeds(_ account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
|
|
|
|
caller.retrieveSubscriptions { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let subscriptions):
|
|
|
|
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
|
2019-06-01 23:05:55 +02:00
|
|
|
BatchUpdate.shared.perform {
|
|
|
|
self.syncFeeds(account, subscriptions)
|
|
|
|
self.syncTaggings(account, subscriptions)
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
2019-06-01 23:05:55 +02:00
|
|
|
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
completion(.success(()))
|
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
func syncFeeds(_ account: Account, _ subscriptions: [ReaderAPISubscription]?) {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
guard let subscriptions = subscriptions else { return }
|
2019-07-13 14:47:00 +02:00
|
|
|
assert(Thread.isMainThread)
|
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
os_log(.debug, log: log, "Syncing feeds with %ld subscriptions.", subscriptions.count)
|
|
|
|
|
|
|
|
let subFeedIds = subscriptions.map { String($0.feedID) }
|
|
|
|
|
|
|
|
// Remove any feeds that are no longer in the subscriptions
|
|
|
|
if let folders = account.folders {
|
|
|
|
for folder in folders {
|
2019-11-15 03:11:41 +01:00
|
|
|
for feed in folder.topLevelWebFeeds {
|
|
|
|
if !subFeedIds.contains(feed.webFeedID) {
|
|
|
|
folder.removeWebFeed(feed)
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
for feed in account.topLevelWebFeeds {
|
|
|
|
if !subFeedIds.contains(feed.webFeedID) {
|
|
|
|
account.removeWebFeed(feed)
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add any feeds we don't have and update any we do
|
|
|
|
subscriptions.forEach { subscription in
|
|
|
|
|
|
|
|
let subFeedId = String(subscription.feedID)
|
2019-11-15 03:11:41 +01:00
|
|
|
if let feed = account.existingWebFeed(withWebFeedID: subFeedId) {
|
2019-07-13 14:47:00 +02:00
|
|
|
feed.name = subscription.name
|
|
|
|
feed.homePageURL = subscription.homePageURL
|
|
|
|
} else {
|
2019-11-15 03:11:41 +01:00
|
|
|
let feed = account.createWebFeed(with: subscription.name, url: subscription.url, webFeedID: subFeedId, homePageURL: subscription.homePageURL)
|
2019-07-13 14:47:00 +02:00
|
|
|
feed.iconURL = subscription.iconURL
|
|
|
|
feed.subscriptionID = String(subscription.feedID)
|
2019-11-15 03:11:41 +01:00
|
|
|
account.addWebFeed(feed)
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
func syncTaggings(_ account: Account, _ subscriptions: [ReaderAPISubscription]?) {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-06-01 23:05:55 +02:00
|
|
|
guard let subscriptions = subscriptions else { return }
|
2019-07-13 14:47:00 +02:00
|
|
|
assert(Thread.isMainThread)
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-06-01 23:05:55 +02:00
|
|
|
os_log(.debug, log: log, "Syncing taggings with %ld subscriptions.", subscriptions.count)
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
// Set up some structures to make syncing easier
|
|
|
|
let folderDict: [String: Folder] = {
|
|
|
|
if let folders = account.folders {
|
|
|
|
return Dictionary(uniqueKeysWithValues: folders.map { ($0.name ?? "", $0) } )
|
|
|
|
} else {
|
|
|
|
return [String: Folder]()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
let taggingsDict = subscriptions.reduce([String: [ReaderAPISubscription]]()) { (dict, subscription) in
|
2019-05-28 19:08:15 +02:00
|
|
|
var taggedFeeds = dict
|
2019-06-01 23:05:55 +02:00
|
|
|
|
|
|
|
// For each category that this feed belongs to, add the feed to that name in the dict
|
|
|
|
subscription.categories.forEach({ (category) in
|
|
|
|
let categoryName = category.categoryLabel.replacingOccurrences(of: "user/-/label/", with: "")
|
|
|
|
|
|
|
|
if var taggedFeed = taggedFeeds[categoryName] {
|
|
|
|
taggedFeed.append(subscription)
|
|
|
|
taggedFeeds[categoryName] = taggedFeed
|
|
|
|
} else {
|
|
|
|
taggedFeeds[categoryName] = [subscription]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
return taggedFeeds
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync the folders
|
|
|
|
for (folderName, groupedTaggings) in taggingsDict {
|
|
|
|
|
|
|
|
guard let folder = folderDict[folderName] else { return }
|
|
|
|
|
|
|
|
let taggingFeedIDs = groupedTaggings.map { String($0.feedID) }
|
|
|
|
|
|
|
|
// Move any feeds not in the folder to the account
|
2019-11-15 03:11:41 +01:00
|
|
|
for feed in folder.topLevelWebFeeds {
|
|
|
|
if !taggingFeedIDs.contains(feed.webFeedID) {
|
|
|
|
folder.removeWebFeed(feed)
|
2019-07-13 14:47:00 +02:00
|
|
|
clearFolderRelationship(for: feed, withFolderName: folder.name ?? "")
|
2019-11-15 03:11:41 +01:00
|
|
|
account.addWebFeed(feed)
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add any feeds not in the folder
|
2019-11-15 03:11:41 +01:00
|
|
|
let folderFeedIds = folder.topLevelWebFeeds.map { $0.webFeedID }
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-06-01 23:05:55 +02:00
|
|
|
for subscription in groupedTaggings {
|
|
|
|
let taggingFeedID = String(subscription.feedID)
|
2019-05-28 19:08:15 +02:00
|
|
|
if !folderFeedIds.contains(taggingFeedID) {
|
2019-11-15 03:11:41 +01:00
|
|
|
guard let feed = account.existingWebFeed(withWebFeedID: taggingFeedID) else {
|
2019-05-28 19:08:15 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-07-13 14:47:00 +02:00
|
|
|
saveFolderRelationship(for: feed, withFolderName: folderName, id: String(subscription.feedID))
|
2019-11-15 03:11:41 +01:00
|
|
|
folder.addWebFeed(feed)
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-01 23:05:55 +02:00
|
|
|
let taggedFeedIDs = Set(subscriptions.map { String($0.feedID) })
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
// Remove all feeds from the account container that have a tag
|
2019-11-15 03:11:41 +01:00
|
|
|
for feed in account.topLevelWebFeeds {
|
|
|
|
if taggedFeedIDs.contains(feed.webFeedID) {
|
|
|
|
account.removeWebFeed(feed)
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendArticleStatuses(_ statuses: [SyncStatus],
|
|
|
|
apiCall: ([Int], @escaping (Result<Void, Error>) -> Void) -> Void,
|
|
|
|
completion: @escaping (() -> Void)) {
|
|
|
|
|
|
|
|
guard !statuses.isEmpty else {
|
|
|
|
completion()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let group = DispatchGroup()
|
|
|
|
|
|
|
|
let articleIDs = statuses.compactMap { Int($0.articleID) }
|
|
|
|
let articleIDGroups = articleIDs.chunked(into: 1000)
|
|
|
|
for articleIDGroup in articleIDGroups {
|
|
|
|
|
|
|
|
group.enter()
|
|
|
|
apiCall(articleIDGroup) { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
self.database.deleteSelectedForProcessing(articleIDGroup.map { String($0) } )
|
|
|
|
group.leave()
|
|
|
|
case .failure(let error):
|
|
|
|
os_log(.error, log: self.log, "Article status sync call failed: %@.", error.localizedDescription)
|
|
|
|
self.database.resetSelectedForProcessing(articleIDGroup.map { String($0) } )
|
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
group.notify(queue: DispatchQueue.main) {
|
|
|
|
completion()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func clearFolderRelationship(for feed: WebFeed, withFolderName folderName: String) {
|
2019-05-28 19:08:15 +02:00
|
|
|
if var folderRelationship = feed.folderRelationship {
|
|
|
|
folderRelationship[folderName] = nil
|
|
|
|
feed.folderRelationship = folderRelationship
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func saveFolderRelationship(for feed: WebFeed, withFolderName folderName: String, id: String) {
|
2019-05-28 19:08:15 +02:00
|
|
|
if var folderRelationship = feed.folderRelationship {
|
|
|
|
folderRelationship[folderName] = id
|
|
|
|
feed.folderRelationship = folderRelationship
|
|
|
|
} else {
|
|
|
|
feed.folderRelationship = [folderName: id]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func decideBestFeedChoice(account: Account, url: String, name: String?, container: Container, choices: [ReaderAPISubscriptionChoice], completion: @escaping (Result<WebFeed, Error>) -> Void) {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
let feedSpecifiers: [FeedSpecifier] = choices.map { choice in
|
|
|
|
let source = url == choice.url ? FeedSpecifier.Source.UserEntered : FeedSpecifier.Source.HTMLLink
|
|
|
|
let specifier = FeedSpecifier(title: choice.name, urlString: choice.url, source: source)
|
|
|
|
return specifier
|
|
|
|
}
|
|
|
|
|
|
|
|
if let bestSpecifier = FeedSpecifier.bestFeed(in: Set(feedSpecifiers)) {
|
|
|
|
if let bestSubscription = choices.filter({ bestSpecifier.urlString == $0.url }).first {
|
2019-11-15 03:11:41 +01:00
|
|
|
createWebFeed(for: account, url: bestSubscription.url, name: name, container: container, completion: completion)
|
2019-05-28 19:08:15 +02:00
|
|
|
} else {
|
|
|
|
DispatchQueue.main.async {
|
2019-06-19 18:25:37 +02:00
|
|
|
completion(.failure(ReaderAPIAccountDelegateError.invalidParameter))
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DispatchQueue.main.async {
|
2019-06-19 18:25:37 +02:00
|
|
|
completion(.failure(ReaderAPIAccountDelegateError.invalidParameter))
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func createFeed( account: Account, subscription sub: ReaderAPISubscription, name: String?, container: Container, completion: @escaping (Result<WebFeed, Error>) -> Void) {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
let feed = account.createWebFeed(with: sub.name, url: sub.url, webFeedID: String(sub.feedID), homePageURL: sub.homePageURL)
|
2019-06-01 14:08:19 +02:00
|
|
|
feed.subscriptionID = String(sub.feedID)
|
2019-06-01 12:54:10 +02:00
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
account.addWebFeed(feed, to: container) { result in
|
2019-05-28 19:08:15 +02:00
|
|
|
switch result {
|
2019-06-01 12:54:10 +02:00
|
|
|
case .success:
|
|
|
|
if let name = name {
|
2019-11-15 03:11:41 +01:00
|
|
|
account.renameWebFeed(feed, to: name) { result in
|
2019-06-01 12:54:10 +02:00
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
self.initialFeedDownload(account: account, feed: feed, completion: completion)
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-01 12:54:10 +02:00
|
|
|
} else {
|
|
|
|
self.initialFeedDownload(account: account, feed: feed, completion: completion)
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
case .failure(let error):
|
2019-06-01 12:54:10 +02:00
|
|
|
completion(.failure(error))
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-01 12:54:10 +02:00
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func initialFeedDownload( account: Account, feed: WebFeed, completion: @escaping (Result<WebFeed, Error>) -> Void) {
|
2019-06-01 12:54:10 +02:00
|
|
|
|
|
|
|
// Download the initial articles
|
2019-11-15 03:11:41 +01:00
|
|
|
self.caller.retrieveEntries(webFeedID: feed.webFeedID) { result in
|
2019-06-01 12:54:10 +02:00
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let (entries, page)):
|
|
|
|
|
|
|
|
self.processEntries(account: account, entries: entries) {
|
2019-11-05 03:24:21 +01:00
|
|
|
self.refreshArticleStatus(for: account) { _ in
|
2019-08-30 18:47:08 +02:00
|
|
|
self.refreshArticles(account, page: page) {
|
2019-06-01 12:54:10 +02:00
|
|
|
self.refreshMissingArticles(account) {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
completion(.success(feed))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
func refreshArticles(_ account: Account, completion: @escaping (() -> Void)) {
|
|
|
|
|
|
|
|
os_log(.debug, log: log, "Refreshing articles...")
|
|
|
|
|
|
|
|
caller.retrieveEntries() { result in
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let (entries, page, lastPageNumber)):
|
|
|
|
|
|
|
|
if let last = lastPageNumber {
|
|
|
|
self.refreshProgress.addToNumberOfTasksAndRemaining(last - 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
self.processEntries(account: account, entries: entries) {
|
|
|
|
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
self.refreshArticles(account, page: page) {
|
|
|
|
os_log(.debug, log: self.log, "Done refreshing articles.")
|
|
|
|
completion()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
os_log(.error, log: self.log, "Refresh articles failed: %@.", error.localizedDescription)
|
|
|
|
completion()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-17 07:45:59 +01:00
|
|
|
func refreshMissingArticles(_ account: Account, completion: @escaping VoidCompletionBlock) {
|
2019-12-18 06:15:20 +01:00
|
|
|
account.fetchArticleIDsForStatusesWithoutArticlesNewerThanCutoffDate { articleIDsResult in
|
2019-12-17 07:45:59 +01:00
|
|
|
|
2019-12-18 06:15:20 +01:00
|
|
|
func process(_ fetchedArticleIDs: Set<String>) {
|
|
|
|
os_log(.debug, log: self.log, "Refreshing missing articles...")
|
|
|
|
let group = DispatchGroup()
|
2019-07-08 04:56:52 +02:00
|
|
|
|
2019-12-18 06:15:20 +01:00
|
|
|
let articleIDs = Array(fetchedArticleIDs)
|
|
|
|
let chunkedArticleIDs = articleIDs.chunked(into: 100)
|
2019-07-08 04:56:52 +02:00
|
|
|
|
2019-12-18 06:15:20 +01:00
|
|
|
for chunk in chunkedArticleIDs {
|
|
|
|
group.enter()
|
|
|
|
self.caller.retrieveEntries(articleIDs: chunk) { result in
|
2019-07-08 04:56:52 +02:00
|
|
|
|
2019-12-18 06:15:20 +01:00
|
|
|
switch result {
|
|
|
|
case .success(let entries):
|
|
|
|
self.processEntries(account: account, entries: entries) {
|
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
os_log(.error, log: self.log, "Refresh missing articles failed: %@.", error.localizedDescription)
|
|
|
|
group.leave()
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
2019-12-18 06:15:20 +01:00
|
|
|
}
|
2019-08-30 18:47:08 +02:00
|
|
|
|
2019-12-18 06:15:20 +01:00
|
|
|
group.notify(queue: DispatchQueue.main) {
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
os_log(.debug, log: self.log, "Done refreshing missing articles.")
|
|
|
|
completion()
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-18 06:15:20 +01:00
|
|
|
switch articleIDsResult {
|
|
|
|
case .success(let articleIDs):
|
|
|
|
process(articleIDs)
|
|
|
|
case .failure:
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
completion()
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func refreshArticles(_ account: Account, page: String?, completion: @escaping (() -> Void)) {
|
|
|
|
|
|
|
|
guard let page = page else {
|
|
|
|
completion()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
caller.retrieveEntries(page: page) { result in
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let (entries, nextPage)):
|
|
|
|
|
|
|
|
self.processEntries(account: account, entries: entries) {
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
self.refreshArticles(account, page: nextPage, completion: completion)
|
|
|
|
}
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
os_log(.error, log: self.log, "Refresh articles for additional pages failed: %@.", error.localizedDescription)
|
|
|
|
completion()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-17 07:45:59 +01:00
|
|
|
func processEntries(account: Account, entries: [ReaderAPIEntry]?, completion: @escaping VoidCompletionBlock) {
|
2019-06-10 22:53:35 +02:00
|
|
|
let parsedItems = mapEntriesToParsedItems(account: account, entries: entries)
|
2019-11-15 03:11:41 +01:00
|
|
|
let webFeedIDsAndItems = Dictionary(grouping: parsedItems, by: { item in item.feedURL } ).mapValues { Set($0) }
|
2019-12-17 07:45:59 +01:00
|
|
|
account.update(webFeedIDsAndItems: webFeedIDsAndItems, defaultRead: true) { _ in
|
|
|
|
completion()
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
func mapEntriesToParsedItems(account: Account, entries: [ReaderAPIEntry]?) -> Set<ParsedItem> {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
guard let entries = entries else {
|
|
|
|
return Set<ParsedItem>()
|
|
|
|
}
|
|
|
|
|
|
|
|
let parsedItems: [ParsedItem] = entries.map { entry in
|
2019-06-10 22:53:35 +02:00
|
|
|
// let authors = Set([ParsedAuthor(name: entry.authorName, url: entry.jsonFeed?.jsonFeedAuthor?.url, avatarURL: entry.jsonFeed?.jsonFeedAuthor?.avatarURL, emailAddress: nil)])
|
|
|
|
// let feed = account.idToFeedDictionary[entry.origin.streamId!]! // TODO clean this up
|
|
|
|
|
2019-06-11 21:37:21 +02:00
|
|
|
return ParsedItem(syncServiceID: entry.uniqueID(), uniqueID: entry.uniqueID(), feedURL: entry.origin.streamId!, url: nil, externalURL: entry.alternates.first?.url, title: entry.title, contentHTML: entry.summary.content, contentText: nil, summary: entry.summary.content, imageURL: nil, bannerImageURL: nil, datePublished: entry.parseDatePublished(), dateModified: nil, authors: nil, tags: nil, attachments: nil)
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Set(parsedItems)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func syncArticleReadState(account: Account, articleIDs: [Int]?) {
|
|
|
|
guard let articleIDs = articleIDs else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:47:08 +02:00
|
|
|
let feedbinUnreadArticleIDs = Set(articleIDs.map { String($0) } )
|
2019-12-17 07:45:59 +01:00
|
|
|
account.fetchUnreadArticleIDs { articleIDsResult in
|
|
|
|
guard let currentUnreadArticleIDs = try? articleIDsResult.get() else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-08 05:57:23 +01:00
|
|
|
// Mark articles as unread
|
|
|
|
let deltaUnreadArticleIDs = feedbinUnreadArticleIDs.subtracting(currentUnreadArticleIDs)
|
2019-12-17 07:45:59 +01:00
|
|
|
account.markAsUnread(deltaUnreadArticleIDs)
|
2019-12-08 05:57:23 +01:00
|
|
|
|
|
|
|
// Mark articles as read
|
|
|
|
let deltaReadArticleIDs = currentUnreadArticleIDs.subtracting(feedbinUnreadArticleIDs)
|
2019-12-17 07:45:59 +01:00
|
|
|
account.markAsRead(deltaReadArticleIDs)
|
2019-12-08 05:57:23 +01:00
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func syncArticleStarredState(account: Account, articleIDs: [Int]?) {
|
|
|
|
guard let articleIDs = articleIDs else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:47:08 +02:00
|
|
|
let feedbinStarredArticleIDs = Set(articleIDs.map { String($0) } )
|
2019-12-17 07:45:59 +01:00
|
|
|
account.fetchStarredArticleIDs { articleIDsResult in
|
|
|
|
guard let currentStarredArticleIDs = try? articleIDsResult.get() else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-08 07:23:44 +01:00
|
|
|
// Mark articles as starred
|
|
|
|
let deltaStarredArticleIDs = feedbinStarredArticleIDs.subtracting(currentStarredArticleIDs)
|
2019-12-17 07:45:59 +01:00
|
|
|
account.markAsStarred(deltaStarredArticleIDs)
|
2019-12-08 07:23:44 +01:00
|
|
|
|
|
|
|
// Mark articles as unstarred
|
|
|
|
let deltaUnstarredArticleIDs = currentStarredArticleIDs.subtracting(feedbinStarredArticleIDs)
|
2019-12-17 07:45:59 +01:00
|
|
|
account.markAsUnstarred(deltaUnstarredArticleIDs)
|
2019-12-08 07:23:44 +01:00
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
2019-06-01 12:54:10 +02:00
|
|
|
|
2019-08-30 18:47:08 +02:00
|
|
|
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func deleteTagging(for account: Account, with feed: WebFeed, from container: Container?, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-06-01 12:54:10 +02:00
|
|
|
|
2019-06-16 19:14:23 +02:00
|
|
|
if let folder = container as? Folder, let feedName = feed.subscriptionID {
|
|
|
|
caller.deleteTagging(subscriptionID: feedName, tagName: folder.name ?? "") { result in
|
2019-06-01 12:54:10 +02:00
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.clearFolderRelationship(for: feed, withFolderName: folder.name ?? "")
|
2019-11-15 03:11:41 +01:00
|
|
|
folder.removeWebFeed(feed)
|
2019-06-01 12:54:10 +02:00
|
|
|
account.addFeedIfNotInAnyFolder(feed)
|
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
case .failure(let error):
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
let wrappedError = AccountError.wrappedError(error: error, account: account)
|
|
|
|
completion(.failure(wrappedError))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if let account = container as? Account {
|
2019-11-15 03:11:41 +01:00
|
|
|
account.removeWebFeed(feed)
|
2019-06-01 12:54:10 +02:00
|
|
|
}
|
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func deleteSubscription(for account: Account, with feed: WebFeed, from container: Container?, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-06-01 12:54:10 +02:00
|
|
|
|
|
|
|
// This error should never happen
|
|
|
|
guard let subscriptionID = feed.subscriptionID else {
|
|
|
|
completion(.failure(FeedbinAccountDelegateError.invalidParameter))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
caller.deleteSubscription(subscriptionID: subscriptionID) { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
DispatchQueue.main.async {
|
2019-11-15 03:11:41 +01:00
|
|
|
account.removeWebFeed(feed)
|
2019-06-01 12:54:10 +02:00
|
|
|
if let folders = account.folders {
|
|
|
|
for folder in folders {
|
2019-11-15 03:11:41 +01:00
|
|
|
folder.removeWebFeed(feed)
|
2019-06-01 12:54:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
case .failure(let error):
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
let wrappedError = AccountError.wrappedError(error: error, account: account)
|
|
|
|
completion(.failure(wrappedError))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
}
|