2017-09-17 20:32:58 +02:00
|
|
|
//
|
|
|
|
// LocalAccountDelegate.swift
|
2019-07-09 07:58:19 +02:00
|
|
|
// NetNewsWire
|
2017-09-17 20:32:58 +02:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 9/16/17.
|
|
|
|
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2020-04-17 12:32:52 +02:00
|
|
|
import os.log
|
2019-05-26 23:45:09 +02:00
|
|
|
import RSCore
|
2019-05-11 19:26:23 +02:00
|
|
|
import RSParser
|
2019-05-14 22:34:05 +02:00
|
|
|
import Articles
|
2020-04-11 00:23:39 +02:00
|
|
|
import ArticlesDatabase
|
2017-10-08 02:20:19 +02:00
|
|
|
import RSWeb
|
2020-04-10 04:07:56 +02:00
|
|
|
import Secrets
|
2017-09-17 20:32:58 +02:00
|
|
|
|
2019-05-09 00:41:19 +02:00
|
|
|
public enum LocalAccountDelegateError: String, Error {
|
|
|
|
case invalidParameter = "An invalid parameter was used."
|
|
|
|
}
|
|
|
|
|
2017-10-07 23:40:14 +02:00
|
|
|
final class LocalAccountDelegate: AccountDelegate {
|
2019-12-17 00:55:37 +01:00
|
|
|
|
2020-04-17 12:32:52 +02:00
|
|
|
private var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "LocalAccount")
|
|
|
|
|
2020-04-10 18:20:35 +02:00
|
|
|
weak var account: Account?
|
|
|
|
|
|
|
|
private lazy var refresher: LocalAccountRefresher? = {
|
|
|
|
let refresher = LocalAccountRefresher()
|
|
|
|
refresher.delegate = self
|
|
|
|
return refresher
|
|
|
|
}()
|
2020-04-02 19:00:10 +02:00
|
|
|
|
2019-09-20 18:34:31 +02:00
|
|
|
let behaviors: AccountBehaviors = []
|
2019-06-20 00:50:32 +02:00
|
|
|
let isOPMLImportInProgress = false
|
2019-05-17 17:44:22 +02:00
|
|
|
|
2019-05-04 18:48:48 +02:00
|
|
|
let server: String? = nil
|
2019-05-05 10:25:21 +02:00
|
|
|
var credentials: Credentials?
|
2019-05-05 14:49:59 +02:00
|
|
|
var accountMetadata: AccountMetadata?
|
2019-05-05 14:21:26 +02:00
|
|
|
|
2020-04-02 19:00:10 +02:00
|
|
|
let refreshProgress = DownloadProgress(numberOfTasks: 0)
|
2017-10-08 02:20:19 +02:00
|
|
|
|
2020-03-30 09:48:25 +02:00
|
|
|
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any], completion: @escaping () -> Void) {
|
|
|
|
completion()
|
|
|
|
}
|
|
|
|
|
2019-05-26 18:54:32 +02:00
|
|
|
func refreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
|
2020-04-17 12:32:52 +02:00
|
|
|
guard refreshProgress.isComplete else {
|
2019-10-02 23:41:32 +02:00
|
|
|
completion(.success(()))
|
2020-04-10 18:20:35 +02:00
|
|
|
return
|
2019-10-02 23:41:32 +02:00
|
|
|
}
|
2020-04-17 12:32:52 +02:00
|
|
|
|
|
|
|
var refresherWebFeeds = Set<WebFeed>()
|
|
|
|
let webFeeds = account.flattenedWebFeeds()
|
2020-05-08 09:32:13 +02:00
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(webFeeds.count)
|
|
|
|
|
2020-04-17 12:32:52 +02:00
|
|
|
let group = DispatchGroup()
|
|
|
|
|
|
|
|
for webFeed in webFeeds {
|
2020-04-24 20:33:43 +02:00
|
|
|
if let components = URLComponents(string: webFeed.url), let feedProvider = FeedProviderManager.shared.best(for: components) {
|
2020-04-17 12:32:52 +02:00
|
|
|
group.enter()
|
|
|
|
feedProvider.refresh(webFeed) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let parsedItems):
|
|
|
|
account.update(webFeed.webFeedID, with: parsedItems) { _ in
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
case .failure(let error):
|
|
|
|
os_log(.error, log: self.log, "Feed Provider refresh error: %@.", error.localizedDescription)
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
refresherWebFeeds.insert(webFeed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
group.enter()
|
2020-05-08 09:32:13 +02:00
|
|
|
refresher?.refreshFeeds(refresherWebFeeds) {
|
2020-04-17 12:32:52 +02:00
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
|
|
|
|
group.notify(queue: DispatchQueue.main) {
|
2020-05-08 09:32:13 +02:00
|
|
|
self.refreshProgress.clear()
|
2020-04-19 00:30:58 +02:00
|
|
|
account.metadata.lastArticleFetchEndTime = Date()
|
2020-04-17 12:32:52 +02:00
|
|
|
completion(.success(()))
|
|
|
|
}
|
2020-04-10 18:20:35 +02:00
|
|
|
|
2017-09-17 20:32:58 +02:00
|
|
|
}
|
2019-05-15 18:52:56 +02:00
|
|
|
|
2019-11-05 03:24:21 +01:00
|
|
|
func sendArticleStatus(for account: Account, completion: @escaping ((Result<Void, Error>) -> Void)) {
|
|
|
|
completion(.success(()))
|
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)) {
|
|
|
|
completion(.success(()))
|
2019-05-15 18:52:56 +02:00
|
|
|
}
|
2019-05-11 19:26:23 +02:00
|
|
|
|
|
|
|
func importOPML(for account:Account, opmlFile: URL, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
var fileData: Data?
|
|
|
|
|
|
|
|
do {
|
|
|
|
fileData = try Data(contentsOf: opmlFile)
|
|
|
|
} catch {
|
|
|
|
completion(.failure(error))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let opmlData = fileData else {
|
|
|
|
completion(.success(()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let parserData = ParserData(url: opmlFile.absoluteString, data: opmlData)
|
|
|
|
var opmlDocument: RSOPMLDocument?
|
|
|
|
|
|
|
|
do {
|
|
|
|
opmlDocument = try RSOPMLParser.parseOPML(with: parserData)
|
|
|
|
} catch {
|
|
|
|
completion(.failure(error))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let loadDocument = opmlDocument else {
|
|
|
|
completion(.success(()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-13 01:05:29 +02:00
|
|
|
guard let children = loadDocument.children else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-26 23:45:09 +02:00
|
|
|
BatchUpdate.shared.perform {
|
2020-03-31 22:21:53 +02:00
|
|
|
account.loadOPMLItems(children)
|
2019-05-26 23:45:09 +02:00
|
|
|
}
|
2019-09-13 01:05:29 +02:00
|
|
|
|
2019-05-11 19:26:23 +02:00
|
|
|
completion(.success(()))
|
|
|
|
|
|
|
|
}
|
2019-05-06 17:53:20 +02:00
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func createWebFeed(for account: Account, url urlString: String, name: String?, container: Container, completion: @escaping (Result<WebFeed, Error>) -> Void) {
|
2020-04-16 23:19:32 +02:00
|
|
|
guard let url = URL(string: urlString), let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
|
2019-05-09 00:41:19 +02:00
|
|
|
completion(.failure(LocalAccountDelegateError.invalidParameter))
|
|
|
|
return
|
|
|
|
}
|
2019-06-16 21:48:50 +02:00
|
|
|
|
2020-04-16 23:19:32 +02:00
|
|
|
// Username should be part of the URL on new feed adds
|
2020-04-24 20:33:43 +02:00
|
|
|
if let feedProvider = FeedProviderManager.shared.best(for: urlComponents) {
|
2020-04-19 00:30:58 +02:00
|
|
|
createProviderWebFeed(for: account, urlComponents: urlComponents, editedName: name, container: container, feedProvider: feedProvider, completion: completion)
|
2020-04-17 16:11:37 +02:00
|
|
|
} else {
|
2020-04-19 00:30:58 +02:00
|
|
|
createRSSWebFeed(for: account, url: url, editedName: name, container: container, completion: completion)
|
2019-05-31 00:35:08 +02:00
|
|
|
}
|
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-05-09 00:41:19 +02:00
|
|
|
feed.editedName = name
|
|
|
|
completion(.success(()))
|
|
|
|
}
|
2019-05-09 14:25:45 +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) {
|
|
|
|
container.removeWebFeed(feed)
|
2019-05-09 14:25:45 +02:00
|
|
|
completion(.success(()))
|
|
|
|
}
|
2019-05-09 20:31:18 +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) {
|
|
|
|
from.removeWebFeed(feed)
|
|
|
|
to.addWebFeed(feed)
|
2019-05-09 20:31:18 +02:00
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
container.addWebFeed(feed)
|
2019-05-09 20:31:18 +02:00
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func restoreWebFeed(for account: Account, feed: WebFeed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
container.addWebFeed(feed)
|
2019-05-09 20:31:18 +02:00
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
|
2020-03-30 20:35:02 +02:00
|
|
|
func createFolder(for account: Account, name: String, completion: @escaping (Result<Folder, Error>) -> Void) {
|
2019-05-30 21:36:21 +02:00
|
|
|
if let folder = account.ensureFolder(with: name) {
|
|
|
|
completion(.success(folder))
|
2019-05-09 23:09:21 +02:00
|
|
|
} else {
|
2019-05-30 21:36:21 +02:00
|
|
|
completion(.failure(FeedbinAccountDelegateError.invalidParameter))
|
2019-05-09 23:09:21 +02:00
|
|
|
}
|
2019-05-30 21:36:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
folder.name = name
|
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeFolder(for account: Account, with folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
account.removeFolder(folder)
|
2019-05-09 23:09:21 +02:00
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func restoreFolder(for account: Account, folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
account.addFolder(folder)
|
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
|
2019-05-14 22:34:05 +02:00
|
|
|
func markArticles(for account: Account, articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) -> Set<Article>? {
|
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
|
|
|
}
|
|
|
|
|
2017-12-20 02:48:30 +01:00
|
|
|
func accountDidInitialize(_ account: Account) {
|
2020-04-10 18:20:35 +02:00
|
|
|
self.account = account
|
2017-12-20 02:48:30 +01:00
|
|
|
}
|
2019-11-11 08:42:31 +01:00
|
|
|
|
|
|
|
func accountWillBeDeleted(_ account: Account) {
|
|
|
|
}
|
2017-12-20 02:48:30 +01:00
|
|
|
|
2019-05-29 21:16:09 +02:00
|
|
|
static func validateCredentials(transport: Transport, credentials: Credentials, endpoint: URL? = nil, completion: (Result<Credentials?, Error>) -> Void) {
|
2019-05-29 16:54:52 +02:00
|
|
|
return completion(.success(nil))
|
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
|
|
|
func suspendNetwork() {
|
2020-04-10 18:20:35 +02:00
|
|
|
refresher?.suspend()
|
2019-11-30 07:57:14 +01:00
|
|
|
}
|
|
|
|
|
2019-12-05 01:27:39 +01:00
|
|
|
func suspendDatabase() {
|
|
|
|
// Nothing to do
|
|
|
|
}
|
|
|
|
|
2019-11-30 07:57:14 +01:00
|
|
|
func resume() {
|
2020-04-10 18:20:35 +02:00
|
|
|
refresher?.resume()
|
2019-11-30 07:57:14 +01:00
|
|
|
}
|
2017-09-17 20:32:58 +02:00
|
|
|
}
|
2020-04-10 18:20:35 +02:00
|
|
|
|
|
|
|
extension LocalAccountDelegate: LocalAccountRefresherDelegate {
|
2020-05-08 09:32:13 +02:00
|
|
|
|
2020-05-28 23:24:10 +02:00
|
|
|
|
|
|
|
func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed) {
|
2020-04-10 18:20:35 +02:00
|
|
|
refreshProgress.completeTask()
|
|
|
|
}
|
|
|
|
|
2020-05-28 23:24:10 +02:00
|
|
|
func localAccountRefresher(_ refresher: LocalAccountRefresher, articleChanges: ArticleChanges, completion: @escaping () -> Void) {
|
|
|
|
completion()
|
|
|
|
}
|
|
|
|
|
2020-04-10 18:20:35 +02:00
|
|
|
}
|
2020-04-17 16:11:37 +02:00
|
|
|
|
|
|
|
private extension LocalAccountDelegate {
|
|
|
|
|
2020-04-19 00:30:58 +02:00
|
|
|
func createProviderWebFeed(for account: Account, urlComponents: URLComponents, editedName: String?, container: Container, feedProvider: FeedProvider, completion: @escaping (Result<WebFeed, Error>) -> Void) {
|
2020-04-17 16:11:37 +02:00
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(2)
|
|
|
|
|
2020-05-06 00:55:27 +02:00
|
|
|
feedProvider.metaData(urlComponents) { result in
|
2020-04-17 16:11:37 +02:00
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
switch result {
|
|
|
|
|
2020-05-06 00:55:27 +02:00
|
|
|
case .success(let metaData):
|
2020-04-17 16:11:37 +02:00
|
|
|
|
2020-04-24 20:33:43 +02:00
|
|
|
guard let urlString = urlComponents.url?.absoluteString else {
|
2020-04-17 16:11:37 +02:00
|
|
|
completion(.failure(AccountError.createErrorNotFound))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-06 00:55:27 +02:00
|
|
|
let feed = account.createWebFeed(with: metaData.name, url: urlString, webFeedID: urlString, homePageURL: metaData.homePageURL)
|
2020-04-19 00:30:58 +02:00
|
|
|
feed.editedName = editedName
|
2020-04-17 16:11:37 +02:00
|
|
|
container.addWebFeed(feed)
|
|
|
|
|
|
|
|
feedProvider.refresh(feed) { result in
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
switch result {
|
|
|
|
case .success(let parsedItems):
|
2020-04-24 20:33:43 +02:00
|
|
|
account.update(urlString, with: parsedItems) { _ in
|
2020-04-17 16:11:37 +02:00
|
|
|
completion(.success(feed))
|
|
|
|
}
|
2020-05-07 22:09:26 +02:00
|
|
|
case .failure(let error):
|
|
|
|
self.refreshProgress.clear()
|
|
|
|
completion(.failure(error))
|
2020-04-17 16:11:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 22:09:26 +02:00
|
|
|
case .failure:
|
|
|
|
self.refreshProgress.clear()
|
|
|
|
completion(.failure(AccountError.createErrorNotFound))
|
2020-04-17 16:11:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 00:30:58 +02:00
|
|
|
func createRSSWebFeed(for account: Account, url: URL, editedName: String?, container: Container, completion: @escaping (Result<WebFeed, Error>) -> Void) {
|
2020-04-17 16:14:27 +02:00
|
|
|
|
|
|
|
// We need to use a batch update here because we need to assign add the feed to the
|
|
|
|
// container before the name has been downloaded. This will put it in the sidebar
|
|
|
|
// with an Untitled name if we don't delay it being added to the sidebar.
|
2020-04-17 16:11:37 +02:00
|
|
|
BatchUpdate.shared.start()
|
2020-04-17 16:14:27 +02:00
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(1)
|
2020-04-17 16:11:37 +02:00
|
|
|
FeedFinder.find(url: url) { result in
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let feedSpecifiers):
|
|
|
|
guard let bestFeedSpecifier = FeedSpecifier.bestFeed(in: feedSpecifiers),
|
|
|
|
let url = URL(string: bestFeedSpecifier.urlString) else {
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
BatchUpdate.shared.end()
|
|
|
|
completion(.failure(AccountError.createErrorNotFound))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if account.hasWebFeed(withURL: bestFeedSpecifier.urlString) {
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
BatchUpdate.shared.end()
|
|
|
|
completion(.failure(AccountError.createErrorAlreadySubscribed))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let feed = account.createWebFeed(with: nil, url: url.absoluteString, webFeedID: url.absoluteString, homePageURL: nil)
|
2020-04-19 00:30:58 +02:00
|
|
|
feed.editedName = editedName
|
2020-04-17 16:11:37 +02:00
|
|
|
container.addWebFeed(feed)
|
|
|
|
|
|
|
|
InitialFeedDownloader.download(url) { parsedFeed in
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
|
|
|
|
if let parsedFeed = parsedFeed {
|
|
|
|
account.update(feed, with: parsedFeed, {_ in
|
|
|
|
BatchUpdate.shared.end()
|
|
|
|
completion(.success(feed))
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
BatchUpdate.shared.end()
|
|
|
|
completion(.failure(AccountError.createErrorNotFound))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case .failure:
|
|
|
|
BatchUpdate.shared.end()
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
completion(.failure(AccountError.createErrorNotFound))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|