From a99fd2e5df5c7b3974a55c22dfaece790056ecaf Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Fri, 30 Oct 2026 17:16:21 -0700 Subject: [PATCH] Add ArticleThumbnailFinder.loadSavedThumbnailURLs. --- .../Sources/Images/ArticleThumbnailFinder.swift | 10 ++++++++++ Modules/Images/Sources/Images/ImageDatabase.swift | 8 ++++++++ .../ImagesTests/ArticleThumbnailFinderTests.swift | 12 ++++++++++++ 3 files changed, 30 insertions(+) diff --git a/Modules/Images/Sources/Images/ArticleThumbnailFinder.swift b/Modules/Images/Sources/Images/ArticleThumbnailFinder.swift index 4aafe3652..f8f34b278 100644 --- a/Modules/Images/Sources/Images/ArticleThumbnailFinder.swift +++ b/Modules/Images/Sources/Images/ArticleThumbnailFinder.swift @@ -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
) { + 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
) async { diff --git a/Modules/Images/Sources/Images/ImageDatabase.swift b/Modules/Images/Sources/Images/ImageDatabase.swift index 6233a058f..97c01fd8d 100644 --- a/Modules/Images/Sources/Images/ImageDatabase.swift +++ b/Modules/Images/Sources/Images/ImageDatabase.swift @@ -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) diff --git a/Modules/Images/Tests/ImagesTests/ArticleThumbnailFinderTests.swift b/Modules/Images/Tests/ImagesTests/ArticleThumbnailFinderTests.swift index f120b09d7..e1ccef253 100644 --- a/Modules/Images/Tests/ImagesTests/ArticleThumbnailFinderTests.swift +++ b/Modules/Images/Tests/ImagesTests/ArticleThumbnailFinderTests.swift @@ -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: #"

"#) + 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))