From 870393df8e5eba6092b3883fe57728b0a7192863 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 7 Feb 2023 12:31:51 +0100 Subject: [PATCH] introduce 'PepManager' to unify what pubsub service we talk to --- .../conversations/android/xmpp/Managers.java | 2 + .../android/xmpp/XmppConnection.java | 4 ++ .../android/xmpp/manager/AxolotlManager.java | 20 ++++---- .../android/xmpp/manager/BookmarkManager.java | 14 ++---- .../android/xmpp/manager/PepManager.java | 48 +++++++++++++++++++ 5 files changed, 69 insertions(+), 19 deletions(-) create mode 100644 src/main/java/im/conversations/android/xmpp/manager/PepManager.java diff --git a/src/main/java/im/conversations/android/xmpp/Managers.java b/src/main/java/im/conversations/android/xmpp/Managers.java index 82a86a678..6ef4e59f7 100644 --- a/src/main/java/im/conversations/android/xmpp/Managers.java +++ b/src/main/java/im/conversations/android/xmpp/Managers.java @@ -11,6 +11,7 @@ import im.conversations.android.xmpp.manager.BookmarkManager; import im.conversations.android.xmpp.manager.CarbonsManager; import im.conversations.android.xmpp.manager.DiscoManager; import im.conversations.android.xmpp.manager.NickManager; +import im.conversations.android.xmpp.manager.PepManager; import im.conversations.android.xmpp.manager.PresenceManager; import im.conversations.android.xmpp.manager.PubSubManager; import im.conversations.android.xmpp.manager.RosterManager; @@ -29,6 +30,7 @@ public final class Managers { .put(CarbonsManager.class, new CarbonsManager(context, connection)) .put(DiscoManager.class, new DiscoManager(context, connection)) .put(NickManager.class, new NickManager(context, connection)) + .put(PepManager.class, new PepManager(context, connection)) .put(PresenceManager.class, new PresenceManager(context, connection)) .put(PubSubManager.class, new PubSubManager(context, connection)) .put(RosterManager.class, new RosterManager(context, connection)) diff --git a/src/main/java/im/conversations/android/xmpp/XmppConnection.java b/src/main/java/im/conversations/android/xmpp/XmppConnection.java index 413706a8c..da9145811 100644 --- a/src/main/java/im/conversations/android/xmpp/XmppConnection.java +++ b/src/main/java/im/conversations/android/xmpp/XmppConnection.java @@ -2298,6 +2298,10 @@ public class XmppConnection implements Runnable { return this.streamFeatures != null && this.streamFeatures.clientStateIndication(); } + public Jid getBoundAddress() { + return this.connectionAddress; + } + private static class MyKeyManager implements X509KeyManager { private final Context context; 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 0d7e8df7d..2fcdf7b18 100644 --- a/src/main/java/im/conversations/android/xmpp/manager/AxolotlManager.java +++ b/src/main/java/im/conversations/android/xmpp/manager/AxolotlManager.java @@ -194,7 +194,13 @@ public class AxolotlManager extends AbstractManager { } private ListenableFuture publishDeviceId() { - final var currentDeviceIdsFuture = fetchDeviceIds(getAccount().address); + final var currentDeviceIdsFuture = + Futures.transform( + getManager(PepManager.class) + .fetchMostRecentItem( + Namespace.AXOLOTL_DEVICE_LIST, DeviceList.class), + DeviceList::getDeviceIds, + MoreExecutors.directExecutor()); final ListenableFuture> currentDeviceIdsWithFallback = Futures.catching( currentDeviceIdsFuture, @@ -226,12 +232,9 @@ public class AxolotlManager extends AbstractManager { private ListenableFuture publishDeviceIds(final Collection deviceIds) { final var deviceList = new DeviceList(); deviceList.setDeviceIds(deviceIds); - return getManager(PubSubManager.class) + return getManager(PepManager.class) .publishSingleton( - getAccount().address, - deviceList, - Namespace.AXOLOTL_DEVICE_LIST, - NodeConfiguration.OPEN); + deviceList, Namespace.AXOLOTL_DEVICE_LIST, NodeConfiguration.OPEN); } private ListenableFuture publishBundle() { @@ -246,9 +249,8 @@ public class AxolotlManager extends AbstractManager { "%s:%d", Namespace.AXOLOTL_BUNDLES, signalProtocolStore.getLocalRegistrationId()); - return getManager(PubSubManager.class) - .publishSingleton( - getAccount().address, bundle, node, NodeConfiguration.OPEN); + return getManager(PepManager.class) + .publishSingleton(bundle, node, NodeConfiguration.OPEN); }, MoreExecutors.directExecutor()); } 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 ffc2cc435..4564c081f 100644 --- a/src/main/java/im/conversations/android/xmpp/manager/BookmarkManager.java +++ b/src/main/java/im/conversations/android/xmpp/manager/BookmarkManager.java @@ -30,8 +30,7 @@ public class BookmarkManager extends AbstractManager { } public void fetch() { - final var future = - getManager(PubSubManager.class).fetchItems(getAccount().address, Conference.class); + final var future = getManager(PepManager.class).fetchItems(Conference.class); Futures.addCallback( future, new FutureCallback<>() { @@ -79,12 +78,8 @@ public class BookmarkManager extends AbstractManager { 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), + getManager(PepManager.class) + .publish(conference, itemId, NodeConfiguration.WHITELIST_MAX_ITEMS), result -> null, MoreExecutors.directExecutor()); } @@ -92,8 +87,7 @@ public class BookmarkManager extends AbstractManager { public ListenableFuture retractBookmark(final Jid address) { final var itemId = address.toEscapedString(); return Futures.transform( - getManager(PubSubManager.class) - .retract(getAccount().address, itemId, Namespace.BOOKMARKS2), + getManager(PepManager.class).retract(itemId, Namespace.BOOKMARKS2), result -> null, MoreExecutors.directExecutor()); } diff --git a/src/main/java/im/conversations/android/xmpp/manager/PepManager.java b/src/main/java/im/conversations/android/xmpp/manager/PepManager.java new file mode 100644 index 000000000..9116178b6 --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/manager/PepManager.java @@ -0,0 +1,48 @@ +package im.conversations.android.xmpp.manager; + +import android.content.Context; +import com.google.common.util.concurrent.ListenableFuture; +import eu.siacs.conversations.xmpp.Jid; +import im.conversations.android.xmpp.NodeConfiguration; +import im.conversations.android.xmpp.XmppConnection; +import im.conversations.android.xmpp.model.Extension; +import im.conversations.android.xmpp.model.stanza.Iq; +import java.util.Map; + +public class PepManager extends AbstractManager { + + public PepManager(Context context, XmppConnection connection) { + super(context, connection); + } + + public ListenableFuture> fetchItems(final Class clazz) { + return pubSubManager().fetchItems(pepService(), clazz); + } + + public ListenableFuture fetchMostRecentItem( + final String node, final Class clazz) { + return pubSubManager().fetchMostRecentItem(pepService(), node, clazz); + } + + public ListenableFuture publish( + Extension item, final String itemId, final NodeConfiguration nodeConfiguration) { + return pubSubManager().publish(pepService(), item, itemId, nodeConfiguration); + } + + public ListenableFuture publishSingleton( + Extension item, final String node, final NodeConfiguration nodeConfiguration) { + return pubSubManager().publishSingleton(pepService(), item, node, nodeConfiguration); + } + + public ListenableFuture retract(final String itemId, final String node) { + return pubSubManager().retract(pepService(), itemId, node); + } + + private PubSubManager pubSubManager() { + return getManager(PubSubManager.class); + } + + private Jid pepService() { + return connection.getBoundAddress().asBareJid(); + } +}