mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-28 00:04:25 +00:00
237 lines
6.3 KiB
Swift
237 lines
6.3 KiB
Swift
//
|
||
// ArticleUtilities.swift
|
||
// NetNewsWire
|
||
//
|
||
// Created by Brent Simmons on 7/25/15.
|
||
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
|
||
//
|
||
|
||
import Foundation
|
||
import RSCore
|
||
import Articles
|
||
import Account
|
||
import Images
|
||
|
||
// These handle multiple accounts.
|
||
|
||
@MainActor func markArticles(_ articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool, completion: (() -> Void)? = nil) {
|
||
markArticleIDs(articleIDsByAccountID(articles), statusKey: statusKey, flag: flag, completion: completion)
|
||
}
|
||
|
||
@MainActor func markArticleIDs(_ articleIDsByAccountID: [String: Set<String>], statusKey: ArticleStatus.Key, flag: Bool, completion: (() -> Void)? = nil) {
|
||
Task { @MainActor in
|
||
for (accountID, articleIDs) in articleIDsByAccountID {
|
||
guard let account = AccountManager.shared.existingAccount(accountID: accountID) else {
|
||
continue
|
||
}
|
||
try? await account.markArticles(articleIDs: articleIDs, statusKey: statusKey, flag: flag)
|
||
}
|
||
completion?()
|
||
}
|
||
}
|
||
|
||
private func articleIDsByAccountID(_ articles: Set<Article>) -> [String: Set<String>] {
|
||
let d = Dictionary(grouping: articles, by: { $0.accountID })
|
||
return d.mapValues { Set($0.articleIDs()) }
|
||
}
|
||
|
||
extension Article {
|
||
var url: URL? {
|
||
return URL.encodeSpacesIfNeeded(rawLink)
|
||
}
|
||
|
||
var externalURL: URL? {
|
||
return URL.encodeSpacesIfNeeded(rawExternalLink)
|
||
}
|
||
|
||
var preferredURL: URL? {
|
||
return url ?? externalURL
|
||
}
|
||
|
||
var imageURL: URL? {
|
||
return URL.encodeSpacesIfNeeded(rawImageLink)
|
||
}
|
||
|
||
var thumbnailURL: URL? {
|
||
URL.encodeSpacesIfNeeded(rawThumbnailLink)
|
||
}
|
||
|
||
var link: String? {
|
||
// Prefer link from URL, if one can be created, as these are repaired if required.
|
||
// Provide the raw link if URL creation fails.
|
||
return url?.absoluteString ?? rawLink
|
||
}
|
||
|
||
var externalLink: String? {
|
||
// Prefer link from externalURL, if one can be created, as these are repaired if required.
|
||
// Provide the raw link if URL creation fails.
|
||
return externalURL?.absoluteString ?? rawExternalLink
|
||
}
|
||
|
||
var imageLink: String? {
|
||
// Prefer link from imageURL, if one can be created, as these are repaired if required.
|
||
// Provide the raw link if URL creation fails.
|
||
return imageURL?.absoluteString ?? rawImageLink
|
||
}
|
||
|
||
var thumbnailLink: String? {
|
||
thumbnailURL?.absoluteString ?? rawThumbnailLink
|
||
}
|
||
|
||
var preferredLink: String? {
|
||
if let link = link, !link.isEmpty {
|
||
return link
|
||
}
|
||
if let externalLink = externalLink, !externalLink.isEmpty {
|
||
return externalLink
|
||
}
|
||
return nil
|
||
}
|
||
|
||
var body: String? {
|
||
return contentHTML ?? contentText ?? summary
|
||
}
|
||
|
||
var logicalDatePublished: Date {
|
||
return datePublished ?? dateModified ?? status.dateArrived
|
||
}
|
||
|
||
}
|
||
|
||
@MainActor extension Article {
|
||
var feed: Feed? {
|
||
return account?.existingFeed(withFeedID: feedID)
|
||
}
|
||
|
||
func iconImage() -> IconImage? {
|
||
return IconImageCache.shared.imageForArticle(self)
|
||
}
|
||
|
||
/// Grayscale when the current theme’s Info.plist sets `grayscaleIcon`.
|
||
func articleViewIconImage() -> IconImage? {
|
||
guard let iconImage = iconImage() else {
|
||
return nil
|
||
}
|
||
return ArticleThemesManager.shared.currentTheme.usesGrayscaleIcon ? iconImage.grayscaleIconImage : iconImage
|
||
}
|
||
|
||
/// True when an icon notification is about an icon this article might show:
|
||
/// its feed’s icon, or one of its authors’ avatars.
|
||
func mightShowIcon(in notification: Notification) -> Bool {
|
||
switch notification.name {
|
||
case .feedIconImageDidChange:
|
||
guard let feed, let notificationFeed = notification.userInfo?[UserInfoKey.feed] as? Feed else {
|
||
return false
|
||
}
|
||
return notificationFeed == feed
|
||
case .authorAvatarImageDidChange:
|
||
guard let avatarURL = notification.userInfo?[UserInfoKey.url] as? String else {
|
||
return false
|
||
}
|
||
return authors?.contains { $0.avatarURL == avatarURL } ?? false
|
||
default:
|
||
return false
|
||
}
|
||
}
|
||
|
||
func iconImageUrl(feed: Feed) -> URL? {
|
||
if let image = iconImage() {
|
||
guard let imageData = image.image.dataRepresentation() else {
|
||
return nil
|
||
}
|
||
let fm = FileManager.default
|
||
var path = fm.urls(for: .cachesDirectory, in: .userDomainMask)[0]
|
||
let feedID = feed.feedID.replacingOccurrences(of: "/", with: "_")
|
||
path.appendPathComponent(feedID + "_smallIcon.png")
|
||
fm.createFile(atPath: path.path, contents: imageData, attributes: nil)
|
||
return path
|
||
} else {
|
||
return nil
|
||
}
|
||
}
|
||
|
||
var isAvailableToMarkUnread: Bool {
|
||
guard let markUnreadWindow = account?.behaviors.compactMap( { behavior -> Int? in
|
||
switch behavior {
|
||
case .disallowMarkAsUnreadAfterPeriod(let days):
|
||
return days
|
||
default:
|
||
return nil
|
||
}
|
||
}).first else {
|
||
return true
|
||
}
|
||
|
||
if logicalDatePublished.byAdding(days: markUnreadWindow) > Date() {
|
||
return true
|
||
} else {
|
||
return false
|
||
}
|
||
}
|
||
|
||
func byline() -> String {
|
||
guard let authors = authors ?? feed?.authors, !authors.isEmpty else {
|
||
return ""
|
||
}
|
||
|
||
// If the author's name is the same as the feed, then we don't want to display it.
|
||
// This code assumes that multiple authors would never match the feed name so that
|
||
// if there feed owner has an article co-author all authors are given the byline.
|
||
if authors.count == 1, let author = authors.first {
|
||
if author.name == feed?.nameForDisplay {
|
||
return ""
|
||
}
|
||
}
|
||
|
||
var byline = ""
|
||
var isFirstAuthor = true
|
||
|
||
for author in authors {
|
||
if !isFirstAuthor {
|
||
byline += ", "
|
||
}
|
||
isFirstAuthor = false
|
||
|
||
var authorEmailAddress: String?
|
||
if let emailAddress = author.emailAddress, !(emailAddress.contains("noreply@") || emailAddress.contains("no-reply@")) {
|
||
authorEmailAddress = emailAddress
|
||
}
|
||
|
||
if let emailAddress = authorEmailAddress, emailAddress.contains(" ") {
|
||
byline += emailAddress // probably name plus email address
|
||
} else if let name = author.name, let emailAddress = authorEmailAddress {
|
||
byline += "\(name) <\(emailAddress)>"
|
||
} else if let name = author.name {
|
||
byline += name
|
||
} else if let emailAddress = authorEmailAddress {
|
||
byline += "<\(emailAddress)>"
|
||
} else if let url = author.url {
|
||
byline += url
|
||
}
|
||
}
|
||
|
||
return byline
|
||
}
|
||
}
|
||
|
||
// MARK: - Path
|
||
|
||
struct ArticlePathKey {
|
||
static let accountID = "accountID"
|
||
static let accountName = "accountName"
|
||
static let feedID = "feedID"
|
||
static let articleID = "articleID"
|
||
}
|
||
|
||
@MainActor extension Article {
|
||
|
||
public var pathUserInfo: [AnyHashable: Any] {
|
||
return [
|
||
ArticlePathKey.accountID: accountID,
|
||
ArticlePathKey.accountName: account?.nameForDisplay ?? "",
|
||
ArticlePathKey.feedID: feedID,
|
||
ArticlePathKey.articleID: articleID
|
||
]
|
||
}
|
||
}
|