From 199a1cdc64cb16bc79e202be11852e5738154c9c Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 17 Jan 2023 13:49:01 +0100 Subject: [PATCH] add code to fetch items and their infos in one go --- .../android/xmpp/EntityCapabilities.java | 7 +++++++ .../android/xmpp/EntityCapabilities2.java | 7 +++++++ .../android/xmpp/manager/DiscoManager.java | 17 ++++++++++++++--- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/java/im/conversations/android/xmpp/EntityCapabilities.java b/src/main/java/im/conversations/android/xmpp/EntityCapabilities.java index 1f1dbcfba..948f075e0 100644 --- a/src/main/java/im/conversations/android/xmpp/EntityCapabilities.java +++ b/src/main/java/im/conversations/android/xmpp/EntityCapabilities.java @@ -99,6 +99,8 @@ public final class EntityCapabilities { public String encoded() { return BaseEncoding.base64().encode(hash); } + + public abstract String capabilityNode(final String node); } public static class EntityCapsHash extends Hash { @@ -107,6 +109,11 @@ public final class EntityCapabilities { super(hash); } + @Override + public String capabilityNode(String node) { + return String.format("%s#%s", node, encoded()); + } + public static EntityCapsHash of(final String encoded) { return new EntityCapsHash(BaseEncoding.base64().decode(encoded)); } diff --git a/src/main/java/im/conversations/android/xmpp/EntityCapabilities2.java b/src/main/java/im/conversations/android/xmpp/EntityCapabilities2.java index 9942c9c5c..55ab66a94 100644 --- a/src/main/java/im/conversations/android/xmpp/EntityCapabilities2.java +++ b/src/main/java/im/conversations/android/xmpp/EntityCapabilities2.java @@ -8,6 +8,7 @@ import com.google.common.hash.HashFunction; import com.google.common.hash.Hashing; import com.google.common.io.BaseEncoding; import com.google.common.primitives.Bytes; +import eu.siacs.conversations.xml.Namespace; import im.conversations.android.xmpp.model.Hash; import im.conversations.android.xmpp.model.data.Data; import im.conversations.android.xmpp.model.data.Field; @@ -158,5 +159,11 @@ public class EntityCapabilities2 { public static EntityCaps2Hash of(final Hash.Algorithm algorithm, final String encoded) { return new EntityCaps2Hash(algorithm, BaseEncoding.base64().decode(encoded)); } + + @Override + public String capabilityNode(String node) { + return String.format( + "%s#%s.%s", Namespace.ENTITY_CAPABILITIES_2, algorithm.toString(), encoded()); + } } } diff --git a/src/main/java/im/conversations/android/xmpp/manager/DiscoManager.java b/src/main/java/im/conversations/android/xmpp/manager/DiscoManager.java index 9e5bb9ff2..d2e0d43be 100644 --- a/src/main/java/im/conversations/android/xmpp/manager/DiscoManager.java +++ b/src/main/java/im/conversations/android/xmpp/manager/DiscoManager.java @@ -15,6 +15,7 @@ import im.conversations.android.xmpp.model.disco.info.InfoQuery; import im.conversations.android.xmpp.model.disco.items.Item; import im.conversations.android.xmpp.model.disco.items.ItemsQuery; import java.util.Collection; +import java.util.List; import java.util.Objects; public class DiscoManager extends AbstractManager { @@ -29,11 +30,12 @@ public class DiscoManager extends AbstractManager { public ListenableFuture info( final Jid entity, @Nullable final String node, final EntityCapabilities.Hash hash) { - // TODO construct node with appended hash - if (getDatabase().discoDao().set(getAccount(), entity, node, hash)) { + final String capabilityNode = hash.capabilityNode(node); + if (getDatabase().discoDao().set(getAccount(), entity, capabilityNode, hash)) { return Futures.immediateFuture(null); } - return Futures.transform(info(entity, node), f -> null, MoreExecutors.directExecutor()); + return Futures.transform( + info(entity, capabilityNode), f -> null, MoreExecutors.directExecutor()); } public ListenableFuture info(final Jid entity, final String node) { @@ -86,4 +88,13 @@ public class DiscoManager extends AbstractManager { }, MoreExecutors.directExecutor()); } + + public ListenableFuture> itemsWithInfo(final Jid entity) { + final var itemsFutures = items(entity); + return Futures.transformAsync(itemsFutures, items -> { + // TODO filter out items with empty jid + Collection> infoFutures = Collections2.transform(items, i -> info(i.getJid(), i.getNode())); + return Futures.allAsList(infoFutures); + }, MoreExecutors.directExecutor()); + } }