1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-09-09 18:29:45 +02:00

Add support for IoT Friend approvals

This adds supports for an experimental protocol flow where a pending
friend request's decission is later on deliverd to the requestor after
the owner made its decission.
This commit is contained in:
Florian Schmaus 2016-10-31 12:24:05 +01:00
parent 5a2326a856
commit 1d3c48e6ce
11 changed files with 407 additions and 56 deletions

View file

@ -681,6 +681,7 @@ public final class Roster extends Manager {
Objects.requireNonNull(subscribeListener, "SubscribeListener argument must not be null");
if (subscriptionMode != SubscriptionMode.manual) {
previousSubscriptionMode = subscriptionMode;
subscriptionMode = SubscriptionMode.manual;
}
return subscribeListeners.add(subscribeListener);
}
@ -1228,6 +1229,7 @@ public final class Roster extends Manager {
RosterPacket.Item oldItem = RosterEntry.toRosterItem(oldEntry);
if (!oldEntry.equalsDeep(entry) || !item.getGroupNames().equals(oldItem.getGroupNames())) {
updatedEntries.add(item.getJid());
oldEntry.updateItem(item);
} else {
// Record the entry as unchanged, so that it doesn't end up as deleted entry
unchangedEntries.add(item.getJid());

View file

@ -28,6 +28,8 @@ import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;
import org.jivesoftware.smack.roster.packet.RosterPacket;
import org.jxmpp.jid.BareJid;
@ -41,7 +43,7 @@ import org.jxmpp.jid.BareJid;
*/
public final class RosterEntry extends Manager {
private final RosterPacket.Item item;
private RosterPacket.Item item;
final private Roster roster;
/**
@ -120,10 +122,9 @@ public final class RosterEntry extends Manager {
* @param type the subscription type.
* @param subscriptionPending TODO
*/
void updateState(String name, RosterPacket.ItemType type, boolean subscriptionPending) {
item.setName(name);
item.setItemType(type);
item.setSubscriptionPending(subscriptionPending);
void updateItem(RosterPacket.Item item) {
assert(item != null);
this.item = item;
}
/**
@ -209,6 +210,18 @@ public final class RosterEntry extends Manager {
}
}
/**
* Cancel the presence subscription the XMPP entity representing this roster entry has with us.
*
* @throws NotConnectedException
* @throws InterruptedException
* @since 4.2
*/
public void cancelSubscription() throws NotConnectedException, InterruptedException {
Presence unsubscribed = new Presence(item.getJid(), Type.unsubscribed);
connection().sendStanza(unsubscribed);
}
public String toString() {
StringBuilder buf = new StringBuilder();
if (getName() != null) {

View file

@ -25,6 +25,7 @@ import java.util.concurrent.locks.ReentrantLock;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.SmackException.NotLoggedInException;
import org.jivesoftware.smack.XMPPConnection;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.Jid;
@ -90,4 +91,24 @@ public class RosterUtil {
roster.sendSubscriptionRequest(jid);
}
}
public static void ensureNotSubscribedToEachOther(XMPPConnection connectionOne, XMPPConnection connectionTwo)
throws NotConnectedException, InterruptedException {
final Roster rosterOne = Roster.getInstanceFor(connectionOne);
final BareJid jidOne = connectionOne.getUser().asBareJid();
final Roster rosterTwo = Roster.getInstanceFor(connectionTwo);
final BareJid jidTwo = connectionTwo.getUser().asBareJid();
ensureNotSubscribed(rosterOne, jidTwo);
ensureNotSubscribed(rosterTwo, jidOne);
}
public static void ensureNotSubscribed(Roster roster, BareJid jid)
throws NotConnectedException, InterruptedException {
RosterEntry entry = roster.getEntry(jid);
if (entry != null && entry.canSeeMyPresence()) {
entry.cancelSubscription();
}
}
}

View file

@ -108,6 +108,7 @@ public class RosterPacket extends IQ {
* A roster item, which consists of a JID, their name, the type of subscription, and
* the groups the roster item belongs to.
*/
// TODO Make this class immutable.
public static class Item implements NamedElement {
/**
@ -124,6 +125,7 @@ public class RosterPacket extends IQ {
*/
private boolean subscriptionPending;
// TODO Make immutable.
private String name;
private ItemType itemType = ItemType.none;
private boolean approved;