2017-07-01 17:22:19 -07:00
|
|
|
|
//
|
2019-11-14 20:11:41 -06:00
|
|
|
|
// WebFeed.swift
|
2019-07-08 22:58:19 -07:00
|
|
|
|
// NetNewsWire
|
2017-07-01 17:22:19 -07:00
|
|
|
|
//
|
|
|
|
|
// Created by Brent Simmons on 7/1/17.
|
|
|
|
|
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import RSCore
|
2017-09-17 12:08:50 -07:00
|
|
|
|
import RSWeb
|
2018-07-28 12:16:14 -07:00
|
|
|
|
import Articles
|
2017-07-01 17:22:19 -07:00
|
|
|
|
|
2019-11-15 06:19:14 -06:00
|
|
|
|
public final class WebFeed: Feed, Renamable, Hashable {
|
|
|
|
|
|
2019-11-22 09:40:39 -06:00
|
|
|
|
public var defaultReadFilterType: ReadFilterType {
|
2019-11-21 19:54:35 -06:00
|
|
|
|
return .none
|
2019-11-21 18:22:43 -06:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 06:19:14 -06:00
|
|
|
|
public var feedID: FeedIdentifier? {
|
|
|
|
|
guard let accountID = account?.accountID else {
|
|
|
|
|
assertionFailure("Expected feed.account, but got nil.")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return FeedIdentifier.webFeed(accountID, webFeedID)
|
|
|
|
|
}
|
2017-07-01 17:22:19 -07:00
|
|
|
|
|
2018-09-13 22:37:40 -07:00
|
|
|
|
public weak var account: Account?
|
2017-07-01 17:22:19 -07:00
|
|
|
|
public let url: String
|
2019-05-08 09:54:55 -05:00
|
|
|
|
|
2019-11-14 20:11:41 -06:00
|
|
|
|
public var webFeedID: String {
|
2019-05-08 09:54:55 -05:00
|
|
|
|
get {
|
2019-11-14 20:11:41 -06:00
|
|
|
|
return metadata.webFeedID
|
2019-05-08 09:54:55 -05:00
|
|
|
|
}
|
|
|
|
|
set {
|
2019-11-14 20:11:41 -06:00
|
|
|
|
metadata.webFeedID = newValue
|
2019-05-08 09:54:55 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-02 12:14:04 -07:00
|
|
|
|
|
|
|
|
|
public var homePageURL: String? {
|
|
|
|
|
get {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
return metadata.homePageURL
|
2018-09-02 12:14:04 -07:00
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
if let url = newValue {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.homePageURL = url.rs_normalizedURL()
|
2018-09-02 12:14:04 -07:00
|
|
|
|
}
|
|
|
|
|
else {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.homePageURL = nil
|
2018-09-02 12:14:04 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-08 21:17:57 -07: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-14 21:51:05 -07:00
|
|
|
|
public var iconURL: String? {
|
|
|
|
|
get {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
return metadata.iconURL
|
2018-09-14 21:51:05 -07:00
|
|
|
|
}
|
|
|
|
|
set {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.iconURL = newValue
|
2018-09-14 21:51:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-08 21:17:57 -07: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-14 21:51:05 -07:00
|
|
|
|
public var faviconURL: String? {
|
|
|
|
|
get {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
return metadata.faviconURL
|
2018-09-14 21:51:05 -07:00
|
|
|
|
}
|
|
|
|
|
set {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.faviconURL = newValue
|
2018-09-14 21:51:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-23 12:29:48 -05:00
|
|
|
|
public var name: String?
|
2018-09-15 11:45:01 -07:00
|
|
|
|
|
2018-09-15 11:16:05 -07:00
|
|
|
|
public var authors: Set<Author>? {
|
|
|
|
|
get {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
if let authorsArray = metadata.authors {
|
2019-03-13 23:41:43 -07:00
|
|
|
|
return Set(authorsArray)
|
2018-09-15 11:16:05 -07:00
|
|
|
|
}
|
2019-03-13 23:41:43 -07:00
|
|
|
|
return nil
|
2018-09-15 11:16:05 -07:00
|
|
|
|
}
|
|
|
|
|
set {
|
2019-03-13 23:41:43 -07:00
|
|
|
|
if let authorsSet = newValue {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.authors = Array(authorsSet)
|
2018-09-15 11:16:05 -07:00
|
|
|
|
}
|
|
|
|
|
else {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.authors = nil
|
2018-09-15 11:16:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-23 21:49:33 -08:00
|
|
|
|
|
|
|
|
|
public var editedName: String? {
|
2019-02-02 17:46:15 -08:00
|
|
|
|
// Don’t let editedName == ""
|
2018-09-15 11:39:33 -07:00
|
|
|
|
get {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
guard let s = metadata.editedName, !s.isEmpty else {
|
2019-02-02 17:46:15 -08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return s
|
2018-09-15 11:39:33 -07:00
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
if newValue != editedName {
|
2019-02-02 17:46:15 -08:00
|
|
|
|
if let valueToSet = newValue, !valueToSet.isEmpty {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.editedName = valueToSet
|
2019-02-02 17:46:15 -08:00
|
|
|
|
}
|
|
|
|
|
else {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.editedName = nil
|
2019-02-02 17:46:15 -08:00
|
|
|
|
}
|
2018-09-15 11:39:33 -07:00
|
|
|
|
postDisplayNameDidChangeNotification()
|
|
|
|
|
}
|
2018-01-23 21:49:33 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-14 19:33:47 -07:00
|
|
|
|
public var conditionalGetInfo: HTTPConditionalGetInfo? {
|
|
|
|
|
get {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
return metadata.conditionalGetInfo
|
2018-09-14 19:33:47 -07:00
|
|
|
|
}
|
|
|
|
|
set {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.conditionalGetInfo = newValue
|
2018-09-14 19:33:47 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-14 21:51:05 -07:00
|
|
|
|
|
2018-09-13 22:52:34 -07:00
|
|
|
|
public var contentHash: String? {
|
|
|
|
|
get {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
return metadata.contentHash
|
2018-09-13 22:52:34 -07:00
|
|
|
|
}
|
|
|
|
|
set {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.contentHash = newValue
|
2018-09-13 22:52:34 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-16 15:25:38 -07:00
|
|
|
|
|
2019-10-02 19:42:16 -05:00
|
|
|
|
public var isNotifyAboutNewArticles: Bool? {
|
|
|
|
|
get {
|
|
|
|
|
return metadata.isNotifyAboutNewArticles
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
metadata.isNotifyAboutNewArticles = newValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-19 20:12:55 -05:00
|
|
|
|
public var isArticleExtractorAlwaysOn: Bool? {
|
2019-09-19 19:49:11 -05:00
|
|
|
|
get {
|
|
|
|
|
return metadata.isArticleExtractorAlwaysOn
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
metadata.isArticleExtractorAlwaysOn = newValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 18:13:54 -05:00
|
|
|
|
public var subscriptionID: String? {
|
|
|
|
|
get {
|
|
|
|
|
return metadata.subscriptionID
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
metadata.subscriptionID = newValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 18:34:45 -05:00
|
|
|
|
// Folder Name: Sync Service Relationship ID
|
|
|
|
|
public var folderRelationship: [String: String]? {
|
|
|
|
|
get {
|
|
|
|
|
return metadata.folderRelationship
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
metadata.folderRelationship = newValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-16 15:25:38 -07:00
|
|
|
|
// MARK: - DisplayNameProvider
|
|
|
|
|
|
2017-07-01 17:22:19 -07:00
|
|
|
|
public var nameForDisplay: String {
|
2018-02-14 13:14:25 -08:00
|
|
|
|
if let s = editedName, !s.isEmpty {
|
|
|
|
|
return s
|
2017-07-01 17:22:19 -07:00
|
|
|
|
}
|
2018-02-14 13:14:25 -08:00
|
|
|
|
if let s = name, !s.isEmpty {
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
return NSLocalizedString("Untitled", comment: "Feed name")
|
2017-07-01 17:22:19 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-22 13:57:49 -08:00
|
|
|
|
// MARK: - Renamable
|
|
|
|
|
|
2019-05-06 10:53:20 -05:00
|
|
|
|
public func rename(to newName: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-05-08 17:55:53 -05:00
|
|
|
|
guard let account = account else { return }
|
2019-11-14 20:11:41 -06:00
|
|
|
|
account.renameWebFeed(self, to: newName, completion: completion)
|
2018-11-22 13:57:49 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-16 15:25:38 -07:00
|
|
|
|
// MARK: - UnreadCountProvider
|
2017-07-03 10:29:44 -07:00
|
|
|
|
|
2018-09-14 22:06:03 -07:00
|
|
|
|
public var unreadCount: Int {
|
|
|
|
|
get {
|
|
|
|
|
return account?.unreadCount(for: self) ?? 0
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
if unreadCount == newValue {
|
|
|
|
|
return
|
2017-09-16 15:25:38 -07:00
|
|
|
|
}
|
2018-09-14 22:06:03 -07:00
|
|
|
|
account?.setUnreadCount(newValue, for: self)
|
|
|
|
|
postUnreadCountDidChangeNotification()
|
2017-09-16 15:25:38 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-14 20:11:41 -06:00
|
|
|
|
var metadata: WebFeedMetadata
|
2019-09-22 21:20:01 -05:00
|
|
|
|
|
2019-03-17 11:24:21 -07:00
|
|
|
|
// MARK: - Private
|
|
|
|
|
|
2018-09-14 22:15:22 -07:00
|
|
|
|
private let accountID: String // Used for hashing and equality; account may turn nil
|
2019-03-13 23:41:43 -07:00
|
|
|
|
|
2017-09-16 15:25:38 -07:00
|
|
|
|
// MARK: - Init
|
|
|
|
|
|
2019-11-14 20:11:41 -06:00
|
|
|
|
init(account: Account, url: String, metadata: WebFeedMetadata) {
|
2018-09-13 22:37:40 -07:00
|
|
|
|
self.account = account
|
2018-09-14 22:15:22 -07:00
|
|
|
|
self.accountID = account.accountID
|
2017-07-01 17:22:19 -07:00
|
|
|
|
self.url = url
|
2019-03-16 12:08:31 -07:00
|
|
|
|
self.metadata = metadata
|
2017-07-01 17:22:19 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 11:13:15 -08:00
|
|
|
|
// MARK: - Debug
|
|
|
|
|
|
|
|
|
|
public func debugDropConditionalGetInfo() {
|
|
|
|
|
conditionalGetInfo = nil
|
|
|
|
|
contentHash = nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-25 11:54:58 -07:00
|
|
|
|
// MARK: - Hashable
|
|
|
|
|
|
|
|
|
|
public func hash(into hasher: inout Hasher) {
|
2019-11-14 20:11:41 -06:00
|
|
|
|
hasher.combine(webFeedID)
|
2018-08-25 11:54:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 11:13:15 -08:00
|
|
|
|
// MARK: - Equatable
|
|
|
|
|
|
2019-11-14 20:11:41 -06:00
|
|
|
|
public class func ==(lhs: WebFeed, rhs: WebFeed) -> Bool {
|
|
|
|
|
return lhs.webFeedID == rhs.webFeedID && lhs.accountID == rhs.accountID
|
2017-07-01 17:22:19 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-16 15:25:38 -07:00
|
|
|
|
// MARK: - OPMLRepresentable
|
|
|
|
|
|
2019-11-14 20:11:41 -06:00
|
|
|
|
extension WebFeed: OPMLRepresentable {
|
2017-09-16 15:25:38 -07:00
|
|
|
|
|
2019-09-25 17:01:09 -05:00
|
|
|
|
public func OPMLString(indentLevel: Int, strictConformance: Bool) -> String {
|
2019-02-06 21:18:22 -08: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-16 15:25:38 -07: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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-14 20:11:41 -06:00
|
|
|
|
extension Set where Element == WebFeed {
|
2018-07-28 12:16:14 -07:00
|
|
|
|
|
2019-11-14 20:11:41 -06:00
|
|
|
|
func webFeedIDs() -> Set<String> {
|
|
|
|
|
return Set<String>(map { $0.webFeedID })
|
2018-07-28 12:16:14 -07:00
|
|
|
|
}
|
|
|
|
|
}
|