diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+Block.swift b/Mastodon/Protocol/Provider/DataSourceFacade+Block.swift index c8f1f9405..298330219 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+Block.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+Block.swift @@ -52,4 +52,17 @@ extension DataSourceFacade { ) dependency.context.authenticationService.fetchFollowingAndBlockedAsync() } + + static func responseToDomainBlockAction( + dependency: NeedsDependency & AuthContextProvider, + user: ManagedObjectRecord + ) async throws { + let selectionFeedbackGenerator = await UISelectionFeedbackGenerator() + await selectionFeedbackGenerator.selectionChanged() + + let apiService = dependency.context.apiService + let authBox = dependency.authContext.mastodonAuthenticationBox + + _ = try await apiService.toggleDomainBlock(user: user, authenticationBox: authBox) + } } diff --git a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+DomainBlock.swift b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+DomainBlock.swift index 3bfa519c7..6d8c2a6ce 100644 --- a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+DomainBlock.swift +++ b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+DomainBlock.swift @@ -66,7 +66,30 @@ extension APIService { } .eraseToAnyPublisher() } - + + public func toggleDomainBlock( + user: ManagedObjectRecord, + authenticationBox: MastodonAuthenticationBox + ) async throws -> Mastodon.Response.Content { + guard let relationship = 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 managedObjectContext = backgroundManagedObjectContext + guard let user = user.object(in: managedObjectContext) else { throw APIError.implicit(.badRequest) } + + if domainBlocking { + response = try await unblockDomain(user: user, authorizationBox: authenticationBox).singleOutput() + } else { + response = try await blockDomain(user: user, authorizationBox: authenticationBox).singleOutput() + } + + return response + } + func blockDomain( user: MastodonUser, authorizationBox: MastodonAuthenticationBox