send directed presence to transport if endpoints are configured

This commit is contained in:
Daniel Gultsch 2023-01-06 17:04:15 +01:00
parent 0e10ae387a
commit b7c7c40b94
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
2 changed files with 31 additions and 4 deletions

View file

@ -129,6 +129,21 @@ public class UnifiedPushDatabase extends SQLiteOpenHelper {
return null; 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 @Override
public void onUpgrade( public void onUpgrade(
final SQLiteDatabase sqLiteDatabase, final int oldVersion, final int newVersion) {} final SQLiteDatabase sqLiteDatabase, final int oldVersion, final int newVersion) {}

View file

@ -28,6 +28,7 @@ import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xml.Namespace; import eu.siacs.conversations.xml.Namespace;
import eu.siacs.conversations.xmpp.Jid; import eu.siacs.conversations.xmpp.Jid;
import eu.siacs.conversations.xmpp.stanzas.IqPacket; import eu.siacs.conversations.xmpp.stanzas.IqPacket;
import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
public class UnifiedPushBroker { public class UnifiedPushBroker {
@ -52,18 +53,29 @@ public class UnifiedPushBroker {
} }
public void renewUnifiedPushEndpointsOnBind(final Account account) { public void renewUnifiedPushEndpointsOnBind(final Account account) {
final Optional<Transport> transport = getTransport(); final Optional<Transport> transportOptional = getTransport();
if (transport.isPresent()) { if (transportOptional.isPresent()) {
final Account transportAccount = transport.get().account; final Transport transport = transportOptional.get();
final Account transportAccount = transport.account;
if (transportAccount != null && transportAccount.getUuid().equals(account.getUuid())) { if (transportAccount != null && transportAccount.getUuid().equals(account.getUuid())) {
final UnifiedPushDatabase database = UnifiedPushDatabase.getInstance(service);
if (database.hasEndpoints(transport)) {
sendDirectedPresence(transportAccount, transport.transport);
}
Log.d( Log.d(
Config.LOGTAG, Config.LOGTAG,
account.getJid().asBareJid() + ": trigger endpoint renewal on bind"); 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<Transport> renewUnifiedPushEndpoints() { public Optional<Transport> renewUnifiedPushEndpoints() {
final Optional<Transport> transportOptional = getTransport(); final Optional<Transport> transportOptional = getTransport();
if (transportOptional.isPresent()) { if (transportOptional.isPresent()) {