From 330ebaf66afb9d7f1e34082887c04d66729fdcbc Mon Sep 17 00:00:00 2001 From: kean Date: Thu, 2 Jan 2025 18:34:58 -0500 Subject: [PATCH 1/3] Cleanup MediaHost initializers --- .../Networking/MediaHost+Extensions.swift | 46 ++++--------------- WordPress/Classes/Services/MediaHelper.swift | 2 +- .../Classes/Services/MediaImageService.swift | 4 +- .../BlazeCampaignTableViewCell.swift | 4 +- .../Blaze/DashboardBlazeCampaignView.swift | 4 +- .../BlogList/SiteIconViewModel.swift | 2 +- .../RichCommentContentRenderer.swift | 5 +- .../Gutenberg/EditorMediaUtility.swift | 2 +- .../StatsLatestPostSummaryInsightsCell.swift | 4 +- 9 files changed, 18 insertions(+), 55 deletions(-) diff --git a/WordPress/Classes/Networking/MediaHost+Extensions.swift b/WordPress/Classes/Networking/MediaHost+Extensions.swift index 9525bf47677e..7e8cc6f8ebe5 100644 --- a/WordPress/Classes/Networking/MediaHost+Extensions.swift +++ b/WordPress/Classes/Networking/MediaHost+Extensions.swift @@ -1,58 +1,30 @@ import Foundation -import AsyncImageKit -/// Extends `MediaRequestAuthenticator.MediaHost` so that we can easily -/// initialize it from a given `AbstractPost`. -/// extension MediaHost { + // MARK: - MediaHost (AbstractPost) + init(_ post: AbstractPost) { - self.init(with: post.blog, failure: { error in - // We just associate a post with the underlying error for simpler debugging. - WordPressAppDelegate.crashLogging?.logError(error) - }) + self.init(post.blog) } -} - -/// Extends `MediaRequestAuthenticator.MediaHost` so that we can easily -/// initialize it from a given `Blog`. -/// -extension MediaHost { - enum BlogError: Swift.Error { - case baseInitializerError(error: Error) - } - - init(with blog: Blog) { - self.init(with: blog) { error in - // We'll log the error, so we know it's there, but we won't halt execution. - WordPressAppDelegate.crashLogging?.logError(error) - } - } - init(with blog: Blog, failure: (BlogError) -> ()) { - let isAtomic = blog.isAtomic() - self.init(with: blog, isAtomic: isAtomic, failure: failure) - } + // MARK: - MediaHost (Blog) - init(with blog: Blog, isAtomic: Bool, failure: (BlogError) -> ()) { + init(_ blog: Blog) { self.init( isAccessibleThroughWPCom: blog.isAccessibleThroughWPCom(), isPrivate: blog.isPrivate(), - isAtomic: isAtomic, + isAtomic: blog.isAtomic(), siteID: blog.dotComID?.intValue, username: blog.usernameForSite, authToken: blog.authToken, failure: { error in - // We just associate a blog with the underlying error for simpler debugging. - failure(BlogError.baseInitializerError(error: error)) + WordPressAppDelegate.crashLogging?.logError(error) } ) } -} -/// Extends `MediaRequestAuthenticator.MediaHost` so that we can easily -/// initialize it from a given `Blog`. -/// -extension MediaHost { + // MARK: - MediaHost (ReaderPost) + init(_ post: ReaderPost) { let isAccessibleThroughWPCom = post.isWPCom || post.isJetpack diff --git a/WordPress/Classes/Services/MediaHelper.swift b/WordPress/Classes/Services/MediaHelper.swift index be1d0170a007..3285fddab168 100644 --- a/WordPress/Classes/Services/MediaHelper.swift +++ b/WordPress/Classes/Services/MediaHelper.swift @@ -82,7 +82,7 @@ extension Media { return configuration }()) let authenticator = MediaRequestAuthenticator() - let host = MediaHost(with: blog) + let host = MediaHost(blog) let temporaryDirectory = Media.remoteDataTemporaryDirectoryURL var output: [URL] = [] diff --git a/WordPress/Classes/Services/MediaImageService.swift b/WordPress/Classes/Services/MediaImageService.swift index 67768d200317..292ede942201 100644 --- a/WordPress/Classes/Services/MediaImageService.swift +++ b/WordPress/Classes/Services/MediaImageService.swift @@ -110,7 +110,7 @@ final class MediaImageService { } return try? await coreDataStack.performQuery { context in let blog = try context.existingObject(with: media.blogID) - return RemoteImageInfo(imageURL: remoteURL, host: MediaHost(with: blog)) + return RemoteImageInfo(imageURL: remoteURL, host: MediaHost(blog)) } } @@ -266,7 +266,7 @@ final class MediaImageService { return try? await coreDataStack.performQuery { context in let blog = try context.existingObject(with: media.blogID) guard let imageURL = media.getRemoteThumbnailURL(targetSize: targetSize, blog: blog) else { return nil } - return RemoteImageInfo(imageURL: imageURL, host: MediaHost(with: blog)) + return RemoteImageInfo(imageURL: imageURL, host: MediaHost(blog)) } } diff --git a/WordPress/Classes/ViewRelated/Blaze Campaigns/BlazeCampaignTableViewCell.swift b/WordPress/Classes/ViewRelated/Blaze Campaigns/BlazeCampaignTableViewCell.swift index b29d2351bdb2..7f4b06eb16ca 100644 --- a/WordPress/Classes/ViewRelated/Blaze Campaigns/BlazeCampaignTableViewCell.swift +++ b/WordPress/Classes/ViewRelated/Blaze Campaigns/BlazeCampaignTableViewCell.swift @@ -107,9 +107,7 @@ final class BlazeCampaignTableViewCell: UITableViewCell, Reusable { featuredImageView.prepareForReuse() featuredImageView.isHidden = viewModel.imageURL == nil if let imageURL = viewModel.imageURL { - let host = MediaHost(with: blog, failure: { error in - WordPressAppDelegate.crashLogging?.logError(error) - }) + let host = MediaHost(blog) let preferredSize = ImageSize(scaling: CGSize(width: Metrics.featuredImageSize, height: Metrics.featuredImageSize), in: self) featuredImageView.setImage(with: imageURL, host: host, size: preferredSize) } diff --git a/WordPress/Classes/ViewRelated/Blog/Blog Dashboard/Cards/Blaze/DashboardBlazeCampaignView.swift b/WordPress/Classes/ViewRelated/Blog/Blog Dashboard/Cards/Blaze/DashboardBlazeCampaignView.swift index e510fcb26ee6..6ee6f36d99c2 100644 --- a/WordPress/Classes/ViewRelated/Blog/Blog Dashboard/Cards/Blaze/DashboardBlazeCampaignView.swift +++ b/WordPress/Classes/ViewRelated/Blog/Blog Dashboard/Cards/Blaze/DashboardBlazeCampaignView.swift @@ -61,9 +61,7 @@ final class DashboardBlazeCampaignView: UIView { imageView.prepareForReuse() imageView.isHidden = viewModel.imageURL == nil if let imageURL = viewModel.imageURL { - let host = MediaHost(with: blog, failure: { error in - WordPressAppDelegate.crashLogging?.logError(error) - }) + let host = MediaHost(blog) let targetSize = ImageSize(scaling: Constants.imageSize, in: self) imageView.setImage(with: imageURL, host: host, size: targetSize) } diff --git a/WordPress/Classes/ViewRelated/Blog/Site Picker/BlogList/SiteIconViewModel.swift b/WordPress/Classes/ViewRelated/Blog/Site Picker/BlogList/SiteIconViewModel.swift index 903d86bb25bb..8daaa23b1785 100644 --- a/WordPress/Classes/ViewRelated/Blog/Site Picker/BlogList/SiteIconViewModel.swift +++ b/WordPress/Classes/ViewRelated/Blog/Site Picker/BlogList/SiteIconViewModel.swift @@ -34,7 +34,7 @@ struct SiteIconViewModel { if blog.hasIcon, let icon = blog.icon { self.imageURL = SiteIconViewModel.optimizedURL(for: icon, imageSize: size.size, isP2: blog.isAutomatticP2) - self.host = MediaHost(with: blog) + self.host = MediaHost(blog) } } diff --git a/WordPress/Classes/ViewRelated/Comments/Views/Detail/ContentRenderer/RichCommentContentRenderer.swift b/WordPress/Classes/ViewRelated/Comments/Views/Detail/ContentRenderer/RichCommentContentRenderer.swift index 8c8f796324e6..1fb14ad14e7e 100644 --- a/WordPress/Classes/ViewRelated/Comments/Views/Detail/ContentRenderer/RichCommentContentRenderer.swift +++ b/WordPress/Classes/ViewRelated/Comments/Views/Detail/ContentRenderer/RichCommentContentRenderer.swift @@ -74,10 +74,7 @@ private extension RichCommentContentRenderer { var mediaHost: MediaHost { if let blog = comment.blog { - return MediaHost(with: blog, failure: { error in - // We'll log the error, so we know it's there, but we won't halt execution. - WordPressAppDelegate.crashLogging?.logError(error) - }) + return MediaHost(blog) } else if let post = comment.post as? ReaderPost, post.isBlogPrivate { return MediaHost(post) } diff --git a/WordPress/Classes/ViewRelated/Gutenberg/EditorMediaUtility.swift b/WordPress/Classes/ViewRelated/Gutenberg/EditorMediaUtility.swift index dca6a14bbac6..db8140d79a04 100644 --- a/WordPress/Classes/ViewRelated/Gutenberg/EditorMediaUtility.swift +++ b/WordPress/Classes/ViewRelated/Gutenberg/EditorMediaUtility.swift @@ -150,7 +150,7 @@ class EditorMediaUtility { requestURL = PhotonImageURLHelper.photonURL(with: size, forImageURL: url) } - return (requestURL, MediaHost(with: post.blog)) + return (requestURL, MediaHost(post.blog)) } static func fetchRemoteVideoURL(for media: Media, in post: AbstractPost, withToken: Bool = false, completion: @escaping ( Result<(URL), Error> ) -> Void) { diff --git a/WordPress/Classes/ViewRelated/Stats/Insights/StatsLatestPostSummaryInsightsCell.swift b/WordPress/Classes/ViewRelated/Stats/Insights/StatsLatestPostSummaryInsightsCell.swift index 7d7f1e0c1fd8..1d60c04610df 100644 --- a/WordPress/Classes/ViewRelated/Stats/Insights/StatsLatestPostSummaryInsightsCell.swift +++ b/WordPress/Classes/ViewRelated/Stats/Insights/StatsLatestPostSummaryInsightsCell.swift @@ -231,9 +231,7 @@ class StatsLatestPostSummaryInsightsCell: StatsBaseCell, LatestPostSummaryConfig let blog = try? Blog.lookup(withID: siteID, in: ContextManager.shared.mainContext) { postImageView.isHidden = false - let host = MediaHost(with: blog, failure: { error in - DDLogError("Failed to create media host: \(error.localizedDescription)") - }) + let host = MediaHost(blog) let targetSize = CGSize(width: Metrics.thumbnailSize, height: Metrics.thumbnailSize) postImageView.setImage(with: url, host: host, size: ImageSize(scaling: targetSize, in: self)) } else { From 58ec973d8d46877445853f06a5b1ca28db04e055 Mon Sep 17 00:00:00 2001 From: kean Date: Thu, 2 Jan 2025 18:35:48 -0500 Subject: [PATCH 2/3] Optimize account lookup --- WordPress/Classes/Models/WPAccount+Lookup.swift | 3 +++ WordPress/Classes/Networking/MediaHost+Extensions.swift | 1 + 2 files changed, 4 insertions(+) diff --git a/WordPress/Classes/Models/WPAccount+Lookup.swift b/WordPress/Classes/Models/WPAccount+Lookup.swift index 1ba1629f8e73..f64cf6832acd 100644 --- a/WordPress/Classes/Models/WPAccount+Lookup.swift +++ b/WordPress/Classes/Models/WPAccount+Lookup.swift @@ -47,6 +47,7 @@ public extension WPAccount { /// static func lookup(withUUIDString uuidString: String, in context: NSManagedObjectContext) throws -> WPAccount? { let fetchRequest = NSFetchRequest(entityName: WPAccount.entityName()) + fetchRequest.fetchLimit = 1 fetchRequest.predicate = NSPredicate(format: "uuid = %@", uuidString) guard let defaultAccount = try context.fetch(fetchRequest).first else { @@ -70,6 +71,7 @@ public extension WPAccount { /// static func lookup(withUsername username: String, in context: NSManagedObjectContext) throws -> WPAccount? { let fetchRequest = NSFetchRequest(entityName: WPAccount.entityName()) + fetchRequest.fetchLimit = 1 fetchRequest.predicate = NSPredicate(format: "username = [c] %@ || email = [c] %@", username, username) guard let account = try context.fetch(fetchRequest).first else { @@ -88,6 +90,7 @@ public extension WPAccount { /// static func lookup(withUserID userID: Int64, in context: NSManagedObjectContext) throws -> WPAccount? { let fetchRequest = NSFetchRequest(entityName: WPAccount.entityName()) + fetchRequest.fetchLimit = 1 fetchRequest.predicate = NSPredicate(format: "userID = %ld", userID) guard let account = try context.fetch(fetchRequest).first else { diff --git a/WordPress/Classes/Networking/MediaHost+Extensions.swift b/WordPress/Classes/Networking/MediaHost+Extensions.swift index 7e8cc6f8ebe5..197fdf34d15d 100644 --- a/WordPress/Classes/Networking/MediaHost+Extensions.swift +++ b/WordPress/Classes/Networking/MediaHost+Extensions.swift @@ -1,6 +1,7 @@ import Foundation extension MediaHost { + // MARK: - MediaHost (AbstractPost) init(_ post: AbstractPost) { From ebbeb8b36c79c71a1ac66a53a561f9abf4a1746e Mon Sep 17 00:00:00 2001 From: kean Date: Thu, 2 Jan 2025 18:47:46 -0500 Subject: [PATCH 3/3] Fix MediaHostTests --- WordPress/WordPressTest/MediaHostTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPress/WordPressTest/MediaHostTests.swift b/WordPress/WordPressTest/MediaHostTests.swift index 67c0b8a3a51d..bc8086638133 100644 --- a/WordPress/WordPressTest/MediaHostTests.swift +++ b/WordPress/WordPressTest/MediaHostTests.swift @@ -1,5 +1,5 @@ import Testing -import WordPressMedia +@testable import WordPress struct MediaHostTests { @Test func initializationWithPublicSite() {