2017-07-03 19:40:48 +02:00
|
|
|
//
|
|
|
|
// Database.swift
|
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 7/20/15.
|
|
|
|
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSCore
|
|
|
|
import RSDatabase
|
2017-07-03 20:20:14 +02:00
|
|
|
import RSParser
|
2017-07-03 19:40:48 +02:00
|
|
|
import Data
|
|
|
|
|
2017-07-04 00:04:31 +02:00
|
|
|
private let sqlLogging = false
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-07-04 00:04:31 +02:00
|
|
|
private func logSQL(_ sql: String) {
|
2017-07-03 19:40:48 +02:00
|
|
|
if sqlLogging {
|
|
|
|
print("SQL: \(sql)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
typealias ArticleResultBlock = (Set<Article>) -> Void
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-07-04 00:04:31 +02:00
|
|
|
final class Database {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-08-21 00:56:58 +02:00
|
|
|
private let queue: RSDatabaseQueue
|
2017-07-03 19:40:48 +02:00
|
|
|
private let databaseFile: String
|
2017-08-04 06:10:01 +02:00
|
|
|
private let articlesTable: ArticlesTable
|
|
|
|
private let statusesTable: StatusesTable
|
2017-08-21 02:46:15 +02:00
|
|
|
private let authorsLookupTable: DatabaseLookupTable
|
|
|
|
private let attachmentsLookupTable: DatabaseLookupTable
|
2017-08-21 00:56:58 +02:00
|
|
|
private let tagsLookupTable: DatabaseLookupTable
|
|
|
|
private var articleArrivalCutoffDate = NSDate.rs_dateWithNumberOfDays(inThePast: 3 * 31)!
|
|
|
|
private let minimumNumberOfArticles = 10
|
|
|
|
private weak var delegate: AccountDelegate?
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
init(databaseFile: String, delegate: AccountDelegate) {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
self.delegate = delegate
|
2017-07-03 19:40:48 +02:00
|
|
|
self.databaseFile = databaseFile
|
|
|
|
self.queue = RSDatabaseQueue(filepath: databaseFile, excludeFromBackup: false)
|
2017-08-04 06:10:01 +02:00
|
|
|
|
|
|
|
self.articlesTable = ArticlesTable(name: DatabaseTableName.articles, queue: queue)
|
2017-08-21 00:56:58 +02:00
|
|
|
self.statusesTable = StatusesTable(name: DatabaseTableName.statuses)
|
|
|
|
|
2017-08-21 02:46:15 +02:00
|
|
|
let authorsTable = AuthorsTable(name: DatabaseTableName.authors)
|
2017-08-21 00:56:58 +02:00
|
|
|
self.authorsLookupTable = DatabaseLookupTable(name: DatabaseTableName.authorsLookup, objectIDKey: DatabaseKey.articleID, relatedObjectIDKey: DatabaseKey.authorID, relatedTable: authorsTable, relationshipName: RelationshipName.authors)
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-08-21 00:56:58 +02:00
|
|
|
let tagsTable = TagsTable(name: DatabaseTableName.tags)
|
|
|
|
self.tagsLookupTable = DatabaseLookupTable(name: DatabaseTableName.tags, objectIDKey: DatabaseKey.articleID, relatedObjectIDKey: DatabaseKey.tagName, relatedTable: tagsTable, relationshipName: RelationshipName.tags)
|
|
|
|
|
2017-08-21 02:46:15 +02:00
|
|
|
let attachmentsTable = AttachmentsTable(name: DatabaseTableName.attachments)
|
|
|
|
self.attachmentsLookupTable = DatabaseLookupTable(name: DatabaseTableName.tags, objectIDKey: DatabaseKey.articleID, relatedObjectIDKey: DatabaseKey.tagName, relatedTable: tagsTable, relationshipName: RelationshipName.tags)
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
let createStatementsPath = Bundle(for: type(of: self)).path(forResource: "CreateStatements", ofType: "sql")!
|
2017-07-03 19:40:48 +02:00
|
|
|
let createStatements = try! NSString(contentsOfFile: createStatementsPath, encoding: String.Encoding.utf8.rawValue)
|
|
|
|
queue.createTables(usingStatements: createStatements as String)
|
|
|
|
queue.vacuumIfNeeded()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Fetching Articles
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func fetchArticlesForFeed(_ feed: Feed) -> Set<Article> {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
var fetchedArticles = Set<Article>()
|
2017-07-03 19:40:48 +02:00
|
|
|
let feedID = feed.feedID
|
|
|
|
|
|
|
|
queue.fetchSync { (database: FMDatabase!) -> Void in
|
|
|
|
|
|
|
|
fetchedArticles = self.fetchArticlesForFeedID(feedID, database: database)
|
|
|
|
}
|
|
|
|
|
2017-08-06 21:37:47 +02:00
|
|
|
let articles = articleCache.uniquedArticles(fetchedArticles, statusesTable: statusesTable)
|
2017-07-03 19:40:48 +02:00
|
|
|
return filteredArticles(articles, feedCounts: [feed.feedID: fetchedArticles.count])
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func fetchArticlesForFeedAsync(_ feed: Feed, _ resultBlock: @escaping ArticleResultBlock) {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
let feedID = feed.feedID
|
|
|
|
|
|
|
|
queue.fetch { (database: FMDatabase!) -> Void in
|
|
|
|
|
|
|
|
let fetchedArticles = self.fetchArticlesForFeedID(feedID, database: database)
|
|
|
|
|
|
|
|
DispatchQueue.main.async() { () -> Void in
|
|
|
|
|
2017-08-06 21:37:47 +02:00
|
|
|
let articles = self.articleCache.uniquedArticles(fetchedArticles, statusesTable: self.statusesTable)
|
2017-07-03 19:40:48 +02:00
|
|
|
let filteredArticles = self.filteredArticles(articles, feedCounts: [feed.feedID: fetchedArticles.count])
|
|
|
|
resultBlock(filteredArticles)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func feedIDCountDictionariesWithResultSet(_ resultSet: FMResultSet) -> [String: Int] {
|
|
|
|
|
|
|
|
var counts = [String: Int]()
|
|
|
|
|
|
|
|
while (resultSet.next()) {
|
|
|
|
|
|
|
|
if let oneFeedID = resultSet.string(forColumnIndex: 0) {
|
|
|
|
let count = resultSet.int(forColumnIndex: 1)
|
|
|
|
counts[oneFeedID] = Int(count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return counts
|
|
|
|
}
|
|
|
|
|
|
|
|
func countsForAllFeeds(_ database: FMDatabase) -> [String: Int] {
|
|
|
|
|
|
|
|
let sql = "select distinct feedID, count(*) as count from articles group by feedID;"
|
|
|
|
|
|
|
|
if let resultSet = database.executeQuery(sql, withArgumentsIn: []) {
|
|
|
|
return feedIDCountDictionariesWithResultSet(resultSet)
|
|
|
|
}
|
|
|
|
|
|
|
|
return [String: Int]()
|
|
|
|
}
|
|
|
|
|
|
|
|
func countsForFeedIDs(_ feedIDs: [String], _ database: FMDatabase) -> [String: Int] {
|
|
|
|
|
|
|
|
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(feedIDs.count))!
|
|
|
|
let sql = "select distinct feedID, count(*) from articles where feedID in \(placeholders) group by feedID;"
|
|
|
|
logSQL(sql)
|
|
|
|
|
|
|
|
if let resultSet = database.executeQuery(sql, withArgumentsIn: feedIDs) {
|
|
|
|
return feedIDCountDictionariesWithResultSet(resultSet)
|
|
|
|
}
|
|
|
|
|
|
|
|
return [String: Int]()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func fetchUnreadArticlesForFolder(_ folder: Folder) -> Set<Article> {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-07-04 00:04:31 +02:00
|
|
|
return fetchUnreadArticlesForFeedIDs(folder.flattenedFeedIDs())
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func fetchUnreadArticlesForFeedIDs(_ feedIDs: [String]) -> Set<Article> {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
if feedIDs.isEmpty {
|
2017-07-04 00:04:31 +02:00
|
|
|
return Set<Article>()
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
var fetchedArticles = Set<Article>()
|
2017-07-03 19:40:48 +02:00
|
|
|
var counts = [String: Int]()
|
|
|
|
|
|
|
|
queue.fetchSync { (database: FMDatabase!) -> Void in
|
|
|
|
|
|
|
|
counts = self.countsForFeedIDs(feedIDs, database)
|
|
|
|
|
|
|
|
// select * from articles natural join statuses where feedID in ('http://ranchero.com/xml/rss.xml') and read = 0
|
|
|
|
|
|
|
|
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(feedIDs.count))!
|
|
|
|
let sql = "select * from articles natural join statuses where feedID in \(placeholders) and read=0;"
|
|
|
|
logSQL(sql)
|
|
|
|
|
|
|
|
if let resultSet = database.executeQuery(sql, withArgumentsIn: feedIDs) {
|
|
|
|
fetchedArticles = self.articlesWithResultSet(resultSet)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-06 21:37:47 +02:00
|
|
|
let articles = articleCache.uniquedArticles(fetchedArticles, statusesTable: statusesTable)
|
2017-07-03 19:40:48 +02:00
|
|
|
return filteredArticles(articles, feedCounts: counts)
|
|
|
|
}
|
|
|
|
|
|
|
|
typealias UnreadCountCompletionBlock = ([String: Int]) -> Void //feedID: unreadCount
|
|
|
|
|
|
|
|
func updateUnreadCounts(for feedIDs: Set<String>, completion: @escaping UnreadCountCompletionBlock) {
|
|
|
|
|
|
|
|
queue.fetch { (database: FMDatabase!) -> Void in
|
|
|
|
|
|
|
|
var unreadCounts = [String: Int]()
|
|
|
|
for oneFeedID in feedIDs {
|
|
|
|
unreadCounts[oneFeedID] = self.unreadCount(oneFeedID, database)
|
|
|
|
}
|
|
|
|
|
|
|
|
DispatchQueue.main.async() {
|
|
|
|
completion(unreadCounts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: Updating Articles
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func updateFeedWithParsedFeed(_ feed: Feed, parsedFeed: ParsedFeed, completionHandler: @escaping RSVoidCompletionBlock) {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
if parsedFeed.items.isEmpty {
|
|
|
|
completionHandler()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
let parsedArticlesDictionary = self.articlesDictionary(parsedFeed.items as NSSet) as! [String: ParsedItem]
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
fetchArticlesForFeedAsync(feed) { (articles) -> Void in
|
|
|
|
|
2017-07-04 00:04:31 +02:00
|
|
|
let articlesDictionary = self.articlesDictionary(articles as NSSet) as! [String: Article]
|
2017-07-03 19:40:48 +02:00
|
|
|
self.updateArticles(articlesDictionary, parsedArticles: parsedArticlesDictionary, feed: feed, completionHandler: completionHandler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Status
|
|
|
|
|
|
|
|
func markArticles(_ articles: NSSet, statusKey: ArticleStatusKey, flag: Bool) {
|
|
|
|
|
2017-08-06 21:37:47 +02:00
|
|
|
statusesTable.markArticles(articles as! Set<Article>, statusKey: statusKey, flag: flag)
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
2017-07-04 00:04:31 +02:00
|
|
|
private extension Database {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
// MARK: Saving Articles
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func saveUpdatedAndNewArticles(_ articleChanges: Set<NSDictionary>, newArticles: Set<Article>) {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
if articleChanges.isEmpty && newArticles.isEmpty {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-06 21:37:47 +02:00
|
|
|
statusesTable.assertNoMissingStatuses(newArticles)
|
2017-07-03 19:40:48 +02:00
|
|
|
articleCache.cacheArticles(newArticles)
|
|
|
|
|
|
|
|
let newArticleDictionaries = newArticles.map { (oneArticle) in
|
2017-07-04 00:04:31 +02:00
|
|
|
return oneArticle.databaseDictionary()
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
queue.update { (database: FMDatabase!) -> Void in
|
|
|
|
|
|
|
|
if !articleChanges.isEmpty {
|
|
|
|
|
|
|
|
for oneDictionary in articleChanges {
|
|
|
|
|
|
|
|
let oneArticleDictionary = oneDictionary.mutableCopy() as! NSMutableDictionary
|
2017-07-04 00:04:31 +02:00
|
|
|
let articleID = oneArticleDictionary[DatabaseKey.articleID]!
|
2017-07-12 22:25:10 +02:00
|
|
|
oneArticleDictionary.removeObject(forKey: DatabaseKey.articleID)
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-07-12 22:25:10 +02:00
|
|
|
let _ = database.rs_updateRows(with: oneArticleDictionary as [NSObject: AnyObject], whereKey: DatabaseKey.articleID, equalsValue: articleID, tableName: DatabaseTableName.articles)
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if !newArticleDictionaries.isEmpty {
|
|
|
|
|
|
|
|
for oneNewArticleDictionary in newArticleDictionaries {
|
2017-07-12 22:25:10 +02:00
|
|
|
let _ = database.rs_insertRow(with: oneNewArticleDictionary as [NSObject: AnyObject], insertType: RSDatabaseInsertOrReplace, tableName: DatabaseTableName.articles)
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Updating Articles
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func updateArticles(_ articles: [String: Article], parsedArticles: [String: ParsedItem], feed: Feed, completionHandler: @escaping RSVoidCompletionBlock) {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-08-06 21:37:47 +02:00
|
|
|
statusesTable.ensureStatusesForParsedArticles(Set(parsedArticles.values)) {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
let articleChanges = self.updateExistingArticles(articles, parsedArticles)
|
|
|
|
let newArticles = self.createNewArticles(articles, parsedArticles: parsedArticles, feedID: feed.feedID)
|
|
|
|
|
|
|
|
self.saveUpdatedAndNewArticles(articleChanges, newArticles: newArticles)
|
|
|
|
|
|
|
|
completionHandler()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func articlesDictionary(_ articles: NSSet) -> [String: AnyObject] {
|
|
|
|
|
|
|
|
var d = [String: AnyObject]()
|
|
|
|
for oneArticle in articles {
|
2017-07-12 22:25:10 +02:00
|
|
|
let oneArticleID = (oneArticle as AnyObject).value(forKey: DatabaseKey.articleID) as! String
|
2017-07-03 19:40:48 +02:00
|
|
|
d[oneArticleID] = oneArticle as AnyObject
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func updateExistingArticles(_ articles: [String: Article], _ parsedArticles: [String: ParsedItem]) -> Set<NSDictionary> {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
var articleChanges = Set<NSDictionary>()
|
|
|
|
|
|
|
|
for oneArticle in articles.values {
|
|
|
|
if let oneParsedArticle = parsedArticles[oneArticle.articleID] {
|
|
|
|
if let oneArticleChanges = oneArticle.updateWithParsedArticle(oneParsedArticle) {
|
|
|
|
articleChanges.insert(oneArticleChanges)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return articleChanges
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Creating Articles
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func createNewArticlesWithParsedArticles(_ parsedArticles: Set<ParsedItem>, feedID: String) -> Set<Article> {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-07-04 00:04:31 +02:00
|
|
|
return Set(parsedArticles.map { Article(account: account, feedID: feedID, parsedArticle: $0) })
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func articlesWithParsedArticles(_ parsedArticles: Set<ParsedItem>, feedID: String) -> Set<Article> {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
var localArticles = Set<Article>()
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
for oneParsedArticle in parsedArticles {
|
2017-07-04 00:04:31 +02:00
|
|
|
let oneLocalArticle = Article(account: self.account, feedID: feedID, parsedArticle: oneParsedArticle)
|
2017-07-03 19:40:48 +02:00
|
|
|
localArticles.insert(oneLocalArticle)
|
|
|
|
}
|
|
|
|
|
|
|
|
return localArticles
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func createNewArticles(_ existingArticles: [String: Article], parsedArticles: [String: ParsedItem], feedID: String) -> Set<Article> {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
let newParsedArticles = parsedArticlesMinusExistingArticles(parsedArticles, existingArticles: existingArticles)
|
|
|
|
let newArticles = createNewArticlesWithParsedArticles(newParsedArticles, feedID: feedID)
|
|
|
|
|
2017-08-06 21:37:47 +02:00
|
|
|
statusesTable.attachCachedUniqueStatuses(newArticles)
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
return newArticles
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func parsedArticlesMinusExistingArticles(_ parsedArticles: [String: ParsedItem], existingArticles: [String: Article]) -> Set<ParsedItem> {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
var result = Set<ParsedItem>()
|
|
|
|
|
|
|
|
for oneParsedArticle in parsedArticles.values {
|
|
|
|
|
|
|
|
if let _ = existingArticles[oneParsedArticle.databaseID] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result.insert(oneParsedArticle)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Fetching Articles
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func fetchArticlesWithWhereClause(_ database: FMDatabase, whereClause: String, parameters: [AnyObject]?) -> Set<Article> {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-08-04 06:10:01 +02:00
|
|
|
let sql = "select * from articles where \(whereClause);"
|
2017-07-03 19:40:48 +02:00
|
|
|
logSQL(sql)
|
|
|
|
|
|
|
|
if let resultSet = database.executeQuery(sql, withArgumentsIn: parameters) {
|
2017-08-06 21:37:47 +02:00
|
|
|
return articlesWithResultSet(resultSet, database)
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
|
2017-07-04 00:04:31 +02:00
|
|
|
return Set<Article>()
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
|
2017-08-06 21:37:47 +02:00
|
|
|
func articlesWithResultSet(_ resultSet: FMResultSet, _ database: FMDatabase) -> Set<Article> {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-08-06 21:37:47 +02:00
|
|
|
let fetchedArticles = resultSet.mapToSet { Article(account: self.account, row: $0) }
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-08-04 06:10:01 +02:00
|
|
|
statusesTable.attachStatuses(fetchedArticles, database)
|
|
|
|
authorsTable.attachAuthors(fetchedArticles, database)
|
|
|
|
tagsTable.attachTags(fetchedArticles, database)
|
|
|
|
attachmentsTable.attachAttachments(fetchedArticles, database)
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
return fetchedArticles
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func fetchArticlesForFeedID(_ feedID: String, database: FMDatabase) -> Set<Article> {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
return fetchArticlesWithWhereClause(database, whereClause: "articles.feedID = ?", parameters: [feedID as AnyObject])
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Unread counts
|
|
|
|
|
|
|
|
func numberOfArticles(_ feedID: String, _ database: FMDatabase) -> Int {
|
|
|
|
|
|
|
|
let sql = "select count(*) from articles where feedID = ?;"
|
|
|
|
logSQL(sql)
|
|
|
|
|
|
|
|
return numberWithSQLAndParameters(sql, parameters: [feedID], database)
|
|
|
|
}
|
|
|
|
|
|
|
|
func unreadCount(_ feedID: String, _ database: FMDatabase) -> Int {
|
|
|
|
|
|
|
|
let totalNumberOfArticles = numberOfArticles(feedID, database)
|
|
|
|
|
|
|
|
if totalNumberOfArticles <= minimumNumberOfArticles {
|
|
|
|
return unreadCountIgnoringCutoffDate(feedID, database)
|
|
|
|
}
|
|
|
|
return unreadCountRespectingCutoffDate(feedID, database)
|
|
|
|
}
|
|
|
|
|
|
|
|
func unreadCountIgnoringCutoffDate(_ feedID: String, _ database: FMDatabase) -> Int {
|
|
|
|
|
|
|
|
let sql = "select count(*) from articles natural join statuses where feedID=? and read=0 and userDeleted=0;"
|
|
|
|
logSQL(sql)
|
|
|
|
|
|
|
|
return numberWithSQLAndParameters(sql, parameters: [feedID], database)
|
|
|
|
}
|
|
|
|
|
|
|
|
func unreadCountRespectingCutoffDate(_ feedID: String, _ database: FMDatabase) -> Int {
|
|
|
|
|
|
|
|
let sql = "select count(*) from articles natural join statuses where feedID=? and read=0 and userDeleted=0 and (starred=1 or dateArrived>?);"
|
|
|
|
logSQL(sql)
|
|
|
|
|
|
|
|
return numberWithSQLAndParameters(sql, parameters: [feedID, articleArrivalCutoffDate], database)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Filtering out old articles
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func articleIsOlderThanCutoffDate(_ article: Article) -> Bool {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
if let dateArrived = article.status?.dateArrived {
|
|
|
|
return dateArrived < articleArrivalCutoffDate
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func articleShouldBeSavedForever(_ article: Article) -> Bool {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
return article.status.starred
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func articleShouldAppearToUser(_ article: Article, _ numberOfArticlesInFeed: Int) -> Bool {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
if numberOfArticlesInFeed <= minimumNumberOfArticles {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return articleShouldBeSavedForever(article) || !articleIsOlderThanCutoffDate(article)
|
|
|
|
}
|
|
|
|
|
|
|
|
private static let minimumNumberOfArticlesInFeed = 10
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func filteredArticles(_ articles: Set<Article>, feedCounts: [String: Int]) -> Set<Article> {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
var articlesSet = Set<Article>()
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
for oneArticle in articles {
|
|
|
|
if let feedCount = feedCounts[oneArticle.feedID], articleShouldAppearToUser(oneArticle, feedCount) {
|
|
|
|
articlesSet.insert(oneArticle)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return articlesSet
|
|
|
|
}
|
|
|
|
|
2017-07-29 21:29:05 +02:00
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func feedIDsFromArticles(_ articles: Set<Article>) -> Set<String> {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
return Set(articles.map { $0.feedID })
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func deletePossibleOldArticles(_ articles: Set<Article>) {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
let feedIDs = feedIDsFromArticles(articles)
|
|
|
|
if feedIDs.isEmpty {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|