Updated FreshRSS to pull statuses before articles and to work with the new sync status API's

This commit is contained in:
Maurice Parker 2019-08-30 11:47:08 -05:00
parent ea1f21797c
commit 995fca5d6a
1 changed files with 64 additions and 64 deletions

View File

@ -88,12 +88,15 @@ final class ReaderAPIAccountDelegate: AccountDelegate {
refreshAccount(account) { result in refreshAccount(account) { result in
switch result { switch result {
case .success(): case .success():
self.refreshArticles(account) {
self.sendArticleStatus(for: account) {
self.refreshArticleStatus(for: account) { self.refreshArticleStatus(for: account) {
self.refreshMissingArticles(account) { self.refreshArticles(account) {
self.refreshProgress.clear() self.refreshMissingArticles(account) {
DispatchQueue.main.async { self.refreshProgress.clear()
completion(.success(())) DispatchQueue.main.async {
completion(.success(()))
}
} }
} }
} }
@ -756,8 +759,8 @@ private extension ReaderAPIAccountDelegate {
case .success(let (entries, page)): case .success(let (entries, page)):
self.processEntries(account: account, entries: entries) { self.processEntries(account: account, entries: entries) {
self.refreshArticles(account, page: page) { self.refreshArticleStatus(for: account) {
self.refreshArticleStatus(for: account) { self.refreshArticles(account, page: page) {
self.refreshMissingArticles(account) { self.refreshMissingArticles(account) {
DispatchQueue.main.async { DispatchQueue.main.async {
completion(.success(feed)) completion(.success(feed))
@ -812,24 +815,23 @@ private extension ReaderAPIAccountDelegate {
os_log(.debug, log: log, "Refreshing missing articles...") os_log(.debug, log: log, "Refreshing missing articles...")
let group = DispatchGroup() let group = DispatchGroup()
account.fetchArticleIDsForStatusesWithoutArticles { (fetchedArticleIDs) in let fetchedArticleIDs = account.fetchArticleIDsForStatusesWithoutArticles()
let articleIDs = Array(fetchedArticleIDs) let articleIDs = Array(fetchedArticleIDs)
let chunkedArticleIDs = articleIDs.chunked(into: 100) let chunkedArticleIDs = articleIDs.chunked(into: 100)
for chunk in chunkedArticleIDs { for chunk in chunkedArticleIDs {
group.enter() group.enter()
self.caller.retrieveEntries(articleIDs: chunk) { result in self.caller.retrieveEntries(articleIDs: chunk) { result in
switch result { switch result {
case .success(let entries): case .success(let entries):
self.processEntries(account: account, entries: entries) { self.processEntries(account: account, entries: entries) {
group.leave()
}
case .failure(let error):
os_log(.error, log: self.log, "Refresh missing articles failed: %@.", error.localizedDescription)
group.leave() group.leave()
} }
case .failure(let error):
os_log(.error, log: self.log, "Refresh missing articles failed: %@.", error.localizedDescription)
group.leave()
} }
} }
} }
@ -918,64 +920,62 @@ private extension ReaderAPIAccountDelegate {
return return
} }
let unreadArticleIDs = Set(articleIDs.map { String($0) } ) let feedbinUnreadArticleIDs = Set(articleIDs.map { String($0) } )
account.fetchUnreadArticleIDs { (currentUnreadArticleIDs) in let currentUnreadArticleIDs = account.fetchUnreadArticleIDs()
// Mark articles as unread
let deltaUnreadArticleIDs = unreadArticleIDs.subtracting(currentUnreadArticleIDs)
account.fetchArticlesAsync(.articleIDs(deltaUnreadArticleIDs)) { (markUnreadArticles) in
account.update(markUnreadArticles, statusKey: .read, flag: false)
// Save any unread statuses for articles we haven't yet received // Mark articles as unread
let markUnreadArticleIDs = Set(markUnreadArticles.map { $0.articleID }) let deltaUnreadArticleIDs = feedbinUnreadArticleIDs.subtracting(currentUnreadArticleIDs)
let missingUnreadArticleIDs = deltaUnreadArticleIDs.subtracting(markUnreadArticleIDs) let markUnreadArticles = account.fetchArticles(.articleIDs(deltaUnreadArticleIDs))
account.ensureStatuses(missingUnreadArticleIDs, .read, false) account.update(markUnreadArticles, statusKey: .read, flag: false)
}
// Mark articles as read // Save any unread statuses for articles we haven't yet received
let deltaReadArticleIDs = currentUnreadArticleIDs.subtracting(unreadArticleIDs) let markUnreadArticleIDs = Set(markUnreadArticles.map { $0.articleID })
account.fetchArticlesAsync(.articleIDs(deltaReadArticleIDs)) { (markReadArticles) in let missingUnreadArticleIDs = deltaUnreadArticleIDs.subtracting(markUnreadArticleIDs)
account.update(markReadArticles, statusKey: .read, flag: true) account.ensureStatuses(missingUnreadArticleIDs, true, .read, false)
// Mark articles as read
let deltaReadArticleIDs = currentUnreadArticleIDs.subtracting(feedbinUnreadArticleIDs)
let markReadArticles = account.fetchArticles(.articleIDs(deltaReadArticleIDs))
account.update(markReadArticles, statusKey: .read, flag: true)
// Save any read statuses for articles we haven't yet received
let markReadArticleIDs = Set(markReadArticles.map { $0.articleID })
let missingReadArticleIDs = deltaReadArticleIDs.subtracting(markReadArticleIDs)
account.ensureStatuses(missingReadArticleIDs, true, .read, true)
// Save any read statuses for articles we haven't yet received
let markReadArticleIDs = Set(markReadArticles.map { $0.articleID })
let missingReadArticleIDs = deltaReadArticleIDs.subtracting(markReadArticleIDs)
account.ensureStatuses(missingReadArticleIDs, .read, true)
}
}
} }
func syncArticleStarredState(account: Account, articleIDs: [Int]?) { func syncArticleStarredState(account: Account, articleIDs: [Int]?) {
guard let articleIDs = articleIDs else { guard let articleIDs = articleIDs else {
return return
} }
let starredArticleIDs = Set(articleIDs.map { String($0) } ) let feedbinStarredArticleIDs = Set(articleIDs.map { String($0) } )
account.fetchStarredArticleIDs { (currentStarredArticleIDs) in let currentStarredArticleIDs = account.fetchStarredArticleIDs()
// Mark articles as starred
let deltaStarredArticleIDs = starredArticleIDs.subtracting(currentStarredArticleIDs)
account.fetchArticlesAsync(.articleIDs(deltaStarredArticleIDs)) { (markStarredArticles) in
account.update(markStarredArticles, statusKey: .starred, flag: true)
// Save any starred statuses for articles we haven't yet received // Mark articles as starred
let markStarredArticleIDs = Set(markStarredArticles.map { $0.articleID }) let deltaStarredArticleIDs = feedbinStarredArticleIDs.subtracting(currentStarredArticleIDs)
let missingStarredArticleIDs = deltaStarredArticleIDs.subtracting(markStarredArticleIDs) let markStarredArticles = account.fetchArticles(.articleIDs(deltaStarredArticleIDs))
account.ensureStatuses(missingStarredArticleIDs, .starred, true) account.update(markStarredArticles, statusKey: .starred, flag: true)
}
// Mark articles as unstarred // Save any starred statuses for articles we haven't yet received
let deltaUnstarredArticleIDs = currentStarredArticleIDs.subtracting(starredArticleIDs) let markStarredArticleIDs = Set(markStarredArticles.map { $0.articleID })
account.fetchArticlesAsync(.articleIDs(deltaUnstarredArticleIDs)) { (markUnstarredArticles) in let missingStarredArticleIDs = deltaStarredArticleIDs.subtracting(markStarredArticleIDs)
account.update(markUnstarredArticles, statusKey: .starred, flag: false) account.ensureStatuses(missingStarredArticleIDs, true, .starred, true)
// Save any unstarred statuses for articles we haven't yet received // Mark articles as unstarred
let markUnstarredArticleIDs = Set(markUnstarredArticles.map { $0.articleID }) let deltaUnstarredArticleIDs = currentStarredArticleIDs.subtracting(feedbinStarredArticleIDs)
let missingUnstarredArticleIDs = deltaUnstarredArticleIDs.subtracting(markUnstarredArticleIDs) let markUnstarredArticles = account.fetchArticles(.articleIDs(deltaUnstarredArticleIDs))
account.ensureStatuses(missingUnstarredArticleIDs, .starred, false) account.update(markUnstarredArticles, statusKey: .starred, flag: false)
}
} // Save any unstarred statuses for articles we haven't yet received
let markUnstarredArticleIDs = Set(markUnstarredArticles.map { $0.articleID })
let missingUnstarredArticleIDs = deltaUnstarredArticleIDs.subtracting(markUnstarredArticleIDs)
account.ensureStatuses(missingUnstarredArticleIDs, true, .starred, false)
} }
func deleteTagging(for account: Account, with feed: Feed, from container: Container?, completion: @escaping (Result<Void, Error>) -> Void) { func deleteTagging(for account: Account, with feed: Feed, from container: Container?, completion: @escaping (Result<Void, Error>) -> Void) {
if let folder = container as? Folder, let feedName = feed.subscriptionID { if let folder = container as? Folder, let feedName = feed.subscriptionID {