From 98498a66aa403e0cc055251868cde4b959b562ff Mon Sep 17 00:00:00 2001 From: Mikunj Date: Tue, 8 Oct 2019 10:11:46 +1100 Subject: [PATCH] Fix receiving multiple of the same authorisations at once causing us to delete prekeybundles for the user. --- .../thoughtcrime/securesms/jobs/PushDecryptJob.java | 12 ++++++------ .../securesms/loki/LokiPreKeyBundleDatabase.kt | 6 ++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index 68fdeef878..dfc8b5e11f 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -282,14 +282,17 @@ public class PushDecryptJob extends BaseJob implements InjectableType { if (content.lokiServiceMessage.isPresent()) { LokiServiceMessage lokiMessage = content.lokiServiceMessage.get(); if (lokiMessage.getPreKeyBundleMessage() != null) { - Log.d("Loki", "Received a pre key bundle from: " + envelope.getSource() + "."); int registrationID = TextSecurePreferences.getLocalRegistrationId(context); - if (registrationID > 0) { - LokiPreKeyBundleDatabase lokiPreKeyBundleDatabase = DatabaseFactory.getLokiPreKeyBundleDatabase(context); + LokiPreKeyBundleDatabase lokiPreKeyBundleDatabase = DatabaseFactory.getLokiPreKeyBundleDatabase(context); + + // Only store the pre key bundle if we don't have one in our database + if (registrationID > 0 && !lokiPreKeyBundleDatabase.hasPreKeyBundle(envelope.getSource())) { + Log.d("Loki", "Received a pre key bundle from: " + envelope.getSource() + "."); PreKeyBundle preKeyBundle = lokiMessage.getPreKeyBundleMessage().getPreKeyBundle(registrationID); lokiPreKeyBundleDatabase.setPreKeyBundle(envelope.getSource(), preKeyBundle); } } + if (lokiMessage.getAddressMessage() != null) { // TODO: Loki - Handle address message } @@ -1064,9 +1067,6 @@ public class PushDecryptJob extends BaseJob implements InjectableType { DeviceLinkingSession linkingSession = DeviceLinkingSession.Companion.getShared(); if (isValid && linkingSession.isListeningForLinkingRequests()) { linkingSession.processLinkingRequest(authorisation); - } else { - // Remove pre key bundle from the user - DatabaseFactory.getLokiPreKeyBundleDatabase(context).removePreKeyBundle(envelope.getSource()); } } diff --git a/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleDatabase.kt index 9f213d8fd9..e7faf9f6b3 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleDatabase.kt @@ -89,4 +89,10 @@ class LokiPreKeyBundleDatabase(context: Context, helper: SQLCipherOpenHelper) : val database = databaseHelper.writableDatabase database.delete(tableName, "${Companion.hexEncodedPublicKey} = ?", arrayOf( hexEncodedPublicKey )) } + + fun hasPreKeyBundle(hexEncodedPublicKey: String): Boolean { + val database = databaseHelper.readableDatabase + val cursor = database.query(tableName, null, "${Companion.hexEncodedPublicKey} = ?", arrayOf(hexEncodedPublicKey), null, null, null) + return cursor != null && cursor.count > 0 + } } \ No newline at end of file