From a43160b13de19ecfc995cde9c5dd42fe0269e531 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 8 Feb 2023 16:55:56 +0100 Subject: [PATCH] setup stub transformer --- .../android/transformer/Transformation.java | 64 +++++++++++++++++++ .../android/transformer/Transformer.java | 19 ++++++ .../android/xmpp/manager/ArchiveManager.java | 10 ++- .../android/xmpp/model/axolotl/Encrypted.java | 12 ++++ .../android/xmpp/model/axolotl/Header.java | 12 ++++ .../android/xmpp/model/axolotl/Payload.java | 12 ++++ .../android/xmpp/model/jabber/Body.java | 12 ++++ .../android/xmpp/model/jabber/Thread.java | 10 +++ .../xmpp/model/jabber/package-info.java | 5 ++ .../xmpp/processor/MessageProcessor.java | 33 +++++----- 10 files changed, 171 insertions(+), 18 deletions(-) create mode 100644 src/main/java/im/conversations/android/transformer/Transformation.java create mode 100644 src/main/java/im/conversations/android/transformer/Transformer.java create mode 100644 src/main/java/im/conversations/android/xmpp/model/axolotl/Encrypted.java create mode 100644 src/main/java/im/conversations/android/xmpp/model/axolotl/Header.java create mode 100644 src/main/java/im/conversations/android/xmpp/model/axolotl/Payload.java create mode 100644 src/main/java/im/conversations/android/xmpp/model/jabber/Body.java create mode 100644 src/main/java/im/conversations/android/xmpp/model/jabber/Thread.java create mode 100644 src/main/java/im/conversations/android/xmpp/model/jabber/package-info.java diff --git a/src/main/java/im/conversations/android/transformer/Transformation.java b/src/main/java/im/conversations/android/transformer/Transformation.java new file mode 100644 index 000000000..546214063 --- /dev/null +++ b/src/main/java/im/conversations/android/transformer/Transformation.java @@ -0,0 +1,64 @@ +package im.conversations.android.transformer; + +import com.google.common.collect.ImmutableList; +import eu.siacs.conversations.xmpp.Jid; +import im.conversations.android.xmpp.model.DeliveryReceiptRequest; +import im.conversations.android.xmpp.model.Extension; +import im.conversations.android.xmpp.model.axolotl.Encrypted; +import im.conversations.android.xmpp.model.jabber.Body; +import im.conversations.android.xmpp.model.jabber.Thread; +import im.conversations.android.xmpp.model.stanza.Message; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +public class Transformation { + + private static final List> EXTENSION_FOR_TRANSFORMATION = + Arrays.asList(Body.class, Thread.class, Encrypted.class); + + public final Jid to; + public final Jid from; + public final Message.Type type; + public final String messageId; + public final String stanzaId; + + private final List extensions; + + public final Collection deliveryReceiptRequests; + + private Transformation( + final Jid to, + final Jid from, + final Message.Type type, + final String messageId, + final String stanzaId, + final List extensions, + final Collection deliveryReceiptRequests) { + this.to = to; + this.from = from; + this.type = type; + this.messageId = messageId; + this.stanzaId = stanzaId; + this.extensions = extensions; + this.deliveryReceiptRequests = deliveryReceiptRequests; + } + + public boolean isAnythingToTransform() { + return this.extensions.size() > 0; + } + + public static Transformation of(final Message message, final String stanzaId) { + final var to = message.getTo(); + final var from = message.getFrom(); + final var type = message.getType(); + final var messageId = message.getId(); + final ImmutableList.Builder extensionListBuilder = new ImmutableList.Builder<>(); + for (final Class clazz : EXTENSION_FOR_TRANSFORMATION) { + extensionListBuilder.addAll(message.getExtensions(clazz)); + } + final var requests = message.getExtensions(DeliveryReceiptRequest.class); + return new Transformation( + to, from, type, messageId, stanzaId, extensionListBuilder.build(), requests); + } +} diff --git a/src/main/java/im/conversations/android/transformer/Transformer.java b/src/main/java/im/conversations/android/transformer/Transformer.java new file mode 100644 index 000000000..03bbdeafa --- /dev/null +++ b/src/main/java/im/conversations/android/transformer/Transformer.java @@ -0,0 +1,19 @@ +package im.conversations.android.transformer; + +import android.content.Context; +import im.conversations.android.database.model.Account; + +public class Transformer { + + private final Context context; + private final Account account; + + public Transformer(final Context context, final Account account) { + this.context = context; + this.account = account; + } + + public boolean transform(final Transformation transformation) { + return true; + } +} diff --git a/src/main/java/im/conversations/android/xmpp/manager/ArchiveManager.java b/src/main/java/im/conversations/android/xmpp/manager/ArchiveManager.java index c1cdbbf09..f7541d058 100644 --- a/src/main/java/im/conversations/android/xmpp/manager/ArchiveManager.java +++ b/src/main/java/im/conversations/android/xmpp/manager/ArchiveManager.java @@ -2,6 +2,7 @@ package im.conversations.android.xmpp.manager; import android.content.Context; import com.google.common.base.Preconditions; +import im.conversations.android.transformer.Transformation; import im.conversations.android.xmpp.XmppConnection; import im.conversations.android.xmpp.mam.Result; import im.conversations.android.xmpp.model.stanza.Message; @@ -20,13 +21,18 @@ public class ArchiveManager extends AbstractManager { 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 stanzaId = result.getId(); final var queryId = result.getQueryId(); final var forwarded = result.getForwarded(); final var forwardedMessage = forwarded == null ? null : forwarded.getMessage(); - if (forwardedMessage == null || queryId == null) { + if (forwardedMessage == null || queryId == null || stanzaId == null) { LOGGER.info("Received invalid MAM result from {} ", from); return; } - // TODO + // TODO get query based on queryId and from + + final var transformation = Transformation.of(forwardedMessage, stanzaId); + + // TODO create transformation; add transformation to Query.Transformer } } diff --git a/src/main/java/im/conversations/android/xmpp/model/axolotl/Encrypted.java b/src/main/java/im/conversations/android/xmpp/model/axolotl/Encrypted.java new file mode 100644 index 000000000..3319c6bae --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/model/axolotl/Encrypted.java @@ -0,0 +1,12 @@ +package im.conversations.android.xmpp.model.axolotl; + +import im.conversations.android.annotation.XmlElement; +import im.conversations.android.xmpp.model.Extension; + +@XmlElement +public class Encrypted extends Extension { + + public Encrypted() { + super(Encrypted.class); + } +} diff --git a/src/main/java/im/conversations/android/xmpp/model/axolotl/Header.java b/src/main/java/im/conversations/android/xmpp/model/axolotl/Header.java new file mode 100644 index 000000000..85ea0a363 --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/model/axolotl/Header.java @@ -0,0 +1,12 @@ +package im.conversations.android.xmpp.model.axolotl; + +import im.conversations.android.annotation.XmlElement; +import im.conversations.android.xmpp.model.Extension; + +@XmlElement +public class Header extends Extension { + + public Header() { + super(Header.class); + } +} diff --git a/src/main/java/im/conversations/android/xmpp/model/axolotl/Payload.java b/src/main/java/im/conversations/android/xmpp/model/axolotl/Payload.java new file mode 100644 index 000000000..f8c0324c1 --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/model/axolotl/Payload.java @@ -0,0 +1,12 @@ +package im.conversations.android.xmpp.model.axolotl; + +import im.conversations.android.annotation.XmlElement; +import im.conversations.android.xmpp.model.Extension; + +@XmlElement +public class Payload extends Extension { + + public Payload() { + super(Payload.class); + } +} diff --git a/src/main/java/im/conversations/android/xmpp/model/jabber/Body.java b/src/main/java/im/conversations/android/xmpp/model/jabber/Body.java new file mode 100644 index 000000000..21e872661 --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/model/jabber/Body.java @@ -0,0 +1,12 @@ +package im.conversations.android.xmpp.model.jabber; + +import im.conversations.android.annotation.XmlElement; +import im.conversations.android.xmpp.model.Extension; + +@XmlElement +public class Body extends Extension { + + public Body() { + super(Body.class); + } +} diff --git a/src/main/java/im/conversations/android/xmpp/model/jabber/Thread.java b/src/main/java/im/conversations/android/xmpp/model/jabber/Thread.java new file mode 100644 index 000000000..435f60a8b --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/model/jabber/Thread.java @@ -0,0 +1,10 @@ +package im.conversations.android.xmpp.model.jabber; + +import im.conversations.android.xmpp.model.Extension; + +public class Thread extends Extension { + + public Thread() { + super(Thread.class); + } +} diff --git a/src/main/java/im/conversations/android/xmpp/model/jabber/package-info.java b/src/main/java/im/conversations/android/xmpp/model/jabber/package-info.java new file mode 100644 index 000000000..b05f3f297 --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/model/jabber/package-info.java @@ -0,0 +1,5 @@ +@XmlPackage(namespace = Namespace.JABBER_CLIENT) +package im.conversations.android.xmpp.model.jabber; + +import eu.siacs.conversations.xml.Namespace; +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 398fe6abe..f583af1dd 100644 --- a/src/main/java/im/conversations/android/xmpp/processor/MessageProcessor.java +++ b/src/main/java/im/conversations/android/xmpp/processor/MessageProcessor.java @@ -1,7 +1,8 @@ package im.conversations.android.xmpp.processor; import android.content.Context; -import eu.siacs.conversations.xmpp.Jid; +import im.conversations.android.transformer.Transformation; +import im.conversations.android.transformer.Transformer; import im.conversations.android.xmpp.XmppConnection; import im.conversations.android.xmpp.mam.Result; import im.conversations.android.xmpp.manager.ArchiveManager; @@ -10,7 +11,6 @@ import im.conversations.android.xmpp.manager.ChatStateManager; import im.conversations.android.xmpp.manager.PubSubManager; import im.conversations.android.xmpp.manager.ReceiptManager; import im.conversations.android.xmpp.manager.StanzaIdManager; -import im.conversations.android.xmpp.model.DeliveryReceiptRequest; import im.conversations.android.xmpp.model.carbons.Received; import im.conversations.android.xmpp.model.carbons.Sent; import im.conversations.android.xmpp.model.pubsub.event.Event; @@ -59,25 +59,26 @@ public class MessageProcessor extends XmppConnection.Delegate implements Consume return; } - final String id = message.getId(); - final String stanzaId = getManager(StanzaIdManager.class).getStanzaId(message); - final Jid from = message.getFrom(); - - LOGGER.info("Message with stanza-id {} received: {}", stanzaId, message.getExtensionIds()); - - // TODO only do this if transformation was successful or nothing to transform - final var requests = message.getExtensions(DeliveryReceiptRequest.class); - getManager(ReceiptManager.class).received(from, id, requests); - + final var from = message.getFrom(); + final var id = message.getId(); + final var stanzaId = getManager(StanzaIdManager.class).getStanzaId(message); + final var transformation = Transformation.of(message, stanzaId); + final boolean sendReceipts; + if (transformation.isAnythingToTransform()) { + final var transformer = new Transformer(context, getAccount()); + sendReceipts = transformer.transform(transformation); + } else { + sendReceipts = true; + } + if (sendReceipts) { + getManager(ReceiptManager.class) + .received(from, id, transformation.deliveryReceiptRequests); + } final var chatState = message.getExtension(ChatStateNotification.class); if (chatState != null) { getManager(ChatStateManager.class).handle(from, chatState); } - - // TODO collect Extensions that require transformation (everything that will end up in the - // message tables) - // TODO pass JMI to JingleManager }