2017-07-02 02:22:19 +02:00
|
|
|
|
//
|
|
|
|
|
// Feed.swift
|
2019-07-09 07:58:19 +02:00
|
|
|
|
// NetNewsWire
|
2017-07-02 02:22:19 +02:00
|
|
|
|
//
|
|
|
|
|
// 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
|
2017-07-02 02:22:19 +02:00
|
|
|
|
|
2019-10-03 16:53:21 +02:00
|
|
|
|
public final class Feed: DisplayNameProvider, Renamable, UnreadCountProvider, DeepLinkProvider, Hashable {
|
2017-07-02 02:22:19 +02:00
|
|
|
|
|
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
|
2019-05-08 16:54:55 +02:00
|
|
|
|
|
|
|
|
|
public var feedID: String {
|
|
|
|
|
get {
|
|
|
|
|
return metadata.feedID
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
metadata.feedID = newValue
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-02 21:14:04 +02:00
|
|
|
|
|
|
|
|
|
public var homePageURL: String? {
|
|
|
|
|
get {
|
2019-03-16 20:08:31 +01:00
|
|
|
|
return metadata.homePageURL
|
2018-09-02 21:14:04 +02:00
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
if let url = newValue {
|
2019-03-16 20:08:31 +01:00
|
|
|
|
metadata.homePageURL = url.rs_normalizedURL()
|
2018-09-02 21:14:04 +02:00
|
|
|
|
}
|
|
|
|
|
else {
|
2019-03-16 20:08:31 +01:00
|
|
|
|
metadata.homePageURL = nil
|
2018-09-02 21:14:04 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 06:17:57 +02:00
|
|
|
|
// Note: this is available only if the icon URL was available in the feed.
|
|
|
|
|
// The icon URL is a JSON-Feed-only feature.
|
|
|
|
|
// Otherwise we find an icon URL via other means, but we don’t store it
|
|
|
|
|
// as part of feed metadata.
|
2018-09-15 06:51:05 +02:00
|
|
|
|
public var iconURL: String? {
|
|
|
|
|
get {
|
2019-03-16 20:08:31 +01:00
|
|
|
|
return metadata.iconURL
|
2018-09-15 06:51:05 +02:00
|
|
|
|
}
|
|
|
|
|
set {
|
2019-03-16 20:08:31 +01:00
|
|
|
|
metadata.iconURL = newValue
|
2018-09-15 06:51:05 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 06:17:57 +02:00
|
|
|
|
// Note: this is available only if the favicon URL was available in the feed.
|
|
|
|
|
// The favicon URL is a JSON-Feed-only feature.
|
|
|
|
|
// Otherwise we find a favicon URL via other means, but we don’t store it
|
|
|
|
|
// as part of feed metadata.
|
2018-09-15 06:51:05 +02:00
|
|
|
|
public var faviconURL: String? {
|
|
|
|
|
get {
|
2019-03-16 20:08:31 +01:00
|
|
|
|
return metadata.faviconURL
|
2018-09-15 06:51:05 +02:00
|
|
|
|
}
|
|
|
|
|
set {
|
2019-03-16 20:08:31 +01:00
|
|
|
|
metadata.faviconURL = newValue
|
2018-09-15 06:51:05 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-23 19:29:48 +02:00
|
|
|
|
public var name: String?
|
2018-09-15 20:45:01 +02:00
|
|
|
|
|
2018-09-15 20:16:05 +02:00
|
|
|
|
public var authors: Set<Author>? {
|
|
|
|
|
get {
|
2019-03-16 20:08:31 +01:00
|
|
|
|
if let authorsArray = metadata.authors {
|
2019-03-14 07:41:43 +01:00
|
|
|
|
return Set(authorsArray)
|
2018-09-15 20:16:05 +02:00
|
|
|
|
}
|
2019-03-14 07:41:43 +01:00
|
|
|
|
return nil
|
2018-09-15 20:16:05 +02:00
|
|
|
|
}
|
|
|
|
|
set {
|
2019-03-14 07:41:43 +01:00
|
|
|
|
if let authorsSet = newValue {
|
2019-03-16 20:08:31 +01:00
|
|
|
|
metadata.authors = Array(authorsSet)
|
2018-09-15 20:16:05 +02:00
|
|
|
|
}
|
|
|
|
|
else {
|
2019-03-16 20:08:31 +01:00
|
|
|
|
metadata.authors = nil
|
2018-09-15 20:16:05 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-24 06:49:33 +01:00
|
|
|
|
|
|
|
|
|
public var editedName: String? {
|
2019-02-03 02:46:15 +01:00
|
|
|
|
// Don’t let editedName == ""
|
2018-09-15 20:39:33 +02:00
|
|
|
|
get {
|
2019-03-16 20:08:31 +01:00
|
|
|
|
guard let s = metadata.editedName, !s.isEmpty else {
|
2019-02-03 02:46:15 +01:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return s
|
2018-09-15 20:39:33 +02:00
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
if newValue != editedName {
|
2019-02-03 02:46:15 +01:00
|
|
|
|
if let valueToSet = newValue, !valueToSet.isEmpty {
|
2019-03-16 20:08:31 +01:00
|
|
|
|
metadata.editedName = valueToSet
|
2019-02-03 02:46:15 +01:00
|
|
|
|
}
|
|
|
|
|
else {
|
2019-03-16 20:08:31 +01:00
|
|
|
|
metadata.editedName = nil
|
2019-02-03 02:46:15 +01:00
|
|
|
|
}
|
2018-09-15 20:39:33 +02:00
|
|
|
|
postDisplayNameDidChangeNotification()
|
|
|
|
|
}
|
2018-01-24 06:49:33 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-15 04:33:47 +02:00
|
|
|
|
public var conditionalGetInfo: HTTPConditionalGetInfo? {
|
|
|
|
|
get {
|
2019-03-16 20:08:31 +01:00
|
|
|
|
return metadata.conditionalGetInfo
|
2018-09-15 04:33:47 +02:00
|
|
|
|
}
|
|
|
|
|
set {
|
2019-03-16 20:08:31 +01:00
|
|
|
|
metadata.conditionalGetInfo = newValue
|
2018-09-15 04:33:47 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-15 06:51:05 +02:00
|
|
|
|
|
2018-09-14 07:52:34 +02:00
|
|
|
|
public var contentHash: String? {
|
|
|
|
|
get {
|
2019-03-16 20:08:31 +01:00
|
|
|
|
return metadata.contentHash
|
2018-09-14 07:52:34 +02:00
|
|
|
|
}
|
|
|
|
|
set {
|
2019-03-16 20:08:31 +01:00
|
|
|
|
metadata.contentHash = newValue
|
2018-09-14 07:52:34 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-17 00:25:38 +02:00
|
|
|
|
|
2019-10-03 02:42:16 +02:00
|
|
|
|
public var isNotifyAboutNewArticles: Bool? {
|
|
|
|
|
get {
|
|
|
|
|
return metadata.isNotifyAboutNewArticles
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
metadata.isNotifyAboutNewArticles = newValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-20 03:12:55 +02:00
|
|
|
|
public var isArticleExtractorAlwaysOn: Bool? {
|
2019-09-20 02:49:11 +02:00
|
|
|
|
get {
|
|
|
|
|
return metadata.isArticleExtractorAlwaysOn
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
metadata.isArticleExtractorAlwaysOn = newValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 01:13:54 +02:00
|
|
|
|
public var subscriptionID: String? {
|
|
|
|
|
get {
|
|
|
|
|
return metadata.subscriptionID
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
metadata.subscriptionID = newValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 01:34:45 +02:00
|
|
|
|
// Folder Name: Sync Service Relationship ID
|
|
|
|
|
public var folderRelationship: [String: String]? {
|
|
|
|
|
get {
|
|
|
|
|
return metadata.folderRelationship
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
metadata.folderRelationship = newValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2018-11-22 22:57:49 +01:00
|
|
|
|
// MARK: - Renamable
|
|
|
|
|
|
2019-05-06 17:53:20 +02:00
|
|
|
|
public func rename(to newName: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-05-09 00:55:53 +02:00
|
|
|
|
guard let account = account else { return }
|
|
|
|
|
account.renameFeed(self, to: newName, completion: completion)
|
2018-11-22 22:57:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-03 16:53:21 +02:00
|
|
|
|
// MARK: - PathIDUserInfoProvider
|
|
|
|
|
public var deepLinkUserInfo: [AnyHashable : Any] {
|
|
|
|
|
return [
|
|
|
|
|
DeepLinkKey.accountID.rawValue: account?.accountID ?? "",
|
2019-10-03 22:49:27 +02:00
|
|
|
|
DeepLinkKey.accountName.rawValue: account?.nameForDisplay ?? "",
|
2019-10-03 16:53:21 +02:00
|
|
|
|
DeepLinkKey.feedID.rawValue: feedID
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-17 00:25:38 +02:00
|
|
|
|
// MARK: - UnreadCountProvider
|
2017-07-03 19:29:44 +02:00
|
|
|
|
|
2018-09-15 07:06:03 +02:00
|
|
|
|
public var unreadCount: Int {
|
|
|
|
|
get {
|
|
|
|
|
return account?.unreadCount(for: self) ?? 0
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
if unreadCount == newValue {
|
|
|
|
|
return
|
2017-09-17 00:25:38 +02:00
|
|
|
|
}
|
2018-09-15 07:06:03 +02:00
|
|
|
|
account?.setUnreadCount(newValue, for: self)
|
|
|
|
|
postUnreadCountDidChangeNotification()
|
2017-09-17 00:25:38 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-23 04:20:01 +02:00
|
|
|
|
var metadata: FeedMetadata
|
|
|
|
|
|
2019-03-17 19:24:21 +01:00
|
|
|
|
// MARK: - Private
|
|
|
|
|
|
2018-09-15 07:15:22 +02:00
|
|
|
|
private let accountID: String // Used for hashing and equality; account may turn nil
|
2019-03-14 07:41:43 +01:00
|
|
|
|
|
2017-09-17 00:25:38 +02:00
|
|
|
|
// MARK: - Init
|
|
|
|
|
|
2019-05-08 16:54:55 +02:00
|
|
|
|
init(account: Account, url: String, metadata: FeedMetadata) {
|
2018-09-14 07:37:40 +02:00
|
|
|
|
self.account = account
|
2018-09-15 07:15:22 +02:00
|
|
|
|
self.accountID = account.accountID
|
2017-07-02 02:22:19 +02:00
|
|
|
|
self.url = url
|
2019-03-16 20:08:31 +01:00
|
|
|
|
self.metadata = metadata
|
2017-07-02 02:22:19 +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)
|
2018-09-15 07:15:22 +02:00
|
|
|
|
hasher.combine(accountID)
|
2018-08-25 20:54:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2018-09-15 07:15:22 +02:00
|
|
|
|
return lhs.feedID == rhs.feedID && lhs.accountID == rhs.accountID
|
2017-07-02 02:22:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-17 00:25:38 +02:00
|
|
|
|
// MARK: - OPMLRepresentable
|
|
|
|
|
|
|
|
|
|
extension Feed: OPMLRepresentable {
|
|
|
|
|
|
2019-09-26 00:01:09 +02:00
|
|
|
|
public func OPMLString(indentLevel: Int, strictConformance: Bool) -> String {
|
2019-02-07 06:18:22 +01:00
|
|
|
|
// https://github.com/brentsimmons/NetNewsWire/issues/527
|
|
|
|
|
// Don’t use nameForDisplay because that can result in a feed name "Untitled" written to disk,
|
|
|
|
|
// which NetNewsWire may take later to be the actual name.
|
|
|
|
|
var nameToUse = editedName
|
|
|
|
|
if nameToUse == nil {
|
|
|
|
|
nameToUse = name
|
|
|
|
|
}
|
|
|
|
|
if nameToUse == nil {
|
|
|
|
|
nameToUse = ""
|
|
|
|
|
}
|
|
|
|
|
let escapedName = nameToUse!.rs_stringByEscapingSpecialXMLCharacters()
|
|
|
|
|
|
2017-09-17 00:25:38 +02:00
|
|
|
|
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 })
|
|
|
|
|
}
|
|
|
|
|
}
|