Skip to content

Commit

Permalink
Automatically reject presence subscription when the user blocks conta…
Browse files Browse the repository at this point in the history
…ct #beagleim-457
  • Loading branch information
hantu85 committed Jun 10, 2022
1 parent fc862c0 commit 6ae463e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 8 additions & 3 deletions BeagleIM/contact/ContactDetailsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(_):
Expand Down
6 changes: 6 additions & 0 deletions BeagleIM/utils/InvitationsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 6ae463e

Please sign in to comment.