add code to fetch items and their infos in one go

This commit is contained in:
Daniel Gultsch 2023-01-17 13:49:01 +01:00
parent 43a82e504b
commit 199a1cdc64
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
3 changed files with 28 additions and 3 deletions

View file

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

View file

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

View file

@ -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<Void> 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<InfoQuery> info(final Jid entity, final String node) {
@ -86,4 +88,13 @@ public class DiscoManager extends AbstractManager {
},
MoreExecutors.directExecutor());
}
public ListenableFuture<List<InfoQuery>> itemsWithInfo(final Jid entity) {
final var itemsFutures = items(entity);
return Futures.transformAsync(itemsFutures, items -> {
// TODO filter out items with empty jid
Collection<ListenableFuture<InfoQuery>> infoFutures = Collections2.transform(items, i -> info(i.getJid(), i.getNode()));
return Futures.allAsList(infoFutures);
}, MoreExecutors.directExecutor());
}
}