diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/rendezvous/Rendezvous.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/rendezvous/Rendezvous.kt index 25035d1f82..7a13758150 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/rendezvous/Rendezvous.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/rendezvous/Rendezvous.kt @@ -152,7 +152,7 @@ class Rendezvous( try { // explicitly download keys for ourself rather than racing with initial sync which might not complete in time - awaitCallback> { crypto.downloadKeys(listOf(userId), false, it) } + crypto.downloadKeysIfNeeded(listOf(userId), false) } catch (e: Throwable) { // log as warning and continue as initial sync might still complete Timber.tag(TAG).w(e, "Failed to download keys for self") diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt index 076bbb3a7e..a047e70839 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt @@ -146,38 +146,37 @@ internal class RealmCryptoStore @Inject constructor( .setWriteAsyncExecutor(monarchyWriteAsyncExecutor) .build() -// init { -// // Ensure CryptoMetadataEntity is inserted in DB -// doRealmTransaction(realmConfiguration) { realm -> -// var currentMetadata = realm.where().findFirst() -// -// var deleteAll = false -// -// if (currentMetadata != null) { -// // Check credentials -// // The device id may not have been provided in credentials. -// // Check it only if provided, else trust the stored one. -// if (currentMetadata.userId != userId || -// (deviceId != currentMetadata.deviceId)) { -// Timber.w("## open() : Credentials do not match, close this store and delete data") -// deleteAll = true -// currentMetadata = null -// } -// } -// -// if (currentMetadata == null) { -// if (deleteAll) { -// realm.deleteAll() -// } -// -// // Metadata not found, or database cleaned, create it -// realm.createObject(CryptoMetadataEntity::class.java, userId).apply { -// deviceId = this@RealmCryptoStore.deviceId -// } -// } -// } -// } + init { + // Ensure CryptoMetadataEntity is inserted in DB + doRealmTransaction(realmConfiguration) { realm -> + var currentMetadata = realm.where().findFirst() + var deleteAll = false + + if (currentMetadata != null) { + // Check credentials + // The device id may not have been provided in credentials. + // Check it only if provided, else trust the stored one. + if (currentMetadata.userId != userId || + (deviceId != null && deviceId != currentMetadata.deviceId)) { + Timber.w("## open() : Credentials do not match, close this store and delete data") + deleteAll = true + currentMetadata = null + } + } + + if (currentMetadata == null) { + if (deleteAll) { + realm.deleteAll() + } + + // Metadata not found, or database cleaned, create it + realm.createObject(CryptoMetadataEntity::class.java, userId).apply { + deviceId = this@RealmCryptoStore.deviceId + } + } + } + } /* ========================================================================================== * Other data * ========================================================================================== */