From d85af16e03e4ea9e8c670bae9ed1e0e9a9b10c38 Mon Sep 17 00:00:00 2001 From: CMK Date: Fri, 11 Feb 2022 15:28:39 +0800 Subject: [PATCH] fix: follow push notification deep-link not works issue --- .../Profile/RemoteProfileViewModel.swift | 73 +++++++++---------- .../APIService/APIService+Account.swift | 29 -------- 2 files changed, 34 insertions(+), 68 deletions(-) diff --git a/Mastodon/Scene/Profile/RemoteProfileViewModel.swift b/Mastodon/Scene/Profile/RemoteProfileViewModel.swift index 472af0896..bb565c3e0 100644 --- a/Mastodon/Scene/Profile/RemoteProfileViewModel.swift +++ b/Mastodon/Scene/Profile/RemoteProfileViewModel.swift @@ -56,48 +56,43 @@ final class RemoteProfileViewModel: ProfileViewModel { init(context: AppContext, notificationID: Mastodon.Entity.Notification.ID) { super.init(context: context, optionalMastodonUser: nil) - guard let activeMastodonAuthenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { + guard let authenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { return } - let domain = activeMastodonAuthenticationBox.domain - let authorization = activeMastodonAuthenticationBox.userAuthorization -// context.apiService.notification( -// notificationID: notificationID, -// mastodonAuthenticationBox: activeMastodonAuthenticationBox -// ) -// .compactMap { [weak self] response -> AnyPublisher, Error>? in -// let userID = response.value.account.id -// // TODO: use .account directly -// return context.apiService.accountInfo( -// domain: domain, -// userID: userID, -// authorization: authorization -// ) -// } -// .switchToLatest() -// .retry(3) -// .sink { completion in -// switch completion { -// case .failure(let error): -// // TODO: handle error -// os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: remote notification %s user fetch failed: %s", ((#file as NSString).lastPathComponent), #line, #function, notificationID, error.localizedDescription) -// case .finished: -// os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: remote notification %s user fetched", ((#file as NSString).lastPathComponent), #line, #function, notificationID) -// } -// } 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.mastodonUser.value = mastodonUser -// } -// .store(in: &disposeBag) + Task { @MainActor in + let response = try await context.apiService.notification( + notificationID: notificationID, + authenticationBox: authenticationBox + ) + let userID = response.value.account.id + + let _user: MastodonUser? = try await context.managedObjectContext.perform { + let request = MastodonUser.sortedFetchRequest + request.predicate = MastodonUser.predicate(domain: authenticationBox.domain, id: userID) + request.fetchLimit = 1 + return context.managedObjectContext.safeFetch(request).first + } + + if let user = _user { + self.user = user + } else { + _ = try await context.apiService.accountInfo( + domain: authenticationBox.domain, + userID: userID, + authorization: authenticationBox.userAuthorization + ) + + let _user: MastodonUser? = try await context.managedObjectContext.perform { + let request = MastodonUser.sortedFetchRequest + request.predicate = MastodonUser.predicate(domain: authenticationBox.domain, id: userID) + request.fetchLimit = 1 + return context.managedObjectContext.safeFetch(request).first + } + + self.user = _user + } + } // end Task } } diff --git a/Mastodon/Service/APIService/APIService+Account.swift b/Mastodon/Service/APIService/APIService+Account.swift index 7bd262893..11da2f4ee 100644 --- a/Mastodon/Service/APIService/APIService+Account.swift +++ b/Mastodon/Service/APIService/APIService+Account.swift @@ -43,35 +43,6 @@ extension APIService { } return response -// .flatMap { response -> AnyPublisher, Error> in -// let log = OSLog.api -// let account = response.value -// -// return self.backgroundManagedObjectContext.performChanges { -// let (mastodonUser, isCreated) = APIService.CoreData.createOrMergeMastodonUser( -// into: self.backgroundManagedObjectContext, -// for: nil, -// in: domain, -// entity: account, -// userCache: nil, -// networkDate: response.networkDate, -// log: log -// ) -// let flag = isCreated ? "+" : "-" -// os_log(.info, log: log, "%{public}s[%{public}ld], %{public}s: fetch mastodon user [%s](%s)%s", ((#file as NSString).lastPathComponent), #line, #function, flag, mastodonUser.id, mastodonUser.username) -// } -// .setFailureType(to: Error.self) -// .tryMap { result -> Mastodon.Response.Content in -// switch result { -// case .success: -// return response -// case .failure(let error): -// throw error -// } -// } -// .eraseToAnyPublisher() -// } -// .eraseToAnyPublisher() } }