fix device list publication. boomarks add + retract

This commit is contained in:
Daniel Gultsch 2023-02-07 11:19:19 +01:00
parent 3be56b6775
commit e2ea1f9437
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
5 changed files with 73 additions and 4 deletions

View file

@ -18,6 +18,7 @@ import im.conversations.android.xmpp.model.axolotl.Bundle;
import im.conversations.android.xmpp.model.axolotl.DeviceList; import im.conversations.android.xmpp.model.axolotl.DeviceList;
import im.conversations.android.xmpp.model.pubsub.Items; import im.conversations.android.xmpp.model.pubsub.Items;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Locale; import java.util.Locale;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
@ -194,8 +195,18 @@ public class AxolotlManager extends AbstractManager {
private ListenableFuture<Void> publishDeviceId() { private ListenableFuture<Void> publishDeviceId() {
final var currentDeviceIdsFuture = fetchDeviceIds(getAccount().address); final var currentDeviceIdsFuture = fetchDeviceIds(getAccount().address);
final ListenableFuture<Set<Integer>> currentDeviceIdsWithFallback =
Futures.catching(
currentDeviceIdsFuture,
Throwable.class,
throwable -> {
LOGGER.info(
"No current device list found. Defaulting to empty", throwable);
return Collections.emptySet();
},
MoreExecutors.directExecutor());
return Futures.transformAsync( return Futures.transformAsync(
currentDeviceIdsFuture, currentDeviceIdsWithFallback,
currentDeviceIds -> { currentDeviceIds -> {
final var myDeviceId = getAccount().getPublicDeviceIdInt(); final var myDeviceId = getAccount().getPublicDeviceIdInt();
if (currentDeviceIds.contains(myDeviceId)) { if (currentDeviceIds.contains(myDeviceId)) {

View file

@ -5,9 +5,12 @@ import androidx.annotation.NonNull;
import com.google.common.collect.Collections2; import com.google.common.collect.Collections2;
import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.MoreExecutors;
import eu.siacs.conversations.xml.Namespace;
import eu.siacs.conversations.xmpp.Jid; import eu.siacs.conversations.xmpp.Jid;
import im.conversations.android.database.entity.BookmarkEntity; import im.conversations.android.database.entity.BookmarkEntity;
import im.conversations.android.xmpp.NodeConfiguration;
import im.conversations.android.xmpp.XmppConnection; import im.conversations.android.xmpp.XmppConnection;
import im.conversations.android.xmpp.model.bookmark.Conference; import im.conversations.android.xmpp.model.bookmark.Conference;
import im.conversations.android.xmpp.model.pubsub.Items; import im.conversations.android.xmpp.model.pubsub.Items;
@ -71,4 +74,27 @@ public class BookmarkManager extends AbstractManager {
updateItems(itemMap); updateItems(itemMap);
} }
} }
public ListenableFuture<Void> publishBookmark(final Jid address) {
final var itemId = address.toEscapedString();
final var conference = new Conference();
return Futures.transform(
getManager(PubSubManager.class)
.publish(
getAccount().address,
conference,
itemId,
NodeConfiguration.WHITELIST_MAX_ITEMS),
result -> null,
MoreExecutors.directExecutor());
}
public ListenableFuture<Void> retractBookmark(final Jid address) {
final var itemId = address.toEscapedString();
return Futures.transform(
getManager(PubSubManager.class)
.retract(getAccount().address, itemId, Namespace.BOOKMARKS2),
result -> null,
MoreExecutors.directExecutor());
}
} }

View file

@ -20,6 +20,7 @@ import im.conversations.android.xmpp.model.pubsub.Items;
import im.conversations.android.xmpp.model.pubsub.PubSub; import im.conversations.android.xmpp.model.pubsub.PubSub;
import im.conversations.android.xmpp.model.pubsub.Publish; import im.conversations.android.xmpp.model.pubsub.Publish;
import im.conversations.android.xmpp.model.pubsub.PublishOptions; import im.conversations.android.xmpp.model.pubsub.PublishOptions;
import im.conversations.android.xmpp.model.pubsub.Retract;
import im.conversations.android.xmpp.model.pubsub.error.PubSubError; import im.conversations.android.xmpp.model.pubsub.error.PubSubError;
import im.conversations.android.xmpp.model.pubsub.event.Event; import im.conversations.android.xmpp.model.pubsub.event.Event;
import im.conversations.android.xmpp.model.pubsub.event.Purge; import im.conversations.android.xmpp.model.pubsub.event.Purge;
@ -232,12 +233,12 @@ public class PubSubManager extends AbstractManager {
final var iq = new Iq(Iq.Type.SET); final var iq = new Iq(Iq.Type.SET);
iq.setTo(address); iq.setTo(address);
final var pubSub = iq.addExtension(new PubSub()); final var pubSub = iq.addExtension(new PubSub());
pubSub.addExtension(PublishOptions.of(nodeConfiguration));
final var publish = pubSub.addExtension(new Publish()); final var publish = pubSub.addExtension(new Publish());
publish.setNode(node); publish.setNode(node);
final var item = publish.addExtension(new PubSub.Item()); final var item = publish.addExtension(new PubSub.Item());
item.setId(itemId); item.setId(itemId);
item.addExtension(itemPayload); item.addExtension(itemPayload);
pubSub.addExtension(PublishOptions.of(nodeConfiguration));
final ListenableFuture<Void> iqFuture = final ListenableFuture<Void> iqFuture =
Futures.transform( Futures.transform(
connection.sendIqPacket(iq), connection.sendIqPacket(iq),
@ -277,7 +278,6 @@ public class PubSubManager extends AbstractManager {
private ListenableFuture<Void> setNodeConfiguration( private ListenableFuture<Void> setNodeConfiguration(
final Jid address, final String node, final Data data) { final Jid address, final String node, final Data data) {
LOGGER.info("Trying to set node configuration to {}", data.toString());
final Iq iq = new Iq(Iq.Type.SET); final Iq iq = new Iq(Iq.Type.SET);
iq.setTo(address); iq.setTo(address);
final var pubSub = iq.addExtension(new PubSubOwner()); final var pubSub = iq.addExtension(new PubSubOwner());
@ -293,6 +293,18 @@ public class PubSubManager extends AbstractManager {
MoreExecutors.directExecutor()); MoreExecutors.directExecutor());
} }
public ListenableFuture<Iq> retract(final Jid address, final String itemId, final String node) {
final var iq = new Iq(Iq.Type.SET);
iq.setTo(address);
final var pubSub = iq.addExtension(new PubSub());
final var retract = pubSub.addExtension(new Retract());
retract.setNode(node);
retract.setNotify(true);
final var item = retract.addExtension(new PubSub.Item());
item.setId(itemId);
return connection.sendIqPacket(iq);
}
private static class PubSubExceptionTransformer<V> private static class PubSubExceptionTransformer<V>
implements AsyncFunction<IqErrorException, V> { implements AsyncFunction<IqErrorException, V> {

View file

@ -61,7 +61,7 @@ public class Data extends Extension {
} else if (value instanceof Integer) { } else if (value instanceof Integer) {
valueExtension.setContent(String.valueOf(value)); valueExtension.setContent(String.valueOf(value));
} else if (value instanceof Boolean) { } else if (value instanceof Boolean) {
valueExtension.setContent(Boolean.TRUE.equals(value) ? "true" : "false"); valueExtension.setContent(Boolean.TRUE.equals(value) ? "1" : "0");
} else { } else {
throw new IllegalArgumentException( throw new IllegalArgumentException(
String.format( String.format(

View file

@ -0,0 +1,20 @@
package im.conversations.android.xmpp.model.pubsub;
import im.conversations.android.annotation.XmlElement;
import im.conversations.android.xmpp.model.Extension;
@XmlElement
public class Retract extends Extension {
public Retract() {
super(Retract.class);
}
public void setNode(String node) {
this.setAttribute("node", node);
}
public void setNotify(boolean notify) {
this.setAttribute("notify", notify ? 1 : 0);
}
}