From d0710460aba3388f6a0f924233376107fa408c94 Mon Sep 17 00:00:00 2001 From: Marcus Kida Date: Fri, 6 Jan 2023 13:14:18 +0100 Subject: [PATCH 1/2] fix(deeplinking): Fix profile resolving didn't use WebFinger so resolving non-local profiles might fail --- .../Profile/RemoteProfileViewModel.swift | 74 +++++++++---------- .../Service/API/APIService+Account.swift | 31 -------- 2 files changed, 37 insertions(+), 68 deletions(-) diff --git a/Mastodon/Scene/Profile/RemoteProfileViewModel.swift b/Mastodon/Scene/Profile/RemoteProfileViewModel.swift index 89ff03660..705d2806f 100644 --- a/Mastodon/Scene/Profile/RemoteProfileViewModel.swift +++ b/Mastodon/Scene/Profile/RemoteProfileViewModel.swift @@ -90,41 +90,41 @@ final class RemoteProfileViewModel: ProfileViewModel { } // end Task } - init(context: AppContext, authContext: AuthContext, acct: String) { - super.init(context: context, authContext: authContext, optionalMastodonUser: nil) - - let domain = authContext.mastodonAuthenticationBox.domain - let authorization = authContext.mastodonAuthenticationBox.userAuthorization - Just(acct) - .asyncMap { acct in - try await context.apiService.accountSearch( - domain: domain, - query: .init(acct: acct), - authorization: authorization - ) - } - .retry(3) - .receive(on: DispatchQueue.main) - .sink { completion in - switch completion { - case .failure(let error): - // TODO: handle error - os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: remote user %s fetch failed: %s", ((#file as NSString).lastPathComponent), #line, #function, acct, error.localizedDescription) - case .finished: - os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: remote user %s fetched", ((#file as NSString).lastPathComponent), #line, #function, acct) - } - } receiveValue: { [weak self] response in - guard let self = self else { return } - let managedObjectContext = context.managedObjectContext - let request = MastodonUser.sortedFetchRequest - request.fetchLimit = 1 - request.predicate = MastodonUser.predicate(domain: domain, id: response.value.id) - guard let mastodonUser = managedObjectContext.safeFetch(request).first else { - assertionFailure() - return - } - self.user = mastodonUser - } - .store(in: &disposeBag) - } + init(context: AppContext, authContext: AuthContext, acct: String){ + super.init(context: context, authContext: authContext, optionalMastodonUser: nil) + + let domain = authContext.mastodonAuthenticationBox.domain + let authenticationBox = authContext.mastodonAuthenticationBox + + Just(acct) + .asyncMap { acct -> Mastodon.Response.Content in + try await context.apiService.search( + query: .init(q: acct, type: .accounts, resolve: true), + authenticationBox: authenticationBox + ).map { $0.accounts.first } + } + .retry(3) + .receive(on: DispatchQueue.main) + .sink { completion in + switch completion { + case .failure(let error): + // TODO: handle error + os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: remote user %s fetch failed: %s", ((#file as NSString).lastPathComponent), #line, #function, acct, error.localizedDescription) + case .finished: + os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: remote user %s fetched", ((#file as NSString).lastPathComponent), #line, #function, acct) + } + } receiveValue: { [weak self] response in + guard let self = self, let value = response.value else { return } + let managedObjectContext = context.managedObjectContext + let request = MastodonUser.sortedFetchRequest + request.fetchLimit = 1 + request.predicate = MastodonUser.predicate(domain: domain, id: value.id) + guard let mastodonUser = managedObjectContext.safeFetch(request).first else { + assertionFailure() + return + } + self.user = mastodonUser + } + .store(in: &disposeBag) + } } diff --git a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Account.swift b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Account.swift index 34d398633..d68984587 100644 --- a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Account.swift +++ b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Account.swift @@ -240,34 +240,3 @@ extension APIService { return result } } - -extension APIService { - public func accountSearch( - domain: String, - query: Mastodon.API.Account.AccountLookupQuery, - authorization: Mastodon.API.OAuth.Authorization - ) async throws -> Mastodon.Response.Content { - let response = try await Mastodon.API.Account.lookupAccount( - session: session, - domain: domain, - query: query, - authorization: authorization - ).singleOutput() - - // user - let managedObjectContext = self.backgroundManagedObjectContext - try await managedObjectContext.performChanges { - _ = Persistence.MastodonUser.createOrMerge( - in: managedObjectContext, - context: Persistence.MastodonUser.PersistContext( - domain: domain, - entity: response.value, - cache: nil, - networkDate: response.networkDate - ) - ) - } - - return response - } -} From 864ec73a9ad7c0741e81ba5ce64eb56e6f755b86 Mon Sep 17 00:00:00 2001 From: woxtu Date: Wed, 11 Jan 2023 17:10:04 +0900 Subject: [PATCH 2/2] Remove Kingfisher (#858) * Replace image type detection * Replace image processing * Remove Kingfisher --- Documentation/Acknowledgments.md | 1 - .../xcshareddata/swiftpm/Package.resolved | 9 --------- MastodonSDK/Package.swift | 2 -- .../Attachment/AttachmentViewModel+Compress.swift | 14 +++++++------- .../Attachment/AttachmentViewModel+Load.swift | 8 +++++--- .../Attachment/AttachmentViewModel+Upload.swift | 1 - .../Attachment/AttachmentViewModel.swift | 1 - .../ComposeContent/View/ComposeContentView.swift | 1 - 8 files changed, 12 insertions(+), 25 deletions(-) diff --git a/Documentation/Acknowledgments.md b/Documentation/Acknowledgments.md index 3e9a2d7b4..a9389bb9f 100644 --- a/Documentation/Acknowledgments.md +++ b/Documentation/Acknowledgments.md @@ -13,7 +13,6 @@ - [Fuzi](https://github.com/cezheng/Fuzi) - [Kanna](https://github.com/tid-kijyun/Kanna) - [KeychainAccess](https://github.com/kishikawakatsumi/KeychainAccess.git) -- [Kingfisher](https://github.com/onevcat/Kingfisher) - [MetaTextKit](https://github.com/TwidereProject/MetaTextKit) - [Nuke-FLAnimatedImage-Plugin](https://github.com/kean/Nuke-FLAnimatedImage-Plugin) - [Nuke](https://github.com/kean/Nuke) diff --git a/Mastodon.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Mastodon.xcworkspace/xcshareddata/swiftpm/Package.resolved index e029f5bed..cd7395c7e 100644 --- a/Mastodon.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Mastodon.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -73,15 +73,6 @@ "version": "4.2.2" } }, - { - "package": "Kingfisher", - "repositoryURL": "https://github.com/onevcat/Kingfisher.git", - "state": { - "branch": null, - "revision": "44e891bdb61426a95e31492a67c7c0dfad1f87c5", - "version": "7.4.1" - } - }, { "package": "MetaTextKit", "repositoryURL": "https://github.com/TwidereProject/MetaTextKit.git", diff --git a/MastodonSDK/Package.swift b/MastodonSDK/Package.swift index 6d87f3d1b..12aadd888 100644 --- a/MastodonSDK/Package.swift +++ b/MastodonSDK/Package.swift @@ -56,7 +56,6 @@ let package = Package( .package(url: "https://github.com/woxtu/UIHostingConfigurationBackport.git", from: "0.1.0"), .package(url: "https://github.com/SDWebImage/SDWebImage.git", from: "5.12.0"), .package(url: "https://github.com/eneko/Stripes.git", from: "0.2.0"), - .package(url: "https://github.com/onevcat/Kingfisher.git", from: "7.4.1"), .package(url: "https://github.com/NextLevel/NextLevelSessionExporter.git", from: "0.4.6"), ], targets: [ @@ -131,7 +130,6 @@ let package = Package( .product(name: "CropViewController", package: "TOCropViewController"), .product(name: "PanModal", package: "PanModal"), .product(name: "Stripes", package: "Stripes"), - .product(name: "Kingfisher", package: "Kingfisher"), .product(name: "NextLevelSessionExporter", package: "NextLevelSessionExporter"), ] ), diff --git a/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel+Compress.swift b/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel+Compress.swift index 417bc773a..059a5bc3e 100644 --- a/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel+Compress.swift +++ b/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel+Compress.swift @@ -10,7 +10,7 @@ import UIKit import AVKit import MastodonCore import SessionExporter -import Kingfisher +import Nuke extension AttachmentViewModel { func compressVideo(url: URL) async throws -> URL { @@ -99,18 +99,18 @@ extension AttachmentViewModel { func compressImage(data: Data, sizeLimit: SizeLimit) throws -> Output { let maxPayloadSizeInBytes = max((sizeLimit.image ?? 10 * 1024 * 1024), 1 * 1024 * 1024) - guard let image = KFCrossPlatformImage(data: data)?.kf.normalized, - var imageData = image.kf.pngRepresentation() + guard let image = UIImage(data: data)?.normalized(), + var imageData = image.pngData() else { throw AttachmentError.invalidAttachmentType } repeat { - guard let image = KFCrossPlatformImage(data: imageData) else { + guard let image = UIImage(data: imageData) else { throw AttachmentError.invalidAttachmentType } - if imageData.kf.imageFormat == .PNG { + if AssetType(imageData) == .png { // A. png image if imageData.count > maxPayloadSizeInBytes { guard let compressedJpegData = image.jpegData(compressionQuality: 0.8) else { @@ -126,7 +126,7 @@ extension AttachmentViewModel { // B. other image if imageData.count > maxPayloadSizeInBytes { let targetSize = CGSize(width: image.size.width * 0.8, height: image.size.height * 0.8) - let scaledImage = image.kf.resize(to: targetSize) + let scaledImage = image.resized(size: targetSize) guard let compressedJpegData = scaledImage.jpegData(compressionQuality: 0.8) else { throw AttachmentError.invalidAttachmentType } @@ -140,7 +140,7 @@ extension AttachmentViewModel { } while (imageData.count > maxPayloadSizeInBytes) - return .image(imageData, imageKind: imageData.kf.imageFormat == .PNG ? .png : .jpg) + return .image(imageData, imageKind: AssetType(imageData) == .png ? .png : .jpg) } } diff --git a/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel+Load.swift b/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel+Load.swift index 7cfd51eb5..02cd60be9 100644 --- a/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel+Load.swift +++ b/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel+Load.swift @@ -9,6 +9,7 @@ import os.log import UIKit import AVKit import UniformTypeIdentifiers +import Nuke extension AttachmentViewModel { @@ -55,7 +56,7 @@ extension AttachmentViewModel { } defer { url.stopAccessingSecurityScopedResource() } let imageData = try Data(contentsOf: url) - return .image(imageData, imageKind: imageData.kf.imageFormat == .PNG ? .png : .jpg) + return .image(imageData, imageKind: AssetType(imageData) == .png ? .png : .jpg) } else if uti.conforms(to: .movie) { guard url.startAccessingSecurityScopedResource() else { throw AttachmentError.invalidAttachmentType @@ -89,11 +90,12 @@ extension AttachmentViewModel { } let imageData = result.data + let assetType = AssetType(imageData) - if imageData.kf.imageFormat == .PNG { + if assetType == .png { return .png } - if imageData.kf.imageFormat == .JPEG { + if assetType == .jpeg { return .jpg } diff --git a/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel+Upload.swift b/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel+Upload.swift index 4f99f0ba1..607c581ed 100644 --- a/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel+Upload.swift +++ b/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel+Upload.swift @@ -7,7 +7,6 @@ import os.log import UIKit -import Kingfisher import UniformTypeIdentifiers import MastodonCore import MastodonSDK diff --git a/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel.swift b/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel.swift index 3ff97769e..03df6426e 100644 --- a/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel.swift +++ b/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel.swift @@ -9,7 +9,6 @@ import os.log import UIKit import Combine import PhotosUI -import Kingfisher import MastodonCore import MastodonLocalization import func QuartzCore.CACurrentMediaTime diff --git a/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/View/ComposeContentView.swift b/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/View/ComposeContentView.swift index a7f867f60..8d06d4c1e 100644 --- a/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/View/ComposeContentView.swift +++ b/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/View/ComposeContentView.swift @@ -11,7 +11,6 @@ import MastodonAsset import MastodonCore import MastodonLocalization import Stripes -import Kingfisher public struct ComposeContentView: View {