2019-05-03 01:17:52 +02:00
|
|
|
|
//
|
|
|
|
|
// FeedbinAccountDelegate.swift
|
|
|
|
|
// Account
|
|
|
|
|
//
|
|
|
|
|
// Created by Maurice Parker on 5/2/19.
|
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
2019-05-14 22:34:05 +02:00
|
|
|
|
import Articles
|
2019-05-07 18:10:00 +02:00
|
|
|
|
import RSCore
|
2019-12-17 07:45:59 +01:00
|
|
|
|
import RSDatabase
|
2019-05-11 23:07:27 +02:00
|
|
|
|
import RSParser
|
2019-05-03 01:17:52 +02:00
|
|
|
|
import RSWeb
|
2019-05-15 01:24:19 +02:00
|
|
|
|
import SyncDatabase
|
2019-05-08 15:20:29 +02:00
|
|
|
|
import os.log
|
2019-05-03 01:17:52 +02:00
|
|
|
|
|
2019-05-09 01:13:54 +02:00
|
|
|
|
public enum FeedbinAccountDelegateError: String, Error {
|
|
|
|
|
case invalidParameter = "There was an invalid parameter passed."
|
2019-11-05 03:24:21 +01:00
|
|
|
|
case unknown = "An unknown error occurred."
|
2019-05-09 01:13:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-03 01:17:52 +02:00
|
|
|
|
final class FeedbinAccountDelegate: AccountDelegate {
|
2019-05-08 15:20:29 +02:00
|
|
|
|
|
2019-05-15 01:24:19 +02:00
|
|
|
|
private let database: SyncDatabase
|
|
|
|
|
|
2019-05-08 15:20:29 +02:00
|
|
|
|
private let caller: FeedbinAPICaller
|
|
|
|
|
private var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "Feedbin")
|
|
|
|
|
|
2019-09-20 18:34:31 +02:00
|
|
|
|
let behaviors: AccountBehaviors = [.disallowFeedCopyInRootFolder]
|
2019-05-04 18:48:48 +02:00
|
|
|
|
let server: String? = "api.feedbin.com"
|
2019-06-20 00:50:32 +02:00
|
|
|
|
var isOPMLImportInProgress = false
|
2019-05-03 01:17:52 +02:00
|
|
|
|
|
2019-05-05 10:25:21 +02:00
|
|
|
|
var credentials: Credentials? {
|
|
|
|
|
didSet {
|
|
|
|
|
caller.credentials = credentials
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-03 01:17:52 +02:00
|
|
|
|
|
2019-05-22 22:40:34 +02:00
|
|
|
|
weak var accountMetadata: AccountMetadata? {
|
2019-05-05 14:49:59 +02:00
|
|
|
|
didSet {
|
|
|
|
|
caller.accountMetadata = accountMetadata
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-05 14:21:26 +02:00
|
|
|
|
|
2019-05-15 01:24:19 +02:00
|
|
|
|
init(dataFolder: String, transport: Transport?) {
|
|
|
|
|
|
|
|
|
|
let databaseFilePath = (dataFolder as NSString).appendingPathComponent("Sync.sqlite3")
|
|
|
|
|
database = SyncDatabase(databaseFilePath: databaseFilePath)
|
2019-05-12 14:22:33 +02:00
|
|
|
|
|
|
|
|
|
if transport != nil {
|
|
|
|
|
|
|
|
|
|
caller = FeedbinAPICaller(transport: transport!)
|
|
|
|
|
|
|
|
|
|
} 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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
caller = FeedbinAPICaller(transport: URLSession(configuration: sessionConfiguration))
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-03 01:17:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-04 15:54:07 +02:00
|
|
|
|
var refreshProgress = DownloadProgress(numberOfTasks: 0)
|
|
|
|
|
|
2019-05-26 18:54:32 +02:00
|
|
|
|
func refreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-05-13 01:32:32 +02:00
|
|
|
|
|
2019-11-08 02:07:10 +01:00
|
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(5)
|
2019-05-14 20:10:07 +02:00
|
|
|
|
|
2019-05-22 22:40:34 +02:00
|
|
|
|
refreshAccount(account) { result in
|
2019-05-05 22:41:20 +02:00
|
|
|
|
switch result {
|
|
|
|
|
case .success():
|
2019-11-05 03:24:21 +01:00
|
|
|
|
|
|
|
|
|
self.refreshArticlesAndStatuses(account) { result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success():
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
self.refreshProgress.clear()
|
|
|
|
|
let wrappedError = AccountError.wrappedError(error: error, account: account)
|
|
|
|
|
completion(.failure(wrappedError))
|
2019-05-17 21:56:27 +02:00
|
|
|
|
}
|
2019-05-14 00:04:12 +02:00
|
|
|
|
}
|
2019-05-07 17:51:41 +02:00
|
|
|
|
}
|
2019-05-13 01:32:32 +02:00
|
|
|
|
|
2019-05-05 22:41:20 +02:00
|
|
|
|
case .failure(let error):
|
2019-05-07 17:51:41 +02:00
|
|
|
|
DispatchQueue.main.async {
|
2019-05-22 22:40:34 +02:00
|
|
|
|
self.refreshProgress.clear()
|
2019-05-26 18:54:32 +02:00
|
|
|
|
let wrappedError = AccountError.wrappedError(error: error, account: account)
|
|
|
|
|
completion(.failure(wrappedError))
|
2019-05-07 17:51:41 +02:00
|
|
|
|
}
|
2019-05-06 17:53:20 +02:00
|
|
|
|
}
|
2019-05-13 01:32:32 +02:00
|
|
|
|
|
2019-05-06 17:53:20 +02:00
|
|
|
|
}
|
2019-05-13 01:32:32 +02:00
|
|
|
|
|
2019-05-06 17:53:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 03:24:21 +01:00
|
|
|
|
func sendArticleStatus(for account: Account, completion: @escaping ((Result<Void, Error>) -> Void)) {
|
2019-05-14 00:04:12 +02:00
|
|
|
|
|
2019-05-15 18:52:56 +02:00
|
|
|
|
os_log(.debug, log: log, "Sending article statuses...")
|
|
|
|
|
|
2019-12-16 21:49:07 +01:00
|
|
|
|
database.selectForProcessing { result in
|
2019-11-05 03:24:21 +01:00
|
|
|
|
|
2019-12-16 21:49:07 +01:00
|
|
|
|
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()
|
|
|
|
|
var errorOccurred = false
|
|
|
|
|
|
|
|
|
|
group.enter()
|
|
|
|
|
self.sendArticleStatuses(createUnreadStatuses, apiCall: self.caller.createUnreadEntries) { result in
|
|
|
|
|
group.leave()
|
|
|
|
|
if case .failure = result {
|
|
|
|
|
errorOccurred = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group.enter()
|
|
|
|
|
self.sendArticleStatuses(deleteUnreadStatuses, apiCall: self.caller.deleteUnreadEntries) { result in
|
|
|
|
|
group.leave()
|
|
|
|
|
if case .failure = result {
|
|
|
|
|
errorOccurred = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group.enter()
|
|
|
|
|
self.sendArticleStatuses(createStarredStatuses, apiCall: self.caller.createStarredEntries) { result in
|
|
|
|
|
group.leave()
|
|
|
|
|
if case .failure = result {
|
|
|
|
|
errorOccurred = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group.enter()
|
|
|
|
|
self.sendArticleStatuses(deleteStarredStatuses, apiCall: self.caller.deleteStarredEntries) { result in
|
|
|
|
|
group.leave()
|
|
|
|
|
if case .failure = result {
|
|
|
|
|
errorOccurred = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group.notify(queue: DispatchQueue.main) {
|
|
|
|
|
os_log(.debug, log: self.log, "Done sending article statuses.")
|
|
|
|
|
if errorOccurred {
|
|
|
|
|
completion(.failure(FeedbinAccountDelegateError.unknown))
|
|
|
|
|
} else {
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-05 03:24:21 +01:00
|
|
|
|
}
|
2019-12-16 21:49:07 +01:00
|
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
|
case .success(let syncStatuses):
|
|
|
|
|
processStatuses(syncStatuses)
|
|
|
|
|
case .failure(let databaseError):
|
|
|
|
|
completion(.failure(databaseError))
|
2019-11-05 03:24:21 +01:00
|
|
|
|
}
|
2019-05-15 18:52:56 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 03:24:21 +01:00
|
|
|
|
func refreshArticleStatus(for account: Account, completion: @escaping ((Result<Void, Error>) -> Void)) {
|
2019-06-23 20:56:11 +02:00
|
|
|
|
|
2019-05-14 00:04:12 +02:00
|
|
|
|
os_log(.debug, log: log, "Refreshing article statuses...")
|
|
|
|
|
|
|
|
|
|
let group = DispatchGroup()
|
2019-11-05 03:24:21 +01:00
|
|
|
|
var errorOccurred = false
|
|
|
|
|
|
2019-05-14 00:04:12 +02:00
|
|
|
|
group.enter()
|
2019-05-22 22:40:34 +02:00
|
|
|
|
caller.retrieveUnreadEntries() { result in
|
2019-05-14 00:04:12 +02:00
|
|
|
|
switch result {
|
|
|
|
|
case .success(let articleIDs):
|
2019-05-22 22:40:34 +02:00
|
|
|
|
self.syncArticleReadState(account: account, articleIDs: articleIDs)
|
2019-05-14 13:20:53 +02:00
|
|
|
|
group.leave()
|
2019-05-14 00:04:12 +02:00
|
|
|
|
case .failure(let error):
|
2019-11-05 03:24:21 +01:00
|
|
|
|
errorOccurred = true
|
2019-05-14 00:04:12 +02:00
|
|
|
|
os_log(.info, log: self.log, "Retrieving unread entries failed: %@.", error.localizedDescription)
|
2019-05-22 02:15:24 +02:00
|
|
|
|
group.leave()
|
2019-05-14 00:04:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group.enter()
|
2019-05-22 22:40:34 +02:00
|
|
|
|
caller.retrieveStarredEntries() { result in
|
2019-05-14 00:04:12 +02:00
|
|
|
|
switch result {
|
|
|
|
|
case .success(let articleIDs):
|
2019-05-22 22:40:34 +02:00
|
|
|
|
self.syncArticleStarredState(account: account, articleIDs: articleIDs)
|
2019-05-14 13:20:53 +02:00
|
|
|
|
group.leave()
|
2019-05-14 00:04:12 +02:00
|
|
|
|
case .failure(let error):
|
2019-11-05 03:24:21 +01:00
|
|
|
|
errorOccurred = true
|
2019-05-14 00:04:12 +02:00
|
|
|
|
os_log(.info, log: self.log, "Retrieving starred entries failed: %@.", error.localizedDescription)
|
2019-05-22 02:15:24 +02:00
|
|
|
|
group.leave()
|
2019-05-14 00:04:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-22 22:40:34 +02:00
|
|
|
|
group.notify(queue: DispatchQueue.main) {
|
2019-05-14 00:04:12 +02:00
|
|
|
|
os_log(.debug, log: self.log, "Done refreshing article statuses.")
|
2019-11-05 03:24:21 +01:00
|
|
|
|
if errorOccurred {
|
|
|
|
|
completion(.failure(FeedbinAccountDelegateError.unknown))
|
|
|
|
|
} else {
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
2019-05-14 00:04:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 19:26:23 +02:00
|
|
|
|
func importOPML(for account:Account, opmlFile: URL, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-06-23 20:56:11 +02:00
|
|
|
|
|
2019-05-11 23:07:27 +02:00
|
|
|
|
var fileData: Data?
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
fileData = try Data(contentsOf: opmlFile)
|
|
|
|
|
} catch {
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard let opmlData = fileData else {
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 17:04:13 +02:00
|
|
|
|
os_log(.debug, log: log, "Begin importing OPML...")
|
2019-06-20 00:50:32 +02:00
|
|
|
|
isOPMLImportInProgress = true
|
2019-06-10 23:17:54 +02:00
|
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(1)
|
2019-05-17 17:44:22 +02:00
|
|
|
|
|
2019-05-22 22:40:34 +02:00
|
|
|
|
caller.importOPML(opmlData: opmlData) { result in
|
2019-05-17 17:04:13 +02:00
|
|
|
|
switch result {
|
|
|
|
|
case .success(let importResult):
|
|
|
|
|
if importResult.complete {
|
|
|
|
|
os_log(.debug, log: self.log, "Import OPML done.")
|
2019-06-10 23:17:54 +02:00
|
|
|
|
self.refreshProgress.completeTask()
|
2019-06-20 00:50:32 +02:00
|
|
|
|
self.isOPMLImportInProgress = false
|
2019-05-17 17:04:13 +02:00
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-05-22 22:40:34 +02:00
|
|
|
|
self.checkImportResult(opmlImportResultID: importResult.importResultID, completion: completion)
|
2019-05-17 17:04:13 +02:00
|
|
|
|
}
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
os_log(.debug, log: self.log, "Import OPML failed.")
|
2019-06-10 23:17:54 +02:00
|
|
|
|
self.refreshProgress.completeTask()
|
2019-06-20 00:50:32 +02:00
|
|
|
|
self.isOPMLImportInProgress = false
|
2019-05-17 17:04:13 +02:00
|
|
|
|
DispatchQueue.main.async {
|
2019-05-26 18:54:32 +02:00
|
|
|
|
let wrappedError = AccountError.wrappedError(error: error, account: account)
|
|
|
|
|
completion(.failure(wrappedError))
|
2019-05-17 17:04:13 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-11 23:07:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 17:04:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-30 21:36:21 +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-07 00:34:41 +02:00
|
|
|
|
func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-06-23 20:56:11 +02:00
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
guard folder.hasAtLeastOneWebFeed() else {
|
2019-06-07 12:03:54 +02:00
|
|
|
|
folder.name = name
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 21:48:50 +02:00
|
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(1)
|
2019-05-06 17:53:20 +02:00
|
|
|
|
caller.renameTag(oldName: folder.name ?? "", newName: name) { result in
|
2019-06-16 21:48:50 +02:00
|
|
|
|
self.refreshProgress.completeTask()
|
2019-05-06 17:53:20 +02:00
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
2019-05-07 17:51:41 +02:00
|
|
|
|
DispatchQueue.main.async {
|
2019-06-07 12:03:54 +02:00
|
|
|
|
self.renameFolderRelationship(for: account, fromName: folder.name ?? "", toName: name)
|
2019-05-07 17:51:41 +02:00
|
|
|
|
folder.name = name
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
2019-05-06 17:53:20 +02:00
|
|
|
|
case .failure(let error):
|
2019-05-07 17:51:41 +02:00
|
|
|
|
DispatchQueue.main.async {
|
2019-05-26 18:54:32 +02:00
|
|
|
|
let wrappedError = AccountError.wrappedError(error: error, account: account)
|
|
|
|
|
completion(.failure(wrappedError))
|
2019-05-07 17:51:41 +02:00
|
|
|
|
}
|
2019-05-05 22:41:20 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-06 17:53:20 +02:00
|
|
|
|
|
2019-05-03 01:17:52 +02:00
|
|
|
|
}
|
2019-05-07 00:34:41 +02:00
|
|
|
|
|
2019-05-30 03:53:00 +02:00
|
|
|
|
func removeFolder(for account: Account, with folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-06-23 20:56:11 +02:00
|
|
|
|
|
2019-05-07 00:34:41 +02:00
|
|
|
|
// Feedbin uses tags and if at least one feed isn't tagged, then the folder doesn't exist on their system
|
2019-11-15 03:11:41 +01:00
|
|
|
|
guard folder.hasAtLeastOneWebFeed() else {
|
2019-05-30 03:53:00 +02:00
|
|
|
|
account.removeFolder(folder)
|
2019-09-01 17:49:35 +02:00
|
|
|
|
completion(.success(()))
|
2019-05-07 00:34:41 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-30 21:36:21 +02:00
|
|
|
|
let group = DispatchGroup()
|
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
for feed in folder.topLevelWebFeeds {
|
2019-06-06 22:22:25 +02:00
|
|
|
|
|
|
|
|
|
if feed.folderRelationship?.count ?? 0 > 1 {
|
|
|
|
|
|
|
|
|
|
if let feedTaggingID = feed.folderRelationship?[folder.name ?? ""] {
|
|
|
|
|
group.enter()
|
2019-06-16 21:48:50 +02:00
|
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(1)
|
2019-06-06 22:22:25 +02:00
|
|
|
|
caller.deleteTagging(taggingID: feedTaggingID) { result in
|
2019-06-16 21:48:50 +02:00
|
|
|
|
self.refreshProgress.completeTask()
|
2019-06-06 22:22:25 +02:00
|
|
|
|
group.leave()
|
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
self.clearFolderRelationship(for: feed, withFolderName: folder.name ?? "")
|
|
|
|
|
}
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
os_log(.error, log: self.log, "Remove feed error: %@.", error.localizedDescription)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-07 17:51:41 +02:00
|
|
|
|
}
|
2019-06-06 22:22:25 +02:00
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
if let subscriptionID = feed.subscriptionID {
|
|
|
|
|
group.enter()
|
2019-06-16 21:48:50 +02:00
|
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(1)
|
2019-06-06 22:22:25 +02:00
|
|
|
|
caller.deleteSubscription(subscriptionID: subscriptionID) { result in
|
2019-06-16 21:48:50 +02:00
|
|
|
|
self.refreshProgress.completeTask()
|
2019-06-06 22:22:25 +02:00
|
|
|
|
group.leave()
|
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
DispatchQueue.main.async {
|
2019-11-15 03:11:41 +01:00
|
|
|
|
account.clearWebFeedMetadata(feed)
|
2019-06-06 22:22:25 +02:00
|
|
|
|
}
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
os_log(.error, log: self.log, "Remove feed error: %@.", error.localizedDescription)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 17:51:41 +02:00
|
|
|
|
}
|
2019-06-06 22:22:25 +02:00
|
|
|
|
|
2019-05-07 00:34:41 +02:00
|
|
|
|
}
|
2019-06-06 22:22:25 +02:00
|
|
|
|
|
2019-05-07 00:34:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-30 21:36:21 +02:00
|
|
|
|
group.notify(queue: DispatchQueue.main) {
|
|
|
|
|
account.removeFolder(folder)
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 00:34:41 +02:00
|
|
|
|
}
|
2019-05-03 01:17:52 +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-06-23 20:56:11 +02:00
|
|
|
|
|
2019-06-16 21:48:50 +02:00
|
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(1)
|
2019-05-22 22:40:34 +02:00
|
|
|
|
caller.createSubscription(url: url) { result in
|
2019-06-16 21:48:50 +02:00
|
|
|
|
self.refreshProgress.completeTask()
|
2019-05-09 00:41:19 +02:00
|
|
|
|
switch result {
|
|
|
|
|
case .success(let subResult):
|
|
|
|
|
switch subResult {
|
2019-05-10 17:14:24 +02:00
|
|
|
|
case .created(let subscription):
|
2019-05-28 16:45:02 +02:00
|
|
|
|
self.createFeed(account: account, subscription: subscription, name: name, container: container, completion: completion)
|
2019-05-10 17:14:24 +02:00
|
|
|
|
case .multipleChoice(let choices):
|
2019-05-28 16:45:02 +02:00
|
|
|
|
self.decideBestFeedChoice(account: account, url: url, name: name, container: container, choices: choices, completion: completion)
|
2019-05-09 00:41:19 +02:00
|
|
|
|
case .alreadySubscribed:
|
|
|
|
|
DispatchQueue.main.async {
|
2019-05-10 17:14:24 +02:00
|
|
|
|
completion(.failure(AccountError.createErrorAlreadySubscribed))
|
2019-05-09 00:41:19 +02:00
|
|
|
|
}
|
|
|
|
|
case .notFound:
|
|
|
|
|
DispatchQueue.main.async {
|
2019-05-10 17:14:24 +02:00
|
|
|
|
completion(.failure(AccountError.createErrorNotFound))
|
2019-05-09 00:41:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
DispatchQueue.main.async {
|
2019-05-26 18:54:32 +02:00
|
|
|
|
let wrappedError = AccountError.wrappedError(error: error, account: account)
|
|
|
|
|
completion(.failure(wrappedError))
|
2019-05-09 00:41:19 +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-06-23 20:56:11 +02:00
|
|
|
|
|
2019-05-09 01:13:54 +02:00
|
|
|
|
// This error should never happen
|
|
|
|
|
guard let subscriptionID = feed.subscriptionID else {
|
|
|
|
|
completion(.failure(FeedbinAccountDelegateError.invalidParameter))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 21:48:50 +02:00
|
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(1)
|
2019-05-09 14:25:45 +02:00
|
|
|
|
caller.renameSubscription(subscriptionID: subscriptionID, newName: name) { result in
|
2019-06-16 21:48:50 +02:00
|
|
|
|
self.refreshProgress.completeTask()
|
2019-05-09 00:41:19 +02:00
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
feed.editedName = name
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
DispatchQueue.main.async {
|
2019-05-26 18:54:32 +02:00
|
|
|
|
let wrappedError = AccountError.wrappedError(error: error, account: account)
|
|
|
|
|
completion(.failure(wrappedError))
|
2019-05-09 00:41:19 +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-05-30 17:12:34 +02:00
|
|
|
|
if feed.folderRelationship?.count ?? 0 > 1 {
|
|
|
|
|
deleteTagging(for: account, with: feed, from: container, completion: completion)
|
|
|
|
|
} else {
|
|
|
|
|
deleteSubscription(for: account, with: feed, from: container, completion: completion)
|
2019-05-09 14:25:45 +02:00
|
|
|
|
}
|
2019-05-30 17:12:34 +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-05-30 17:12:34 +02:00
|
|
|
|
if from is Account {
|
2019-11-15 03:11:41 +01:00
|
|
|
|
addWebFeed(for: account, with: feed, to: to, completion: completion)
|
2019-05-30 17:12:34 +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-05-30 17:12:34 +02:00
|
|
|
|
case .failure(let error):
|
|
|
|
|
completion(.failure(error))
|
2019-05-09 14:25:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-30 17:12:34 +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-06-23 20:56:11 +02:00
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
if let folder = container as? Folder, let webFeedID = Int(feed.webFeedID) {
|
2019-06-16 21:48:50 +02:00
|
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(1)
|
2019-11-15 03:11:41 +01:00
|
|
|
|
caller.createTagging(webFeedID: webFeedID, name: folder.name ?? "") { result in
|
2019-06-16 21:48:50 +02:00
|
|
|
|
self.refreshProgress.completeTask()
|
2019-05-09 20:31:18 +02:00
|
|
|
|
switch result {
|
|
|
|
|
case .success(let taggingID):
|
|
|
|
|
DispatchQueue.main.async {
|
2019-05-22 22:40:34 +02:00
|
|
|
|
self.saveFolderRelationship(for: feed, withFolderName: folder.name ?? "", id: String(taggingID))
|
2019-11-15 03:11:41 +01:00
|
|
|
|
account.removeWebFeed(feed)
|
|
|
|
|
folder.addWebFeed(feed)
|
2019-05-09 20:31:18 +02:00
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
DispatchQueue.main.async {
|
2019-05-26 18:54:32 +02:00
|
|
|
|
let wrappedError = AccountError.wrappedError(error: error, account: account)
|
|
|
|
|
completion(.failure(wrappedError))
|
2019-05-09 20:31:18 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
DispatchQueue.main.async {
|
2019-05-30 17:12:34 +02:00
|
|
|
|
if let account = container as? Account {
|
|
|
|
|
account.addFeedIfNotInAnyFolder(feed)
|
2019-05-09 20:31:18 +02:00
|
|
|
|
}
|
2019-05-30 17:12:34 +02:00
|
|
|
|
completion(.success(()))
|
2019-05-09 20:31:18 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2019-05-09 23:09:21 +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-06-23 20:56:11 +02:00
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
if let existingFeed = account.existingWebFeed(withURL: feed.url) {
|
|
|
|
|
account.addWebFeed(existingFeed, to: container) { result in
|
2019-06-06 22:22:25 +02:00
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-11-15 03:11:41 +01:00
|
|
|
|
createWebFeed(for: account, url: feed.url, name: feed.editedName, container: container) { result in
|
2019-06-06 22:22:25 +02:00
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
2019-05-09 23:09:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-10 01:20:09 +02:00
|
|
|
|
func restoreFolder(for account: Account, folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-06-23 20:56:11 +02:00
|
|
|
|
|
2019-05-10 01:20:09 +02:00
|
|
|
|
let group = DispatchGroup()
|
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
for feed in folder.topLevelWebFeeds {
|
2019-05-09 23:09:21 +02:00
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
folder.topLevelWebFeeds.remove(feed)
|
2019-06-06 22:22:25 +02:00
|
|
|
|
|
2019-05-10 01:20:09 +02:00
|
|
|
|
group.enter()
|
2019-11-15 03:11:41 +01:00
|
|
|
|
restoreWebFeed(for: account, feed: feed, container: folder) { result in
|
2019-05-10 01:20:09 +02:00
|
|
|
|
group.leave()
|
2019-06-06 22:22:25 +02:00
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
break
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
os_log(.error, log: self.log, "Restore folder feed error: %@.", error.localizedDescription)
|
|
|
|
|
}
|
2019-05-09 23:09:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-10 01:20:09 +02:00
|
|
|
|
group.notify(queue: DispatchQueue.main) {
|
2019-06-06 22:22:25 +02:00
|
|
|
|
account.addFolder(folder)
|
2019-05-10 01:20:09 +02:00
|
|
|
|
completion(.success(()))
|
2019-05-09 23:09:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 22:34:05 +02:00
|
|
|
|
func markArticles(for account: Account, articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) -> Set<Article>? {
|
2019-06-23 20:56:11 +02:00
|
|
|
|
|
2019-05-15 01:24:19 +02:00
|
|
|
|
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-27 19:03:12 +02:00
|
|
|
|
}
|
2019-12-16 19:50:13 +01:00
|
|
|
|
|
2019-12-17 07:45:59 +01:00
|
|
|
|
return try? account.update(articles, statusKey: statusKey, flag: flag)
|
2019-05-14 22:34:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-03 01:17:52 +02:00
|
|
|
|
func accountDidInitialize(_ account: Account) {
|
2019-09-15 17:03:47 +02:00
|
|
|
|
credentials = try? account.retrieveCredentials(type: .basic)
|
2019-05-03 01:17:52 +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? = nil, completion: @escaping (Result<Credentials?, Error>) -> Void) {
|
2019-05-06 17:53:20 +02:00
|
|
|
|
|
|
|
|
|
let caller = FeedbinAPICaller(transport: transport)
|
|
|
|
|
caller.credentials = credentials
|
|
|
|
|
caller.validateCredentials() { result in
|
2019-05-07 17:51:41 +02:00
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(result)
|
|
|
|
|
}
|
2019-05-06 17:53:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
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.suspend()
|
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() {
|
2019-12-02 21:14:35 +01:00
|
|
|
|
caller.resume()
|
2019-11-30 07:57:14 +01:00
|
|
|
|
database.resume()
|
|
|
|
|
}
|
2019-05-03 01:17:52 +02:00
|
|
|
|
}
|
2019-05-05 22:41:20 +02:00
|
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
|
|
private extension FeedbinAccountDelegate {
|
|
|
|
|
|
2019-05-17 17:44:22 +02:00
|
|
|
|
func checkImportResult(opmlImportResultID: Int, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
|
2019-05-22 22:40:34 +02:00
|
|
|
|
Timer.scheduledTimer(withTimeInterval: 15, repeats: true) { timer in
|
2019-05-17 17:44:22 +02:00
|
|
|
|
|
|
|
|
|
os_log(.debug, log: self.log, "Checking status of OPML import...")
|
|
|
|
|
|
|
|
|
|
self.caller.retrieveOPMLImportResult(importID: opmlImportResultID) { result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success(let importResult):
|
|
|
|
|
if let result = importResult, result.complete {
|
|
|
|
|
os_log(.debug, log: self.log, "Checking status of OPML import successfully completed.")
|
|
|
|
|
timer.invalidate()
|
2019-06-10 23:17:54 +02:00
|
|
|
|
self.refreshProgress.completeTask()
|
2019-06-20 00:50:32 +02:00
|
|
|
|
self.isOPMLImportInProgress = false
|
2019-05-17 17:44:22 +02:00
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
os_log(.debug, log: self.log, "Import OPML check failed.")
|
|
|
|
|
timer.invalidate()
|
2019-06-10 23:17:54 +02:00
|
|
|
|
self.refreshProgress.completeTask()
|
2019-06-20 00:50:32 +02:00
|
|
|
|
self.isOPMLImportInProgress = false
|
2019-05-17 17:44:22 +02:00
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-26 18:35:07 +02:00
|
|
|
|
func refreshAccount(_ account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
|
|
|
|
|
|
caller.retrieveTags { result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success(let tags):
|
|
|
|
|
|
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
|
self.caller.retrieveSubscriptions { result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success(let subscriptions):
|
|
|
|
|
|
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
|
self.forceExpireFolderFeedRelationship(account, tags)
|
|
|
|
|
self.caller.retrieveTaggings { result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success(let taggings):
|
|
|
|
|
|
2019-11-08 02:07:10 +01:00
|
|
|
|
BatchUpdate.shared.perform {
|
|
|
|
|
self.syncFolders(account, tags)
|
|
|
|
|
self.syncFeeds(account, subscriptions)
|
|
|
|
|
self.syncFeedFolderRelationship(account, taggings)
|
2019-09-26 18:35:07 +02:00
|
|
|
|
}
|
2019-11-08 02:07:10 +01:00
|
|
|
|
|
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
|
completion(.success(()))
|
2019-09-26 18:35:07 +02:00
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 03:24:21 +01:00
|
|
|
|
func refreshArticlesAndStatuses(_ account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
|
self.sendArticleStatus(for: account) { result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
|
|
|
|
|
self.refreshArticleStatus(for: account) { result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
|
|
|
|
|
self.refreshArticles(account) { result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
|
|
|
|
|
self.refreshMissingArticles(account) { result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
self.refreshProgress.clear()
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-26 18:35:07 +02:00
|
|
|
|
// This function can be deleted if Feedbin updates their taggings.json service to
|
|
|
|
|
// show a change when a tag is renamed.
|
|
|
|
|
func forceExpireFolderFeedRelationship(_ account: Account, _ tags: [FeedbinTag]?) {
|
|
|
|
|
guard let tags = tags else { return }
|
|
|
|
|
|
|
|
|
|
let folderNames: [String] = {
|
|
|
|
|
if let folders = account.folders {
|
|
|
|
|
return folders.map { $0.name ?? "" }
|
|
|
|
|
} else {
|
|
|
|
|
return [String]()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
// Feedbin has a tag that we don't have a folder for. We might not get a new
|
|
|
|
|
// taggings response for it if it is a folder rename. Force expire the tagging
|
|
|
|
|
// so that we will for sure get the new tagging information.
|
|
|
|
|
tags.forEach { tag in
|
|
|
|
|
if !folderNames.contains(tag.name) {
|
|
|
|
|
accountMetadata?.conditionalGetInfo[FeedbinAPICaller.ConditionalGetKeys.taggings] = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-06 12:28:02 +02:00
|
|
|
|
func syncFolders(_ account: Account, _ tags: [FeedbinTag]?) {
|
|
|
|
|
guard let tags = tags else { return }
|
2019-07-07 23:24:50 +02:00
|
|
|
|
assert(Thread.isMainThread)
|
2019-05-08 15:20:29 +02:00
|
|
|
|
|
|
|
|
|
os_log(.debug, log: log, "Syncing folders with %ld tags.", tags.count)
|
|
|
|
|
|
2019-05-05 22:41:20 +02:00
|
|
|
|
let tagNames = tags.map { $0.name }
|
|
|
|
|
|
|
|
|
|
// Delete any folders not at Feedbin
|
|
|
|
|
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-07 23:24:50 +02:00
|
|
|
|
clearFolderRelationship(for: feed, withFolderName: folder.name ?? "")
|
2019-05-07 17:51:41 +02:00
|
|
|
|
}
|
2019-07-07 23:24:50 +02:00
|
|
|
|
account.removeFolder(folder)
|
2019-05-05 22:41:20 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let folderNames: [String] = {
|
|
|
|
|
if let folders = account.folders {
|
|
|
|
|
return folders.map { $0.name ?? "" }
|
|
|
|
|
} else {
|
|
|
|
|
return [String]()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
// Make any folders Feedbin has, but we don't
|
|
|
|
|
tagNames.forEach { tagName in
|
|
|
|
|
if !folderNames.contains(tagName) {
|
2019-07-07 23:24:50 +02:00
|
|
|
|
_ = account.ensureFolder(with: tagName)
|
2019-05-07 17:51:41 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func syncFeeds(_ account: Account, _ subscriptions: [FeedbinSubscription]?) {
|
2019-05-08 00:41:32 +02:00
|
|
|
|
|
2019-05-07 17:51:41 +02:00
|
|
|
|
guard let subscriptions = subscriptions else { return }
|
2019-07-07 23:24:50 +02:00
|
|
|
|
assert(Thread.isMainThread)
|
|
|
|
|
|
2019-05-08 15:20:29 +02:00
|
|
|
|
os_log(.debug, log: log, "Syncing feeds with %ld subscriptions.", subscriptions.count)
|
|
|
|
|
|
2019-05-08 00:41:32 +02:00
|
|
|
|
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-08 00:41:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-07 18:10:00 +02:00
|
|
|
|
}
|
2019-05-07 17:51:41 +02:00
|
|
|
|
}
|
2019-05-08 00:41:32 +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-08 00:41:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add any feeds we don't have and update any we do
|
2019-10-03 08:05:35 +02:00
|
|
|
|
var subscriptionsToAdd = Set<FeedbinSubscription>()
|
2019-05-08 00:41:32 +02:00
|
|
|
|
subscriptions.forEach { subscription in
|
2019-10-06 21:37:46 +02:00
|
|
|
|
|
2019-05-08 00:41:32 +02:00
|
|
|
|
let subFeedId = String(subscription.feedID)
|
2019-10-06 21:37:46 +02:00
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
if let feed = account.existingWebFeed(withWebFeedID: subFeedId) {
|
2019-07-07 23:24:50 +02:00
|
|
|
|
feed.name = subscription.name
|
|
|
|
|
// If the name has been changed on the server remove the locally edited name
|
|
|
|
|
feed.editedName = nil
|
|
|
|
|
feed.homePageURL = subscription.homePageURL
|
|
|
|
|
feed.subscriptionID = String(subscription.subscriptionID)
|
2019-11-08 02:07:10 +01:00
|
|
|
|
feed.faviconURL = subscription.jsonFeed?.favicon
|
|
|
|
|
feed.iconURL = subscription.jsonFeed?.icon
|
2019-05-08 00:41:32 +02:00
|
|
|
|
}
|
2019-10-03 08:05:35 +02:00
|
|
|
|
else {
|
|
|
|
|
subscriptionsToAdd.insert(subscription)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Actually add subscriptions all in one go, so we don’t trigger various rebuilding things that Account does.
|
|
|
|
|
subscriptionsToAdd.forEach { subscription in
|
2019-11-15 03:11:41 +01:00
|
|
|
|
let feed = account.createWebFeed(with: subscription.name, url: subscription.url, webFeedID: String(subscription.feedID), homePageURL: subscription.homePageURL)
|
2019-10-03 08:05:35 +02:00
|
|
|
|
feed.subscriptionID = String(subscription.subscriptionID)
|
2019-11-15 03:11:41 +01:00
|
|
|
|
account.addWebFeed(feed)
|
2019-05-08 00:41:32 +02:00
|
|
|
|
}
|
2019-05-07 17:51:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-26 18:35:07 +02:00
|
|
|
|
func syncFeedFolderRelationship(_ account: Account, _ taggings: [FeedbinTagging]?) {
|
2019-05-07 17:51:41 +02:00
|
|
|
|
|
2019-05-08 00:41:32 +02:00
|
|
|
|
guard let taggings = taggings else { return }
|
2019-07-07 23:24:50 +02:00
|
|
|
|
assert(Thread.isMainThread)
|
2019-05-08 00:41:32 +02:00
|
|
|
|
|
2019-05-08 15:20:29 +02:00
|
|
|
|
os_log(.debug, log: log, "Syncing taggings with %ld taggings.", taggings.count)
|
|
|
|
|
|
2019-05-08 00:41:32 +02:00
|
|
|
|
// Set up some structures to make syncing easier
|
2020-01-22 07:03:33 +01:00
|
|
|
|
let folderDict = nameToFolderDictionary(with: account.folders)
|
2019-05-08 01:34:45 +02:00
|
|
|
|
let taggingsDict = taggings.reduce([String: [FeedbinTagging]]()) { (dict, tagging) in
|
2019-05-08 00:41:32 +02:00
|
|
|
|
var taggedFeeds = dict
|
|
|
|
|
if var taggedFeed = taggedFeeds[tagging.name] {
|
2019-05-08 01:34:45 +02:00
|
|
|
|
taggedFeed.append(tagging)
|
2019-05-08 00:41:32 +02:00
|
|
|
|
taggedFeeds[tagging.name] = taggedFeed
|
|
|
|
|
} else {
|
2019-05-08 01:34:45 +02:00
|
|
|
|
taggedFeeds[tagging.name] = [tagging]
|
2019-05-08 00:41:32 +02:00
|
|
|
|
}
|
|
|
|
|
return taggedFeeds
|
2019-05-05 22:41:20 +02:00
|
|
|
|
}
|
2019-05-07 17:51:41 +02:00
|
|
|
|
|
2019-05-08 00:41:32 +02:00
|
|
|
|
// Sync the folders
|
2019-05-08 01:34:45 +02:00
|
|
|
|
for (folderName, groupedTaggings) in taggingsDict {
|
2019-05-08 00:41:32 +02:00
|
|
|
|
|
|
|
|
|
guard let folder = folderDict[folderName] else { return }
|
2019-05-08 01:34:45 +02:00
|
|
|
|
|
|
|
|
|
let taggingFeedIDs = groupedTaggings.map { String($0.feedID) }
|
|
|
|
|
|
2019-05-08 00:41:32 +02:00
|
|
|
|
// 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-07 23:24:50 +02:00
|
|
|
|
clearFolderRelationship(for: feed, withFolderName: folder.name ?? "")
|
2019-11-15 03:11:41 +01:00
|
|
|
|
account.addWebFeed(feed)
|
2019-05-08 00:41:32 +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-08 00:41:32 +02:00
|
|
|
|
|
2019-05-08 01:34:45 +02:00
|
|
|
|
for tagging in groupedTaggings {
|
|
|
|
|
let taggingFeedID = String(tagging.feedID)
|
|
|
|
|
if !folderFeedIds.contains(taggingFeedID) {
|
2019-11-15 03:11:41 +01:00
|
|
|
|
guard let feed = account.existingWebFeed(withWebFeedID: taggingFeedID) else {
|
2019-05-08 00:41:32 +02:00
|
|
|
|
continue
|
|
|
|
|
}
|
2019-07-07 23:24:50 +02:00
|
|
|
|
saveFolderRelationship(for: feed, withFolderName: folderName, id: String(tagging.taggingID))
|
2019-11-15 03:11:41 +01:00
|
|
|
|
folder.addWebFeed(feed)
|
2019-05-08 00:41:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2019-05-05 22:41:20 +02:00
|
|
|
|
|
2019-05-08 01:34:45 +02:00
|
|
|
|
let taggedFeedIDs = Set(taggings.map { String($0.feedID) })
|
2019-05-08 00:41:32 +02:00
|
|
|
|
|
2019-05-08 15:20:29 +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-09 20:31:18 +02:00
|
|
|
|
}
|
2019-05-08 00:41:32 +02:00
|
|
|
|
}
|
2019-05-07 17:51:41 +02:00
|
|
|
|
}
|
2020-01-22 07:03:33 +01:00
|
|
|
|
|
|
|
|
|
func nameToFolderDictionary(with folders: Set<Folder>?) -> [String: Folder] {
|
|
|
|
|
guard let folders = folders else {
|
|
|
|
|
return [String: Folder]()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var d = [String: Folder]()
|
|
|
|
|
for folder in folders {
|
|
|
|
|
let name = folder.name ?? ""
|
|
|
|
|
if d[name] == nil {
|
|
|
|
|
d[name] = folder
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return d
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-15 18:52:56 +02:00
|
|
|
|
func sendArticleStatuses(_ statuses: [SyncStatus],
|
|
|
|
|
apiCall: ([Int], @escaping (Result<Void, Error>) -> Void) -> Void,
|
2019-11-05 03:24:21 +01:00
|
|
|
|
completion: @escaping ((Result<Void, Error>) -> Void)) {
|
2019-05-15 18:52:56 +02:00
|
|
|
|
|
|
|
|
|
guard !statuses.isEmpty else {
|
2019-11-05 03:24:21 +01:00
|
|
|
|
completion(.success(()))
|
2019-05-15 18:52:56 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 21:56:27 +02:00
|
|
|
|
let group = DispatchGroup()
|
2019-11-05 03:24:21 +01:00
|
|
|
|
var errorOccurred = false
|
2019-05-17 21:56:27 +02:00
|
|
|
|
|
2019-05-15 18:52:56 +02:00
|
|
|
|
let articleIDs = statuses.compactMap { Int($0.articleID) }
|
|
|
|
|
let articleIDGroups = articleIDs.chunked(into: 1000)
|
|
|
|
|
for articleIDGroup in articleIDGroups {
|
|
|
|
|
|
2019-05-17 21:56:27 +02:00
|
|
|
|
group.enter()
|
2019-05-22 22:40:34 +02:00
|
|
|
|
apiCall(articleIDGroup) { result in
|
2019-05-15 18:52:56 +02:00
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
2019-05-22 22:40:34 +02:00
|
|
|
|
self.database.deleteSelectedForProcessing(articleIDGroup.map { String($0) } )
|
2019-05-17 21:56:27 +02:00
|
|
|
|
group.leave()
|
2019-05-15 18:52:56 +02:00
|
|
|
|
case .failure(let error):
|
2019-11-05 03:24:21 +01:00
|
|
|
|
errorOccurred = true
|
2019-05-15 18:52:56 +02:00
|
|
|
|
os_log(.error, log: self.log, "Article status sync call failed: %@.", error.localizedDescription)
|
|
|
|
|
self.database.resetSelectedForProcessing(articleIDGroup.map { String($0) } )
|
2019-05-17 21:56:27 +02:00
|
|
|
|
group.leave()
|
2019-05-15 18:52:56 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2019-05-17 21:56:27 +02:00
|
|
|
|
|
|
|
|
|
group.notify(queue: DispatchQueue.main) {
|
2019-11-05 03:24:21 +01:00
|
|
|
|
if errorOccurred {
|
|
|
|
|
completion(.failure(FeedbinAccountDelegateError.unknown))
|
|
|
|
|
} else {
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
2019-05-17 21:56:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-15 18:52:56 +02:00
|
|
|
|
}
|
2019-05-13 01:32:32 +02:00
|
|
|
|
|
2019-06-07 12:03:54 +02:00
|
|
|
|
func renameFolderRelationship(for account: Account, fromName: String, toName: String) {
|
2019-11-15 03:11:41 +01:00
|
|
|
|
for feed in account.flattenedWebFeeds() {
|
2019-06-07 12:03:54 +02:00
|
|
|
|
if var folderRelationship = feed.folderRelationship {
|
|
|
|
|
let relationship = folderRelationship[fromName]
|
|
|
|
|
folderRelationship[fromName] = nil
|
|
|
|
|
folderRelationship[toName] = relationship
|
|
|
|
|
feed.folderRelationship = folderRelationship
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
func clearFolderRelationship(for feed: WebFeed, withFolderName folderName: String) {
|
2019-05-09 20:35:40 +02:00
|
|
|
|
if var folderRelationship = feed.folderRelationship {
|
|
|
|
|
folderRelationship[folderName] = nil
|
|
|
|
|
feed.folderRelationship = folderRelationship
|
2019-05-08 01:34:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
func saveFolderRelationship(for feed: WebFeed, withFolderName folderName: String, id: String) {
|
2019-05-09 20:35:40 +02:00
|
|
|
|
if var folderRelationship = feed.folderRelationship {
|
|
|
|
|
folderRelationship[folderName] = id
|
|
|
|
|
feed.folderRelationship = folderRelationship
|
|
|
|
|
} else {
|
|
|
|
|
feed.folderRelationship = [folderName: id]
|
2019-05-08 01:34:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-10 17:14:24 +02:00
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
func decideBestFeedChoice(account: Account, url: String, name: String?, container: Container, choices: [FeedbinSubscriptionChoice], completion: @escaping (Result<WebFeed, Error>) -> Void) {
|
2019-05-10 17:14:24 +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-10 17:14:24 +02:00
|
|
|
|
} else {
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(.failure(FeedbinAccountDelegateError.invalidParameter))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(.failure(FeedbinAccountDelegateError.invalidParameter))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
func createFeed( account: Account, subscription sub: FeedbinSubscription, name: String?, container: Container, completion: @escaping (Result<WebFeed, Error>) -> Void) {
|
2019-05-13 18:18:54 +02:00
|
|
|
|
|
2019-05-22 22:40:34 +02:00
|
|
|
|
DispatchQueue.main.async {
|
2019-05-13 18:18:54 +02:00
|
|
|
|
|
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-05-10 17:14:24 +02:00
|
|
|
|
feed.subscriptionID = String(sub.subscriptionID)
|
2019-11-08 02:07:10 +01:00
|
|
|
|
feed.iconURL = sub.jsonFeed?.icon
|
|
|
|
|
feed.faviconURL = sub.jsonFeed?.favicon
|
2019-05-13 18:18:54 +02:00
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
account.addWebFeed(feed, to: container) { result in
|
2019-05-13 18:18:54 +02:00
|
|
|
|
switch result {
|
2019-05-28 16:45:02 +02:00
|
|
|
|
case .success:
|
|
|
|
|
if let name = name {
|
2019-11-15 03:11:41 +01:00
|
|
|
|
account.renameWebFeed(feed, to: name) { result in
|
2019-05-28 16:45:02 +02:00
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
self.initialFeedDownload(account: account, feed: feed, completion: completion)
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
completion(.failure(error))
|
2019-05-13 18:18:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-28 16:45:02 +02:00
|
|
|
|
} else {
|
|
|
|
|
self.initialFeedDownload(account: account, feed: feed, completion: completion)
|
2019-05-13 18:18:54 +02:00
|
|
|
|
}
|
|
|
|
|
case .failure(let error):
|
2019-05-28 16:45:02 +02:00
|
|
|
|
completion(.failure(error))
|
2019-05-13 18:18:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-28 16:45:02 +02:00
|
|
|
|
|
2019-05-10 17:14:24 +02:00
|
|
|
|
}
|
2019-05-13 18:18:54 +02:00
|
|
|
|
|
2019-05-10 17:14:24 +02:00
|
|
|
|
}
|
2019-05-13 01:32:32 +02:00
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
func initialFeedDownload( account: Account, feed: WebFeed, completion: @escaping (Result<WebFeed, Error>) -> Void) {
|
2019-06-16 21:48:50 +02:00
|
|
|
|
|
|
|
|
|
// refreshArticles is being reused and will clear one of the tasks for us
|
|
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(4)
|
|
|
|
|
|
2019-05-28 16:45:02 +02:00
|
|
|
|
// Download the initial articles
|
2019-11-15 03:11:41 +01:00
|
|
|
|
self.caller.retrieveEntries(feedID: feed.webFeedID) { result in
|
2019-06-16 21:48:50 +02:00
|
|
|
|
self.refreshProgress.completeTask()
|
2019-05-28 16:45:02 +02:00
|
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
|
case .success(let (entries, page)):
|
|
|
|
|
|
2019-12-17 07:45:59 +01:00
|
|
|
|
self.processEntries(account: account, entries: entries) { error in
|
|
|
|
|
if let error = error {
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 03:24:21 +01:00
|
|
|
|
self.refreshArticleStatus(for: account) { result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
|
|
|
|
|
self.refreshArticles(account, page: page, updateFetchDate: nil) { result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
|
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
|
self.refreshMissingArticles(account) { result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
|
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(.success(feed))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
completion(.failure(error))
|
2019-05-28 16:45:02 +02:00
|
|
|
|
}
|
2019-11-05 03:24:21 +01:00
|
|
|
|
|
2019-05-28 16:45:02 +02:00
|
|
|
|
}
|
2019-11-05 03:24:21 +01:00
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
completion(.failure(error))
|
2019-05-28 16:45:02 +02:00
|
|
|
|
}
|
2019-05-13 18:18:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 16:45:02 +02:00
|
|
|
|
case .failure(let error):
|
|
|
|
|
completion(.failure(error))
|
2019-05-13 18:18:54 +02:00
|
|
|
|
}
|
2019-05-28 16:45:02 +02:00
|
|
|
|
|
2019-05-10 17:14:24 +02:00
|
|
|
|
}
|
2019-05-30 21:36:21 +02:00
|
|
|
|
|
2019-05-10 17:14:24 +02:00
|
|
|
|
}
|
2019-05-28 16:45:02 +02:00
|
|
|
|
|
2019-12-17 07:45:59 +01:00
|
|
|
|
func refreshArticles(_ account: Account, completion: @escaping VoidResultCompletionBlock) {
|
2019-05-13 01:32:32 +02:00
|
|
|
|
|
|
|
|
|
os_log(.debug, log: log, "Refreshing articles...")
|
|
|
|
|
|
2019-05-22 22:40:34 +02:00
|
|
|
|
caller.retrieveEntries() { result in
|
2019-05-13 13:34:27 +02:00
|
|
|
|
|
2019-05-13 17:29:34 +02:00
|
|
|
|
switch result {
|
2019-11-02 22:21:37 +01:00
|
|
|
|
case .success(let (entries, page, updateFetchDate, lastPageNumber)):
|
2019-05-14 20:10:07 +02:00
|
|
|
|
|
|
|
|
|
if let last = lastPageNumber {
|
2019-05-22 22:40:34 +02:00
|
|
|
|
self.refreshProgress.addToNumberOfTasksAndRemaining(last - 1)
|
2019-05-14 20:10:07 +02:00
|
|
|
|
}
|
2019-05-13 01:32:32 +02:00
|
|
|
|
|
2019-12-17 07:45:59 +01:00
|
|
|
|
self.processEntries(account: account, entries: entries) { error in
|
2019-05-14 20:10:07 +02:00
|
|
|
|
|
2019-05-22 22:40:34 +02:00
|
|
|
|
self.refreshProgress.completeTask()
|
2019-12-17 07:45:59 +01:00
|
|
|
|
|
|
|
|
|
if let error = error {
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 03:24:21 +01:00
|
|
|
|
self.refreshArticles(account, page: page, updateFetchDate: updateFetchDate) { result in
|
2019-05-13 17:29:34 +02:00
|
|
|
|
os_log(.debug, log: self.log, "Done refreshing articles.")
|
2019-11-05 03:24:21 +01:00
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
2019-05-13 13:34:27 +02:00
|
|
|
|
}
|
2019-05-13 01:32:32 +02:00
|
|
|
|
}
|
2019-05-13 17:29:34 +02:00
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
2019-11-05 03:24:21 +01:00
|
|
|
|
completion(.failure(error))
|
2019-05-13 01:32:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 03:24:21 +01:00
|
|
|
|
func refreshMissingArticles(_ account: Account, completion: @escaping ((Result<Void, Error>) -> Void)) {
|
2019-05-17 21:56:27 +02:00
|
|
|
|
os_log(.debug, log: log, "Refreshing missing articles...")
|
2019-12-17 07:45:59 +01:00
|
|
|
|
|
2019-12-18 01:43:08 +01:00
|
|
|
|
account.fetchArticleIDsForStatusesWithoutArticlesNewerThanCutoffDate { result in
|
|
|
|
|
|
2019-12-18 06:24:17 +01:00
|
|
|
|
func process(_ fetchedArticleIDs: Set<String>) {
|
2019-12-18 01:43:08 +01:00
|
|
|
|
let group = DispatchGroup()
|
|
|
|
|
var errorOccurred = false
|
|
|
|
|
|
|
|
|
|
let articleIDs = Array(fetchedArticleIDs)
|
|
|
|
|
let chunkedArticleIDs = articleIDs.chunked(into: 100)
|
2019-07-08 00:05:36 +02:00
|
|
|
|
|
2019-12-18 01:43:08 +01:00
|
|
|
|
for chunk in chunkedArticleIDs {
|
|
|
|
|
group.enter()
|
|
|
|
|
self.caller.retrieveEntries(articleIDs: chunk) { result in
|
2019-07-08 00:05:36 +02:00
|
|
|
|
|
2019-12-18 01:43:08 +01:00
|
|
|
|
switch result {
|
|
|
|
|
case .success(let entries):
|
2019-07-08 00:05:36 +02:00
|
|
|
|
|
2019-12-18 01:43:08 +01:00
|
|
|
|
self.processEntries(account: account, entries: entries) { error in
|
|
|
|
|
group.leave()
|
|
|
|
|
if error != nil {
|
|
|
|
|
errorOccurred = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-08 00:05:36 +02:00
|
|
|
|
|
2019-12-18 01:43:08 +01:00
|
|
|
|
case .failure(let error):
|
2019-12-17 21:25:01 +01:00
|
|
|
|
errorOccurred = true
|
2019-12-18 01:43:08 +01:00
|
|
|
|
os_log(.error, log: self.log, "Refresh missing articles failed: %@.", error.localizedDescription)
|
|
|
|
|
group.leave()
|
2019-12-17 21:25:01 +01:00
|
|
|
|
}
|
2019-05-17 21:56:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-18 01:43:08 +01:00
|
|
|
|
group.notify(queue: DispatchQueue.main) {
|
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
|
os_log(.debug, log: self.log, "Done refreshing missing articles.")
|
|
|
|
|
if errorOccurred {
|
|
|
|
|
completion(.failure(FeedbinAccountDelegateError.unknown))
|
|
|
|
|
} else {
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-18 06:24:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
|
case .success(let fetchedArticleIDs):
|
|
|
|
|
process(fetchedArticleIDs)
|
2019-12-18 01:43:08 +01:00
|
|
|
|
case .failure(let error):
|
2019-12-18 06:24:17 +01:00
|
|
|
|
self.refreshProgress.completeTask()
|
2019-12-18 01:43:08 +01:00
|
|
|
|
completion(.failure(error))
|
2019-11-05 03:24:21 +01:00
|
|
|
|
}
|
2019-05-17 21:56:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 03:24:21 +01:00
|
|
|
|
func refreshArticles(_ account: Account, page: String?, updateFetchDate: Date?, completion: @escaping ((Result<Void, Error>) -> Void)) {
|
2019-05-13 01:32:32 +02:00
|
|
|
|
guard let page = page else {
|
2019-11-02 22:21:37 +01:00
|
|
|
|
if let lastArticleFetch = updateFetchDate {
|
2019-12-10 02:34:26 +01:00
|
|
|
|
self.accountMetadata?.lastArticleFetchStartTime = lastArticleFetch
|
|
|
|
|
self.accountMetadata?.lastArticleFetchEndTime = Date()
|
2019-11-02 22:21:37 +01:00
|
|
|
|
}
|
2019-11-05 03:24:21 +01:00
|
|
|
|
completion(.success(()))
|
2019-05-13 01:32:32 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-22 22:40:34 +02:00
|
|
|
|
caller.retrieveEntries(page: page) { result in
|
2019-05-13 01:32:32 +02:00
|
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
|
case .success(let (entries, nextPage)):
|
|
|
|
|
|
2019-12-17 21:25:01 +01:00
|
|
|
|
self.processEntries(account: account, entries: entries) { error in
|
2019-05-22 22:40:34 +02:00
|
|
|
|
self.refreshProgress.completeTask()
|
2019-12-17 21:25:01 +01:00
|
|
|
|
|
|
|
|
|
if let error = error {
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-02 22:21:37 +01:00
|
|
|
|
self.refreshArticles(account, page: nextPage, updateFetchDate: updateFetchDate, completion: completion)
|
2019-05-13 13:34:27 +02:00
|
|
|
|
}
|
2019-05-13 01:32:32 +02:00
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
2019-11-05 03:24:21 +01:00
|
|
|
|
completion(.failure(error))
|
2019-05-13 01:32:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-17 07:45:59 +01:00
|
|
|
|
func processEntries(account: Account, entries: [FeedbinEntry]?, completion: @escaping DatabaseCompletionBlock) {
|
2019-05-13 01:32:32 +02:00
|
|
|
|
let parsedItems = mapEntriesToParsedItems(entries: entries)
|
2019-11-15 03:11:41 +01:00
|
|
|
|
let webFeedIDsAndItems = Dictionary(grouping: parsedItems, by: { item in item.feedURL } ).mapValues { Set($0) }
|
|
|
|
|
account.update(webFeedIDsAndItems: webFeedIDsAndItems, defaultRead: true, completion: completion)
|
2019-05-13 01:32:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mapEntriesToParsedItems(entries: [FeedbinEntry]?) -> Set<ParsedItem> {
|
|
|
|
|
guard let entries = entries else {
|
|
|
|
|
return Set<ParsedItem>()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let parsedItems: [ParsedItem] = entries.map { entry in
|
2019-05-27 01:35:54 +02:00
|
|
|
|
let authors = Set([ParsedAuthor(name: entry.authorName, url: entry.jsonFeed?.jsonFeedAuthor?.url, avatarURL: entry.jsonFeed?.jsonFeedAuthor?.avatarURL, emailAddress: nil)])
|
2020-01-20 01:15:21 +01:00
|
|
|
|
return ParsedItem(syncServiceID: String(entry.articleID), uniqueID: String(entry.articleID), feedURL: String(entry.feedID), url: entry.url, externalURL: nil, title: entry.title, contentHTML: entry.contentHTML, contentText: nil, summary: entry.summary, imageURL: nil, bannerImageURL: nil, datePublished: entry.parsedDatePublished, dateModified: nil, authors: authors, tags: nil, attachments: nil)
|
2019-05-13 01:32:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Set(parsedItems)
|
|
|
|
|
|
|
|
|
|
}
|
2019-05-08 01:34:45 +02:00
|
|
|
|
|
2019-05-14 13:20:53 +02:00
|
|
|
|
func syncArticleReadState(account: Account, articleIDs: [Int]?) {
|
2019-05-16 13:01:10 +02:00
|
|
|
|
guard let articleIDs = articleIDs else {
|
2019-05-14 00:04:12 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 02:21:18 +01:00
|
|
|
|
database.selectPendingReadStatusArticleIDs() { result in
|
|
|
|
|
|
|
|
|
|
func process(_ pendingArticleIDs: Set<String>) {
|
|
|
|
|
|
|
|
|
|
let feedbinUnreadArticleIDs = Set(articleIDs.map { String($0) } )
|
|
|
|
|
let updatableFeedbinUnreadArticleIDs = feedbinUnreadArticleIDs.subtracting(pendingArticleIDs)
|
|
|
|
|
|
|
|
|
|
account.fetchUnreadArticleIDs { articleIDsResult in
|
|
|
|
|
guard let currentUnreadArticleIDs = try? articleIDsResult.get() else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-12-17 07:45:59 +01:00
|
|
|
|
|
2020-01-03 02:21:18 +01:00
|
|
|
|
// Mark articles as unread
|
|
|
|
|
let deltaUnreadArticleIDs = updatableFeedbinUnreadArticleIDs.subtracting(currentUnreadArticleIDs)
|
|
|
|
|
account.markAsUnread(deltaUnreadArticleIDs)
|
|
|
|
|
|
|
|
|
|
// Mark articles as read
|
|
|
|
|
let deltaReadArticleIDs = currentUnreadArticleIDs.subtracting(updatableFeedbinUnreadArticleIDs)
|
|
|
|
|
account.markAsRead(deltaReadArticleIDs)
|
|
|
|
|
}
|
2019-12-08 05:57:23 +01:00
|
|
|
|
|
2020-01-03 02:21:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
|
case .success(let pendingArticleIDs):
|
|
|
|
|
process(pendingArticleIDs)
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
os_log(.error, log: self.log, "Sync Article Read Status failed: %@.", error.localizedDescription)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 05:57:23 +01:00
|
|
|
|
}
|
2020-01-03 02:21:18 +01:00
|
|
|
|
|
2019-05-14 00:04:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 13:20:53 +02:00
|
|
|
|
func syncArticleStarredState(account: Account, articleIDs: [Int]?) {
|
2019-05-16 13:01:10 +02:00
|
|
|
|
guard let articleIDs = articleIDs else {
|
2019-05-14 00:04:12 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 02:21:18 +01:00
|
|
|
|
database.selectPendingStarredStatusArticleIDs() { result in
|
|
|
|
|
|
|
|
|
|
func process(_ pendingArticleIDs: Set<String>) {
|
|
|
|
|
|
|
|
|
|
let feedbinStarredArticleIDs = Set(articleIDs.map { String($0) } )
|
|
|
|
|
let updatableFeedbinUnreadArticleIDs = feedbinStarredArticleIDs.subtracting(pendingArticleIDs)
|
|
|
|
|
|
|
|
|
|
account.fetchStarredArticleIDs { articleIDsResult in
|
|
|
|
|
guard let currentStarredArticleIDs = try? articleIDsResult.get() else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-12-17 07:45:59 +01:00
|
|
|
|
|
2020-01-03 02:21:18 +01:00
|
|
|
|
// Mark articles as starred
|
|
|
|
|
let deltaStarredArticleIDs = updatableFeedbinUnreadArticleIDs.subtracting(currentStarredArticleIDs)
|
|
|
|
|
account.markAsStarred(deltaStarredArticleIDs)
|
|
|
|
|
|
|
|
|
|
// Mark articles as unstarred
|
|
|
|
|
let deltaUnstarredArticleIDs = currentStarredArticleIDs.subtracting(updatableFeedbinUnreadArticleIDs)
|
|
|
|
|
account.markAsUnstarred(deltaUnstarredArticleIDs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
|
case .success(let pendingArticleIDs):
|
|
|
|
|
process(pendingArticleIDs)
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
os_log(.error, log: self.log, "Sync Article Starred Status failed: %@.", error.localizedDescription)
|
|
|
|
|
}
|
2019-12-08 07:23:44 +01:00
|
|
|
|
|
|
|
|
|
}
|
2020-01-03 02:21:18 +01:00
|
|
|
|
|
2019-05-14 00:04:12 +02:00
|
|
|
|
}
|
2019-05-30 17:12:34 +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-05-30 17:12:34 +02:00
|
|
|
|
|
|
|
|
|
if let folder = container as? Folder, let feedTaggingID = feed.folderRelationship?[folder.name ?? ""] {
|
2019-06-16 21:48:50 +02:00
|
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(1)
|
2019-05-30 17:12:34 +02:00
|
|
|
|
caller.deleteTagging(taggingID: feedTaggingID) { result in
|
2019-06-16 21:48:50 +02:00
|
|
|
|
self.refreshProgress.completeTask()
|
2019-05-30 17:12:34 +02:00
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
DispatchQueue.main.async {
|
2019-05-30 21:36:21 +02:00
|
|
|
|
self.clearFolderRelationship(for: feed, withFolderName: folder.name ?? "")
|
2019-11-15 03:11:41 +01:00
|
|
|
|
folder.removeWebFeed(feed)
|
2019-05-30 17:12:34 +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-05-30 17:12:34 +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-05-30 17:12:34 +02:00
|
|
|
|
|
|
|
|
|
// This error should never happen
|
|
|
|
|
guard let subscriptionID = feed.subscriptionID else {
|
|
|
|
|
completion(.failure(FeedbinAccountDelegateError.invalidParameter))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 21:48:50 +02:00
|
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(1)
|
2019-05-30 17:12:34 +02:00
|
|
|
|
caller.deleteSubscription(subscriptionID: subscriptionID) { result in
|
2019-06-16 21:48:50 +02:00
|
|
|
|
self.refreshProgress.completeTask()
|
2019-05-30 17:12:34 +02:00
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
DispatchQueue.main.async {
|
2019-11-15 03:11:41 +01:00
|
|
|
|
account.clearWebFeedMetadata(feed)
|
|
|
|
|
account.removeWebFeed(feed)
|
2019-05-30 17:12:34 +02:00
|
|
|
|
if let folders = account.folders {
|
|
|
|
|
for folder in folders {
|
2019-11-15 03:11:41 +01:00
|
|
|
|
folder.removeWebFeed(feed)
|
2019-05-30 17:12:34 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
let wrappedError = AccountError.wrappedError(error: error, account: account)
|
|
|
|
|
completion(.failure(wrappedError))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2019-05-14 00:04:12 +02:00
|
|
|
|
|
2019-05-05 22:41:20 +02:00
|
|
|
|
}
|