From 1d3073c593593ec580d9e1efabc203e31420618a Mon Sep 17 00:00:00 2001 From: Anh Do Date: Sat, 14 Mar 2020 17:51:21 -0400 Subject: [PATCH] Add missing story fetching --- .../NewsBlur/NewsBlurAccountDelegate.swift | 54 ++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/Frameworks/Account/NewsBlur/NewsBlurAccountDelegate.swift b/Frameworks/Account/NewsBlur/NewsBlurAccountDelegate.swift index f6e781cb8..74f277b2b 100644 --- a/Frameworks/Account/NewsBlur/NewsBlurAccountDelegate.swift +++ b/Frameworks/Account/NewsBlur/NewsBlurAccountDelegate.swift @@ -239,9 +239,59 @@ final class NewsBlurAccountDelegate: AccountDelegate { } func refreshMissingStories(for account: Account, completion: @escaping (Result)-> Void) { - completion(.success(())) + os_log(.debug, log: log, "Refreshing missing stories...") + + account.fetchArticleIDsForStatusesWithoutArticlesNewerThanCutoffDate { result in + func process(_ fetchedHashes: Set) { + 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)) + } + } } - + func processStories(account: Account, stories: [NewsBlurStory]?, completion: @escaping DatabaseCompletionBlock) { let parsedItems = mapStoriesToParsedItems(stories: stories) let webFeedIDsAndItems = Dictionary(grouping: parsedItems, by: { item in item.feedURL } ).mapValues { Set($0) }