From e2ea1f9437f07c9f983fea6da2dc4b6f974b01de Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 7 Feb 2023 11:19:19 +0100 Subject: [PATCH] fix device list publication. boomarks add + retract --- .../android/xmpp/manager/AxolotlManager.java | 13 +++++++++- .../android/xmpp/manager/BookmarkManager.java | 26 +++++++++++++++++++ .../android/xmpp/manager/PubSubManager.java | 16 ++++++++++-- .../android/xmpp/model/data/Data.java | 2 +- .../android/xmpp/model/pubsub/Retract.java | 20 ++++++++++++++ 5 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 src/main/java/im/conversations/android/xmpp/model/pubsub/Retract.java diff --git a/src/main/java/im/conversations/android/xmpp/manager/AxolotlManager.java b/src/main/java/im/conversations/android/xmpp/manager/AxolotlManager.java index 383cfd9b7..0d7e8df7d 100644 --- a/src/main/java/im/conversations/android/xmpp/manager/AxolotlManager.java +++ b/src/main/java/im/conversations/android/xmpp/manager/AxolotlManager.java @@ -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.pubsub.Items; import java.util.Collection; +import java.util.Collections; import java.util.Locale; import java.util.Set; import java.util.concurrent.TimeoutException; @@ -194,8 +195,18 @@ public class AxolotlManager extends AbstractManager { private ListenableFuture publishDeviceId() { final var currentDeviceIdsFuture = fetchDeviceIds(getAccount().address); + final ListenableFuture> 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( - currentDeviceIdsFuture, + currentDeviceIdsWithFallback, currentDeviceIds -> { final var myDeviceId = getAccount().getPublicDeviceIdInt(); if (currentDeviceIds.contains(myDeviceId)) { diff --git a/src/main/java/im/conversations/android/xmpp/manager/BookmarkManager.java b/src/main/java/im/conversations/android/xmpp/manager/BookmarkManager.java index e9a059dfb..ffc2cc435 100644 --- a/src/main/java/im/conversations/android/xmpp/manager/BookmarkManager.java +++ b/src/main/java/im/conversations/android/xmpp/manager/BookmarkManager.java @@ -5,9 +5,12 @@ import androidx.annotation.NonNull; import com.google.common.collect.Collections2; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; +import eu.siacs.conversations.xml.Namespace; import eu.siacs.conversations.xmpp.Jid; import im.conversations.android.database.entity.BookmarkEntity; +import im.conversations.android.xmpp.NodeConfiguration; import im.conversations.android.xmpp.XmppConnection; import im.conversations.android.xmpp.model.bookmark.Conference; import im.conversations.android.xmpp.model.pubsub.Items; @@ -71,4 +74,27 @@ public class BookmarkManager extends AbstractManager { updateItems(itemMap); } } + + public ListenableFuture 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 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()); + } } diff --git a/src/main/java/im/conversations/android/xmpp/manager/PubSubManager.java b/src/main/java/im/conversations/android/xmpp/manager/PubSubManager.java index 7fc4f0d00..ccf066678 100644 --- a/src/main/java/im/conversations/android/xmpp/manager/PubSubManager.java +++ b/src/main/java/im/conversations/android/xmpp/manager/PubSubManager.java @@ -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.Publish; 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.event.Event; 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); iq.setTo(address); final var pubSub = iq.addExtension(new PubSub()); - pubSub.addExtension(PublishOptions.of(nodeConfiguration)); final var publish = pubSub.addExtension(new Publish()); publish.setNode(node); final var item = publish.addExtension(new PubSub.Item()); item.setId(itemId); item.addExtension(itemPayload); + pubSub.addExtension(PublishOptions.of(nodeConfiguration)); final ListenableFuture iqFuture = Futures.transform( connection.sendIqPacket(iq), @@ -277,7 +278,6 @@ public class PubSubManager extends AbstractManager { private ListenableFuture setNodeConfiguration( 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); iq.setTo(address); final var pubSub = iq.addExtension(new PubSubOwner()); @@ -293,6 +293,18 @@ public class PubSubManager extends AbstractManager { MoreExecutors.directExecutor()); } + public ListenableFuture 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 implements AsyncFunction { diff --git a/src/main/java/im/conversations/android/xmpp/model/data/Data.java b/src/main/java/im/conversations/android/xmpp/model/data/Data.java index 39102c099..c754ee48d 100644 --- a/src/main/java/im/conversations/android/xmpp/model/data/Data.java +++ b/src/main/java/im/conversations/android/xmpp/model/data/Data.java @@ -61,7 +61,7 @@ public class Data extends Extension { } else if (value instanceof Integer) { valueExtension.setContent(String.valueOf(value)); } else if (value instanceof Boolean) { - valueExtension.setContent(Boolean.TRUE.equals(value) ? "true" : "false"); + valueExtension.setContent(Boolean.TRUE.equals(value) ? "1" : "0"); } else { throw new IllegalArgumentException( String.format( diff --git a/src/main/java/im/conversations/android/xmpp/model/pubsub/Retract.java b/src/main/java/im/conversations/android/xmpp/model/pubsub/Retract.java new file mode 100644 index 000000000..309381197 --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/model/pubsub/Retract.java @@ -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); + } +}