From 5c9750fb0741fb2db5cb818e4d3e379d85f18480 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 2 Mar 2021 19:18:19 +0100 Subject: [PATCH] Moar cleanup after rebase and before merge --- CHANGES.md | 2 +- .../session/room/crypto/RoomCryptoService.kt | 1 - .../algorithms/megolm/MXMegolmEncryption.kt | 31 ++++++++--------- .../megolm/MXMegolmEncryptionFactory.kt | 34 +++++++++---------- .../home/room/detail/RoomDetailViewModel.kt | 8 ++--- 5 files changed, 35 insertions(+), 41 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a3eb4bfdf9..8692a2cff8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,7 @@ Improvements šŸ™Œ: - Improve initial sync performance (#983) - PIP support for Jitsi call (#2418) - Add tooltip for room quick actions + - Pre-share session keys when opening a room or start typing (#2771) Bugfix šŸ›: - Try to fix crash about UrlPreview (#2640) @@ -41,7 +42,6 @@ Improvements šŸ™Œ: - Improve room profile UX - Upgrade Jitsi library from 2.9.3 to 3.1.0 - a11y improvements - - Pre-share session keys when opening a room or start typing (#2771) Bugfix šŸ›: - VoIP : fix audio devices output diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/crypto/RoomCryptoService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/crypto/RoomCryptoService.kt index d8bec878a1..6581247b90 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/crypto/RoomCryptoService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/crypto/RoomCryptoService.kt @@ -33,7 +33,6 @@ interface RoomCryptoService { /** * Ensures all members of the room are loaded and outbound session keys are shared. - * Call this method according to [OutboundSessionKeySharingStrategy]. * If this method is not called, CryptoService will ensure it before sending events. */ suspend fun prepareToEncrypt() diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/algorithms/megolm/MXMegolmEncryption.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/algorithms/megolm/MXMegolmEncryption.kt index bb75d014ff..6b91c0b859 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/algorithms/megolm/MXMegolmEncryption.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/algorithms/megolm/MXMegolmEncryption.kt @@ -18,8 +18,6 @@ package org.matrix.android.sdk.internal.crypto.algorithms.megolm import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch -import org.matrix.android.sdk.api.MatrixCallback -import org.matrix.android.sdk.api.auth.data.Credentials import org.matrix.android.sdk.api.session.crypto.MXCryptoError import org.matrix.android.sdk.api.session.events.model.Content import org.matrix.android.sdk.api.session.events.model.Event @@ -40,8 +38,6 @@ import org.matrix.android.sdk.internal.crypto.model.forEach import org.matrix.android.sdk.internal.crypto.repository.WarnOnUnknownDeviceRepository import org.matrix.android.sdk.internal.crypto.store.IMXCryptoStore import org.matrix.android.sdk.internal.crypto.tasks.SendToDeviceTask -import org.matrix.android.sdk.internal.task.TaskExecutor -import org.matrix.android.sdk.internal.task.configureWith import org.matrix.android.sdk.internal.util.JsonCanonicalizer import org.matrix.android.sdk.internal.util.MatrixCoroutineDispatchers import org.matrix.android.sdk.internal.util.convertToUTF8 @@ -55,11 +51,11 @@ internal class MXMegolmEncryption( private val cryptoStore: IMXCryptoStore, private val deviceListManager: DeviceListManager, private val ensureOlmSessionsForDevicesAction: EnsureOlmSessionsForDevicesAction, - private val credentials: Credentials, + private val userId: String, + private val deviceId: String, private val sendToDeviceTask: SendToDeviceTask, private val messageEncrypter: MessageEncrypter, private val warnOnUnknownDevicesRepository: WarnOnUnknownDeviceRepository, - private val taskExecutor: TaskExecutor, private val coroutineDispatchers: MatrixCoroutineDispatchers, private val cryptoCoroutineScope: CoroutineScope ) : IMXEncrypting, IMXGroupEncryption { @@ -282,7 +278,7 @@ internal class MXMegolmEncryption( gossipingEventBuffer.add( Event( type = EventType.ROOM_KEY, - senderId = credentials.userId, + senderId = this.userId, content = submap.apply { this["session_key"] = "" // we add a fake key for trail @@ -310,8 +306,11 @@ internal class MXMegolmEncryption( } } - private fun notifyKeyWithHeld(targets: List, sessionId: String, senderKey: String?, code: WithHeldCode) { - Timber.i("## CRYPTO |Ā notifyKeyWithHeld() :sending withheld key for $targets session:$sessionId and code $code ") + private suspend fun notifyKeyWithHeld(targets: List, + sessionId: String, + senderKey: String?, + code: WithHeldCode) { + Timber.i("## CRYPTO |Ā notifyKeyWithHeld() :sending withheld key for $targets session:$sessionId and code $code") val withHeldContent = RoomKeyWithHeldContent( roomId = roomId, senderKey = senderKey, @@ -327,13 +326,11 @@ internal class MXMegolmEncryption( } } ) - sendToDeviceTask.configureWith(params) { - callback = object : MatrixCallback { - override fun onFailure(failure: Throwable) { - Timber.e("## CRYPTO |Ā notifyKeyWithHeld() : Failed to notify withheld key for $targets session: $sessionId ") - } - } - }.executeBy(taskExecutor) + try { + sendToDeviceTask.execute(params) + } catch (failure: Throwable) { + Timber.e("## CRYPTO |Ā notifyKeyWithHeld() : Failed to notify withheld key for $targets session: $sessionId ") + } } /** @@ -359,7 +356,7 @@ internal class MXMegolmEncryption( // Include our device ID so that recipients can send us a // m.new_device message if they don't have our session key. - map["device_id"] = credentials.deviceId!! + map["device_id"] = deviceId session.useCount++ return map } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/algorithms/megolm/MXMegolmEncryptionFactory.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/algorithms/megolm/MXMegolmEncryptionFactory.kt index f0cc15fb63..9f6312ea97 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/algorithms/megolm/MXMegolmEncryptionFactory.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/algorithms/megolm/MXMegolmEncryptionFactory.kt @@ -17,7 +17,6 @@ package org.matrix.android.sdk.internal.crypto.algorithms.megolm import kotlinx.coroutines.CoroutineScope -import org.matrix.android.sdk.api.auth.data.Credentials import org.matrix.android.sdk.internal.crypto.DeviceListManager import org.matrix.android.sdk.internal.crypto.MXOlmDevice import org.matrix.android.sdk.internal.crypto.actions.EnsureOlmSessionsForDevicesAction @@ -26,7 +25,8 @@ import org.matrix.android.sdk.internal.crypto.keysbackup.DefaultKeysBackupServic import org.matrix.android.sdk.internal.crypto.repository.WarnOnUnknownDeviceRepository import org.matrix.android.sdk.internal.crypto.store.IMXCryptoStore import org.matrix.android.sdk.internal.crypto.tasks.SendToDeviceTask -import org.matrix.android.sdk.internal.task.TaskExecutor +import org.matrix.android.sdk.internal.di.DeviceId +import org.matrix.android.sdk.internal.di.UserId import org.matrix.android.sdk.internal.util.MatrixCoroutineDispatchers import javax.inject.Inject @@ -36,29 +36,29 @@ internal class MXMegolmEncryptionFactory @Inject constructor( private val cryptoStore: IMXCryptoStore, private val deviceListManager: DeviceListManager, private val ensureOlmSessionsForDevicesAction: EnsureOlmSessionsForDevicesAction, - private val credentials: Credentials, + @UserId private val userId: String, + @DeviceId private val deviceId: String?, private val sendToDeviceTask: SendToDeviceTask, private val messageEncrypter: MessageEncrypter, private val warnOnUnknownDevicesRepository: WarnOnUnknownDeviceRepository, - private val taskExecutor: TaskExecutor, private val coroutineDispatchers: MatrixCoroutineDispatchers, private val cryptoCoroutineScope: CoroutineScope) { fun create(roomId: String): MXMegolmEncryption { return MXMegolmEncryption( - roomId, - olmDevice, - defaultKeysBackupService, - cryptoStore, - deviceListManager, - ensureOlmSessionsForDevicesAction, - credentials, - sendToDeviceTask, - messageEncrypter, - warnOnUnknownDevicesRepository, - taskExecutor, - coroutineDispatchers, - cryptoCoroutineScope + roomId = roomId, + olmDevice = olmDevice, + defaultKeysBackupService = defaultKeysBackupService, + cryptoStore = cryptoStore, + deviceListManager = deviceListManager, + ensureOlmSessionsForDevicesAction = ensureOlmSessionsForDevicesAction, + userId = userId, + deviceId = deviceId!!, + sendToDeviceTask = sendToDeviceTask, + messageEncrypter = messageEncrypter, + warnOnUnknownDevicesRepository = warnOnUnknownDevicesRepository, + coroutineDispatchers = coroutineDispatchers, + cryptoCoroutineScope = cryptoCoroutineScope ) } } diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewModel.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewModel.kt index 7365801589..935b4ca2f8 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewModel.kt @@ -195,11 +195,11 @@ class RoomDetailViewModel @AssistedInject constructor( } private fun prepareForEncryption() { - // check if there is not already a call made - if (prepareToEncrypt !is Loading) { + // check if there is not already a call made, or if there has been an error + if (prepareToEncrypt.shouldLoad) { prepareToEncrypt = Loading() viewModelScope.launch { - kotlin.runCatching { + runCatching { room.prepareToEncrypt() }.fold({ prepareToEncrypt = Success(Unit) @@ -629,8 +629,6 @@ class RoomDetailViewModel @AssistedInject constructor( if (action.focused) { // Should we add some rate limit here, or do it only once per model lifecycle? prepareForEncryption() - } else { - // we could eventually cancel here :/ } } }