From 0b2d7c6733f426b1b0b4318a9da54429c9aaf3b8 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Tue, 26 Dec 2017 11:27:55 -0800 Subject: [PATCH] Validate unread counts on fetching articles. Fix #274. --- Frameworks/Account/Account.swift | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index 7f9918455..20f07c257 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -328,13 +328,36 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, } public func fetchArticles(for feed: Feed) -> Set
{ - - return database.fetchArticles(for: feed) + + let articles = database.fetchArticles(for: feed) + validateUnreadCount(feed, articles) + return articles } public func fetchArticles(folder: Folder) -> Set
{ - - return database.fetchUnreadArticles(for: folder.flattenedFeeds()) + + let feeds = folder.flattenedFeeds() + let articles = database.fetchUnreadArticles(for: feeds) + feeds.forEach { validateUnreadCount($0, articles) } + return articles + } + + private func validateUnreadCount(_ feed: Feed, _ articles: Set
) { + + // articles must contain all the unread articles for the feed. + // The unread number should match the feed’s unread count. + + let feedUnreadCount = articles.reduce(0) { (result, article) -> Int in + if article.feed == feed && !article.status.read { + return result + 1 + } + return result + } + + if feedUnreadCount != feed.unreadCount { + assertionFailure("Expected feed.unreadCount \(feed.unreadCount) to equal number of fetched unread articles \(feedUnreadCount).") + feed.unreadCount = feedUnreadCount + } } public func fetchUnreadCountForToday(_ callback: @escaping (Int) -> Void) {