2017-07-02 02:22:19 +02:00
|
|
|
|
//
|
|
|
|
|
// Feed.swift
|
|
|
|
|
// DataModel
|
|
|
|
|
//
|
|
|
|
|
// Created by Brent Simmons on 7/1/17.
|
|
|
|
|
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import RSCore
|
2017-09-17 21:08:50 +02:00
|
|
|
|
import RSWeb
|
2018-07-28 21:16:14 +02:00
|
|
|
|
import Articles
|
2018-09-14 07:25:10 +02:00
|
|
|
|
import RSDatabase
|
2017-07-02 02:22:19 +02:00
|
|
|
|
|
2017-09-26 22:26:28 +02:00
|
|
|
|
public final class Feed: DisplayNameProvider, UnreadCountProvider, Hashable {
|
2017-07-02 02:22:19 +02:00
|
|
|
|
|
2018-09-14 07:52:34 +02:00
|
|
|
|
private struct Key {
|
|
|
|
|
static let url = "url"
|
|
|
|
|
static let feedID = "feedID"
|
|
|
|
|
static let homePageURL = "homePageURL"
|
|
|
|
|
static let iconURL = "iconURL"
|
|
|
|
|
static let faviconURL = "faviconURL"
|
|
|
|
|
static let name = "name"
|
|
|
|
|
static let editedName = "editedName"
|
|
|
|
|
static let authors = "authors"
|
|
|
|
|
static let conditionalGetInfo = "conditionalGetInfo"
|
2018-09-15 04:33:47 +02:00
|
|
|
|
static let conditionalGetLastModified = "lastModified"
|
|
|
|
|
static let conditionalGetEtag = "etag"
|
2018-09-14 07:52:34 +02:00
|
|
|
|
static let contentHash = "contentHash"
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-14 07:37:40 +02:00
|
|
|
|
public weak var account: Account?
|
2017-07-02 02:22:19 +02:00
|
|
|
|
public let url: String
|
|
|
|
|
public let feedID: String
|
2018-09-02 21:14:04 +02:00
|
|
|
|
|
|
|
|
|
private var _homePageURL: String?
|
|
|
|
|
public var homePageURL: String? {
|
|
|
|
|
get {
|
|
|
|
|
return _homePageURL
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
if let url = newValue {
|
|
|
|
|
_homePageURL = url.rs_normalizedURL()
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
_homePageURL = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-15 06:51:05 +02:00
|
|
|
|
public var iconURL: String? {
|
|
|
|
|
get {
|
|
|
|
|
return settingsTable.string(for: Key.iconURL)
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
settingsTable.setString(newValue, for: Key.iconURL)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public var faviconURL: String? {
|
|
|
|
|
get {
|
|
|
|
|
return settingsTable.string(for: Key.faviconURL)
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
settingsTable.setString(newValue, for: Key.faviconURL)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-02 02:22:19 +02:00
|
|
|
|
public var name: String?
|
2017-12-02 23:20:58 +01:00
|
|
|
|
public var authors: Set<Author>?
|
2018-01-24 06:49:33 +01:00
|
|
|
|
|
|
|
|
|
public var editedName: String? {
|
|
|
|
|
didSet {
|
|
|
|
|
postDisplayNameDidChangeNotification()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-15 04:33:47 +02:00
|
|
|
|
public var conditionalGetInfo: HTTPConditionalGetInfo? {
|
|
|
|
|
get {
|
|
|
|
|
let lastModified = settingsTable.string(for: Key.conditionalGetLastModified)
|
|
|
|
|
let etag = settingsTable.string(for: Key.conditionalGetEtag)
|
|
|
|
|
return HTTPConditionalGetInfo(lastModified: lastModified, etag: etag)
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
settingsTable.setString(newValue?.lastModified, for: Key.conditionalGetLastModified)
|
|
|
|
|
settingsTable.setString(newValue?.etag, for: Key.conditionalGetEtag)
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-15 06:51:05 +02:00
|
|
|
|
|
2018-09-14 07:52:34 +02:00
|
|
|
|
public var contentHash: String? {
|
|
|
|
|
get {
|
|
|
|
|
return settingsTable.string(for: Key.contentHash)
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
settingsTable.setString(newValue, for: Key.contentHash)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-17 00:25:38 +02:00
|
|
|
|
|
|
|
|
|
// MARK: - DisplayNameProvider
|
|
|
|
|
|
2017-07-02 02:22:19 +02:00
|
|
|
|
public var nameForDisplay: String {
|
2018-02-14 22:14:25 +01:00
|
|
|
|
if let s = editedName, !s.isEmpty {
|
|
|
|
|
return s
|
2017-07-02 02:22:19 +02:00
|
|
|
|
}
|
2018-02-14 22:14:25 +01:00
|
|
|
|
if let s = name, !s.isEmpty {
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
return NSLocalizedString("Untitled", comment: "Feed name")
|
2017-07-02 02:22:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-17 00:25:38 +02:00
|
|
|
|
// MARK: - UnreadCountProvider
|
2017-07-03 19:29:44 +02:00
|
|
|
|
|
2017-09-17 00:25:38 +02:00
|
|
|
|
public var unreadCount = 0 {
|
|
|
|
|
didSet {
|
|
|
|
|
if unreadCount != oldValue {
|
|
|
|
|
postUnreadCountDidChangeNotification()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-14 07:37:40 +02:00
|
|
|
|
private let settingsTable: ODBRawValueTable
|
2018-09-14 07:25:10 +02:00
|
|
|
|
|
2017-09-17 00:25:38 +02:00
|
|
|
|
// MARK: - Init
|
|
|
|
|
|
2018-09-14 07:37:40 +02:00
|
|
|
|
public init(account: Account, url: String, feedID: String) {
|
2017-07-02 02:22:19 +02:00
|
|
|
|
|
2018-09-14 07:37:40 +02:00
|
|
|
|
self.account = account
|
2017-07-02 02:22:19 +02:00
|
|
|
|
self.url = url
|
|
|
|
|
self.feedID = feedID
|
2018-09-14 07:37:40 +02:00
|
|
|
|
self.settingsTable = account.settingsTableForFeed(feedID: feedID)!
|
2017-07-02 02:22:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-26 22:26:28 +02:00
|
|
|
|
// MARK: - Disk Dictionary
|
|
|
|
|
|
2018-09-14 07:37:40 +02:00
|
|
|
|
convenience public init?(account: Account, dictionary: [String: Any]) {
|
2017-09-26 22:26:28 +02:00
|
|
|
|
|
2017-10-06 06:08:27 +02:00
|
|
|
|
guard let url = dictionary[Key.url] as? String else {
|
2017-09-26 22:26:28 +02:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-10-06 06:08:27 +02:00
|
|
|
|
let feedID = dictionary[Key.feedID] as? String ?? url
|
|
|
|
|
|
2018-09-14 07:37:40 +02:00
|
|
|
|
self.init(account: account, url: url, feedID: feedID)
|
2018-09-02 21:14:04 +02:00
|
|
|
|
self.homePageURL = dictionary[Key.homePageURL] as? String
|
2017-09-26 22:26:28 +02:00
|
|
|
|
self.name = dictionary[Key.name] as? String
|
|
|
|
|
self.editedName = dictionary[Key.editedName] as? String
|
|
|
|
|
|
2017-12-02 23:20:58 +01:00
|
|
|
|
if let authorsDiskArray = dictionary[Key.authors] as? [[String: Any]] {
|
|
|
|
|
self.authors = Author.authorsWithDiskArray(authorsDiskArray)
|
|
|
|
|
}
|
2017-09-26 22:26:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-28 22:16:47 +02:00
|
|
|
|
public static func isFeedDictionary(_ d: [String: Any]) -> Bool {
|
|
|
|
|
|
|
|
|
|
return d[Key.url] != nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-26 22:26:28 +02:00
|
|
|
|
public var dictionary: [String: Any] {
|
2018-02-14 22:14:25 +01:00
|
|
|
|
var d = [String: Any]()
|
|
|
|
|
|
|
|
|
|
d[Key.url] = url
|
|
|
|
|
|
|
|
|
|
// feedID is not repeated when it’s the same as url
|
|
|
|
|
if (feedID != url) {
|
|
|
|
|
d[Key.feedID] = feedID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let homePageURL = homePageURL {
|
|
|
|
|
d[Key.homePageURL] = homePageURL
|
|
|
|
|
}
|
|
|
|
|
if let name = name {
|
|
|
|
|
d[Key.name] = name
|
|
|
|
|
}
|
|
|
|
|
if let editedName = editedName {
|
|
|
|
|
d[Key.editedName] = editedName
|
|
|
|
|
}
|
|
|
|
|
if let authorsArray = authors?.diskArray() {
|
|
|
|
|
d[Key.authors] = authorsArray
|
|
|
|
|
}
|
2018-09-15 06:51:05 +02:00
|
|
|
|
|
2018-02-14 22:14:25 +01:00
|
|
|
|
return d
|
2017-09-26 22:26:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 20:13:15 +01:00
|
|
|
|
// MARK: - Debug
|
|
|
|
|
|
|
|
|
|
public func debugDropConditionalGetInfo() {
|
|
|
|
|
|
|
|
|
|
conditionalGetInfo = nil
|
|
|
|
|
contentHash = nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-25 20:54:58 +02:00
|
|
|
|
// MARK: - Hashable
|
|
|
|
|
|
|
|
|
|
public func hash(into hasher: inout Hasher) {
|
|
|
|
|
hasher.combine(feedID)
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 20:13:15 +01:00
|
|
|
|
// MARK: - Equatable
|
|
|
|
|
|
2017-07-02 02:22:19 +02:00
|
|
|
|
public class func ==(lhs: Feed, rhs: Feed) -> Bool {
|
|
|
|
|
|
|
|
|
|
return lhs === rhs
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-17 00:25:38 +02:00
|
|
|
|
// MARK: - OPMLRepresentable
|
|
|
|
|
|
|
|
|
|
extension Feed: OPMLRepresentable {
|
|
|
|
|
|
|
|
|
|
public func OPMLString(indentLevel: Int) -> String {
|
|
|
|
|
|
|
|
|
|
let escapedName = nameForDisplay.rs_stringByEscapingSpecialXMLCharacters()
|
|
|
|
|
var escapedHomePageURL = ""
|
|
|
|
|
if let homePageURL = homePageURL {
|
|
|
|
|
escapedHomePageURL = homePageURL.rs_stringByEscapingSpecialXMLCharacters()
|
|
|
|
|
}
|
|
|
|
|
let escapedFeedURL = url.rs_stringByEscapingSpecialXMLCharacters()
|
|
|
|
|
|
|
|
|
|
var s = "<outline text=\"\(escapedName)\" title=\"\(escapedName)\" description=\"\" type=\"rss\" version=\"RSS\" htmlUrl=\"\(escapedHomePageURL)\" xmlUrl=\"\(escapedFeedURL)\"/>\n"
|
|
|
|
|
s = s.rs_string(byPrependingNumberOfTabs: indentLevel)
|
|
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
|
extension Set where Element == Feed {
|
|
|
|
|
|
|
|
|
|
func feedIDs() -> Set<String> {
|
|
|
|
|
|
|
|
|
|
return Set<String>(map { $0.feedID })
|
|
|
|
|
}
|
|
|
|
|
}
|