2019-05-02 18:17:52 -05:00
|
|
|
|
//
|
|
|
|
|
// FeedbinAccountDelegate.swift
|
|
|
|
|
// Account
|
|
|
|
|
//
|
|
|
|
|
// Created by Maurice Parker on 5/2/19.
|
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
2019-05-05 15:41:20 -05:00
|
|
|
|
#if os(macOS)
|
|
|
|
|
import AppKit
|
|
|
|
|
#else
|
|
|
|
|
import UIKit
|
|
|
|
|
import RSCore
|
|
|
|
|
#endif
|
2019-05-07 11:10:00 -05:00
|
|
|
|
import RSCore
|
2019-05-11 16:07:27 -05:00
|
|
|
|
import RSParser
|
2019-05-02 18:17:52 -05:00
|
|
|
|
import RSWeb
|
2019-05-08 08:20:29 -05:00
|
|
|
|
import os.log
|
2019-05-02 18:17:52 -05:00
|
|
|
|
|
2019-05-08 18:13:54 -05:00
|
|
|
|
public enum FeedbinAccountDelegateError: String, Error {
|
|
|
|
|
case invalidParameter = "There was an invalid parameter passed."
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 18:17:52 -05:00
|
|
|
|
final class FeedbinAccountDelegate: AccountDelegate {
|
2019-05-08 08:20:29 -05:00
|
|
|
|
|
|
|
|
|
private let caller: FeedbinAPICaller
|
|
|
|
|
private var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "Feedbin")
|
|
|
|
|
|
2019-05-02 18:17:52 -05:00
|
|
|
|
let supportsSubFolders = false
|
2019-05-04 11:48:48 -05:00
|
|
|
|
let server: String? = "api.feedbin.com"
|
2019-05-02 18:17:52 -05:00
|
|
|
|
|
2019-05-05 03:25:21 -05:00
|
|
|
|
var credentials: Credentials? {
|
|
|
|
|
didSet {
|
|
|
|
|
caller.credentials = credentials
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-02 18:17:52 -05:00
|
|
|
|
|
2019-05-05 07:49:59 -05:00
|
|
|
|
var accountMetadata: AccountMetadata? {
|
|
|
|
|
didSet {
|
|
|
|
|
caller.accountMetadata = accountMetadata
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-05 07:21:26 -05:00
|
|
|
|
|
2019-05-12 07:22:33 -05:00
|
|
|
|
init(transport: Transport?) {
|
|
|
|
|
|
|
|
|
|
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-02 18:17:52 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-04 08:54:07 -05:00
|
|
|
|
var refreshProgress = DownloadProgress(numberOfTasks: 0)
|
|
|
|
|
|
2019-05-06 10:53:20 -05:00
|
|
|
|
func refreshAll(for account: Account, completion: (() -> Void)? = nil) {
|
2019-05-12 18:32:32 -05:00
|
|
|
|
|
|
|
|
|
refreshAccount(account) { [weak self] result in
|
2019-05-05 15:41:20 -05:00
|
|
|
|
switch result {
|
|
|
|
|
case .success():
|
2019-05-12 18:32:32 -05:00
|
|
|
|
|
|
|
|
|
self?.refreshArticles(account) { result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success():
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion?()
|
|
|
|
|
}
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion?()
|
|
|
|
|
self?.handleError(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-07 10:51:41 -05:00
|
|
|
|
}
|
2019-05-12 18:32:32 -05:00
|
|
|
|
|
2019-05-05 15:41:20 -05:00
|
|
|
|
case .failure(let error):
|
2019-05-07 10:51:41 -05:00
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion?()
|
2019-05-07 11:10:00 -05:00
|
|
|
|
self?.handleError(error)
|
2019-05-07 10:51:41 -05:00
|
|
|
|
}
|
2019-05-06 10:53:20 -05:00
|
|
|
|
}
|
2019-05-12 18:32:32 -05:00
|
|
|
|
|
2019-05-06 10:53:20 -05:00
|
|
|
|
}
|
2019-05-12 18:32:32 -05:00
|
|
|
|
|
2019-05-06 10:53:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 12:26:23 -05:00
|
|
|
|
func importOPML(for account:Account, opmlFile: URL, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
|
|
2019-05-11 16:07:27 -05:00
|
|
|
|
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, let children = loadDocument.children else {
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
importOPMLItems(account, items: children, parentFolder: nil)
|
2019-05-11 12:26:23 -05:00
|
|
|
|
|
2019-05-11 16:07:27 -05:00
|
|
|
|
completion(.success(()))
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-06 17:34:41 -05:00
|
|
|
|
func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-05-06 10:53:20 -05:00
|
|
|
|
|
|
|
|
|
caller.renameTag(oldName: folder.name ?? "", newName: name) { result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
2019-05-07 10:51:41 -05:00
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
folder.name = name
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
2019-05-06 10:53:20 -05:00
|
|
|
|
case .failure(let error):
|
2019-05-07 10:51:41 -05:00
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
2019-05-05 15:41:20 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-06 10:53:20 -05:00
|
|
|
|
|
2019-05-02 18:17:52 -05:00
|
|
|
|
}
|
2019-05-06 17:34:41 -05:00
|
|
|
|
|
|
|
|
|
func deleteFolder(for account: Account, with folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
|
|
|
|
|
|
// Feedbin uses tags and if at least one feed isn't tagged, then the folder doesn't exist on their system
|
|
|
|
|
guard folder.hasAtLeastOneFeed() else {
|
|
|
|
|
account.deleteFolder(folder)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 17:41:32 -05:00
|
|
|
|
// After we successfully delete at Feedbin, we add all the feeds to the account to save them. We then
|
|
|
|
|
// delete the folder. We then sync the taggings we received on the delete to remove any feeds from
|
|
|
|
|
// the account that might be in another folder.
|
|
|
|
|
caller.deleteTag(name: folder.name ?? "") { [weak self] result in
|
2019-05-06 17:34:41 -05:00
|
|
|
|
switch result {
|
2019-05-07 17:41:32 -05:00
|
|
|
|
case .success(let taggings):
|
|
|
|
|
DispatchQueue.main.sync {
|
|
|
|
|
BatchUpdate.shared.perform {
|
|
|
|
|
for feed in folder.topLevelFeeds {
|
2019-05-09 13:31:18 -05:00
|
|
|
|
account.addFeed(feed)
|
2019-05-07 18:34:45 -05:00
|
|
|
|
self?.clearFolderRelationship(for: feed, withFolderName: folder.name ?? "")
|
2019-05-07 17:41:32 -05:00
|
|
|
|
}
|
|
|
|
|
account.deleteFolder(folder)
|
|
|
|
|
}
|
2019-05-07 10:51:41 -05:00
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
2019-05-07 17:41:32 -05:00
|
|
|
|
self?.syncTaggings(account, taggings)
|
2019-05-06 17:34:41 -05:00
|
|
|
|
case .failure(let error):
|
2019-05-07 10:51:41 -05:00
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
2019-05-06 17:34:41 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2019-05-02 18:17:52 -05:00
|
|
|
|
|
2019-05-10 10:14:24 -05:00
|
|
|
|
func createFeed(for account: Account, url: String, completion: @escaping (Result<Feed, Error>) -> Void) {
|
2019-05-08 17:41:19 -05:00
|
|
|
|
|
2019-05-10 10:14:24 -05:00
|
|
|
|
caller.createSubscription(url: url) { [weak self] result in
|
2019-05-08 17:41:19 -05:00
|
|
|
|
switch result {
|
|
|
|
|
case .success(let subResult):
|
|
|
|
|
switch subResult {
|
2019-05-10 10:14:24 -05:00
|
|
|
|
case .created(let subscription):
|
|
|
|
|
self?.createFeed(account: account, subscription: subscription, completion: completion)
|
|
|
|
|
case .multipleChoice(let choices):
|
|
|
|
|
self?.decideBestFeedChoice(account: account, url: url, choices: choices, completion: completion)
|
2019-05-08 17:41:19 -05:00
|
|
|
|
case .alreadySubscribed:
|
|
|
|
|
DispatchQueue.main.async {
|
2019-05-10 10:14:24 -05:00
|
|
|
|
completion(.failure(AccountError.createErrorAlreadySubscribed))
|
2019-05-08 17:41:19 -05:00
|
|
|
|
}
|
|
|
|
|
case .notFound:
|
|
|
|
|
DispatchQueue.main.async {
|
2019-05-10 10:14:24 -05:00
|
|
|
|
completion(.failure(AccountError.createErrorNotFound))
|
2019-05-08 17:41:19 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func renameFeed(for account: Account, with feed: Feed, to name: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
|
|
2019-05-08 18:13:54 -05:00
|
|
|
|
// This error should never happen
|
|
|
|
|
guard let subscriptionID = feed.subscriptionID else {
|
|
|
|
|
completion(.failure(FeedbinAccountDelegateError.invalidParameter))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 07:25:45 -05:00
|
|
|
|
caller.renameSubscription(subscriptionID: subscriptionID, newName: name) { result in
|
2019-05-08 17:41:19 -05:00
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
feed.editedName = name
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 13:31:18 -05:00
|
|
|
|
func deleteFeed(for account: Account, with feed: Feed, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-05-09 07:25:45 -05: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-05-09 13:31:18 -05:00
|
|
|
|
account.removeFeed(feed)
|
|
|
|
|
if let folders = account.folders {
|
|
|
|
|
for folder in folders {
|
|
|
|
|
folder.removeFeed(feed)
|
|
|
|
|
}
|
2019-05-09 07:25:45 -05:00
|
|
|
|
}
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 13:31:18 -05:00
|
|
|
|
func addFeed(for account: Account, to container: Container, with feed: Feed, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
|
|
|
|
|
|
if let folder = container as? Folder, let feedID = Int(feed.feedID) {
|
|
|
|
|
caller.createTagging(feedID: feedID, name: folder.name ?? "") { [weak self] result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success(let taggingID):
|
|
|
|
|
DispatchQueue.main.async {
|
2019-05-09 13:35:40 -05:00
|
|
|
|
self?.saveFolderRelationship(for: feed, withFolderName: folder.name ?? "", id: String(taggingID))
|
2019-05-09 13:31:18 -05:00
|
|
|
|
folder.addFeed(feed)
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if let account = container as? Account {
|
|
|
|
|
account.addFeed(feed)
|
|
|
|
|
}
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func removeFeed(for account: Account, from container: Container, with feed: Feed, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
|
|
|
|
|
|
if let folder = container as? Folder, let feedTaggingID = feed.folderRelationship?[folder.name ?? ""] {
|
|
|
|
|
caller.deleteTagging(taggingID: feedTaggingID) { result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
folder.removeFeed(feed)
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if let account = container as? Account {
|
|
|
|
|
account.removeFeed(feed)
|
|
|
|
|
}
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2019-05-09 16:09:21 -05:00
|
|
|
|
|
|
|
|
|
func restoreFeed(for account: Account, feed: Feed, folder: Folder?, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
|
|
|
|
|
|
let editedName = feed.editedName
|
|
|
|
|
|
|
|
|
|
createFeed(for: account, url: feed.url) { [weak self] result in
|
|
|
|
|
switch result {
|
2019-05-10 10:14:24 -05:00
|
|
|
|
case .success(let feed):
|
|
|
|
|
self?.processRestoredFeed(for: account, feed: feed, editedName: editedName, folder: folder, completion: completion)
|
2019-05-09 16:09:21 -05:00
|
|
|
|
case .failure(let error):
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 18:20:09 -05:00
|
|
|
|
func restoreFolder(for account: Account, folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-05-09 16:09:21 -05:00
|
|
|
|
|
2019-05-09 18:20:09 -05:00
|
|
|
|
account.addFolder(folder)
|
|
|
|
|
let group = DispatchGroup()
|
|
|
|
|
|
|
|
|
|
for feed in folder.topLevelFeeds {
|
2019-05-09 16:09:21 -05:00
|
|
|
|
|
2019-05-09 18:20:09 -05:00
|
|
|
|
group.enter()
|
|
|
|
|
addFeed(for: account, to: folder, with: feed) { result in
|
|
|
|
|
if account.topLevelFeeds.contains(feed) {
|
|
|
|
|
account.removeFeed(feed)
|
2019-05-09 16:09:21 -05:00
|
|
|
|
}
|
2019-05-09 18:20:09 -05:00
|
|
|
|
group.leave()
|
2019-05-09 16:09:21 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 18:20:09 -05:00
|
|
|
|
group.notify(queue: DispatchQueue.main) {
|
|
|
|
|
completion(.success(()))
|
2019-05-09 16:09:21 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 18:17:52 -05:00
|
|
|
|
func accountDidInitialize(_ account: Account) {
|
2019-05-05 07:21:26 -05:00
|
|
|
|
credentials = try? account.retrieveBasicCredentials()
|
2019-05-05 17:46:53 -05:00
|
|
|
|
accountMetadata = account.metadata
|
2019-05-02 18:17:52 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-06 10:53:20 -05:00
|
|
|
|
static func validateCredentials(transport: Transport, credentials: Credentials, completion: @escaping (Result<Bool, Error>) -> Void) {
|
|
|
|
|
|
|
|
|
|
let caller = FeedbinAPICaller(transport: transport)
|
|
|
|
|
caller.credentials = credentials
|
|
|
|
|
caller.validateCredentials() { result in
|
2019-05-07 10:51:41 -05:00
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(result)
|
|
|
|
|
}
|
2019-05-06 10:53:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 18:17:52 -05:00
|
|
|
|
}
|
2019-05-05 15:41:20 -05:00
|
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
|
|
private extension FeedbinAccountDelegate {
|
|
|
|
|
|
2019-05-06 10:53:20 -05:00
|
|
|
|
func handleError(_ error: Error) {
|
|
|
|
|
// TODO: We should do a better job of error handling here.
|
2019-05-07 10:51:41 -05:00
|
|
|
|
// We need to prompt for credentials if they are expired.
|
2019-05-06 10:53:20 -05:00
|
|
|
|
#if os(macOS)
|
|
|
|
|
NSApplication.shared.presentError(error)
|
|
|
|
|
#else
|
|
|
|
|
UIApplication.shared.presentError(error)
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-12 18:32:32 -05:00
|
|
|
|
func refreshAccount(_ account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-05-05 15:41:20 -05:00
|
|
|
|
|
|
|
|
|
caller.retrieveTags { [weak self] result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success(let tags):
|
2019-05-07 11:10:00 -05:00
|
|
|
|
BatchUpdate.shared.perform {
|
|
|
|
|
self?.syncFolders(account, tags)
|
|
|
|
|
}
|
2019-05-07 10:51:41 -05:00
|
|
|
|
self?.refreshFeeds(account, completion: completion)
|
2019-05-05 15:41:20 -05:00
|
|
|
|
case .failure(let error):
|
2019-05-06 05:28:02 -05:00
|
|
|
|
completion(.failure(error))
|
2019-05-05 15:41:20 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-06 05:28:02 -05:00
|
|
|
|
func syncFolders(_ account: Account, _ tags: [FeedbinTag]?) {
|
|
|
|
|
|
|
|
|
|
guard let tags = tags else { return }
|
2019-05-08 08:20:29 -05:00
|
|
|
|
|
|
|
|
|
os_log(.debug, log: log, "Syncing folders with %ld tags.", tags.count)
|
|
|
|
|
|
2019-05-05 15:41:20 -05: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-05-07 10:51:41 -05:00
|
|
|
|
DispatchQueue.main.sync {
|
2019-05-07 17:41:32 -05:00
|
|
|
|
for feed in folder.topLevelFeeds {
|
2019-05-09 13:31:18 -05:00
|
|
|
|
account.addFeed(feed)
|
2019-05-07 18:34:45 -05:00
|
|
|
|
clearFolderRelationship(for: feed, withFolderName: folder.name ?? "")
|
2019-05-07 17:41:32 -05:00
|
|
|
|
}
|
2019-05-07 10:51:41 -05:00
|
|
|
|
account.deleteFolder(folder)
|
|
|
|
|
}
|
2019-05-05 15:41:20 -05: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-05-07 10:51:41 -05:00
|
|
|
|
DispatchQueue.main.sync {
|
|
|
|
|
_ = account.ensureFolder(with: tagName)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func refreshFeeds(_ account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-05-07 17:41:32 -05:00
|
|
|
|
|
2019-05-07 10:51:41 -05:00
|
|
|
|
caller.retrieveSubscriptions { [weak self] result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success(let subscriptions):
|
2019-05-07 17:41:32 -05:00
|
|
|
|
|
|
|
|
|
self?.caller.retrieveTaggings { [weak self] result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success(let taggings):
|
|
|
|
|
|
|
|
|
|
self?.caller.retrieveIcons { [weak self] result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success(let icons):
|
|
|
|
|
|
|
|
|
|
BatchUpdate.shared.perform {
|
|
|
|
|
self?.syncFeeds(account, subscriptions)
|
|
|
|
|
self?.syncTaggings(account, taggings)
|
|
|
|
|
self?.syncFavicons(account, icons)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 10:51:41 -05:00
|
|
|
|
case .failure(let error):
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
2019-05-07 17:41:32 -05:00
|
|
|
|
|
2019-05-07 10:51:41 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func syncFeeds(_ account: Account, _ subscriptions: [FeedbinSubscription]?) {
|
2019-05-07 17:41:32 -05:00
|
|
|
|
|
2019-05-07 10:51:41 -05:00
|
|
|
|
guard let subscriptions = subscriptions else { return }
|
2019-05-07 17:41:32 -05:00
|
|
|
|
|
2019-05-08 08:20:29 -05:00
|
|
|
|
os_log(.debug, log: log, "Syncing feeds with %ld subscriptions.", subscriptions.count)
|
|
|
|
|
|
2019-05-07 17:41:32 -05: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 {
|
|
|
|
|
for feed in folder.topLevelFeeds {
|
|
|
|
|
if !subFeedIds.contains(feed.feedID) {
|
|
|
|
|
DispatchQueue.main.sync {
|
2019-05-09 13:31:18 -05:00
|
|
|
|
folder.removeFeed(feed)
|
2019-05-07 17:41:32 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-07 11:10:00 -05:00
|
|
|
|
}
|
2019-05-07 10:51:41 -05:00
|
|
|
|
}
|
2019-05-07 17:41:32 -05:00
|
|
|
|
|
|
|
|
|
for feed in account.topLevelFeeds {
|
|
|
|
|
if !subFeedIds.contains(feed.feedID) {
|
|
|
|
|
DispatchQueue.main.sync {
|
2019-05-09 13:31:18 -05:00
|
|
|
|
account.removeFeed(feed)
|
2019-05-07 17:41:32 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add any feeds we don't have and update any we do
|
|
|
|
|
subscriptions.forEach { subscription in
|
|
|
|
|
|
|
|
|
|
let subFeedId = String(subscription.feedID)
|
|
|
|
|
|
|
|
|
|
DispatchQueue.main.sync {
|
|
|
|
|
if let feed = account.idToFeedDictionary[subFeedId] {
|
|
|
|
|
feed.name = subscription.name
|
|
|
|
|
feed.homePageURL = subscription.homePageURL
|
|
|
|
|
} else {
|
2019-05-08 17:41:19 -05:00
|
|
|
|
let feed = account.createFeed(with: subscription.name, url: subscription.url, feedID: subFeedId, homePageURL: subscription.homePageURL)
|
2019-05-08 18:13:54 -05:00
|
|
|
|
feed.subscriptionID = String(subscription.subscriptionID)
|
2019-05-09 13:31:18 -05:00
|
|
|
|
account.addFeed(feed)
|
2019-05-07 17:41:32 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 10:51:41 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 17:41:32 -05:00
|
|
|
|
func syncTaggings(_ account: Account, _ taggings: [FeedbinTagging]?) {
|
2019-05-07 10:51:41 -05:00
|
|
|
|
|
2019-05-07 17:41:32 -05:00
|
|
|
|
guard let taggings = taggings else { return }
|
|
|
|
|
|
2019-05-08 08:20:29 -05:00
|
|
|
|
os_log(.debug, log: log, "Syncing taggings with %ld taggings.", taggings.count)
|
|
|
|
|
|
2019-05-07 17:41:32 -05: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) } )
|
2019-05-07 10:51:41 -05:00
|
|
|
|
} else {
|
2019-05-07 17:41:32 -05:00
|
|
|
|
return [String: Folder]()
|
2019-05-05 15:41:20 -05:00
|
|
|
|
}
|
2019-05-07 17:41:32 -05:00
|
|
|
|
}()
|
|
|
|
|
|
2019-05-07 18:34:45 -05:00
|
|
|
|
let taggingsDict = taggings.reduce([String: [FeedbinTagging]]()) { (dict, tagging) in
|
2019-05-07 17:41:32 -05:00
|
|
|
|
var taggedFeeds = dict
|
|
|
|
|
if var taggedFeed = taggedFeeds[tagging.name] {
|
2019-05-07 18:34:45 -05:00
|
|
|
|
taggedFeed.append(tagging)
|
2019-05-07 17:41:32 -05:00
|
|
|
|
taggedFeeds[tagging.name] = taggedFeed
|
|
|
|
|
} else {
|
2019-05-07 18:34:45 -05:00
|
|
|
|
taggedFeeds[tagging.name] = [tagging]
|
2019-05-07 17:41:32 -05:00
|
|
|
|
}
|
|
|
|
|
return taggedFeeds
|
2019-05-05 15:41:20 -05:00
|
|
|
|
}
|
2019-05-07 10:51:41 -05:00
|
|
|
|
|
2019-05-07 17:41:32 -05:00
|
|
|
|
// Sync the folders
|
2019-05-07 18:34:45 -05:00
|
|
|
|
for (folderName, groupedTaggings) in taggingsDict {
|
2019-05-07 17:41:32 -05:00
|
|
|
|
|
|
|
|
|
guard let folder = folderDict[folderName] else { return }
|
2019-05-07 18:34:45 -05:00
|
|
|
|
|
|
|
|
|
let taggingFeedIDs = groupedTaggings.map { String($0.feedID) }
|
|
|
|
|
|
2019-05-07 17:41:32 -05:00
|
|
|
|
// Move any feeds not in the folder to the account
|
|
|
|
|
for feed in folder.topLevelFeeds {
|
2019-05-07 18:34:45 -05:00
|
|
|
|
if !taggingFeedIDs.contains(feed.feedID) {
|
2019-05-07 17:41:32 -05:00
|
|
|
|
DispatchQueue.main.sync {
|
2019-05-09 13:31:18 -05:00
|
|
|
|
folder.removeFeed(feed)
|
2019-05-07 18:34:45 -05:00
|
|
|
|
clearFolderRelationship(for: feed, withFolderName: folder.name ?? "")
|
2019-05-09 13:31:18 -05:00
|
|
|
|
account.addFeed(feed)
|
2019-05-07 17:41:32 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add any feeds not in the folder
|
|
|
|
|
let folderFeedIds = folder.topLevelFeeds.map { $0.feedID }
|
|
|
|
|
|
2019-05-07 18:34:45 -05:00
|
|
|
|
for tagging in groupedTaggings {
|
|
|
|
|
let taggingFeedID = String(tagging.feedID)
|
|
|
|
|
if !folderFeedIds.contains(taggingFeedID) {
|
|
|
|
|
guard let feed = account.idToFeedDictionary[taggingFeedID] else {
|
2019-05-07 17:41:32 -05:00
|
|
|
|
continue
|
|
|
|
|
}
|
2019-05-09 13:31:18 -05:00
|
|
|
|
DispatchQueue.main.sync {
|
2019-05-09 13:35:40 -05:00
|
|
|
|
saveFolderRelationship(for: feed, withFolderName: folderName, id: String(tagging.taggingID))
|
2019-05-09 13:31:18 -05:00
|
|
|
|
folder.addFeed(feed)
|
|
|
|
|
}
|
2019-05-07 17:41:32 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2019-05-05 15:41:20 -05:00
|
|
|
|
|
2019-05-07 18:34:45 -05:00
|
|
|
|
let taggedFeedIDs = Set(taggings.map { String($0.feedID) })
|
2019-05-07 17:41:32 -05:00
|
|
|
|
|
2019-05-08 08:20:29 -05:00
|
|
|
|
// Remove all feeds from the account container that have a tag
|
2019-05-07 17:41:32 -05:00
|
|
|
|
DispatchQueue.main.sync {
|
2019-05-09 13:31:18 -05:00
|
|
|
|
for feed in account.topLevelFeeds {
|
|
|
|
|
if taggedFeedIDs.contains(feed.feedID) {
|
|
|
|
|
account.removeFeed(feed)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-07 17:41:32 -05:00
|
|
|
|
}
|
2019-05-09 13:31:18 -05:00
|
|
|
|
|
2019-05-07 10:51:41 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 17:41:32 -05:00
|
|
|
|
func syncFavicons(_ account: Account, _ icons: [FeedbinIcon]?) {
|
2019-05-07 10:51:41 -05:00
|
|
|
|
|
|
|
|
|
guard let icons = icons else { return }
|
|
|
|
|
|
2019-05-08 08:20:29 -05:00
|
|
|
|
os_log(.debug, log: log, "Syncing favicons with %ld icons.", icons.count)
|
|
|
|
|
|
2019-05-07 10:51:41 -05:00
|
|
|
|
let iconDict = Dictionary(uniqueKeysWithValues: icons.map { ($0.host, $0.url) } )
|
|
|
|
|
|
|
|
|
|
for feed in account.flattenedFeeds() {
|
|
|
|
|
for (key, value) in iconDict {
|
|
|
|
|
if feed.homePageURL?.contains(key) ?? false {
|
|
|
|
|
DispatchQueue.main.sync {
|
|
|
|
|
feed.faviconURL = value
|
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-05 15:41:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-12 18:32:32 -05:00
|
|
|
|
func importOPMLItems(_ account: Account, items: [RSOPMLItem], parentFolder: Folder?) {
|
|
|
|
|
|
|
|
|
|
items.forEach { (item) in
|
|
|
|
|
|
|
|
|
|
if let feedSpecifier = item.feedSpecifier {
|
|
|
|
|
importFeedSpecifier(account, feedSpecifier: feedSpecifier, parentFolder: parentFolder)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard let folderName = item.titleFromAttributes else {
|
|
|
|
|
// Folder doesn’t have a name, so it won’t be created, and its items will go one level up.
|
|
|
|
|
if let itemChildren = item.children {
|
|
|
|
|
importOPMLItems(account, items: itemChildren, parentFolder: parentFolder)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let folder = account.ensureFolder(with: folderName) {
|
|
|
|
|
if let itemChildren = item.children {
|
|
|
|
|
importOPMLItems(account, items: itemChildren, parentFolder: folder)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func importFeedSpecifier(_ account: Account, feedSpecifier: RSOPMLFeedSpecifier, parentFolder: Folder?) {
|
|
|
|
|
|
|
|
|
|
caller.createSubscription(url: feedSpecifier.feedURL) { [weak self] result in
|
|
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
|
case .success(let subResult):
|
|
|
|
|
switch subResult {
|
|
|
|
|
case .created(let sub):
|
|
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
|
|
|
|
|
let feed = account.createFeed(with: sub.name, url: sub.url, feedID: String(sub.feedID), homePageURL: sub.homePageURL)
|
|
|
|
|
feed.subscriptionID = String(sub.subscriptionID)
|
|
|
|
|
|
|
|
|
|
self?.importFeedSpecifierPostProcess(account: account, sub: sub, feedSpecifier: feedSpecifier, feed: feed, parentFolder: parentFolder)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
guard let self = self else { return }
|
|
|
|
|
os_log(.error, log: self.log, "Create feed on OPML import failed: %@.", error.localizedDescription)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func importFeedSpecifierPostProcess(account: Account, sub: FeedbinSubscription, feedSpecifier: RSOPMLFeedSpecifier, feed: Feed, parentFolder: Folder?) {
|
|
|
|
|
|
|
|
|
|
// Rename the feed if its name in the OPML file doesn't match the found name
|
|
|
|
|
if sub.name != feedSpecifier.title, let newName = feedSpecifier.title {
|
|
|
|
|
|
|
|
|
|
self.caller.renameSubscription(subscriptionID: String(sub.subscriptionID), newName: newName) { [weak self] result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
feed.editedName = newName
|
|
|
|
|
}
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
guard let self = self else { return }
|
|
|
|
|
os_log(.error, log: self.log, "Rename feed on OPML import failed: %@.", error.localizedDescription)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Move the new feed if it is in a folder
|
|
|
|
|
if let folder = parentFolder, let feedID = Int(feed.feedID) {
|
|
|
|
|
|
|
|
|
|
self.caller.createTagging(feedID: feedID, name: folder.name ?? "") { [weak self] result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success(let taggingID):
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
self?.saveFolderRelationship(for: feed, withFolderName: folder.name ?? "", id: String(taggingID))
|
|
|
|
|
folder.addFeed(feed)
|
|
|
|
|
}
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
guard let self = self else { return }
|
|
|
|
|
os_log(.error, log: self.log, "Move feed to folder on OPML import failed: %@.", error.localizedDescription)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
account.addFeed(feed)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 18:20:09 -05:00
|
|
|
|
func processRestoredFeed(for account: Account, feed: Feed, editedName: String?, folder: Folder?, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
|
|
|
|
|
|
if let folder = folder {
|
|
|
|
|
|
|
|
|
|
addFeed(for: account, to: folder, with: feed) { [weak self] result in
|
|
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
|
|
|
|
|
if editedName != nil {
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
folder.addFeed(feed)
|
|
|
|
|
}
|
|
|
|
|
self?.processRestoredFeedName(for: account, feed: feed, editedName: editedName!, completion: completion)
|
|
|
|
|
} else {
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
folder.addFeed(feed)
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
account.addFeed(feed)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 16:07:27 -05:00
|
|
|
|
if editedName != nil {
|
|
|
|
|
processRestoredFeedName(for: account, feed: feed, editedName: editedName!, completion: completion)
|
|
|
|
|
}
|
2019-05-09 18:20:09 -05:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func processRestoredFeedName(for account: Account, feed: Feed, editedName: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
|
|
|
|
|
|
renameFeed(for: account, with: feed, to: editedName) { result in
|
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
feed.editedName = editedName
|
|
|
|
|
completion(.success(()))
|
|
|
|
|
}
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 18:34:45 -05:00
|
|
|
|
func clearFolderRelationship(for feed: Feed, withFolderName folderName: String) {
|
2019-05-09 13:35:40 -05:00
|
|
|
|
if var folderRelationship = feed.folderRelationship {
|
|
|
|
|
folderRelationship[folderName] = nil
|
|
|
|
|
feed.folderRelationship = folderRelationship
|
2019-05-07 18:34:45 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func saveFolderRelationship(for feed: Feed, withFolderName folderName: String, id: String) {
|
2019-05-09 13:35:40 -05:00
|
|
|
|
if var folderRelationship = feed.folderRelationship {
|
|
|
|
|
folderRelationship[folderName] = id
|
|
|
|
|
feed.folderRelationship = folderRelationship
|
|
|
|
|
} else {
|
|
|
|
|
feed.folderRelationship = [folderName: id]
|
2019-05-07 18:34:45 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-10 10:14:24 -05:00
|
|
|
|
|
|
|
|
|
func decideBestFeedChoice(account: Account, url: String, choices: [FeedbinSubscriptionChoice], completion: @escaping (Result<Feed, Error>) -> Void) {
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
createFeed(for: account, url: bestSubscription.url, completion: completion)
|
|
|
|
|
} else {
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(.failure(FeedbinAccountDelegateError.invalidParameter))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
completion(.failure(FeedbinAccountDelegateError.invalidParameter))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func createFeed( account: Account, subscription sub: FeedbinSubscription, completion: @escaping (Result<Feed, Error>) -> Void) {
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
let feed = account.createFeed(with: sub.name, url: sub.url, feedID: String(sub.feedID), homePageURL: sub.homePageURL)
|
|
|
|
|
feed.subscriptionID = String(sub.subscriptionID)
|
|
|
|
|
completion(.success(feed))
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-12 18:32:32 -05:00
|
|
|
|
|
|
|
|
|
func refreshArticles(_ account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
|
|
|
|
|
|
os_log(.debug, log: log, "Refreshing articles...")
|
|
|
|
|
|
|
|
|
|
for feed in account.flattenedFeeds() {
|
|
|
|
|
|
|
|
|
|
caller.retrieveEntries(feed.feedID) { [weak self] result in
|
|
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
|
case .success(let (entries, page)):
|
|
|
|
|
|
|
|
|
|
self?.processEntries(account: account, entries: entries, completion: completion)
|
|
|
|
|
self?.refreshArticles(account, page: page)
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
guard let self = self else { return }
|
|
|
|
|
os_log(.error, log: self.log, "Refresh articles failed: %@.", error.localizedDescription)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func refreshArticles(_ account: Account, page: String?) {
|
|
|
|
|
|
|
|
|
|
guard let page = page else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
caller.retrieveEntries(page: page) { [weak self] result in
|
|
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
|
case .success(let (entries, nextPage)):
|
|
|
|
|
|
|
|
|
|
self?.processEntries(account: account, entries: entries, completion: nil)
|
|
|
|
|
self?.refreshArticles(account, page: nextPage)
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
guard let self = self else { return }
|
|
|
|
|
os_log(.error, log: self.log, "Refresh articles for additional pages failed: %@.", error.localizedDescription)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func processEntries(account: Account, entries: [FeedbinEntry]?, completion: ((Result<Void, Error>) -> Void)?) {
|
|
|
|
|
|
|
|
|
|
let parsedItems = mapEntriesToParsedItems(entries: entries)
|
|
|
|
|
let parsedMap = Dictionary(grouping: parsedItems, by: { item in item.feedURL } )
|
|
|
|
|
|
|
|
|
|
for (feedID, mapItems) in parsedMap {
|
|
|
|
|
if let feed = account.idToFeedDictionary[feedID] {
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
account.update(feed, parsedItems: Set(mapItems)) {
|
|
|
|
|
completion?(.success(()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mapEntriesToParsedItems(entries: [FeedbinEntry]?) -> Set<ParsedItem> {
|
|
|
|
|
|
|
|
|
|
guard let entries = entries else {
|
|
|
|
|
return Set<ParsedItem>()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let parsedItems: [ParsedItem] = entries.map { entry in
|
|
|
|
|
let authors = Set([ParsedAuthor(name: entry.authorName, url: nil, avatarURL: nil, emailAddress: nil)])
|
|
|
|
|
return ParsedItem(syncServiceID: String(entry.articleID), uniqueID: String(entry.articleID), feedURL: String(entry.feedID), url: nil, externalURL: entry.url, title: entry.title, contentHTML: entry.contentHTML, contentText: nil, summary: entry.summary, imageURL: nil, bannerImageURL: nil, datePublished: entry.parseDatePublished(), dateModified: nil, authors: authors, tags: nil, attachments: nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Set(parsedItems)
|
|
|
|
|
|
|
|
|
|
}
|
2019-05-07 18:34:45 -05:00
|
|
|
|
|
2019-05-05 15:41:20 -05:00
|
|
|
|
}
|