Add domain block action to profiles

This commit is contained in:
Shadowfacts 2022-11-11 22:44:58 -05:00
parent 5c479e3bf0
commit f1511039ef
1 changed files with 38 additions and 18 deletions

View File

@ -394,29 +394,49 @@ extension MenuActionProvider {
@MainActor @MainActor
private func blockAction(for relationship: RelationshipMO, mastodonController: MastodonController) -> UIMenuElement { private func blockAction(for relationship: RelationshipMO, mastodonController: MastodonController) -> UIMenuElement {
let accountID = relationship.accountID let accountID = relationship.accountID
let blocking = relationship.blocking let displayName = relationship.account!.displayOrUserName
let handler = { (_: UIAction) in let host = relationship.account!.url.host!
let request = (blocking ? Account.unblock : Account.block)(accountID) let handleFailure = { (isActionBlock: Bool, error: Client.Error) in
mastodonController.run(request) { response in if let toastable = self.toastableViewController {
switch response { let config = ToastConfiguration(from: error, with: "Error \(isActionBlock ? "B" : "Unb")locking", in: toastable, retryAction: nil)
case .failure(let error): DispatchQueue.main.async {
if let toastable = self.toastableViewController { toastable.showToast(configuration: config, animated: true)
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)
} }
} }
} }
if blocking { let handler = { (block: Bool) in
return createAction(identifier: "block", title: "Unblock", systemImageName: "circle.slash", handler: handler) 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 { } 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: "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))
]) ])
} }
} }