From 948d366a4e10933f120f1896fc137ceb2b65726a Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Tue, 17 Dec 2019 13:28:48 -0800 Subject: [PATCH] =?UTF-8?q?Created=20fetchArticleIDsForStatusesWithoutArti?= =?UTF-8?q?clesNewerThanCutoffDate=20=E2=80=94=C2=A0this=20gets=20articleI?= =?UTF-8?q?Ds=20of=20articles=20we=20care=20about.=20They=E2=80=99re=20not?= =?UTF-8?q?=20deleted.=20They=E2=80=99re=20either=20(starred)=20or=20(unre?= =?UTF-8?q?ad=20and=20newer=20than=20cutoffDate).=20Also:=20it=E2=80=99s?= =?UTF-8?q?=20async.=20Use=20this=20instead=20of=20fetchArticleIDsForStatu?= =?UTF-8?q?sesWithoutArticles,=20which=20is=20deprecated.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ArticlesDatabase/ArticlesDatabase.swift | 8 ++++- .../ArticlesDatabase/ArticlesTable.swift | 6 +++- .../ArticlesDatabase/StatusesTable.swift | 33 +++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/Frameworks/ArticlesDatabase/ArticlesDatabase.swift b/Frameworks/ArticlesDatabase/ArticlesDatabase.swift index 3fca7e321..93bfda0f5 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 288aca756..c6637fc71 100644 --- a/Frameworks/ArticlesDatabase/ArticlesTable.swift +++ b/Frameworks/ArticlesDatabase/ArticlesTable.swift @@ -387,7 +387,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?