2017-07-03 20:20:14 +02:00
//
2017-07-29 21:08:10 +02:00
// A r t i c l e s T a b l e . s w i f t
2018-08-29 07:18:24 +02:00
// N e t N e w s W i r e
2017-07-03 20:20:14 +02:00
//
// C r e a t e d b y B r e n t S i m m o n s o n 5 / 9 / 1 6 .
// C o p y r i g h t © 2 0 1 6 R a n c h e r o S o f t w a r e , L L C . A l l r i g h t s r e s e r v e d .
//
import Foundation
2017-08-23 22:23:12 +02:00
import RSCore
2017-07-29 21:29:05 +02:00
import RSDatabase
2017-08-23 22:23:12 +02:00
import RSParser
2018-07-24 03:29:08 +02:00
import Articles
2017-07-03 20:20:14 +02:00
2017-08-21 22:31:14 +02:00
final class ArticlesTable : DatabaseTable {
let name : String
2017-09-05 17:53:45 +02:00
private let accountID : String
2017-08-27 00:37:15 +02:00
private let queue : RSDatabaseQueue
2017-09-05 17:53:45 +02:00
private let statusesTable : StatusesTable
2017-08-23 22:23:12 +02:00
private let authorsLookupTable : DatabaseLookupTable
private let attachmentsLookupTable : DatabaseLookupTable
2017-09-05 03:29:02 +02:00
2017-08-31 22:35:48 +02:00
// TODO: u p d a t e a r t i c l e C u t o f f D a t e a s t i m e p a s s e s a n d b a s e d o n u s e r p r e f e r e n c e s .
private var articleCutoffDate = NSDate . rs_dateWithNumberOfDays ( inThePast : 3 * 31 ) !
2017-09-05 02:10:02 +02:00
private var maximumArticleCutoffDate = NSDate . rs_dateWithNumberOfDays ( inThePast : 4 * 31 ) !
2017-08-31 22:35:48 +02:00
2017-09-05 17:53:45 +02:00
init ( name : String , accountID : String , queue : RSDatabaseQueue ) {
2017-07-29 21:29:05 +02:00
2017-08-21 22:31:14 +02:00
self . name = name
2017-09-05 17:53:45 +02:00
self . accountID = accountID
2017-08-27 00:37:15 +02:00
self . queue = queue
2017-09-10 03:46:58 +02:00
self . statusesTable = StatusesTable ( queue : queue )
2017-09-05 17:53:45 +02:00
2017-08-23 22:23:12 +02:00
let authorsTable = AuthorsTable ( name : DatabaseTableName . authors )
self . authorsLookupTable = DatabaseLookupTable ( name : DatabaseTableName . authorsLookup , objectIDKey : DatabaseKey . articleID , relatedObjectIDKey : DatabaseKey . authorID , relatedTable : authorsTable , relationshipName : RelationshipName . authors )
let attachmentsTable = AttachmentsTable ( name : DatabaseTableName . attachments )
self . attachmentsLookupTable = DatabaseLookupTable ( name : DatabaseTableName . attachmentsLookup , objectIDKey : DatabaseKey . articleID , relatedObjectIDKey : DatabaseKey . attachmentID , relatedTable : attachmentsTable , relationshipName : RelationshipName . attachments )
2017-08-21 22:31:14 +02:00
}
2017-07-29 21:29:05 +02:00
2017-08-21 22:31:14 +02:00
// MARK: F e t c h i n g
2018-07-28 21:16:14 +02:00
func fetchArticles ( _ feedID : String ) -> Set < Article > {
2017-08-21 22:31:14 +02:00
2017-08-27 22:03:15 +02:00
var articles = Set < Article > ( )
2017-08-27 00:37:15 +02:00
2017-09-05 03:29:02 +02:00
queue . fetchSync { ( database ) in
2017-09-05 02:10:02 +02:00
articles = self . fetchArticlesForFeedID ( feedID , withLimits : true , database : database )
2017-08-27 00:37:15 +02:00
}
2017-09-05 03:29:02 +02:00
return articles
2017-08-21 22:31:14 +02:00
}
2018-07-28 21:16:14 +02:00
func fetchArticlesAsync ( _ feedID : String , withLimits : Bool , _ resultBlock : @ escaping ArticleResultBlock ) {
2017-08-27 00:37:15 +02:00
2017-09-05 03:29:02 +02:00
queue . fetch { ( database ) in
2017-08-27 00:37:15 +02:00
2017-09-05 03:29:02 +02:00
let articles = self . fetchArticlesForFeedID ( feedID , withLimits : withLimits , database : database )
2017-08-27 00:37:15 +02:00
DispatchQueue . main . async {
resultBlock ( articles )
}
}
2017-08-21 22:31:14 +02:00
}
2018-07-28 21:16:14 +02:00
func fetchUnreadArticles ( for feedIDs : Set < String > ) -> Set < Article > {
2017-08-27 22:03:15 +02:00
2018-07-28 21:16:14 +02:00
return fetchUnreadArticles ( feedIDs )
2017-08-21 22:31:14 +02:00
}
2017-09-01 22:31:27 +02:00
2018-07-28 21:16:14 +02:00
public func fetchTodayArticles ( for feedIDs : Set < String > ) -> Set < Article > {
2018-02-11 02:37:47 +01:00
2018-07-28 21:16:14 +02:00
return fetchTodayArticles ( feedIDs )
2018-02-11 02:37:47 +01:00
}
2018-07-28 21:16:14 +02:00
public func fetchStarredArticles ( for feedIDs : Set < String > ) -> Set < Article > {
2018-02-11 21:07:55 +01:00
2018-07-28 21:16:14 +02:00
return fetchStarredArticles ( feedIDs )
2018-02-11 21:07:55 +01:00
}
2019-02-19 07:29:43 +01:00
func fetchArticlesMatching ( _ searchString : String , _ resultBlock : @ escaping ArticleResultBlock ) {
queue . fetch { ( database ) in
let articles = self . fetchArticlesMatching ( searchString , database )
DispatchQueue . main . async {
resultBlock ( articles )
}
}
}
2017-08-23 22:23:12 +02:00
// MARK: U p d a t i n g
2018-07-28 21:16:14 +02:00
func update ( _ feedID : String , _ parsedFeed : ParsedFeed , _ completion : @ escaping UpdateArticlesWithFeedCompletionBlock ) {
2017-09-02 23:19:42 +02:00
if parsedFeed . items . isEmpty {
2017-09-06 22:33:04 +02:00
completion ( nil , nil )
2017-09-02 23:19:42 +02:00
return
}
2017-09-19 07:00:35 +02:00
// 1 . E n s u r e s t a t u s e s f o r a l l t h e i n c o m i n g a r t i c l e s .
// 2 . C r e a t e i n c o m i n g a r t i c l e s w i t h p a r s e d I t e m s .
2017-09-11 15:46:32 +02:00
// 3 . I g n o r e i n c o m i n g a r t i c l e s t h a t a r e u s e r D e l e t e d | | ( ! s t a r r e d a n d r e a l l y o l d )
// 4 . F e t c h a l l a r t i c l e s f o r t h e f e e d .
2017-09-06 22:33:04 +02:00
// 5 . C r e a t e a r r a y o f A r t i c l e s n o t i n d a t a b a s e a n d s a v e t h e m .
// 6 . C r e a t e a r r a y o f u p d a t e d A r t i c l e s a n d s a v e w h a t ’ s c h a n g e d .
// 7 . C a l l b a c k w i t h n e w a n d u p d a t e d A r t i c l e s .
2017-09-19 07:00:35 +02:00
let articleIDs = Set ( parsedFeed . items . map { $0 . articleID } )
2017-09-05 17:53:45 +02:00
2017-09-19 07:00:35 +02:00
self . queue . update { ( database ) in
2017-09-11 15:46:32 +02:00
2017-09-19 07:00:35 +02:00
let statusesDictionary = self . statusesTable . ensureStatusesForArticleIDs ( articleIDs , database ) // 1
assert ( statusesDictionary . count = = articleIDs . count )
let allIncomingArticles = Article . articlesWithParsedItems ( parsedFeed . items , self . accountID , feedID , statusesDictionary ) // 2
2017-09-11 15:46:32 +02:00
if allIncomingArticles . isEmpty {
self . callUpdateArticlesCompletionBlock ( nil , nil , completion )
2017-09-05 17:53:45 +02:00
return
}
2017-09-11 15:46:32 +02:00
2017-09-22 03:14:37 +02:00
let incomingArticles = self . filterIncomingArticles ( allIncomingArticles ) // 3
2017-09-19 07:00:35 +02:00
if incomingArticles . isEmpty {
self . callUpdateArticlesCompletionBlock ( nil , nil , completion )
return
2017-09-05 17:53:45 +02:00
}
2017-09-19 07:00:35 +02:00
let fetchedArticles = self . fetchArticlesForFeedID ( feedID , withLimits : false , database : database ) // 4
let fetchedArticlesDictionary = fetchedArticles . dictionary ( )
let newArticles = self . findAndSaveNewArticles ( incomingArticles , fetchedArticlesDictionary , database ) // 5
let updatedArticles = self . findAndSaveUpdatedArticles ( incomingArticles , fetchedArticlesDictionary , database ) // 6
self . callUpdateArticlesCompletionBlock ( newArticles , updatedArticles , completion ) // 7
2017-09-02 23:19:42 +02:00
}
2017-08-23 22:23:12 +02:00
}
2017-09-01 22:31:27 +02:00
2017-08-23 22:23:12 +02:00
// MARK: U n r e a d C o u n t s
2018-07-28 21:16:14 +02:00
func fetchUnreadCounts ( _ feedIDs : Set < String > , _ completion : @ escaping UnreadCountCompletionBlock ) {
2017-08-23 22:23:12 +02:00
2018-07-28 21:16:14 +02:00
if feedIDs . isEmpty {
2017-11-25 22:11:19 +01:00
completion ( UnreadCountDictionary ( ) )
return
}
2017-09-16 19:21:39 +02:00
var unreadCountDictionary = UnreadCountDictionary ( )
2017-09-01 22:31:27 +02:00
queue . fetch { ( database ) in
for feedID in feedIDs {
2017-09-16 19:21:39 +02:00
unreadCountDictionary [ feedID ] = self . fetchUnreadCount ( feedID , database )
2017-09-01 22:31:27 +02:00
}
DispatchQueue . main . async ( ) {
2017-09-16 19:21:39 +02:00
completion ( unreadCountDictionary )
2017-09-01 22:31:27 +02:00
}
}
2017-08-23 22:23:12 +02:00
}
2017-09-01 22:31:27 +02:00
2018-07-28 21:16:14 +02:00
func fetchUnreadCount ( _ feedIDs : Set < String > , _ since : Date , _ callback : @ escaping ( Int ) -> Void ) {
2017-11-19 21:44:17 +01:00
// G e t u n r e a d c o u n t f o r t o d a y , f o r i n s t a n c e .
2018-07-28 21:16:14 +02:00
if feedIDs . isEmpty {
2017-11-25 22:11:19 +01:00
callback ( 0 )
return
}
2017-11-19 21:44:17 +01:00
queue . fetch { ( database ) in
let placeholders = NSString . rs_SQLValueList ( withPlaceholders : UInt ( feedIDs . count ) ) !
2019-02-09 06:05:55 +01:00
let sql = " select count(*) from articles natural join statuses where feedID in \( placeholders ) and (datePublished > ? or (datePublished is null and dateArrived > ?)) and read=0 and userDeleted=0; "
2017-11-19 21:44:17 +01:00
var parameters = [ Any ] ( )
parameters += Array ( feedIDs ) as [ Any ]
parameters += [ since ] as [ Any ]
2019-02-09 06:05:55 +01:00
parameters += [ since ] as [ Any ]
2017-11-19 21:44:17 +01:00
let unreadCount = self . numberWithSQLAndParameters ( sql , parameters , in : database )
DispatchQueue . main . async ( ) {
callback ( unreadCount )
}
}
}
2017-12-03 20:57:53 +01:00
func fetchAllUnreadCounts ( _ completion : @ escaping UnreadCountCompletionBlock ) {
// R e t u r n s o n l y w h e r e u n r e a d C o u n t > 0 .
let cutoffDate = articleCutoffDate
queue . fetch { ( database ) in
let sql = " select distinct feedID, count(*) from articles natural join statuses where read=0 and userDeleted=0 and (starred=1 or dateArrived>?) group by feedID; "
guard let resultSet = database . executeQuery ( sql , withArgumentsIn : [ cutoffDate ] ) else {
DispatchQueue . main . async ( ) {
completion ( UnreadCountDictionary ( ) )
}
return
}
var d = UnreadCountDictionary ( )
while resultSet . next ( ) {
let unreadCount = resultSet . long ( forColumnIndex : 1 )
if let feedID = resultSet . string ( forColumnIndex : 0 ) {
d [ feedID ] = unreadCount
}
}
DispatchQueue . main . async ( ) {
completion ( d )
}
}
}
2018-07-28 21:16:14 +02:00
func fetchStarredAndUnreadCount ( _ feedIDs : Set < String > , _ callback : @ escaping ( Int ) -> Void ) {
2017-11-20 00:40:02 +01:00
2018-07-28 21:16:14 +02:00
if feedIDs . isEmpty {
2017-11-25 22:11:19 +01:00
callback ( 0 )
return
}
2017-11-20 00:40:02 +01:00
queue . fetch { ( database ) in
let placeholders = NSString . rs_SQLValueList ( withPlaceholders : UInt ( feedIDs . count ) ) !
let sql = " select count(*) from articles natural join statuses where feedID in \( placeholders ) and read=0 and starred=1 and userDeleted=0; "
let parameters = Array ( feedIDs ) as [ Any ]
let unreadCount = self . numberWithSQLAndParameters ( sql , parameters , in : database )
DispatchQueue . main . async ( ) {
callback ( unreadCount )
}
}
}
2017-08-23 22:23:12 +02:00
// MARK: S t a t u s
2017-10-09 06:06:25 +02:00
func mark ( _ articles : Set < Article > , _ statusKey : ArticleStatus . Key , _ flag : Bool ) -> Set < ArticleStatus > ? {
2017-09-16 20:04:29 +02:00
2017-10-09 06:06:25 +02:00
return statusesTable . mark ( articles . statuses ( ) , statusKey , flag )
2017-08-23 22:23:12 +02:00
}
2017-11-20 01:28:26 +01:00
func markEverywhereAsRead ( ) {
return statusesTable . markEverywhereAsRead ( )
}
2017-08-21 22:31:14 +02:00
}
2017-07-29 21:29:05 +02:00
2017-09-02 23:19:42 +02:00
// MARK: - P r i v a t e
2017-08-27 00:37:15 +02:00
private extension ArticlesTable {
// MARK: F e t c h i n g
func articlesWithResultSet ( _ resultSet : FMResultSet , _ database : FMDatabase ) -> Set < Article > {
2017-09-21 22:25:14 +02:00
// 1 . C r e a t e D a t a b a s e A r t i c l e s w i t h o u t r e l a t e d o b j e c t s .
2017-09-20 22:29:21 +02:00
// 2 . T h e n f e t c h t h e r e l a t e d o b j e c t s , g i v e n t h e s e t o f a r t i c l e I D s .
2017-09-21 22:25:14 +02:00
// 3 . T h e n c r e a t e s e t o f A r t i c l e s w i t h D a t a b a s e A r t i c l e s a n d r e l a t e d o b j e c t s a n d r e t u r n i t .
2017-09-20 22:29:21 +02:00
2017-09-21 22:25:14 +02:00
// 1 . C r e a t e d a t a b a s e A r t i c l e s ( i n t e r m e d i a t e r e p r e s e n t a t i o n s ) .
2017-09-14 22:32:06 +02:00
2017-09-21 22:25:14 +02:00
let databaseArticles = makeDatabaseArticles ( with : resultSet )
if databaseArticles . isEmpty {
2017-09-19 22:36:13 +02:00
return Set < Article > ( )
2017-09-14 06:41:01 +02:00
}
2017-09-21 22:25:14 +02:00
let articleIDs = databaseArticles . articleIDs ( )
2017-09-20 22:29:21 +02:00
// 2 . F e t c h r e l a t e d o b j e c t s .
2017-09-14 06:41:01 +02:00
let authorsMap = authorsLookupTable . fetchRelatedObjects ( for : articleIDs , in : database )
let attachmentsMap = attachmentsLookupTable . fetchRelatedObjects ( for : articleIDs , in : database )
2017-09-19 22:36:13 +02:00
2017-09-21 22:25:14 +02:00
// 3 . C r e a t e a r t i c l e s w i t h r e l a t e d o b j e c t s .
2017-09-19 22:36:13 +02:00
2017-09-21 22:25:14 +02:00
let articles = databaseArticles . map { ( databaseArticle ) -> Article in
2017-12-19 03:20:13 +01:00
return articleWithDatabaseArticle ( databaseArticle , authorsMap , attachmentsMap )
2017-09-20 22:29:21 +02:00
}
return Set ( articles )
2017-08-27 00:37:15 +02:00
}
2017-12-19 03:20:13 +01:00
func articleWithDatabaseArticle ( _ databaseArticle : DatabaseArticle , _ authorsMap : RelatedObjectsMap ? , _ attachmentsMap : RelatedObjectsMap ? ) -> Article {
2017-09-19 22:36:13 +02:00
2017-09-21 22:25:14 +02:00
let articleID = databaseArticle . articleID
2017-09-19 22:36:13 +02:00
let authors = authorsMap ? . authors ( for : articleID )
let attachments = attachmentsMap ? . attachments ( for : articleID )
2017-12-19 03:20:13 +01:00
return Article ( databaseArticle : databaseArticle , accountID : accountID , authors : authors , attachments : attachments )
2017-09-19 22:36:13 +02:00
}
2017-09-21 22:25:14 +02:00
func makeDatabaseArticles ( with resultSet : FMResultSet ) -> Set < DatabaseArticle > {
2017-09-19 22:36:13 +02:00
2017-09-21 22:25:14 +02:00
let articles = resultSet . mapToSet { ( row ) -> DatabaseArticle ? in
2017-09-19 22:36:13 +02:00
// T h e r e s u l t S e t i s a r e s u l t o f a J O I N q u e r y w i t h t h e s t a t u s e s t a b l e ,
// s o w e c a n g e t t h e s t a t u s e s a t t h e s a m e t i m e a n d a v o i d a d d i t i o n a l d a t a b a s e l o o k u p s .
2017-09-21 22:25:14 +02:00
guard let status = statusesTable . statusWithRow ( resultSet ) else {
2017-09-19 22:36:13 +02:00
assertionFailure ( " Expected status. " )
return nil
}
2017-09-20 22:29:21 +02:00
guard let articleID = row . string ( forColumn : DatabaseKey . articleID ) else {
2017-09-21 22:25:14 +02:00
assertionFailure ( " Expected articleID. " )
2017-09-19 22:36:13 +02:00
return nil
}
guard let feedID = row . string ( forColumn : DatabaseKey . feedID ) else {
2017-09-21 22:25:14 +02:00
assertionFailure ( " Expected feedID. " )
2017-09-19 22:36:13 +02:00
return nil
}
guard let uniqueID = row . string ( forColumn : DatabaseKey . uniqueID ) else {
2017-09-21 22:25:14 +02:00
assertionFailure ( " Expected uniqueID. " )
2017-09-19 22:36:13 +02:00
return nil
}
2017-09-21 22:25:14 +02:00
let title = row . string ( forColumn : DatabaseKey . title )
let contentHTML = row . string ( forColumn : DatabaseKey . contentHTML )
let contentText = row . string ( forColumn : DatabaseKey . contentText )
let url = row . string ( forColumn : DatabaseKey . url )
let externalURL = row . string ( forColumn : DatabaseKey . externalURL )
let summary = row . string ( forColumn : DatabaseKey . summary )
let imageURL = row . string ( forColumn : DatabaseKey . imageURL )
let bannerImageURL = row . string ( forColumn : DatabaseKey . bannerImageURL )
let datePublished = row . date ( forColumn : DatabaseKey . datePublished )
let dateModified = row . date ( forColumn : DatabaseKey . dateModified )
2017-09-22 17:06:06 +02:00
return DatabaseArticle ( articleID : articleID , feedID : feedID , uniqueID : uniqueID , title : title , contentHTML : contentHTML , contentText : contentText , url : url , externalURL : externalURL , summary : summary , imageURL : imageURL , bannerImageURL : bannerImageURL , datePublished : datePublished , dateModified : dateModified , status : status )
2017-09-19 22:36:13 +02:00
}
2017-09-21 22:25:14 +02:00
return articles
2017-09-19 22:36:13 +02:00
}
2017-09-05 02:10:02 +02:00
func fetchArticlesWithWhereClause ( _ database : FMDatabase , whereClause : String , parameters : [ AnyObject ] , withLimits : Bool ) -> Set < Article > {
2017-08-27 00:37:15 +02:00
2017-08-31 22:35:48 +02:00
// D o n ’ t f e t c h a r t i c l e s t h a t s h o u l d n ’ t a p p e a r i n t h e U I . T h e r u l e s :
// * M u s t n o t b e d e l e t e d .
// * M u s t b e e i t h e r 1 ) s t a r r e d o r 2 ) d a t e A r r i v e d m u s t b e n e w e r t h a n c u t o f f d a t e .
2018-02-11 02:37:47 +01:00
if withLimits {
let sql = " select * from articles natural join statuses where \( whereClause ) and userDeleted=0 and (starred=1 or dateArrived>?); "
return articlesWithSQL ( sql , parameters + [ articleCutoffDate as AnyObject ] , database )
}
else {
let sql = " select * from articles natural join statuses where \( whereClause ) ; "
return articlesWithSQL ( sql , parameters , database )
}
2017-08-27 22:03:15 +02:00
}
2017-08-31 22:35:48 +02:00
func fetchUnreadCount ( _ feedID : String , _ database : FMDatabase ) -> Int {
// C o u n t o n l y t h e a r t i c l e s t h a t w o u l d a p p e a r i n t h e U I .
// * M u s t b e u n r e a d .
// * M u s t n o t b e d e l e t e d .
// * M u s t b e e i t h e r 1 ) s t a r r e d o r 2 ) d a t e A r r i v e d m u s t b e n e w e r t h a n c u t o f f d a t e .
let sql = " select count(*) from articles natural join statuses where feedID=? and read=0 and userDeleted=0 and (starred=1 or dateArrived>?); "
return numberWithSQLAndParameters ( sql , [ feedID , articleCutoffDate ] , in : database )
}
2017-09-05 02:10:02 +02:00
func fetchArticlesForFeedID ( _ feedID : String , withLimits : Bool , database : FMDatabase ) -> Set < Article > {
2017-08-27 22:03:15 +02:00
2017-09-05 02:10:02 +02:00
return fetchArticlesWithWhereClause ( database , whereClause : " articles.feedID = ? " , parameters : [ feedID as AnyObject ] , withLimits : withLimits )
2017-08-27 22:03:15 +02:00
}
2017-08-27 00:37:15 +02:00
2017-08-27 22:03:15 +02:00
func fetchUnreadArticles ( _ feedIDs : Set < String > ) -> Set < Article > {
if feedIDs . isEmpty {
return Set < Article > ( )
2017-08-27 00:37:15 +02:00
}
2017-08-27 22:03:15 +02:00
var articles = Set < Article > ( )
queue . fetchSync { ( database ) in
// s e l e c t * f r o m a r t i c l e s n a t u r a l j o i n s t a t u s e s w h e r e f e e d I D i n ( ' h t t p : / / r a n c h e r o . c o m / x m l / r s s . x m l ' ) a n d r e a d = 0
let parameters = feedIDs . map { $0 as AnyObject }
let placeholders = NSString . rs_SQLValueList ( withPlaceholders : UInt ( feedIDs . count ) ) !
let whereClause = " feedID in \( placeholders ) and read=0 "
2017-09-05 02:10:02 +02:00
articles = self . fetchArticlesWithWhereClause ( database , whereClause : whereClause , parameters : parameters , withLimits : true )
2017-08-27 22:03:15 +02:00
}
2017-09-10 20:36:28 +02:00
return articles
2017-08-27 00:37:15 +02:00
}
2018-02-11 02:37:47 +01:00
func fetchTodayArticles ( _ feedIDs : Set < String > ) -> Set < Article > {
if feedIDs . isEmpty {
return Set < Article > ( )
}
var articles = Set < Article > ( )
queue . fetchSync { ( database ) in
// s e l e c t * f r o m a r t i c l e s n a t u r a l j o i n s t a t u s e s w h e r e f e e d I D i n ( ' h t t p : / / r a n c h e r o . c o m / x m l / r s s . x m l ' ) a n d ( d a t e P u b l i s h e d > ? | | ( d a t e P u b l i s h e d i s n u l l a n d d a t e A r r i v e d > ? )
//
// d a t e P u b l i s h e d m a y b e n i l , s o w e f a l l b a c k t o d a t e A r r i v e d .
let startOfToday = NSCalendar . startOfToday ( )
let parameters = feedIDs . map { $0 as AnyObject } + [ startOfToday as AnyObject , startOfToday as AnyObject ]
let placeholders = NSString . rs_SQLValueList ( withPlaceholders : UInt ( feedIDs . count ) ) !
let whereClause = " feedID in \( placeholders ) and (datePublished > ? or (datePublished is null and dateArrived > ?)) and userDeleted = 0 "
// l e t w h e r e C l a u s e = " f e e d I D i n \ ( p l a c e h o l d e r s ) a n d d a t e P u b l i s h e d > ? a n d u s e r D e l e t e d = 0 "
articles = self . fetchArticlesWithWhereClause ( database , whereClause : whereClause , parameters : parameters , withLimits : false )
}
return articles
}
2018-02-11 21:07:55 +01:00
func fetchStarredArticles ( _ feedIDs : Set < String > ) -> Set < Article > {
if feedIDs . isEmpty {
return Set < Article > ( )
}
var articles = Set < Article > ( )
queue . fetchSync { ( database ) in
// s e l e c t * f r o m a r t i c l e s n a t u r a l j o i n s t a t u s e s w h e r e f e e d I D i n ( ' h t t p : / / r a n c h e r o . c o m / x m l / r s s . x m l ' ) a n d s t a r r e d = 1 a n d u s e r D e l e t e d = 0 ;
let parameters = feedIDs . map { $0 as AnyObject }
let placeholders = NSString . rs_SQLValueList ( withPlaceholders : UInt ( feedIDs . count ) ) !
let whereClause = " feedID in \( placeholders ) and starred = 1 and userDeleted = 0 "
articles = self . fetchArticlesWithWhereClause ( database , whereClause : whereClause , parameters : parameters , withLimits : false )
}
return articles
}
2019-02-19 07:29:43 +01:00
func fetchArticlesMatching ( _ searchString : String , _ database : FMDatabase ) -> Set < Article > {
let whereClause = " (starred=1 or dateArrived>?) and userDeleted=0 and (textMatchesSearchString(title,?) or textMatchesSearchString(contentHTML,?) or textMatchesSearchString(contentText,?) or textMatchesSearchString(summary,?)) "
let parameters : [ AnyObject ] = [ articleCutoffDate as AnyObject , searchString as AnyObject , searchString as AnyObject , searchString as AnyObject , searchString as AnyObject ]
return self . fetchArticlesWithWhereClause ( database , whereClause : whereClause , parameters : parameters , withLimits : false )
}
2018-02-11 21:07:55 +01:00
2017-08-27 22:03:15 +02:00
func articlesWithSQL ( _ sql : String , _ parameters : [ AnyObject ] , _ database : FMDatabase ) -> Set < Article > {
2017-08-27 00:37:15 +02:00
2017-08-27 22:03:15 +02:00
guard let resultSet = database . executeQuery ( sql , withArgumentsIn : parameters ) else {
return Set < Article > ( )
}
return articlesWithResultSet ( resultSet , database )
2017-08-27 00:37:15 +02:00
}
2017-09-02 23:19:42 +02:00
2017-09-11 15:46:32 +02:00
// MARK: S a v i n g P a r s e d I t e m s
func callUpdateArticlesCompletionBlock ( _ newArticles : Set < Article > ? , _ updatedArticles : Set < Article > ? , _ completion : @ escaping UpdateArticlesWithFeedCompletionBlock ) {
DispatchQueue . main . async {
completion ( newArticles , updatedArticles )
}
}
2017-09-08 22:36:30 +02:00
// MARK: S a v e N e w A r t i c l e s
2017-09-05 02:10:02 +02:00
2017-09-11 15:46:32 +02:00
func findNewArticles ( _ incomingArticles : Set < Article > , _ fetchedArticlesDictionary : [ String : Article ] ) -> Set < Article > ? {
let newArticles = Set ( incomingArticles . filter { fetchedArticlesDictionary [ $0 . articleID ] = = nil } )
return newArticles . isEmpty ? nil : newArticles
}
func findAndSaveNewArticles ( _ incomingArticles : Set < Article > , _ fetchedArticlesDictionary : [ String : Article ] , _ database : FMDatabase ) -> Set < Article > ? { // 5
guard let newArticles = findNewArticles ( incomingArticles , fetchedArticlesDictionary ) else {
return nil
}
self . saveNewArticles ( newArticles , database )
return newArticles
}
2017-09-08 22:36:30 +02:00
func saveNewArticles ( _ articles : Set < Article > , _ database : FMDatabase ) {
2017-09-05 02:10:02 +02:00
2017-09-08 22:36:30 +02:00
saveRelatedObjectsForNewArticles ( articles , database )
2017-09-05 02:10:02 +02:00
2017-09-13 22:29:52 +02:00
if let databaseDictionaries = articles . databaseDictionaries ( ) {
insertRows ( databaseDictionaries , insertType : . orReplace , in : database )
}
2017-09-08 22:36:30 +02:00
}
2017-09-05 02:10:02 +02:00
2017-09-08 22:36:30 +02:00
func saveRelatedObjectsForNewArticles ( _ articles : Set < Article > , _ database : FMDatabase ) {
let databaseObjects = articles . databaseObjects ( )
authorsLookupTable . saveRelatedObjects ( for : databaseObjects , in : database )
attachmentsLookupTable . saveRelatedObjects ( for : databaseObjects , in : database )
2017-09-05 02:10:02 +02:00
}
2017-09-08 22:36:30 +02:00
// MARK: U p d a t e E x i s t i n g A r t i c l e s
2017-09-05 02:10:02 +02:00
2017-09-10 20:36:28 +02:00
func articlesWithRelatedObjectChanges < T > ( _ comparisonKeyPath : KeyPath < Article , Set < T > ? > , _ updatedArticles : Set < Article > , _ fetchedArticles : [ String : Article ] ) -> Set < Article > {
2017-09-08 22:36:30 +02:00
return updatedArticles . filter { ( updatedArticle ) -> Bool in
if let fetchedArticle = fetchedArticles [ updatedArticle . articleID ] {
2017-09-09 20:02:02 +02:00
return updatedArticle [ keyPath : comparisonKeyPath ] != fetchedArticle [ keyPath : comparisonKeyPath ]
2017-09-08 22:36:30 +02:00
}
assertionFailure ( " Expected to find matching fetched article. " ) ;
2017-09-05 02:10:02 +02:00
return true
}
}
2017-09-10 20:36:28 +02:00
func updateRelatedObjects < T > ( _ comparisonKeyPath : KeyPath < Article , Set < T > ? > , _ updatedArticles : Set < Article > , _ fetchedArticles : [ String : Article ] , _ lookupTable : DatabaseLookupTable , _ database : FMDatabase ) {
2017-09-08 22:36:30 +02:00
2017-09-09 20:02:02 +02:00
let articlesWithChanges = articlesWithRelatedObjectChanges ( comparisonKeyPath , updatedArticles , fetchedArticles )
2017-09-08 22:36:30 +02:00
if ! articlesWithChanges . isEmpty {
2017-09-09 20:02:02 +02:00
lookupTable . saveRelatedObjects ( for : articlesWithChanges . databaseObjects ( ) , in : database )
2017-09-08 22:36:30 +02:00
}
}
2017-09-03 01:08:02 +02:00
2017-09-09 21:57:24 +02:00
func saveUpdatedRelatedObjects ( _ updatedArticles : Set < Article > , _ fetchedArticles : [ String : Article ] , _ database : FMDatabase ) {
2017-09-05 02:10:02 +02:00
2017-09-09 20:02:02 +02:00
updateRelatedObjects ( \ Article . authors , updatedArticles , fetchedArticles , authorsLookupTable , database )
updateRelatedObjects ( \ Article . attachments , updatedArticles , fetchedArticles , attachmentsLookupTable , database )
2017-09-05 02:10:02 +02:00
}
2017-09-11 15:46:32 +02:00
func findUpdatedArticles ( _ incomingArticles : Set < Article > , _ fetchedArticlesDictionary : [ String : Article ] ) -> Set < Article > ? {
let updatedArticles = incomingArticles . filter { ( incomingArticle ) -> Bool in // 6
if let existingArticle = fetchedArticlesDictionary [ incomingArticle . articleID ] {
if existingArticle != incomingArticle {
return true
}
}
return false
}
return updatedArticles . isEmpty ? nil : updatedArticles
}
func findAndSaveUpdatedArticles ( _ incomingArticles : Set < Article > , _ fetchedArticlesDictionary : [ String : Article ] , _ database : FMDatabase ) -> Set < Article > ? { // 6
guard let updatedArticles = findUpdatedArticles ( incomingArticles , fetchedArticlesDictionary ) else {
return nil
}
saveUpdatedArticles ( Set ( updatedArticles ) , fetchedArticlesDictionary , database )
return updatedArticles
}
2017-09-08 22:36:30 +02:00
func saveUpdatedArticles ( _ updatedArticles : Set < Article > , _ fetchedArticles : [ String : Article ] , _ database : FMDatabase ) {
2017-09-09 20:10:15 +02:00
saveUpdatedRelatedObjects ( updatedArticles , fetchedArticles , database )
2017-09-09 21:09:48 +02:00
for updatedArticle in updatedArticles {
2017-09-09 21:24:30 +02:00
saveUpdatedArticle ( updatedArticle , fetchedArticles , database )
2017-09-09 21:09:48 +02:00
}
2017-09-08 22:36:30 +02:00
}
2017-09-09 21:09:48 +02:00
func saveUpdatedArticle ( _ updatedArticle : Article , _ fetchedArticles : [ String : Article ] , _ database : FMDatabase ) {
// O n l y u p d a t e e x a c t l y w h a t h a s c h a n g e d i n t h e A r t i c l e ( i f a n y t h i n g ) .
// U n t e s t e d t h e o r y : t h i s g e t s u s b e t t e r p e r f o r m a n c e a n d l e s s d a t a b a s e f r a g m e n t a t i o n .
2017-09-14 06:41:01 +02:00
guard let fetchedArticle = fetchedArticles [ updatedArticle . articleID ] else {
2017-09-09 21:09:48 +02:00
assertionFailure ( " Expected to find matching fetched article. " ) ;
saveNewArticles ( Set ( [ updatedArticle ] ) , database )
return
}
2017-09-14 06:41:01 +02:00
guard let changesDictionary = updatedArticle . changesFrom ( fetchedArticle ) , changesDictionary . count > 0 else {
2017-09-09 21:09:48 +02:00
// N o t u n e x p e c t e d . T h e r e m a y b e n o c h a n g e s .
return
}
updateRowsWithDictionary ( changesDictionary , whereKey : DatabaseKey . articleID , matches : updatedArticle . articleID , database : database )
2017-09-05 02:10:02 +02:00
}
2017-09-09 21:09:48 +02:00
2017-09-05 02:10:02 +02:00
func statusIndicatesArticleIsIgnorable ( _ status : ArticleStatus ) -> Bool {
// I g n o r a b l e a r t i c l e s : e i t h e r u s e r D e l e t e d = = 1 o r ( n o t s t a r r e d a n d a r r i v a l d a t e > 4 m o n t h s ) .
if status . userDeleted {
return true
}
if status . starred {
return false
}
return status . dateArrived < maximumArticleCutoffDate
}
2017-09-22 03:14:37 +02:00
func filterIncomingArticles ( _ articles : Set < Article > ) -> Set < Article > {
2017-09-11 15:46:32 +02:00
// D r o p A r t i c l e s t h a t w e c a n i g n o r e .
2017-09-22 03:14:37 +02:00
return Set ( articles . filter { ! statusIndicatesArticleIsIgnorable ( $0 . status ) } )
2017-09-05 02:10:02 +02:00
}
2017-08-27 00:37:15 +02:00
}
2017-07-29 21:29:05 +02:00