setup stub transformer

This commit is contained in:
Daniel Gultsch 2023-02-08 16:55:56 +01:00
parent 458f0ef280
commit a43160b13d
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
10 changed files with 171 additions and 18 deletions

View file

@ -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<Class<? extends Extension>> 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<Extension> extensions;
public final Collection<DeliveryReceiptRequest> deliveryReceiptRequests;
private Transformation(
final Jid to,
final Jid from,
final Message.Type type,
final String messageId,
final String stanzaId,
final List<Extension> extensions,
final Collection<DeliveryReceiptRequest> 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<Extension> extensionListBuilder = new ImmutableList.Builder<>();
for (final Class<? extends Extension> 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);
}
}

View file

@ -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;
}
}

View file

@ -2,6 +2,7 @@ package im.conversations.android.xmpp.manager;
import android.content.Context; import android.content.Context;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import im.conversations.android.transformer.Transformation;
import im.conversations.android.xmpp.XmppConnection; import im.conversations.android.xmpp.XmppConnection;
import im.conversations.android.xmpp.mam.Result; import im.conversations.android.xmpp.mam.Result;
import im.conversations.android.xmpp.model.stanza.Message; import im.conversations.android.xmpp.model.stanza.Message;
@ -20,13 +21,18 @@ public class ArchiveManager extends AbstractManager {
final var result = message.getExtension(Result.class); final var result = message.getExtension(Result.class);
Preconditions.checkArgument(result != null, "The message needs to contain a MAM result"); Preconditions.checkArgument(result != null, "The message needs to contain a MAM result");
final var from = message.getFrom(); final var from = message.getFrom();
final var stanzaId = result.getId();
final var queryId = result.getQueryId(); final var queryId = result.getQueryId();
final var forwarded = result.getForwarded(); final var forwarded = result.getForwarded();
final var forwardedMessage = forwarded == null ? null : forwarded.getMessage(); 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); LOGGER.info("Received invalid MAM result from {} ", from);
return; 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
} }
} }

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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;

View file

@ -1,7 +1,8 @@
package im.conversations.android.xmpp.processor; package im.conversations.android.xmpp.processor;
import android.content.Context; 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.XmppConnection;
import im.conversations.android.xmpp.mam.Result; import im.conversations.android.xmpp.mam.Result;
import im.conversations.android.xmpp.manager.ArchiveManager; 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.PubSubManager;
import im.conversations.android.xmpp.manager.ReceiptManager; import im.conversations.android.xmpp.manager.ReceiptManager;
import im.conversations.android.xmpp.manager.StanzaIdManager; 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.Received;
import im.conversations.android.xmpp.model.carbons.Sent; import im.conversations.android.xmpp.model.carbons.Sent;
import im.conversations.android.xmpp.model.pubsub.event.Event; import im.conversations.android.xmpp.model.pubsub.event.Event;
@ -59,25 +59,26 @@ public class MessageProcessor extends XmppConnection.Delegate implements Consume
return; return;
} }
final String id = message.getId(); final var from = message.getFrom();
final String stanzaId = getManager(StanzaIdManager.class).getStanzaId(message); final var id = message.getId();
final Jid from = message.getFrom(); final var stanzaId = getManager(StanzaIdManager.class).getStanzaId(message);
final var transformation = Transformation.of(message, stanzaId);
LOGGER.info("Message with stanza-id {} received: {}", stanzaId, message.getExtensionIds()); final boolean sendReceipts;
if (transformation.isAnythingToTransform()) {
// TODO only do this if transformation was successful or nothing to transform final var transformer = new Transformer(context, getAccount());
final var requests = message.getExtensions(DeliveryReceiptRequest.class); sendReceipts = transformer.transform(transformation);
getManager(ReceiptManager.class).received(from, id, requests); } else {
sendReceipts = true;
}
if (sendReceipts) {
getManager(ReceiptManager.class)
.received(from, id, transformation.deliveryReceiptRequests);
}
final var chatState = message.getExtension(ChatStateNotification.class); final var chatState = message.getExtension(ChatStateNotification.class);
if (chatState != null) { if (chatState != null) {
getManager(ChatStateManager.class).handle(from, chatState); 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 // TODO pass JMI to JingleManager
} }