add code to fetch items and their infos in one go
This commit is contained in:
parent
43a82e504b
commit
199a1cdc64
|
@ -99,6 +99,8 @@ public final class EntityCapabilities {
|
||||||
public String encoded() {
|
public String encoded() {
|
||||||
return BaseEncoding.base64().encode(hash);
|
return BaseEncoding.base64().encode(hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract String capabilityNode(final String node);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class EntityCapsHash extends Hash {
|
public static class EntityCapsHash extends Hash {
|
||||||
|
@ -107,6 +109,11 @@ public final class EntityCapabilities {
|
||||||
super(hash);
|
super(hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String capabilityNode(String node) {
|
||||||
|
return String.format("%s#%s", node, encoded());
|
||||||
|
}
|
||||||
|
|
||||||
public static EntityCapsHash of(final String encoded) {
|
public static EntityCapsHash of(final String encoded) {
|
||||||
return new EntityCapsHash(BaseEncoding.base64().decode(encoded));
|
return new EntityCapsHash(BaseEncoding.base64().decode(encoded));
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import com.google.common.hash.HashFunction;
|
||||||
import com.google.common.hash.Hashing;
|
import com.google.common.hash.Hashing;
|
||||||
import com.google.common.io.BaseEncoding;
|
import com.google.common.io.BaseEncoding;
|
||||||
import com.google.common.primitives.Bytes;
|
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.Hash;
|
||||||
import im.conversations.android.xmpp.model.data.Data;
|
import im.conversations.android.xmpp.model.data.Data;
|
||||||
import im.conversations.android.xmpp.model.data.Field;
|
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) {
|
public static EntityCaps2Hash of(final Hash.Algorithm algorithm, final String encoded) {
|
||||||
return new EntityCaps2Hash(algorithm, BaseEncoding.base64().decode(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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.Item;
|
||||||
import im.conversations.android.xmpp.model.disco.items.ItemsQuery;
|
import im.conversations.android.xmpp.model.disco.items.ItemsQuery;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class DiscoManager extends AbstractManager {
|
public class DiscoManager extends AbstractManager {
|
||||||
|
@ -29,11 +30,12 @@ public class DiscoManager extends AbstractManager {
|
||||||
|
|
||||||
public ListenableFuture<Void> info(
|
public ListenableFuture<Void> info(
|
||||||
final Jid entity, @Nullable final String node, final EntityCapabilities.Hash hash) {
|
final Jid entity, @Nullable final String node, final EntityCapabilities.Hash hash) {
|
||||||
// TODO construct node with appended hash
|
final String capabilityNode = hash.capabilityNode(node);
|
||||||
if (getDatabase().discoDao().set(getAccount(), entity, node, hash)) {
|
if (getDatabase().discoDao().set(getAccount(), entity, capabilityNode, hash)) {
|
||||||
return Futures.immediateFuture(null);
|
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) {
|
public ListenableFuture<InfoQuery> info(final Jid entity, final String node) {
|
||||||
|
@ -86,4 +88,13 @@ public class DiscoManager extends AbstractManager {
|
||||||
},
|
},
|
||||||
MoreExecutors.directExecutor());
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue