From 30314cdd347a47d43b9b31a1906ecec47c35f579 Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Mon, 18 Dec 2023 20:20:32 +0100 Subject: [PATCH] Update relationship (IOS-5) --- .../Service/API/APIService+DomainBlock.swift | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+DomainBlock.swift b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+DomainBlock.swift index 6d8c2a6ce..d50eaa099 100644 --- a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+DomainBlock.swift +++ b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+DomainBlock.swift @@ -71,20 +71,43 @@ extension APIService { user: ManagedObjectRecord, authenticationBox: MastodonAuthenticationBox ) async throws -> Mastodon.Response.Content { - guard let relationship = try await relationship(records: [user], authenticationBox: authenticationBox).value.first else { + guard let originalRelationship = try await relationship(records: [user], authenticationBox: authenticationBox).value.first else { throw APIError.implicit(.badRequest) } let response: Mastodon.Response.Content - let domainBlocking = relationship.domainBlocking ?? false + let domainBlocking = originalRelationship.domainBlocking ?? false let managedObjectContext = backgroundManagedObjectContext - guard let user = user.object(in: managedObjectContext) else { throw APIError.implicit(.badRequest) } + + guard let _user = user.object(in: managedObjectContext) else { throw APIError.implicit(.badRequest) } if domainBlocking { - response = try await unblockDomain(user: user, authorizationBox: authenticationBox).singleOutput() + response = try await unblockDomain(user: _user, authorizationBox: authenticationBox).singleOutput() } else { - response = try await blockDomain(user: user, authorizationBox: authenticationBox).singleOutput() + response = try await blockDomain(user: _user, authorizationBox: authenticationBox).singleOutput() + } + + guard let relationship = try await self.relationship(records: [user], authenticationBox: authenticationBox).value.first else { + throw APIError.implicit(.badRequest) + } + + try await managedObjectContext.performChanges { + let authentication = authenticationBox.authentication + + guard + let user = user.object(in: managedObjectContext), + let me = authentication.user(in: managedObjectContext) + else { return } + + Persistence.MastodonUser.update( + mastodonUser: user, + context: Persistence.MastodonUser.RelationshipContext( + entity: relationship, + me: me, + networkDate: response.networkDate + ) + ) } return response