mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-30 17:29:23 +00:00
Add ArticleThumbnailFinder.loadSavedThumbnailURLs.
This commit is contained in:
1 parent
3d9fe5da3c
commit
a99fd2e5df
3 files changed
+30
No files matched your search
@@ -140,6 +140,16 @@ extension Notification.Name {
|
||||
return thumbnailURL
|
||||
}
|
||||
|
||||
/// Loads HTML thumbnail URLs saved in the database, without waiting for `discoverThumbnailURLs(for:)`,
|
||||
/// so that rows laid out next can use them. Blocks until the database has answered, and posts no notification.
|
||||
public func loadSavedThumbnailURLs(for articles: some Collection<Article>) {
|
||||
let articleIDs = articles.filter(needsHTMLDiscovery).compactMap(ArticleHTML.init).map(\.articleID)
|
||||
guard !articleIDs.isEmpty else {
|
||||
return
|
||||
}
|
||||
cacheRecords(imageDatabase.fetchArticleThumbnailsSync(articleIDs: articleIDs))
|
||||
}
|
||||
|
||||
/// Looks for thumbnails in the HTML of articles that have no usable provided image.
|
||||
/// Uses results saved in the database, parses the rest off the main thread, and saves those results.
|
||||
public func discoverThumbnailURLs(for articles: some Collection<Article>) async {
|
||||
|
||||
@@ -136,6 +136,14 @@ import RSParser
|
||||
}
|
||||
}
|
||||
|
||||
func fetchArticleThumbnailsSync(articleIDs: [String]) -> [ArticleThumbnailRecord] {
|
||||
nonisolated(unsafe) var records = [ArticleThumbnailRecord]()
|
||||
queue.runInDatabaseSync { database in
|
||||
records = ArticleThumbnailTable.fetch(articleIDs: articleIDs, database: database)
|
||||
}
|
||||
return records
|
||||
}
|
||||
|
||||
func saveArticleThumbnails(_ records: [ArticleThumbnailRecord]) {
|
||||
queue.runInTransaction { database in
|
||||
ArticleThumbnailTable.save(records, database: database)
|
||||
|
||||
@@ -75,6 +75,18 @@ private typealias ArticleAttachment = Articles.Attachment
|
||||
#expect(finder.thumbnailURL(for: articleWithImage, mode: .small) == "https://example.com/uploads/second-photo.jpg")
|
||||
}
|
||||
|
||||
@Test func savedHTMLThumbnailIsLoadedWithoutDiscovery() async {
|
||||
let imageDatabase = ImageDatabase(databasePath: ":memory:")
|
||||
let article = makeArticle(contentHTML: #"<p><img src="/uploads/photo.jpg" width="640" height="480"></p>"#)
|
||||
await ArticleThumbnailFinder(imageDatabase: imageDatabase).discoverThumbnailURLs(for: [article])
|
||||
|
||||
let finderAfterRelaunch = ArticleThumbnailFinder(imageDatabase: imageDatabase)
|
||||
#expect(finderAfterRelaunch.thumbnailURL(for: article, mode: .large) == nil)
|
||||
|
||||
finderAfterRelaunch.loadSavedThumbnailURLs(for: [article])
|
||||
#expect(finderAfterRelaunch.thumbnailURL(for: article, mode: .large) == "https://example.com/uploads/photo.jpg")
|
||||
}
|
||||
|
||||
@Test func thumbnailTooSmallIsRejected() async throws {
|
||||
let smallImageData = try #require(jpegData(width: 48, height: 48))
|
||||
|
||||
|
||||
Reference in new issue
Block a user