From f1511039efb7d3fd6933b6504f25c64169cb7ba9 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 11 Nov 2022 22:44:58 -0500 Subject: [PATCH] Add domain block action to profiles --- Tusker/Screens/Utilities/Previewing.swift | 56 +++++++++++++++-------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/Tusker/Screens/Utilities/Previewing.swift b/Tusker/Screens/Utilities/Previewing.swift index cb94b7172..cb41de65a 100644 --- a/Tusker/Screens/Utilities/Previewing.swift +++ b/Tusker/Screens/Utilities/Previewing.swift @@ -394,29 +394,49 @@ extension MenuActionProvider { @MainActor private func blockAction(for relationship: RelationshipMO, mastodonController: MastodonController) -> UIMenuElement { let accountID = relationship.accountID - let blocking = relationship.blocking - let handler = { (_: UIAction) in - let request = (blocking ? Account.unblock : Account.block)(accountID) - mastodonController.run(request) { response in - switch response { - case .failure(let error): - if let toastable = self.toastableViewController { - let config = ToastConfiguration(from: error, with: "Error \(blocking ? "Unb" : "B")locking", in: toastable, retryAction: nil) - DispatchQueue.main.async { - toastable.showToast(configuration: config, animated: true) - } - } - case .success(let relationship, _): - mastodonController.persistentContainer.addOrUpdate(relationship: relationship) + let displayName = relationship.account!.displayOrUserName + let host = relationship.account!.url.host! + let handleFailure = { (isActionBlock: Bool, error: Client.Error) in + if let toastable = self.toastableViewController { + let config = ToastConfiguration(from: error, with: "Error \(isActionBlock ? "B" : "Unb")locking", in: toastable, retryAction: nil) + DispatchQueue.main.async { + toastable.showToast(configuration: config, animated: true) } } } - if blocking { - return createAction(identifier: "block", title: "Unblock", systemImageName: "circle.slash", handler: handler) + let handler = { (block: Bool) in + return { (_: UIAction) in + let req = block ? Account.block(accountID) : Account.unblock(accountID) + _ = mastodonController.run(req) { response in + switch response { + case .failure(let error): + handleFailure(block, error) + case .success(let relationship, _): + mastodonController.persistentContainer.addOrUpdate(relationship: relationship) + } + } + } + } + let domainHandler = { (block: Bool) in + return { (_: UIAction) in + let req = block ? Client.block(domain: host) : Client.unblock(domain: host) + mastodonController.run(req) { response in + if case .failure(let error) = response { + handleFailure(block, error) + } + } + } + } + if relationship.domainBlocking { + return createAction(identifier: "block", title: "Unblock \(host)", systemImageName: "circle.slash", handler: domainHandler(false)) + } else if relationship.blocking { + return createAction(identifier: "block", title: "Unblock \(displayName)", systemImageName: "circle.slash", handler: handler(false)) } else { - return UIMenu(title: "Block", image: UIImage(systemName: "circle.slash"), children: [ + let image = UIImage(systemName: "circle.slash") + return UIMenu(title: "Block", image: image, children: [ UIAction(title: "Cancel", handler: { _ in }), - UIAction(title: "Block \(relationship.account!.displayOrUserName)", image: UIImage(systemName: "circle.slash"), attributes: .destructive, handler: handler), + UIAction(title: "Block \(displayName)", image: image, attributes: .destructive, handler: handler(true)), + UIAction(title: "Block \(host)", image: image, attributes: .destructive, handler: domainHandler(true)) ]) } }