1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-22 20:47:57 +01:00

Rework blocking command code.

Mostly remove the helper utils. The server is required to present the
client with a consisent state of the block list and corresponding
modifications, so we should not end up with duplicate entires if we
don't check for them.

SMACK-731
This commit is contained in:
Florian Schmaus 2016-08-31 08:20:13 +02:00
parent 9554c7be26
commit 98bacb144d
5 changed files with 24 additions and 40 deletions

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2016 Fernando Ramirez
* Copyright 2016 Fernando Ramirez, Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -44,6 +44,7 @@ import org.jxmpp.jid.Jid;
* Blocking command manager class.
*
* @author Fernando Ramirez
* @author Florian Schmaus
* @see <a href="http://xmpp.org/extensions/xep-0191.html">XEP-0191: Blocking
* Command</a>
*/
@ -96,7 +97,7 @@ public final class BlockingCommandManager extends Manager {
}
List<Jid> blockedJids = blockContactIQ.getJids();
addToBlockList(blockedJids);
blockListCached.addAll(blockedJids);
return IQ.createResultIQ(blockContactIQ);
}
@ -117,7 +118,7 @@ public final class BlockingCommandManager extends Manager {
if (unblockedJids == null) { // remove all
blockListCached.clear();
} else { // remove only some
removeFromBlockList(unblockedJids);
blockListCached.removeAll(unblockedJids);
}
return IQ.createResultIQ(unblockContactIQ);
@ -136,33 +137,6 @@ public final class BlockingCommandManager extends Manager {
});
}
private void addToBlockList(List<Jid> blockedJids) {
for (Jid jid : blockedJids) {
if (searchJid(jid) == -1) {
blockListCached.add(jid);
}
}
}
private void removeFromBlockList(List<Jid> unblockedJids) {
for (Jid jid : unblockedJids) {
int position = searchJid(jid);
if (position != -1) {
blockListCached.remove(position);
}
}
}
private int searchJid(Jid jid) {
int i = -1;
for (int j = 0; j < blockListCached.size(); j++) {
if (blockListCached.get(j).equals(jid)) {
i = j;
}
}
return i;
}
/**
* Returns true if Blocking Command is supported by the server.
*
@ -197,8 +171,7 @@ public final class BlockingCommandManager extends Manager {
BlockListIQ blockListIQResult = connection().createPacketCollectorAndSend(blockListIQ).nextResultOrThrow();
blockListCached = blockListIQResult.getJids();
List<Jid> emptyList = Collections.emptyList();
return (blockListCached == null) ? emptyList : Collections.unmodifiableList(blockListCached);
return Collections.unmodifiableList(blockListCached);
}
/**
@ -241,7 +214,7 @@ public final class BlockingCommandManager extends Manager {
*/
public void unblockAll()
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
UnblockContactsIQ unblockContactIQ = new UnblockContactsIQ(null);
UnblockContactsIQ unblockContactIQ = new UnblockContactsIQ();
connection().createPacketCollectorAndSend(unblockContactIQ).nextResultOrThrow();
}

View file

@ -16,6 +16,7 @@
*/
package org.jivesoftware.smackx.blocking.element;
import java.util.Collections;
import java.util.List;
import org.jivesoftware.smack.packet.IQ;
@ -50,6 +51,9 @@ public class BlockListIQ extends IQ {
*/
public BlockListIQ(List<Jid> jids) {
super(ELEMENT, NAMESPACE);
if (jids == null) {
jids = Collections.emptyList();
}
this.jids = jids;
}
@ -71,10 +75,8 @@ public class BlockListIQ extends IQ {
@Override
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
if (jids == null) {
if (jids.isEmpty()) {
xml.setEmptyElement();
} else {
xml.rightAngleBracket();

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2016 Fernando Ramirez
* Copyright 2016 Fernando Ramirez, Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -26,6 +26,7 @@ import org.jxmpp.jid.Jid;
* Unblock contact IQ class.
*
* @author Fernando Ramirez
* @author Florian Schmaus
* @see <a href="http://xmpp.org/extensions/xep-0191.html">XEP-0191: Blocking
* Command</a>
*/
@ -54,6 +55,13 @@ public class UnblockContactsIQ extends IQ {
this.jids = jids;
}
/**
* Constructs a new unblock IQ which will unblock <b>all</b> JIDs.
*/
public UnblockContactsIQ() {
this(null);
}
/**
* Get the JIDs.
*

View file

@ -21,9 +21,9 @@ import java.util.List;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.ParserUtils;
import org.jivesoftware.smackx.blocking.element.BlockListIQ;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.impl.JidCreate;
import org.xmlpull.v1.XmlPullParser;
/**
@ -49,7 +49,8 @@ public class BlockListIQProvider extends IQProvider<BlockListIQ> {
if (jids == null) {
jids = new ArrayList<>();
}
jids.add(JidCreate.from(parser.getAttributeValue("", "jid")));
Jid jid = ParserUtils.getJidAttribute(parser);
jids.add(jid);
}
break;

View file

@ -53,7 +53,7 @@ public class GetBlockingListTest {
IQ iq2 = (IQ) PacketParserUtils.parseStanza(emptyBlockListIQExample);
BlockListIQ emptyBlockListIQ = (BlockListIQ) iq2;
Assert.assertNull(emptyBlockListIQ.getJids());
Assert.assertEquals(0, emptyBlockListIQ.getJids().size());
}
}