From 03050c3f25599a1681d3b31df15cb61019edcc3a Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 5 Jul 2019 16:11:54 +0200 Subject: [PATCH] Cleanup --- .../algorithms/megolm/MXMegolmDecryption.kt | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmDecryption.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmDecryption.kt index 14f90862..fb90db7d 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmDecryption.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmDecryption.kt @@ -68,17 +68,20 @@ internal class MXMegolmDecryption(private val credentials: Credentials, } private fun decryptEvent(event: Event, timeline: String, requestKeysOnFail: Boolean): Try { - val encryptedEventContent = event.content.toModel() - ?: return Try.Failure(MXCryptoError.Base(MXCryptoError.ErrorType.MISSING_FIELDS, MXCryptoError.MISSING_FIELDS_REASON)) - - if (TextUtils.isEmpty(encryptedEventContent.senderKey) - || TextUtils.isEmpty(encryptedEventContent.sessionId) - || TextUtils.isEmpty(encryptedEventContent.ciphertext)) { + if (event.roomId.isNullOrBlank()) { return Try.Failure(MXCryptoError.Base(MXCryptoError.ErrorType.MISSING_FIELDS, MXCryptoError.MISSING_FIELDS_REASON)) } - // TODO Why AS says this code is unreachable? - return olmDevice.decryptGroupMessage(encryptedEventContent.ciphertext!!, event.roomId!!, timeline, encryptedEventContent.sessionId!!, encryptedEventContent.senderKey!!) + val encryptedEventContent = event.content.toModel() + ?: return Try.Failure(MXCryptoError.Base(MXCryptoError.ErrorType.MISSING_FIELDS, MXCryptoError.MISSING_FIELDS_REASON)) + + if (encryptedEventContent.senderKey.isNullOrBlank() + || encryptedEventContent.sessionId.isNullOrBlank() + || encryptedEventContent.ciphertext.isNullOrBlank()) { + return Try.Failure(MXCryptoError.Base(MXCryptoError.ErrorType.MISSING_FIELDS, MXCryptoError.MISSING_FIELDS_REASON)) + } + + return olmDevice.decryptGroupMessage(encryptedEventContent.ciphertext, event.roomId, timeline, encryptedEventContent.sessionId, encryptedEventContent.senderKey) .fold( { throwable -> if (throwable is MXCryptoError.OlmError) { @@ -93,7 +96,7 @@ internal class MXMegolmDecryption(private val credentials: Credentials, val reason = String.format(MXCryptoError.OLM_REASON, throwable.olmException.message) val detailedReason = String.format(MXCryptoError.DETAILED_OLM_REASON, encryptedEventContent.ciphertext, reason) - return Try.Failure(MXCryptoError.Base( + Try.Failure(MXCryptoError.Base( MXCryptoError.ErrorType.OLM, reason, detailedReason)) @@ -107,17 +110,17 @@ internal class MXMegolmDecryption(private val credentials: Credentials, } } - return Try.Failure(throwable) + Try.Failure(throwable) }, - { decryptionResult -> + { olmDecryptionResult -> // the decryption succeeds - return if (decryptionResult.payload != null) { + if (olmDecryptionResult.payload != null) { Try.just( MXEventDecryptionResult( - clearEvent = decryptionResult.payload, - senderCurve25519Key = decryptionResult.senderKey, - claimedEd25519Key = decryptionResult.keysClaimed?.get("ed25519"), - forwardingCurve25519KeyChain = decryptionResult.forwardingCurve25519KeyChain ?: emptyList() + clearEvent = olmDecryptionResult.payload, + senderCurve25519Key = olmDecryptionResult.senderKey, + claimedEd25519Key = olmDecryptionResult.keysClaimed?.get("ed25519"), + forwardingCurve25519KeyChain = olmDecryptionResult.forwardingCurve25519KeyChain ?: emptyList() ) ) } else {