NetNewsWire/Frameworks/Account/NewsBlur/NewsBlurAccountDelegate.swift

463 lines
14 KiB
Swift
Raw Normal View History

2020-03-10 01:19:24 +01:00
//
// NewsBlurAccountDelegate.swift
// Account
//
// Created by Anh-Quang Do on 3/9/20.
// Copyright (c) 2020 Ranchero Software, LLC. All rights reserved.
//
import Articles
import RSCore
import RSDatabase
import RSParser
import RSWeb
import SyncDatabase
import os.log
final class NewsBlurAccountDelegate: AccountDelegate {
var behaviors: AccountBehaviors = []
var isOPMLImportInProgress: Bool = false
var server: String? = "newsblur.com"
var credentials: Credentials? {
didSet {
caller.credentials = credentials
}
}
var accountMetadata: AccountMetadata? = nil
var refreshProgress = DownloadProgress(numberOfTasks: 0)
let caller: NewsBlurAPICaller
let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "NewsBlur")
let database: SyncDatabase
2020-03-10 01:19:24 +01:00
init(dataFolder: String, transport: Transport?) {
if let transport = transport {
caller = NewsBlurAPICaller(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
}
let session = URLSession(configuration: sessionConfiguration)
caller = NewsBlurAPICaller(transport: session)
}
database = SyncDatabase(databaseFilePath: dataFolder.appending("/DB.sqlite3"))
}
func refreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> ()) {
2020-03-11 02:08:56 +01:00
self.refreshProgress.addToNumberOfTasksAndRemaining(5)
refreshFeeds(for: account) { result in
2020-03-11 02:08:56 +01:00
self.refreshProgress.completeTask()
2020-03-10 04:26:48 +01:00
switch result {
case .success:
2020-03-11 02:08:56 +01:00
self.sendArticleStatus(for: account) { result in
self.refreshProgress.completeTask()
switch result {
case .success:
self.refreshArticleStatus(for: account) { result in
self.refreshProgress.completeTask()
switch result {
case .success:
self.refreshStories(for: account) { result in
2020-03-11 02:08:56 +01:00
self.refreshProgress.completeTask()
switch result {
case .success:
self.refreshMissingStories(for: account) { result in
2020-03-11 02:08:56 +01:00
self.refreshProgress.completeTask()
switch result {
case .success:
DispatchQueue.main.async {
completion(.success(()))
}
case .failure(let error):
2020-03-14 23:33:38 +01:00
DispatchQueue.main.async {
self.refreshProgress.clear()
let wrappedError = AccountError.wrappedError(error: error, account: account)
completion(.failure(wrappedError))
}
2020-03-11 02:08:56 +01:00
}
}
case .failure(let error):
completion(.failure(error))
}
}
case .failure(let error):
completion(.failure(error))
}
}
case .failure(let error):
completion(.failure(error))
}
}
2020-03-10 04:26:48 +01:00
case .failure(let error):
completion(.failure(error))
}
}
2020-03-10 01:19:24 +01:00
}
func sendArticleStatus(for account: Account, completion: @escaping (Result<Void, Error>) -> ()) {
2020-03-14 04:16:52 +01:00
os_log(.debug, log: log, "Sending story statuses...")
database.selectForProcessing { result in
func processStatuses(_ syncStatuses: [SyncStatus]) {
let createUnreadStatuses = syncStatuses.filter { $0.key == ArticleStatus.Key.read && $0.flag == false }
let deleteUnreadStatuses = syncStatuses.filter { $0.key == ArticleStatus.Key.read && $0.flag == true }
let createStarredStatuses = syncStatuses.filter { $0.key == ArticleStatus.Key.starred && $0.flag == true }
let deleteStarredStatuses = syncStatuses.filter { $0.key == ArticleStatus.Key.starred && $0.flag == false }
let group = DispatchGroup()
var errorOccurred = false
group.enter()
self.sendStoryStatuses(createUnreadStatuses, throttle: true, apiCall: self.caller.markAsUnread) { result in
group.leave()
if case .failure = result {
errorOccurred = true
}
}
group.enter()
2020-03-14 21:09:19 +01:00
self.sendStoryStatuses(deleteUnreadStatuses, throttle: false, apiCall: self.caller.markAsRead) { result in
2020-03-14 04:16:52 +01:00
group.leave()
if case .failure = result {
errorOccurred = true
}
}
group.enter()
self.sendStoryStatuses(createStarredStatuses, throttle: true, apiCall: self.caller.star) { result in
group.leave()
if case .failure = result {
errorOccurred = true
}
}
group.enter()
self.sendStoryStatuses(deleteStarredStatuses, throttle: true, apiCall: self.caller.unstar) { 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(NewsBlurError.unknown))
} else {
completion(.success(()))
}
}
}
switch result {
case .success(let syncStatuses):
processStatuses(syncStatuses)
case .failure(let databaseError):
completion(.failure(databaseError))
}
}
2020-03-10 01:19:24 +01:00
}
func refreshArticleStatus(for account: Account, completion: @escaping (Result<Void, Error>) -> ()) {
2020-03-14 21:10:05 +01:00
os_log(.debug, log: log, "Refreshing story statuses...")
2020-03-14 04:16:52 +01:00
2020-03-14 21:10:05 +01:00
let group = DispatchGroup()
var errorOccurred = false
group.enter()
caller.retrieveUnreadStoryHashes { result in
switch result {
case .success(let storyHashes):
self.syncStoryReadState(account: account, hashes: storyHashes)
group.leave()
case .failure(let error):
errorOccurred = true
os_log(.info, log: self.log, "Retrieving unread stories failed: %@.", error.localizedDescription)
group.leave()
}
}
group.enter()
caller.retrieveStarredStoryHashes { result in
switch result {
case .success(let storyHashes):
self.syncStoryStarredState(account: account, hashes: storyHashes)
group.leave()
case .failure(let error):
errorOccurred = true
os_log(.info, log: self.log, "Retrieving starred stories failed: %@.", error.localizedDescription)
group.leave()
}
}
group.notify(queue: DispatchQueue.main) {
os_log(.debug, log: self.log, "Done refreshing article statuses.")
if errorOccurred {
completion(.failure(NewsBlurError.unknown))
} else {
completion(.success(()))
}
}
2020-03-10 01:19:24 +01:00
}
func refreshStories(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
os_log(.debug, log: log, "Refreshing stories...")
os_log(.debug, log: log, "Refreshing unread stories...")
2020-03-11 02:08:56 +01:00
caller.retrieveUnreadStoryHashes { result in
2020-03-11 02:08:56 +01:00
switch result {
case .success(let storyHashes):
2020-03-13 23:18:47 +01:00
self.refreshProgress.completeTask()
2020-03-14 23:33:38 +01:00
if let count = storyHashes?.count, count > 0 {
self.refreshProgress.addToNumberOfTasksAndRemaining((count - 1) / 100 + 1)
}
self.refreshUnreadStories(for: account, hashes: storyHashes, updateFetchDate: nil, completion: completion)
2020-03-13 23:18:47 +01:00
case .failure(let error):
completion(.failure(error))
}
}
}
func refreshMissingStories(for account: Account, completion: @escaping (Result<Void, Error>)-> Void) {
2020-03-14 22:51:21 +01:00
os_log(.debug, log: log, "Refreshing missing stories...")
account.fetchArticleIDsForStatusesWithoutArticlesNewerThanCutoffDate { result in
func process(_ fetchedHashes: Set<String>) {
let group = DispatchGroup()
var errorOccurred = false
let storyHashes = Array(fetchedHashes).map { NewsBlurStoryHash(hash: $0, timestamp: Date()) }
let chunkedStoryHashes = storyHashes.chunked(into: 100)
for chunk in chunkedStoryHashes {
group.enter()
self.caller.retrieveStories(hashes: chunk) { result in
switch result {
case .success(let stories):
self.processStories(account: account, stories: stories) { error in
group.leave()
if error != nil {
errorOccurred = true
}
}
case .failure(let error):
errorOccurred = true
os_log(.error, log: self.log, "Refresh missing stories failed: %@.", error.localizedDescription)
group.leave()
}
}
}
group.notify(queue: DispatchQueue.main) {
self.refreshProgress.completeTask()
os_log(.debug, log: self.log, "Done refreshing missing stories.")
if errorOccurred {
completion(.failure(NewsBlurError.unknown))
} else {
completion(.success(()))
}
}
}
switch result {
case .success(let fetchedArticleIDs):
process(fetchedArticleIDs)
case .failure(let error):
self.refreshProgress.completeTask()
completion(.failure(error))
}
}
2020-03-11 02:08:56 +01:00
}
2020-03-14 22:51:21 +01:00
func processStories(account: Account, stories: [NewsBlurStory]?, completion: @escaping DatabaseCompletionBlock) {
let parsedItems = mapStoriesToParsedItems(stories: stories)
2020-03-13 23:18:47 +01:00
let webFeedIDsAndItems = Dictionary(grouping: parsedItems, by: { item in item.feedURL } ).mapValues { Set($0) }
account.update(webFeedIDsAndItems: webFeedIDsAndItems, defaultRead: true, completion: completion)
}
2020-03-10 01:19:24 +01:00
func importOPML(for account: Account, opmlFile: URL, completion: @escaping (Result<Void, Error>) -> ()) {
completion(.success(()))
}
func addFolder(for account: Account, name: String, completion: @escaping (Result<Folder, Error>) -> ()) {
2020-03-15 01:00:29 +01:00
self.refreshProgress.addToNumberOfTasksAndRemaining(1)
caller.addFolder(named: name) { result in
self.refreshProgress.completeTask()
switch result {
case .success():
if let folder = account.ensureFolder(with: name) {
completion(.success(folder))
} else {
completion(.failure(NewsBlurError.invalidParameter))
}
case .failure(let error):
completion(.failure(error))
}
}
2020-03-10 01:19:24 +01:00
}
func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result<Void, Error>) -> ()) {
2020-03-15 01:42:45 +01:00
guard let folderToRename = folder.name else {
completion(.failure(NewsBlurError.invalidParameter))
return
}
refreshProgress.addToNumberOfTasksAndRemaining(1)
let nameBefore = folder.name
caller.renameFolder(with: folderToRename, to: name) { result in
self.refreshProgress.completeTask()
switch result {
case .success:
completion(.success(()))
case .failure(let error):
folder.name = nameBefore
completion(.failure(error))
}
}
folder.name = name
2020-03-10 01:19:24 +01:00
}
func removeFolder(for account: Account, with folder: Folder, completion: @escaping (Result<Void, Error>) -> ()) {
2020-03-15 01:42:45 +01:00
guard let folderToRemove = folder.name else {
completion(.failure(NewsBlurError.invalidParameter))
return
}
2020-03-15 02:09:42 +01:00
var feedIDs: [String] = []
for feed in folder.topLevelWebFeeds {
if feed.folderRelationship?.count ?? 0 > 1 {
clearFolderRelationship(for: feed, withFolderName: folderToRemove)
} else if let subscriptionID = feed.subscriptionID {
feedIDs.append(subscriptionID)
}
}
2020-03-15 01:42:45 +01:00
refreshProgress.addToNumberOfTasksAndRemaining(1)
2020-03-15 02:09:42 +01:00
caller.removeFolder(named: folderToRemove, feedIDs: feedIDs) { result in
2020-03-15 01:42:45 +01:00
self.refreshProgress.completeTask()
switch result {
case .success:
account.removeFolder(folder)
completion(.success(()))
case .failure(let error):
completion(.failure(error))
}
}
2020-03-10 01:19:24 +01:00
}
func createWebFeed(for account: Account, url: String, name: String?, container: Container, completion: @escaping (Result<WebFeed, Error>) -> ()) {
}
func renameWebFeed(for account: Account, with feed: WebFeed, to name: String, completion: @escaping (Result<Void, Error>) -> ()) {
completion(.success(()))
}
func addWebFeed(for account: Account, with: WebFeed, to container: Container, completion: @escaping (Result<Void, Error>) -> ()) {
completion(.success(()))
}
func removeWebFeed(for account: Account, with feed: WebFeed, from container: Container, completion: @escaping (Result<Void, Error>) -> ()) {
completion(.success(()))
}
func moveWebFeed(for account: Account, with feed: WebFeed, from: Container, to: Container, completion: @escaping (Result<Void, Error>) -> ()) {
completion(.success(()))
}
func restoreWebFeed(for account: Account, feed: WebFeed, container: Container, completion: @escaping (Result<Void, Error>) -> ()) {
completion(.success(()))
}
func restoreFolder(for account: Account, folder: Folder, completion: @escaping (Result<Void, Error>) -> ()) {
completion(.success(()))
}
func markArticles(for account: Account, articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) -> Set<Article>? {
2020-03-14 04:16:52 +01:00
let syncStatuses = articles.map { article in
return SyncStatus(articleID: article.articleID, key: statusKey, flag: flag)
}
database.insertStatuses(syncStatuses)
database.selectPendingCount { result in
if let count = try? result.get(), count > 100 {
self.sendArticleStatus(for: account) { _ in }
}
}
return try? account.update(articles, statusKey: statusKey, flag: flag)
2020-03-10 01:19:24 +01:00
}
func accountDidInitialize(_ account: Account) {
2020-03-10 04:26:11 +01:00
credentials = try? account.retrieveCredentials(type: .newsBlurSessionId)
2020-03-10 01:19:24 +01:00
}
func accountWillBeDeleted(_ account: Account) {
caller.logout() { _ in }
}
class func validateCredentials(transport: Transport, credentials: Credentials, endpoint: URL? = nil, completion: @escaping (Result<Credentials?, Error>) -> ()) {
let caller = NewsBlurAPICaller(transport: transport)
caller.credentials = credentials
caller.validateCredentials() { result in
DispatchQueue.main.async {
completion(result)
}
}
}
2020-03-14 03:23:54 +01:00
// MARK: Suspend and Resume (for iOS)
/// Suspend all network activity
2020-03-10 01:19:24 +01:00
func suspendNetwork() {
2020-03-14 03:23:54 +01:00
caller.suspend()
2020-03-10 01:19:24 +01:00
}
2020-03-14 03:23:54 +01:00
/// Suspend the SQLLite databases
2020-03-10 01:19:24 +01:00
func suspendDatabase() {
database.suspend()
}
2020-03-14 03:23:54 +01:00
/// Make sure no SQLite databases are open and we are ready to issue network requests.
2020-03-10 01:19:24 +01:00
func resume() {
2020-03-14 03:23:54 +01:00
caller.resume()
2020-03-10 01:19:24 +01:00
database.resume()
}
}