From 6ae463e485b237bfa923885131a0e94370887aeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Wo=CC=81jcik?= Date: Fri, 10 Jun 2022 19:08:09 +0200 Subject: [PATCH] Automatically reject presence subscription when the user blocks contact #beagleim-457 --- BeagleIM/contact/ContactDetailsViewController.swift | 11 ++++++++--- BeagleIM/utils/InvitationsManager.swift | 6 ++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/BeagleIM/contact/ContactDetailsViewController.swift b/BeagleIM/contact/ContactDetailsViewController.swift index 483ed85..8d9f946 100644 --- a/BeagleIM/contact/ContactDetailsViewController.swift +++ b/BeagleIM/contact/ContactDetailsViewController.swift @@ -300,19 +300,24 @@ open class ConversationSettingsViewController: NSViewController, ContactDetailsA @objc func blockContactChanged(_ sender: NSButton) { if let chat = self.chat as? Chat, let context = chat.context { + let jid = JID(chat.jid); if sender.state == .on { - context.module(.blockingCommand).block(jids: [JID(chat.jid)], completionHandler: { [weak sender] result in + if DBRosterStore.instance.item(for: chat.account, jid: jid) == nil, let invitation = InvitationManager.instance.invitation(type: .presenceSubscription, account: chat.account, jid: jid) { + InvitationManager.instance.remove(invitation: invitation); + context.module(.presence).unsubscribed(by: jid); + } + context.module(.blockingCommand).block(jids: [jid], completionHandler: { [weak sender] result in DispatchQueue.main.async { switch result { case .failure(_): sender?.state = .off; - default: + case .success(_): break; } } }) } else { - context.module(.blockingCommand).unblock(jids: [JID(chat.jid)], completionHandler: { [weak sender] result in + context.module(.blockingCommand).unblock(jids: [jid], completionHandler: { [weak sender] result in DispatchQueue.main.async { switch result { case .failure(_): diff --git a/BeagleIM/utils/InvitationsManager.swift b/BeagleIM/utils/InvitationsManager.swift index b9bdaf6..970b63b 100644 --- a/BeagleIM/utils/InvitationsManager.swift +++ b/BeagleIM/utils/InvitationsManager.swift @@ -49,6 +49,12 @@ class InvitationManager { return order; } + func invitation(type: InvitationItemType, account: BareJID, jid: JID) -> InvitationItem? { + dispatcher.sync { + return self.volatileItems.first(where: { $0.type == type && $0.account == account && $0.jid == jid }) ?? self.peristentItems.first(where: { $0.type == type && $0.account == account && $0.jid == jid }); + } + } + func addPresenceSubscribe(for account: BareJID, from jid: JID) { dispatcher.async { let invitation = InvitationItem(type: .presenceSubscription, account: account, jid: jid, object: nil, order: self.nextOrder());