From 9a34e9e870447588c350dca8199a6b07ad32e773 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 6 Nov 2017 19:00:36 +0100 Subject: [PATCH] Add RosterUtil.preApproveSubscriptionIfRequiredAndPossible() --- .../jivesoftware/smack/roster/RosterUtil.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/smack-im/src/main/java/org/jivesoftware/smack/roster/RosterUtil.java b/smack-im/src/main/java/org/jivesoftware/smack/roster/RosterUtil.java index 0d46406e3..03b1c6ad5 100644 --- a/smack-im/src/main/java/org/jivesoftware/smack/roster/RosterUtil.java +++ b/smack-im/src/main/java/org/jivesoftware/smack/roster/RosterUtil.java @@ -23,6 +23,7 @@ import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; +import org.jivesoftware.smack.SmackException.FeatureNotSupportedException; import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.SmackException.NotLoggedInException; import org.jivesoftware.smack.XMPPConnection; @@ -86,6 +87,33 @@ public class RosterUtil { } } + /** + * Pre-approve the subscription if it is required and possible. + * + * @param roster The roster which should be used for the pre-approval. + * @param jid The XMPP address which should be pre-approved. + * @throws NotLoggedInException + * @throws NotConnectedException + * @throws InterruptedException + * @since 4.2.2 + */ + public static void preApproveSubscriptionIfRequiredAndPossible(Roster roster, BareJid jid) + throws NotLoggedInException, NotConnectedException, InterruptedException { + if (!roster.isSubscriptionPreApprovalSupported()) { + return; + } + + RosterEntry entry = roster.getEntry(jid); + if (entry == null || (!entry.canSeeMyPresence() && !entry.isApproved())) { + try { + roster.preApprove(jid); + } catch (FeatureNotSupportedException e) { + // Should never happen since we checked for the feature above. + throw new AssertionError(e); + } + } + } + public static void askForSubscriptionIfRequired(Roster roster, BareJid jid) throws NotLoggedInException, NotConnectedException, InterruptedException { RosterEntry entry = roster.getEntry(jid);