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].