1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-22 18:48:00 +01:00

Merge pull request #344 from vanitasvitae/fixStanzaIDs

XEP-0359: Fix filtering of messages applicable to SIDs.
This commit is contained in:
Florian Schmaus 2019-09-02 13:18:27 +02:00 committed by GitHub
commit a36e74832e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -27,6 +27,7 @@ import org.jivesoftware.smack.XMPPConnectionRegistry;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.NotFilter;
import org.jivesoftware.smack.filter.StanzaExtensionFilter;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.filter.ToTypeFilter;
import org.jivesoftware.smack.packet.Message;
@ -35,6 +36,15 @@ import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.sid.element.OriginIdElement;
/**
* Manager class for Stable and Unique Stanza IDs.
*
* In order to start automatically appending origin ids to outgoing messages, use {@link #enable()}.
* This will announce support via the {@link ServiceDiscoveryManager}. If you want to stop appending origin-ids
* and de-announce support, call {@link #disable()}.
*
* @see <a href="https://xmpp.org/extensions/xep-0359.html">XEP-0359: Stable and Unique Stanza IDs</a>
*/
public final class StableUniqueStanzaIdManager extends Manager {
public static final String NAMESPACE = "urn:xmpp:sid:0";
@ -46,6 +56,9 @@ public final class StableUniqueStanzaIdManager extends Manager {
MessageTypeFilter.NORMAL_OR_CHAT_OR_HEADLINE,
ToTypeFilter.ENTITY_FULL_OR_BARE_JID);
// Filter that filters for messages with an origin id
private static final StanzaFilter ORIGIN_ID_FILTER = new StanzaExtensionFilter(OriginIdElement.ELEMENT, NAMESPACE);
// Listener for outgoing stanzas that adds origin-ids to outgoing stanzas.
private static final StanzaListener ADD_ORIGIN_ID_INTERCEPTOR = new StanzaListener() {
@Override
@ -66,7 +79,7 @@ public final class StableUniqueStanzaIdManager extends Manager {
/**
* Private constructor.
* @param connection TODO javadoc me please
* @param connection XMPP connection
*/
private StableUniqueStanzaIdManager(XMPPConnection connection) {
super(connection);
@ -93,7 +106,8 @@ public final class StableUniqueStanzaIdManager extends Manager {
*/
public synchronized void enable() {
ServiceDiscoveryManager.getInstanceFor(connection()).addFeature(NAMESPACE);
StanzaFilter filter = new AndFilter(OUTGOING_FILTER, new NotFilter(OUTGOING_FILTER));
// We need a filter for outgoing messages that do not carry an origin-id already
StanzaFilter filter = new AndFilter(OUTGOING_FILTER, new NotFilter(ORIGIN_ID_FILTER));
connection().addStanzaInterceptor(ADD_ORIGIN_ID_INTERCEPTOR, filter);
}