From 3f59dd26881c8e3aeffa3ed92198eb14d52bd28d Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 8 Feb 2023 11:44:16 +0100 Subject: [PATCH] add model for MAM result + MAM manager --- .../eu/siacs/conversations/xml/Namespace.java | 1 + .../conversations/android/xmpp/Managers.java | 2 ++ .../android/xmpp/mam/Result.java | 25 +++++++++++++++ .../android/xmpp/mam/package-info.java | 5 +++ .../android/xmpp/manager/ArchiveManager.java | 32 +++++++++++++++++++ .../xmpp/manager/ChatStateManager.java | 8 ++--- .../xmpp/model/state/package-info.java | 2 +- .../xmpp/processor/MessageProcessor.java | 29 +++++++++-------- 8 files changed, 84 insertions(+), 20 deletions(-) create mode 100644 src/main/java/im/conversations/android/xmpp/mam/Result.java create mode 100644 src/main/java/im/conversations/android/xmpp/mam/package-info.java create mode 100644 src/main/java/im/conversations/android/xmpp/manager/ArchiveManager.java diff --git a/src/main/java/eu/siacs/conversations/xml/Namespace.java b/src/main/java/eu/siacs/conversations/xml/Namespace.java index b708c2757..58e1c725c 100644 --- a/src/main/java/eu/siacs/conversations/xml/Namespace.java +++ b/src/main/java/eu/siacs/conversations/xml/Namespace.java @@ -91,6 +91,7 @@ public final class Namespace { public static final String SASL_2 = "urn:xmpp:sasl:2"; public static final String STANZAS = "urn:ietf:params:xml:ns:xmpp-stanzas"; public static final String STANZA_IDS = "urn:xmpp:sid:0"; + public static final String MESSAGE_ARCHIVE_MANAGEMENT = "urn:xmpp:mam:2"; public static final String STREAMS = "http://etherx.jabber.org/streams"; public static final String STREAM_MANAGEMENT = "urn:xmpp:sm:3"; public static final String SYNCHRONIZATION = "im.quicksy.synchronization:0"; diff --git a/src/main/java/im/conversations/android/xmpp/Managers.java b/src/main/java/im/conversations/android/xmpp/Managers.java index 41d7a8a4d..da6a54fde 100644 --- a/src/main/java/im/conversations/android/xmpp/Managers.java +++ b/src/main/java/im/conversations/android/xmpp/Managers.java @@ -4,6 +4,7 @@ import android.content.Context; import com.google.common.collect.ClassToInstanceMap; import com.google.common.collect.ImmutableClassToInstanceMap; import im.conversations.android.xmpp.manager.AbstractManager; +import im.conversations.android.xmpp.manager.ArchiveManager; import im.conversations.android.xmpp.manager.AvatarManager; import im.conversations.android.xmpp.manager.AxolotlManager; import im.conversations.android.xmpp.manager.BlockingManager; @@ -25,6 +26,7 @@ public final class Managers { public static ClassToInstanceMap initialize( final Context context, final XmppConnection connection) { return new ImmutableClassToInstanceMap.Builder() + .put(ArchiveManager.class, new ArchiveManager(context, connection)) .put(AvatarManager.class, new AvatarManager(context, connection)) .put(AxolotlManager.class, new AxolotlManager(context, connection)) .put(BlockingManager.class, new BlockingManager(context, connection)) diff --git a/src/main/java/im/conversations/android/xmpp/mam/Result.java b/src/main/java/im/conversations/android/xmpp/mam/Result.java new file mode 100644 index 000000000..d278b754c --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/mam/Result.java @@ -0,0 +1,25 @@ +package im.conversations.android.xmpp.mam; + +import im.conversations.android.annotation.XmlElement; +import im.conversations.android.xmpp.model.Extension; +import im.conversations.android.xmpp.model.forward.Forwarded; + +@XmlElement +public class Result extends Extension { + + public Result() { + super(Result.class); + } + + public Forwarded getForwarded() { + return this.getExtension(Forwarded.class); + } + + public String getId() { + return this.getAttribute("id"); + } + + public String getQueryId() { + return this.getAttribute("queryid"); + } +} diff --git a/src/main/java/im/conversations/android/xmpp/mam/package-info.java b/src/main/java/im/conversations/android/xmpp/mam/package-info.java new file mode 100644 index 000000000..ce14cb1a0 --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/mam/package-info.java @@ -0,0 +1,5 @@ +@XmlPackage(namespace = Namespace.MESSAGE_ARCHIVE_MANAGEMENT) +package im.conversations.android.xmpp.mam; + +import eu.siacs.conversations.xml.Namespace; +import im.conversations.android.annotation.XmlPackage; diff --git a/src/main/java/im/conversations/android/xmpp/manager/ArchiveManager.java b/src/main/java/im/conversations/android/xmpp/manager/ArchiveManager.java new file mode 100644 index 000000000..c1cdbbf09 --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/manager/ArchiveManager.java @@ -0,0 +1,32 @@ +package im.conversations.android.xmpp.manager; + +import android.content.Context; +import com.google.common.base.Preconditions; +import im.conversations.android.xmpp.XmppConnection; +import im.conversations.android.xmpp.mam.Result; +import im.conversations.android.xmpp.model.stanza.Message; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ArchiveManager extends AbstractManager { + + private static final Logger LOGGER = LoggerFactory.getLogger(ArchiveManager.class); + + public ArchiveManager(Context context, XmppConnection connection) { + super(context, connection); + } + + public void handle(final Message message) { + final var result = message.getExtension(Result.class); + Preconditions.checkArgument(result != null, "The message needs to contain a MAM result"); + final var from = message.getFrom(); + final var queryId = result.getQueryId(); + final var forwarded = result.getForwarded(); + final var forwardedMessage = forwarded == null ? null : forwarded.getMessage(); + if (forwardedMessage == null || queryId == null) { + LOGGER.info("Received invalid MAM result from {} ", from); + return; + } + // TODO + } +} diff --git a/src/main/java/im/conversations/android/xmpp/manager/ChatStateManager.java b/src/main/java/im/conversations/android/xmpp/manager/ChatStateManager.java index aad83570a..7633540f9 100644 --- a/src/main/java/im/conversations/android/xmpp/manager/ChatStateManager.java +++ b/src/main/java/im/conversations/android/xmpp/manager/ChatStateManager.java @@ -1,15 +1,13 @@ package im.conversations.android.xmpp.manager; import android.content.Context; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import eu.siacs.conversations.xmpp.Jid; import im.conversations.android.xmpp.XmppConnection; import im.conversations.android.xmpp.model.state.ChatStateNotification; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -public class ChatStateManager extends AbstractManager{ +public class ChatStateManager extends AbstractManager { private static final Logger LOGGER = LoggerFactory.getLogger(ChatStateManager.class); diff --git a/src/main/java/im/conversations/android/xmpp/model/state/package-info.java b/src/main/java/im/conversations/android/xmpp/model/state/package-info.java index 681ddf7e3..2886400c5 100644 --- a/src/main/java/im/conversations/android/xmpp/model/state/package-info.java +++ b/src/main/java/im/conversations/android/xmpp/model/state/package-info.java @@ -2,4 +2,4 @@ package im.conversations.android.xmpp.model.state; import eu.siacs.conversations.xml.Namespace; -import im.conversations.android.annotation.XmlPackage; \ No newline at end of file +import im.conversations.android.annotation.XmlPackage; diff --git a/src/main/java/im/conversations/android/xmpp/processor/MessageProcessor.java b/src/main/java/im/conversations/android/xmpp/processor/MessageProcessor.java index 373284446..d345cc0b4 100644 --- a/src/main/java/im/conversations/android/xmpp/processor/MessageProcessor.java +++ b/src/main/java/im/conversations/android/xmpp/processor/MessageProcessor.java @@ -3,6 +3,8 @@ package im.conversations.android.xmpp.processor; import android.content.Context; import eu.siacs.conversations.xmpp.Jid; import im.conversations.android.xmpp.XmppConnection; +import im.conversations.android.xmpp.mam.Result; +import im.conversations.android.xmpp.manager.ArchiveManager; import im.conversations.android.xmpp.manager.CarbonsManager; import im.conversations.android.xmpp.manager.ChatStateManager; import im.conversations.android.xmpp.manager.PubSubManager; @@ -13,7 +15,6 @@ import im.conversations.android.xmpp.model.carbons.Sent; import im.conversations.android.xmpp.model.pubsub.event.Event; import im.conversations.android.xmpp.model.stanza.Message; import im.conversations.android.xmpp.model.state.ChatStateNotification; - import java.util.function.Consumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,22 +53,26 @@ public class MessageProcessor extends XmppConnection.Delegate implements Consume return; } + if (isRoot() && message.hasExtension(Result.class)) { + getManager(ArchiveManager.class).handle(message); + return; + } + final String id = message.getId(); final Jid from = message.getFrom(); LOGGER.info("Message received {}", message.getExtensionIds()); // TODO only do this if transformation was successful or nothing to transform - if (isRealtimeProcessor()) { - final var requests = message.getExtensions(DeliveryReceiptRequest.class); - getManager(ReceiptManager.class).received(from, id, requests); - final var chatState = message.getExtension(ChatStateNotification.class); - if (chatState != null) { - getManager(ChatStateManager.class).handle(from, chatState); - } + final var requests = message.getExtensions(DeliveryReceiptRequest.class); + getManager(ReceiptManager.class).received(from, id, requests); + + final var chatState = message.getExtension(ChatStateNotification.class); + if (chatState != null) { + getManager(ChatStateManager.class).handle(from, chatState); } - // TODO parse chat states + // TODO parse and validate stanza-id // TODO collect Extensions that require transformation (everything that will end up in the // message tables) @@ -79,13 +84,9 @@ public class MessageProcessor extends XmppConnection.Delegate implements Consume return this.level == Level.ROOT; } - private boolean isRealtimeProcessor() { - return this.level != Level.ARCHIVE; - } - public enum Level { ROOT, CARBON, - ARCHIVE + STANZA_CONTENT_ENCRYPTION } }