diff --git a/Frameworks/ArticlesDatabase/ArticlesDatabase.swift b/Frameworks/ArticlesDatabase/ArticlesDatabase.swift index 6f079f450..f4b597594 100644 --- a/Frameworks/ArticlesDatabase/ArticlesDatabase.swift +++ b/Frameworks/ArticlesDatabase/ArticlesDatabase.swift @@ -176,10 +176,16 @@ public final class ArticlesDatabase { articlesTable.fetchStarredArticleIDsAsync(webFeedIDs, completion) } + /// Deprecated. Use `fetchArticleIDsForStatusesWithoutArticlesNewerThanCutoffDate` instead. public func fetchArticleIDsForStatusesWithoutArticles() throws -> Set { return try articlesTable.fetchArticleIDsForStatusesWithoutArticles() } - + + /// Fetch articleIDs for articles that we should have, but don’t. These articles are not userDeleted, and they are either (starred) or (unread and newer than the article cutoff date). + func fetchArticleIDsForStatusesWithoutArticlesNewerThanCutoffDate(_ completion: @escaping ArticleIDsCompletionBlock) { + articlesTable.fetchArticleIDsForStatusesWithoutArticlesNewerThanCutoffDate(completion) + } + public func mark(_ articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) throws -> Set? { return try articlesTable.mark(articles, statusKey, flag) } diff --git a/Frameworks/ArticlesDatabase/ArticlesTable.swift b/Frameworks/ArticlesDatabase/ArticlesTable.swift index 4036650d1..688a749f6 100644 --- a/Frameworks/ArticlesDatabase/ArticlesTable.swift +++ b/Frameworks/ArticlesDatabase/ArticlesTable.swift @@ -383,7 +383,11 @@ final class ArticlesTable: DatabaseTable { func fetchArticleIDsForStatusesWithoutArticles() throws -> Set { return try statusesTable.fetchArticleIDsForStatusesWithoutArticles() } - + + func fetchArticleIDsForStatusesWithoutArticlesNewerThanCutoffDate(_ completion: @escaping ArticleIDsCompletionBlock) { + statusesTable.fetchArticleIDsForStatusesWithoutArticlesNewerThan(articleCutoffDate, completion) + } + func mark(_ articles: Set
, _ statusKey: ArticleStatus.Key, _ flag: Bool) throws -> Set? { var statuses: Set? var error: DatabaseError? diff --git a/Frameworks/ArticlesDatabase/StatusesTable.swift b/Frameworks/ArticlesDatabase/StatusesTable.swift index 3bf1a109a..651387094 100644 --- a/Frameworks/ArticlesDatabase/StatusesTable.swift +++ b/Frameworks/ArticlesDatabase/StatusesTable.swift @@ -104,6 +104,39 @@ final class StatusesTable: DatabaseTable { func fetchArticleIDsForStatusesWithoutArticles() throws -> Set { return try fetchArticleIDs("select articleID from statuses s where (read=0 or starred=1) and userDeleted=0 and not exists (select 1 from articles a where a.articleID = s.articleID);") } + + func fetchArticleIDsForStatusesWithoutArticlesNewerThan(_ cutoffDate: Date, _ completion: @escaping ArticleIDsCompletionBlock) { + queue.runInDatabase { databaseResult in + + var error: DatabaseError? + var articleIDs = Set() + + func makeDatabaseCall(_ database: FMDatabase) { + let sql = "select articleID from statuses s where ((starred=1) || (read=0 and dateArrived > ?)) and userDeleted=0 and not exists (select 1 from articles a where a.articleID = s.articleID);" + if let resultSet = database.executeQuery(sql, withArgumentsIn: [cutoffDate]) { + articleIDs = resultSet.mapToSet(self.articleIDWithRow) + } + } + + switch databaseResult { + case .success(let database): + makeDatabaseCall(database) + case .failure(let databaseError): + error = databaseError + } + + if let error = error { + DispatchQueue.main.async { + completion(.failure(error)) + } + } + else { + DispatchQueue.main.async { + completion(.success(articleIDs)) + } + } + } + } func fetchArticleIDs(_ sql: String) throws -> Set { var error: DatabaseError?