From b7c7c40b946bc21086e55db65c8dceb17a1907cf Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 6 Jan 2023 17:04:15 +0100 Subject: [PATCH] send directed presence to transport if endpoints are configured --- .../persistance/UnifiedPushDatabase.java | 15 ++++++++++++++ .../services/UnifiedPushBroker.java | 20 +++++++++++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/persistance/UnifiedPushDatabase.java b/src/main/java/eu/siacs/conversations/persistance/UnifiedPushDatabase.java index 1b1adc92f..f36506bd1 100644 --- a/src/main/java/eu/siacs/conversations/persistance/UnifiedPushDatabase.java +++ b/src/main/java/eu/siacs/conversations/persistance/UnifiedPushDatabase.java @@ -129,6 +129,21 @@ public class UnifiedPushDatabase extends SQLiteOpenHelper { return null; } + public boolean hasEndpoints(final UnifiedPushBroker.Transport transport) { + final SQLiteDatabase sqLiteDatabase = getReadableDatabase(); + try (final Cursor cursor = + sqLiteDatabase.rawQuery( + "SELECT EXISTS(SELECT endpoint FROM push WHERE account = ? AND transport = ?)", + new String[] { + transport.account.getUuid(), transport.transport.toEscapedString() + })) { + if (cursor != null && cursor.moveToFirst()) { + return cursor.getInt(0) > 0; + } + } + return false; + } + @Override public void onUpgrade( final SQLiteDatabase sqLiteDatabase, final int oldVersion, final int newVersion) {} diff --git a/src/main/java/eu/siacs/conversations/services/UnifiedPushBroker.java b/src/main/java/eu/siacs/conversations/services/UnifiedPushBroker.java index 5c6f1bbc8..8bfd018a7 100644 --- a/src/main/java/eu/siacs/conversations/services/UnifiedPushBroker.java +++ b/src/main/java/eu/siacs/conversations/services/UnifiedPushBroker.java @@ -28,6 +28,7 @@ import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Namespace; import eu.siacs.conversations.xmpp.Jid; import eu.siacs.conversations.xmpp.stanzas.IqPacket; +import eu.siacs.conversations.xmpp.stanzas.PresencePacket; public class UnifiedPushBroker { @@ -52,18 +53,29 @@ public class UnifiedPushBroker { } public void renewUnifiedPushEndpointsOnBind(final Account account) { - final Optional transport = getTransport(); - if (transport.isPresent()) { - final Account transportAccount = transport.get().account; + final Optional transportOptional = getTransport(); + if (transportOptional.isPresent()) { + final Transport transport = transportOptional.get(); + final Account transportAccount = transport.account; if (transportAccount != null && transportAccount.getUuid().equals(account.getUuid())) { + final UnifiedPushDatabase database = UnifiedPushDatabase.getInstance(service); + if (database.hasEndpoints(transport)) { + sendDirectedPresence(transportAccount, transport.transport); + } Log.d( Config.LOGTAG, account.getJid().asBareJid() + ": trigger endpoint renewal on bind"); - renewUnifiedEndpoint(transport.get()); + renewUnifiedEndpoint(transportOptional.get()); } } } + private void sendDirectedPresence(final Account account, Jid to) { + final PresencePacket presence = new PresencePacket(); + presence.setTo(to); + service.sendPresencePacket(account, presence); + } + public Optional renewUnifiedPushEndpoints() { final Optional transportOptional = getTransport(); if (transportOptional.isPresent()) {