2019-09-18 01:18:06 +02:00
|
|
|
//
|
|
|
|
// FeedlyAccountDelegate.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Kiel Gillard on 3/9/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Articles
|
|
|
|
import RSCore
|
|
|
|
import RSParser
|
|
|
|
import RSWeb
|
|
|
|
import SyncDatabase
|
|
|
|
import os.log
|
|
|
|
|
|
|
|
final class FeedlyAccountDelegate: AccountDelegate {
|
2019-09-20 18:34:31 +02:00
|
|
|
|
|
|
|
// TODO: Kiel, if you decide not to support OPML import you will have to disallow it in the behaviors
|
|
|
|
// See https://developer.feedly.com/v3/opml/
|
|
|
|
var behaviors: AccountBehaviors = [.disallowFeedInRootFolder]
|
2019-09-18 01:18:06 +02:00
|
|
|
|
|
|
|
let isOPMLImportSupported = false
|
|
|
|
|
|
|
|
var isOPMLImportInProgress = false
|
|
|
|
|
|
|
|
var server: String? {
|
|
|
|
return caller.server
|
|
|
|
}
|
|
|
|
|
2019-09-19 04:56:43 +02:00
|
|
|
var credentials: Credentials? {
|
|
|
|
didSet {
|
|
|
|
// https://developer.feedly.com/v3/developer/
|
|
|
|
if let devToken = ProcessInfo.processInfo.environment["FEEDLY_DEV_ACCESS_TOKEN"], !devToken.isEmpty {
|
|
|
|
caller.credentials = Credentials(type: .oauthAccessToken, username: "", secret: devToken)
|
|
|
|
} else {
|
|
|
|
caller.credentials = credentials
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 01:18:06 +02:00
|
|
|
|
|
|
|
var accountMetadata: AccountMetadata?
|
|
|
|
|
|
|
|
var refreshProgress = DownloadProgress(numberOfTasks: 0)
|
|
|
|
|
|
|
|
private let caller: FeedlyAPICaller
|
2019-09-19 04:56:43 +02:00
|
|
|
private let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "Feedly")
|
2019-09-23 09:29:53 +02:00
|
|
|
private let articleStatusCoodinator: FeedlyArticleStatusCoordinator
|
2019-09-18 01:18:06 +02:00
|
|
|
|
|
|
|
init(dataFolder: String, transport: Transport?, api: FeedlyAPICaller.API = .default) {
|
|
|
|
|
|
|
|
if let transport = transport {
|
|
|
|
caller = FeedlyAPICaller(transport: transport, api: api)
|
|
|
|
|
|
|
|
} 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
|
|
|
|
}
|
|
|
|
|
|
|
|
let session = URLSession(configuration: sessionConfiguration)
|
|
|
|
caller = FeedlyAPICaller(transport: session, api: api)
|
|
|
|
}
|
|
|
|
|
2019-09-23 09:29:53 +02:00
|
|
|
articleStatusCoodinator = FeedlyArticleStatusCoordinator(dataFolderPath: dataFolder,
|
|
|
|
caller: caller,
|
|
|
|
log: log)
|
2019-09-18 01:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Account API
|
|
|
|
|
2019-09-19 04:56:43 +02:00
|
|
|
private var syncStrategy: FeedlySyncStrategy?
|
|
|
|
|
2019-09-18 01:18:06 +02:00
|
|
|
func refreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-09-19 04:56:43 +02:00
|
|
|
let date = Date()
|
|
|
|
let log = self.log
|
|
|
|
let progress = refreshProgress
|
|
|
|
progress.addToNumberOfTasksAndRemaining(1)
|
|
|
|
syncStrategy?.startSync { result in
|
|
|
|
os_log(.debug, log: log, "Sync took %.3f seconds", -date.timeIntervalSinceNow)
|
2019-09-20 17:16:51 +02:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
progress.completeTask()
|
|
|
|
}
|
2019-09-19 04:56:43 +02:00
|
|
|
}
|
2019-09-18 01:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func sendArticleStatus(for account: Account, completion: @escaping (() -> Void)) {
|
2019-09-23 09:29:53 +02:00
|
|
|
// Ensure remote articles have the same status as they do locally.
|
|
|
|
articleStatusCoodinator.sendArticleStatus(for: account, completion: completion)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to ensure local articles have the same status as they do remotely.
|
|
|
|
/// So if the user is using another client roughly simultaneously with this app,
|
|
|
|
/// this app does its part to ensure the articles have a consistent status between both.
|
|
|
|
///
|
|
|
|
/// Feedly has no API that allows the app to fetch the identifiers of unread articles only.
|
|
|
|
/// The only way to identify unread articles is to pull all of the article data,
|
|
|
|
/// which is effectively equivalent of a full refresh.
|
|
|
|
///
|
|
|
|
/// - Parameter account: The account whose articles have a remote status.
|
|
|
|
/// - Parameter completion: Call on the main queue.
|
2019-09-18 01:18:06 +02:00
|
|
|
func refreshArticleStatus(for account: Account, completion: @escaping (() -> Void)) {
|
2019-09-23 09:29:53 +02:00
|
|
|
refreshAll(for: account) { _ in
|
|
|
|
completion()
|
|
|
|
}
|
2019-09-18 01:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func importOPML(for account: Account, opmlFile: URL, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-09-23 09:29:53 +02:00
|
|
|
let data: Data
|
|
|
|
|
|
|
|
do {
|
|
|
|
data = try Data(contentsOf: opmlFile)
|
|
|
|
} catch {
|
|
|
|
completion(.failure(error))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
os_log(.debug, log: log, "Begin importing OPML...")
|
|
|
|
isOPMLImportInProgress = true
|
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(1)
|
|
|
|
|
|
|
|
caller.importOpml(data) { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
os_log(.debug, log: self.log, "Import OPML done.")
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
self.isOPMLImportInProgress = false
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
case .failure(let error):
|
|
|
|
os_log(.debug, log: self.log, "Import OPML failed.")
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
self.isOPMLImportInProgress = false
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
let wrappedError = AccountError.wrappedError(error: error, account: account)
|
|
|
|
completion(.failure(wrappedError))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 01:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func addFolder(for account: Account, name: String, completion: @escaping (Result<Folder, Error>) -> Void) {
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
|
|
|
|
func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeFolder(for account: Account, with folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
|
|
|
|
func createFeed(for account: Account, url: String, name: String?, container: Container, completion: @escaping (Result<Feed, Error>) -> Void) {
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
|
|
|
|
func renameFeed(for account: Account, with feed: Feed, to name: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
|
|
|
|
func addFeed(for account: Account, with: Feed, to container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeFeed(for account: Account, with feed: Feed, from container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
|
|
|
|
func moveFeed(for account: Account, with feed: Feed, from: Container, to: Container, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
|
|
|
|
func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
|
|
|
|
func restoreFolder(for account: Account, folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
|
|
|
|
func markArticles(for account: Account, articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) -> Set<Article>? {
|
2019-09-19 04:56:43 +02:00
|
|
|
|
2019-09-23 09:29:53 +02:00
|
|
|
let acceptedStatuses = articleStatusCoodinator.articles(articles,
|
|
|
|
for: account,
|
|
|
|
didChangeStatus: statusKey,
|
|
|
|
flag: flag)
|
2019-09-19 04:56:43 +02:00
|
|
|
|
2019-09-23 09:29:53 +02:00
|
|
|
return acceptedStatuses
|
2019-09-18 01:18:06 +02:00
|
|
|
}
|
2019-09-23 09:29:53 +02:00
|
|
|
|
2019-09-18 01:18:06 +02:00
|
|
|
func accountDidInitialize(_ account: Account) {
|
|
|
|
credentials = try? account.retrieveCredentials(type: .oauthAccessToken)
|
2019-09-19 04:56:43 +02:00
|
|
|
|
2019-09-23 09:29:53 +02:00
|
|
|
syncStrategy = FeedlySyncStrategy(account: account,
|
|
|
|
caller: caller,
|
|
|
|
articleStatusCoordinator: articleStatusCoodinator,
|
|
|
|
log: log)
|
2019-09-19 04:56:43 +02:00
|
|
|
|
|
|
|
//TODO: Figure out how other accounts get refreshed automatically.
|
|
|
|
refreshAll(for: account) { result in
|
|
|
|
print("sync after initialise did complete")
|
|
|
|
}
|
2019-09-18 01:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static func validateCredentials(transport: Transport, credentials: Credentials, endpoint: URL?, completion: @escaping (Result<Credentials?, Error>) -> Void) {
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
}
|