2017-07-04 00:04:31 +02:00
|
|
|
|
//
|
|
|
|
|
// Article+Database.swift
|
|
|
|
|
// Database
|
|
|
|
|
//
|
|
|
|
|
// Created by Brent Simmons on 7/3/17.
|
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import RSDatabase
|
|
|
|
|
import Data
|
2017-09-05 02:10:02 +02:00
|
|
|
|
import RSParser
|
2017-07-04 00:04:31 +02:00
|
|
|
|
|
|
|
|
|
extension Article {
|
|
|
|
|
|
2017-12-19 03:20:13 +01:00
|
|
|
|
init(databaseArticle: DatabaseArticle, accountID: String, authors: Set<Author>?, attachments: Set<Attachment>?) {
|
2017-09-19 22:36:13 +02:00
|
|
|
|
|
2017-12-19 03:20:13 +01:00
|
|
|
|
self.init(accountID: accountID, articleID: databaseArticle.articleID, feedID: databaseArticle.feedID, uniqueID: databaseArticle.uniqueID, title: databaseArticle.title, contentHTML: databaseArticle.contentHTML, contentText: databaseArticle.contentText, url: databaseArticle.url, externalURL: databaseArticle.externalURL, summary: databaseArticle.summary, imageURL: databaseArticle.imageURL, bannerImageURL: databaseArticle.bannerImageURL, datePublished: databaseArticle.datePublished, dateModified: databaseArticle.dateModified, authors: authors, attachments: attachments, status: databaseArticle.status)
|
2017-09-19 22:36:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-19 07:00:35 +02:00
|
|
|
|
init(parsedItem: ParsedItem, accountID: String, feedID: String, status: ArticleStatus) {
|
2017-09-05 02:10:02 +02:00
|
|
|
|
|
2017-09-14 06:41:01 +02:00
|
|
|
|
let authors = Author.authorsWithParsedAuthors(parsedItem.authors)
|
2017-09-05 02:10:02 +02:00
|
|
|
|
let attachments = Attachment.attachmentsWithParsedAttachments(parsedItem.attachments)
|
|
|
|
|
|
2018-02-11 02:37:47 +01:00
|
|
|
|
self.init(accountID: accountID, articleID: parsedItem.syncServiceID, feedID: feedID, uniqueID: parsedItem.uniqueID, title: parsedItem.title, contentHTML: parsedItem.contentHTML, contentText: parsedItem.contentText, url: parsedItem.url, externalURL: parsedItem.externalURL, summary: parsedItem.summary, imageURL: parsedItem.imageURL, bannerImageURL: parsedItem.bannerImageURL, datePublished: parsedItem.datePublished ?? parsedItem.dateModified, dateModified: parsedItem.dateModified, authors: authors, attachments: attachments, status: status)
|
2017-07-04 00:04:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-09 21:57:24 +02:00
|
|
|
|
private func addPossibleStringChangeWithKeyPath(_ comparisonKeyPath: KeyPath<Article,String?>, _ otherArticle: Article, _ key: String, _ dictionary: NSMutableDictionary) {
|
2017-09-09 21:09:48 +02:00
|
|
|
|
|
|
|
|
|
if self[keyPath: comparisonKeyPath] != otherArticle[keyPath: comparisonKeyPath] {
|
|
|
|
|
dictionary.addOptionalStringDefaultingEmpty(self[keyPath: comparisonKeyPath], key)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func changesFrom(_ otherArticle: Article) -> NSDictionary? {
|
|
|
|
|
|
|
|
|
|
if self == otherArticle {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let d = NSMutableDictionary()
|
2017-09-09 21:57:24 +02:00
|
|
|
|
if uniqueID != otherArticle.uniqueID {
|
|
|
|
|
// This should be super-rare, if ever.
|
|
|
|
|
if !otherArticle.uniqueID.isEmpty {
|
|
|
|
|
d[DatabaseKey.uniqueID] = otherArticle.uniqueID
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-09 21:09:48 +02:00
|
|
|
|
|
|
|
|
|
addPossibleStringChangeWithKeyPath(\Article.title, otherArticle, DatabaseKey.title, d)
|
|
|
|
|
addPossibleStringChangeWithKeyPath(\Article.contentHTML, otherArticle, DatabaseKey.contentHTML, d)
|
|
|
|
|
addPossibleStringChangeWithKeyPath(\Article.contentText, otherArticle, DatabaseKey.contentText, d)
|
|
|
|
|
addPossibleStringChangeWithKeyPath(\Article.url, otherArticle, DatabaseKey.url, d)
|
|
|
|
|
addPossibleStringChangeWithKeyPath(\Article.externalURL, otherArticle, DatabaseKey.externalURL, d)
|
|
|
|
|
addPossibleStringChangeWithKeyPath(\Article.summary, otherArticle, DatabaseKey.summary, d)
|
|
|
|
|
addPossibleStringChangeWithKeyPath(\Article.imageURL, otherArticle, DatabaseKey.imageURL, d)
|
|
|
|
|
addPossibleStringChangeWithKeyPath(\Article.bannerImageURL, otherArticle, DatabaseKey.bannerImageURL, d)
|
|
|
|
|
|
|
|
|
|
// If updated versions of dates are nil, and we have existing dates, keep the existing dates.
|
|
|
|
|
// This is data that’s good to have, and it’s likely that a feed removing dates is doing so in error.
|
|
|
|
|
|
2017-09-09 21:57:24 +02:00
|
|
|
|
if datePublished != otherArticle.datePublished {
|
2017-09-09 21:09:48 +02:00
|
|
|
|
if let updatedDatePublished = otherArticle.datePublished {
|
|
|
|
|
d[DatabaseKey.datePublished] = updatedDatePublished
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-09 21:57:24 +02:00
|
|
|
|
if dateModified != otherArticle.dateModified {
|
2017-09-09 21:09:48 +02:00
|
|
|
|
if let updatedDateModified = otherArticle.dateModified {
|
|
|
|
|
d[DatabaseKey.dateModified] = updatedDateModified
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-09 21:57:24 +02:00
|
|
|
|
if d.count < 1 {
|
2017-09-09 21:09:48 +02:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return d
|
|
|
|
|
}
|
2017-09-05 02:10:02 +02:00
|
|
|
|
|
2017-09-19 07:00:35 +02:00
|
|
|
|
static func articlesWithParsedItems(_ parsedItems: Set<ParsedItem>, _ accountID: String, _ feedID: String, _ statusesDictionary: [String: ArticleStatus]) -> Set<Article> {
|
2017-09-05 17:53:45 +02:00
|
|
|
|
|
2017-09-19 07:00:35 +02:00
|
|
|
|
return Set(parsedItems.map{ Article(parsedItem: $0, accountID: accountID, feedID: feedID, status: statusesDictionary[$0.articleID]!) })
|
2017-09-05 02:10:02 +02:00
|
|
|
|
}
|
2017-09-05 17:53:45 +02:00
|
|
|
|
|
2017-07-04 00:04:31 +02:00
|
|
|
|
}
|
2017-08-07 06:16:13 +02:00
|
|
|
|
|
2017-08-21 02:46:15 +02:00
|
|
|
|
extension Article: DatabaseObject {
|
2017-09-13 22:29:52 +02:00
|
|
|
|
|
|
|
|
|
public func databaseDictionary() -> NSDictionary? {
|
|
|
|
|
|
|
|
|
|
let d = NSMutableDictionary()
|
|
|
|
|
|
|
|
|
|
d[DatabaseKey.articleID] = articleID
|
|
|
|
|
d[DatabaseKey.feedID] = feedID
|
|
|
|
|
d[DatabaseKey.uniqueID] = uniqueID
|
|
|
|
|
|
|
|
|
|
d.addOptionalString(title, DatabaseKey.title)
|
|
|
|
|
d.addOptionalString(contentHTML, DatabaseKey.contentHTML)
|
|
|
|
|
d.addOptionalString(contentText, DatabaseKey.contentText)
|
|
|
|
|
d.addOptionalString(url, DatabaseKey.url)
|
|
|
|
|
d.addOptionalString(externalURL, DatabaseKey.externalURL)
|
|
|
|
|
d.addOptionalString(summary, DatabaseKey.summary)
|
|
|
|
|
d.addOptionalString(imageURL, DatabaseKey.imageURL)
|
|
|
|
|
d.addOptionalString(bannerImageURL, DatabaseKey.bannerImageURL)
|
|
|
|
|
|
|
|
|
|
d.addOptionalDate(datePublished, DatabaseKey.datePublished)
|
|
|
|
|
d.addOptionalDate(dateModified, DatabaseKey.dateModified)
|
|
|
|
|
|
|
|
|
|
return (d.copy() as! NSDictionary)
|
|
|
|
|
}
|
2017-08-21 02:46:15 +02:00
|
|
|
|
|
2017-08-21 07:43:46 +02:00
|
|
|
|
public var databaseID: String {
|
2018-02-14 22:14:25 +01:00
|
|
|
|
return articleID
|
2017-08-21 02:46:15 +02:00
|
|
|
|
}
|
2017-12-02 22:20:27 +01:00
|
|
|
|
|
|
|
|
|
public func relatedObjectsWithName(_ name: String) -> [DatabaseObject]? {
|
|
|
|
|
|
|
|
|
|
switch name {
|
|
|
|
|
case RelationshipName.authors:
|
|
|
|
|
return databaseObjectArray(with: authors)
|
|
|
|
|
case RelationshipName.attachments:
|
|
|
|
|
return databaseObjectArray(with: attachments)
|
|
|
|
|
default:
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func databaseObjectArray<T: DatabaseObject>(with objects: Set<T>?) -> [DatabaseObject]? {
|
|
|
|
|
|
|
|
|
|
guard let objects = objects else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return Array(objects)
|
|
|
|
|
}
|
2017-08-21 02:46:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 06:16:13 +02:00
|
|
|
|
extension Set where Element == Article {
|
|
|
|
|
|
2017-09-18 22:17:30 +02:00
|
|
|
|
func statuses() -> Set<ArticleStatus> {
|
|
|
|
|
|
|
|
|
|
return Set<ArticleStatus>(map { $0.status })
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-02 23:19:42 +02:00
|
|
|
|
func dictionary() -> [String: Article] {
|
|
|
|
|
|
|
|
|
|
var d = [String: Article]()
|
|
|
|
|
for article in self {
|
|
|
|
|
d[article.articleID] = article
|
|
|
|
|
}
|
|
|
|
|
return d
|
|
|
|
|
}
|
2017-09-05 02:10:02 +02:00
|
|
|
|
|
|
|
|
|
func databaseObjects() -> [DatabaseObject] {
|
|
|
|
|
|
|
|
|
|
return self.map{ $0 as DatabaseObject }
|
|
|
|
|
}
|
2017-09-13 22:29:52 +02:00
|
|
|
|
|
|
|
|
|
func databaseDictionaries() -> [NSDictionary]? {
|
|
|
|
|
|
2018-01-28 03:50:48 +01:00
|
|
|
|
return self.compactMap { $0.databaseDictionary() }
|
2017-09-13 22:29:52 +02:00
|
|
|
|
}
|
2017-09-05 02:10:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private extension NSMutableDictionary {
|
|
|
|
|
|
|
|
|
|
func addOptionalString(_ value: String?, _ key: String) {
|
|
|
|
|
|
|
|
|
|
if let value = value {
|
|
|
|
|
self[key] = value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-09 21:09:48 +02:00
|
|
|
|
func addOptionalStringDefaultingEmpty(_ value: String?, _ key: String) {
|
|
|
|
|
|
|
|
|
|
if let value = value {
|
|
|
|
|
self[key] = value
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
self[key] = ""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-05 02:10:02 +02:00
|
|
|
|
func addOptionalDate(_ date: Date?, _ key: String) {
|
|
|
|
|
|
|
|
|
|
if let date = date {
|
|
|
|
|
self[key] = date as NSDate
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-07 06:16:13 +02:00
|
|
|
|
}
|