diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index 99011abf2..ab8c2f463 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -791,6 +791,24 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, return updatedArticles } + /// Make sure statuses exist. Any existing statuses won’t be touched. + /// All created statuses will be marked as read and not starred. + /// Sends a .StatusesDidChange notification. + func createStatusesIfNeeded(articleIDs: Set, completion: DatabaseCompletionBlock? = nil) { + guard !articleIDs.isEmpty else { + completion?(nil) + return + } + database.createStatusesIfNeeded(articleIDs: articleIDs) { error in + if let error = error { + completion?(error) + return + } + self.noteStatusesForArticleIDsDidChange(articleIDs) + completion?(nil) + } + } + /// Mark articleIDs statuses based on statusKey and flag. /// Will create statuses in the database and in memory as needed. Sends a .StatusesDidChange notification. func mark(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool, completion: DatabaseCompletionBlock? = nil) { diff --git a/Frameworks/ArticlesDatabase/ArticlesDatabase.swift b/Frameworks/ArticlesDatabase/ArticlesDatabase.swift index de2e9e286..97121dd6d 100644 --- a/Frameworks/ArticlesDatabase/ArticlesDatabase.swift +++ b/Frameworks/ArticlesDatabase/ArticlesDatabase.swift @@ -189,6 +189,12 @@ public final class ArticlesDatabase { articlesTable.mark(articleIDs, statusKey, flag, completion) } + /// Create statuses for specified articleIDs. For existing statuses, don’t do anything. + /// For newly-created statuses, mark them as read and not-starred. + public func createStatusesIfNeeded(articleIDs: Set, completion: @escaping DatabaseCompletionBlock) { + articlesTable.createStatusesIfNeeded(articleIDs, completion) + } + // MARK: - Suspend and Resume (for iOS) /// Close the database and stop running database calls. diff --git a/Frameworks/ArticlesDatabase/ArticlesTable.swift b/Frameworks/ArticlesDatabase/ArticlesTable.swift index b921ac4b8..a537ab9ee 100644 --- a/Frameworks/ArticlesDatabase/ArticlesTable.swift +++ b/Frameworks/ArticlesDatabase/ArticlesTable.swift @@ -421,6 +421,22 @@ final class ArticlesTable: DatabaseTable { } } + func createStatusesIfNeeded(_ articleIDs: Set, _ completion: @escaping DatabaseCompletionBlock) { + queue.runInTransaction { databaseResult in + switch databaseResult { + case .success(let database): + let _ = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, true, database) + DispatchQueue.main.async { + completion(nil) + } + case .failure(let databaseError): + DispatchQueue.main.async { + completion(databaseError) + } + } + } + } + // MARK: - Indexing func indexUnindexedArticles() {