diff --git a/Tusker/Screens/Utilities/Previewing.swift b/Tusker/Screens/Utilities/Previewing.swift index cb94b7172d..cb41de65a4 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)) ]) } }