diff --git a/Mastodon/Service/APIService/APIService+Block.swift b/Mastodon/Service/APIService/APIService+Block.swift index 124b65155..c28db9da9 100644 --- a/Mastodon/Service/APIService/APIService+Block.swift +++ b/Mastodon/Service/APIService/APIService+Block.swift @@ -136,7 +136,8 @@ extension APIService { ) -> AnyPublisher, Error> { let domain = mastodonAuthenticationBox.domain let authorization = mastodonAuthenticationBox.userAuthorization - + let requestMastodonUserID = mastodonAuthenticationBox.userID + return Mastodon.API.Account.block( session: session, domain: domain, @@ -144,11 +145,39 @@ extension APIService { blockQueryType: blockQueryType, authorization: authorization ) + .flatMap { response -> AnyPublisher, Error> in + let managedObjectContext = self.backgroundManagedObjectContext + return managedObjectContext.performChanges { + let requestMastodonUserRequest = MastodonUser.sortedFetchRequest + requestMastodonUserRequest.predicate = MastodonUser.predicate(domain: domain, id: requestMastodonUserID) + requestMastodonUserRequest.fetchLimit = 1 + guard let requestMastodonUser = managedObjectContext.safeFetch(requestMastodonUserRequest).first else { return } + + let lookUpMastodonUserRequest = MastodonUser.sortedFetchRequest + lookUpMastodonUserRequest.predicate = MastodonUser.predicate(domain: domain, id: mastodonUserID) + lookUpMastodonUserRequest.fetchLimit = 1 + let lookUpMastodonUser = managedObjectContext.safeFetch(lookUpMastodonUserRequest).first + + if let lookUpMastodonUser = lookUpMastodonUser { + let entity = response.value + APIService.CoreData.update(user: lookUpMastodonUser, entity: entity, requestMastodonUser: requestMastodonUser, domain: domain, networkDate: response.networkDate) + } + } + .tryMap { result -> Mastodon.Response.Content in + switch result { + case .success: + return response + case .failure(let error): + throw error + } + } + .eraseToAnyPublisher() + } .handleEvents(receiveCompletion: { [weak self] completion in guard let _ = self else { return } switch completion { case .failure(let error): - // TODO: handle error + // TODO: handle error in banner os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: [Relationship] block update fail: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription) case .finished: diff --git a/Mastodon/Service/APIService/APIService+Follow.swift b/Mastodon/Service/APIService/APIService+Follow.swift index 53634ab4b..f527878f4 100644 --- a/Mastodon/Service/APIService/APIService+Follow.swift +++ b/Mastodon/Service/APIService/APIService+Follow.swift @@ -196,11 +196,11 @@ extension APIService { let lookUpMastodonUserRequest = MastodonUser.sortedFetchRequest lookUpMastodonUserRequest.predicate = MastodonUser.predicate(domain: domain, id: mastodonUserID) lookUpMastodonUserRequest.fetchLimit = 1 - let lookUpMastodonuser = managedObjectContext.safeFetch(lookUpMastodonUserRequest).first + let lookUpMastodonUser = managedObjectContext.safeFetch(lookUpMastodonUserRequest).first - if let lookUpMastodonuser = lookUpMastodonuser { + if let lookUpMastodonUser = lookUpMastodonUser { let entity = response.value - APIService.CoreData.update(user: lookUpMastodonuser, entity: entity, requestMastodonUser: requestMastodonUser, domain: domain, networkDate: response.networkDate) + APIService.CoreData.update(user: lookUpMastodonUser, entity: entity, requestMastodonUser: requestMastodonUser, domain: domain, networkDate: response.networkDate) } } .tryMap { result -> Mastodon.Response.Content in