mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-30 17:29:23 +00:00
Fix concurrency crash in ArticlePasteboardWriter.
This commit is contained in:
1 parent
4248cdaf83
commit
2ac2a3c1bf
2 files changed
+56
-48
No files matched your search
@@ -11,26 +11,32 @@ import Articles
|
||||
import RSCore
|
||||
|
||||
extension Article: @retroactive PasteboardWriterOwner {
|
||||
@MainActor public var pasteboardWriter: NSPasteboardWriting {
|
||||
return ArticlePasteboardWriter(article: self)
|
||||
public var pasteboardWriter: NSPasteboardWriting {
|
||||
ArticlePasteboardWriter(article: self)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor @objc final class ArticlePasteboardWriter: NSObject, @MainActor NSPasteboardWriting {
|
||||
|
||||
// This can’t be @MainActor — AppKit will call methods outside the main thread.
|
||||
@objc final class ArticlePasteboardWriter: NSObject, NSPasteboardWriting {
|
||||
let article: Article
|
||||
private let renderedHTML: String
|
||||
private let feedURL: String?
|
||||
private let feedNameForDisplay: String?
|
||||
private let feedHomePageURL: String?
|
||||
|
||||
static let articleUTI = "com.ranchero.article"
|
||||
static let articleUTIType = NSPasteboard.PasteboardType(rawValue: articleUTI)
|
||||
static let articleUTIInternal = "com.ranchero.NetNewsWire-Evergreen.internal.article"
|
||||
static let articleUTIInternalType = NSPasteboard.PasteboardType(rawValue: articleUTIInternal)
|
||||
|
||||
private lazy var renderedHTML: String = {
|
||||
let rendering = ArticleRenderer.articleHTML(article: article, theme: ArticleThemesManager.shared.currentTheme)
|
||||
return rendering.html
|
||||
}()
|
||||
|
||||
init(article: Article) {
|
||||
@MainActor init(article: Article) {
|
||||
self.article = article
|
||||
let rendering = ArticleRenderer.articleHTML(article: article, theme: ArticleThemesManager.shared.currentTheme)
|
||||
self.renderedHTML = rendering.html
|
||||
|
||||
self.feedURL = article.feed?.url
|
||||
self.feedNameForDisplay = article.feed?.nameForDisplay
|
||||
self.feedHomePageURL = article.feed?.homePageURL
|
||||
}
|
||||
|
||||
// MARK: - NSPasteboardWriting
|
||||
@@ -94,12 +100,14 @@ private extension ArticlePasteboardWriter {
|
||||
|
||||
s += "Date: \(article.logicalDatePublished)\n\n"
|
||||
|
||||
if let feed = article.feed {
|
||||
s += "Feed: \(feed.nameForDisplay)\n"
|
||||
if let homePageURL = feed.homePageURL {
|
||||
s += "Home page: \(homePageURL)\n"
|
||||
}
|
||||
s += "URL: \(feed.url)"
|
||||
if let feedNameForDisplay {
|
||||
s += "Feed: \(feedNameForDisplay)\n"
|
||||
}
|
||||
if let feedHomePageURL {
|
||||
s += "Home page: \(feedHomePageURL)\n"
|
||||
}
|
||||
if let feedURL {
|
||||
s += "URL: \(feedURL)"
|
||||
}
|
||||
|
||||
return s
|
||||
@@ -140,11 +148,7 @@ private extension ArticlePasteboardWriter {
|
||||
|
||||
d[Key.articleID] = article.articleID
|
||||
d[Key.uniqueID] = article.uniqueID
|
||||
|
||||
if let feed = article.feed {
|
||||
d[Key.feedURL] = feed.url
|
||||
}
|
||||
|
||||
d[Key.feedURL] = feedURL ?? nil
|
||||
d[Key.feedID] = article.feedID
|
||||
d[Key.title] = article.title ?? nil
|
||||
d[Key.contentHTML] = article.contentHTML ?? nil
|
||||
|
||||
@@ -38,11 +38,7 @@ private func accountAndArticlesDictionary(_ articles: Set<Article>) -> [String:
|
||||
return d.mapValues { Set($0) }
|
||||
}
|
||||
|
||||
@MainActor extension Article {
|
||||
var feed: Feed? {
|
||||
return account?.existingFeed(withFeedID: feedID)
|
||||
}
|
||||
|
||||
extension Article {
|
||||
var url: URL? {
|
||||
return URL.encodeSpacesIfNeeded(rawLink)
|
||||
}
|
||||
@@ -51,6 +47,10 @@ private func accountAndArticlesDictionary(_ articles: Set<Article>) -> [String:
|
||||
return URL.encodeSpacesIfNeeded(rawExternalLink)
|
||||
}
|
||||
|
||||
var preferredURL: URL? {
|
||||
return url ?? externalURL
|
||||
}
|
||||
|
||||
var imageURL: URL? {
|
||||
return URL.encodeSpacesIfNeeded(rawImageLink)
|
||||
}
|
||||
@@ -83,10 +83,6 @@ private func accountAndArticlesDictionary(_ articles: Set<Article>) -> [String:
|
||||
return nil
|
||||
}
|
||||
|
||||
var preferredURL: URL? {
|
||||
return url ?? externalURL
|
||||
}
|
||||
|
||||
var body: String? {
|
||||
return contentHTML ?? contentText ?? summary
|
||||
}
|
||||
@@ -95,6 +91,31 @@ private func accountAndArticlesDictionary(_ articles: Set<Article>) -> [String:
|
||||
return datePublished ?? dateModified ?? status.dateArrived
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@MainActor extension Article {
|
||||
var feed: Feed? {
|
||||
return account?.existingFeed(withFeedID: feedID)
|
||||
}
|
||||
|
||||
func iconImage() -> IconImage? {
|
||||
return IconImageCache.shared.imageForArticle(self)
|
||||
}
|
||||
|
||||
func iconImageUrl(feed: Feed) -> URL? {
|
||||
if let image = iconImage() {
|
||||
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: image.image.dataRepresentation()!, attributes: nil)
|
||||
return path
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
var isAvailableToMarkUnread: Bool {
|
||||
guard let markUnreadWindow = account?.behaviors.compactMap( { behavior -> Int? in
|
||||
switch behavior {
|
||||
@@ -114,23 +135,6 @@ private func accountAndArticlesDictionary(_ articles: Set<Article>) -> [String:
|
||||
}
|
||||
}
|
||||
|
||||
func iconImage() -> IconImage? {
|
||||
return IconImageCache.shared.imageForArticle(self)
|
||||
}
|
||||
|
||||
func iconImageUrl(feed: Feed) -> URL? {
|
||||
if let image = iconImage() {
|
||||
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: image.image.dataRepresentation()!, attributes: nil)
|
||||
return path
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func byline() -> String {
|
||||
guard let authors = authors ?? feed?.authors, !authors.isEmpty else {
|
||||
return ""
|
||||
@@ -176,7 +180,7 @@ private func accountAndArticlesDictionary(_ articles: Set<Article>) -> [String:
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Path
|
||||
// MARK: - Path
|
||||
|
||||
struct ArticlePathKey {
|
||||
static let accountID = "accountID"
|
||||
|
||||
Reference in new issue
Block a user