From 09fd5845aac69c075da645ee2e9c28b9e35cc3b8 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Wed, 7 Jun 2023 18:54:05 +0200 Subject: [PATCH] feat(core): Re-fix the subscription functions --- packages/moxxmpp/lib/src/presence.dart | 44 +++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/packages/moxxmpp/lib/src/presence.dart b/packages/moxxmpp/lib/src/presence.dart index db68ace..f248601 100644 --- a/packages/moxxmpp/lib/src/presence.dart +++ b/packages/moxxmpp/lib/src/presence.dart @@ -161,13 +161,39 @@ class PresenceManager extends XmppManagerBase { ); } - /// Sends a subscription request to [to]. - // TODO(PapaTutuWawa): Check if we're allowed to pre-approve - Future requestSubscription(JID to, {bool preApprove = false}) async { + /// Similar to [requestSubscription], but it also tells the server to automatically + /// accept a subscription request from [to], should it arrive. + /// This requires a [PresenceNegotiator] to be registered as this feature is optional. + /// + /// Returns true, when the stanza was sent. Returns false, when the stanza was not sent, + /// for example because the server does not support subscription pre-approvals. + Future preApproveSubscription(JID to) async { + final negotiator = getAttributes() + .getNegotiatorById(presenceNegotiator); + assert(negotiator != null, 'No PresenceNegotiator registered'); + + if (!negotiator!.preApprovalSupported) { + return false; + } + await getAttributes().sendStanza( StanzaDetails( Stanza.presence( - type: preApprove ? 'subscribed' : 'subscribe', + type: 'subscribed', + to: to.toString(), + ), + awaitable: false, + ), + ); + return true; + } + + /// Sends a subscription request to [to]. + Future requestSubscription(JID to) async { + await getAttributes().sendStanza( + StanzaDetails( + Stanza.presence( + type: 'subscribe', to: to.toString(), ), awaitable: false, @@ -177,7 +203,15 @@ class PresenceManager extends XmppManagerBase { /// Accept a subscription request from [to]. Future acceptSubscriptionRequest(JID to) async { - await requestSubscription(to, preApprove: true); + await getAttributes().sendStanza( + StanzaDetails( + Stanza.presence( + type: 'subscribed', + to: to.toString(), + ), + awaitable: false, + ), + ); } /// Send a subscription request rejection to [to].