Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions WordPress/Classes/Models/WPAccount+Lookup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public extension WPAccount {
///
static func lookup(withUUIDString uuidString: String, in context: NSManagedObjectContext) throws -> WPAccount? {
let fetchRequest = NSFetchRequest<Self>(entityName: WPAccount.entityName())
fetchRequest.fetchLimit = 1

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimizes this query a bit.

fetchRequest.predicate = NSPredicate(format: "uuid = %@", uuidString)

guard let defaultAccount = try context.fetch(fetchRequest).first else {
Expand All @@ -70,6 +71,7 @@ public extension WPAccount {
///
static func lookup(withUsername username: String, in context: NSManagedObjectContext) throws -> WPAccount? {
let fetchRequest = NSFetchRequest<Self>(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 {
Expand All @@ -88,6 +90,7 @@ public extension WPAccount {
///
static func lookup(withUserID userID: Int64, in context: NSManagedObjectContext) throws -> WPAccount? {
let fetchRequest = NSFetchRequest<Self>(entityName: WPAccount.entityName())
fetchRequest.fetchLimit = 1
fetchRequest.predicate = NSPredicate(format: "userID = %ld", userID)

guard let account = try context.fetch(fetchRequest).first else {
Expand Down
47 changes: 10 additions & 37 deletions WordPress/Classes/Networking/MediaHost+Extensions.swift
Original file line number Diff line number Diff line change
@@ -1,58 +1,31 @@
import Foundation
import AsyncImageKit

/// Extends `MediaRequestAuthenticator.MediaHost` so that we can easily
/// initialize it from a given `AbstractPost`.
///
extension MediaHost {
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)
})
}
}

/// Extends `MediaRequestAuthenticator.MediaHost` so that we can easily
/// initialize it from a given `Blog`.
///
extension MediaHost {
enum BlogError: Swift.Error {
case baseInitializerError(error: Error)
}
// MARK: - MediaHost (AbstractPost)

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(_ post: AbstractPost) {
self.init(post.blog)
}

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

Expand Down
2 changes: 1 addition & 1 deletion WordPress/Classes/Services/MediaHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
Expand Down
4 changes: 2 additions & 2 deletions WordPress/Classes/Services/MediaImageService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion WordPress/WordPressTest/MediaHostTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Testing
import WordPressMedia
@testable import WordPress

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes tests.


struct MediaHostTests {
@Test func initializationWithPublicSite() {
Expand Down