From 395d48f946246b7297c39782fe91f5a6904e2fcb Mon Sep 17 00:00:00 2001 From: ariskotsomitopoulos Date: Fri, 29 Apr 2022 14:02:26 +0300 Subject: [PATCH] Refactor code structure and improve naming --- .../sdk/api/session/crypto/CryptoService.kt | 4 ++-- .../internal/crypto/DefaultCryptoService.kt | 13 ++++++----- .../crypto/algorithms/IMXDecrypting.kt | 2 +- .../sdk/internal/crypto/model/SessionInfo.kt | 22 +++++++++++++++++++ .../database/helper/ChunkEntityHelper.kt | 11 +++++----- .../membership/DefaultMembershipService.kt | 6 +++-- 6 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/model/SessionInfo.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/CryptoService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/CryptoService.kt index 95db92c9b4..b84c5e2a80 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/CryptoService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/CryptoService.kt @@ -40,7 +40,7 @@ import org.matrix.android.sdk.api.session.crypto.verification.VerificationServic import org.matrix.android.sdk.api.session.events.model.Content import org.matrix.android.sdk.api.session.events.model.Event import org.matrix.android.sdk.api.session.events.model.content.RoomKeyWithHeldContent -import org.matrix.android.sdk.internal.database.helper.SessionInfoPair +import org.matrix.android.sdk.internal.crypto.model.SessionInfo interface CryptoService { @@ -181,5 +181,5 @@ interface CryptoService { /** * Share all inbound sessions of the last chunk messages to the provided userId devices */ - fun sendSharedHistoryKeysToLastChunk(roomId: String, userId: String, sessionInfoSet: Set?) + fun sendSharedHistoryKeys(roomId: String, userId: String, sessionInfoSet: Set?) } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt index 202cc2273c..044f3e86f3 100755 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt @@ -82,6 +82,7 @@ import org.matrix.android.sdk.internal.crypto.algorithms.olm.MXOlmEncryptionFact import org.matrix.android.sdk.internal.crypto.crosssigning.DefaultCrossSigningService import org.matrix.android.sdk.internal.crypto.keysbackup.DefaultKeysBackupService import org.matrix.android.sdk.internal.crypto.model.MXKey.Companion.KEY_SIGNED_CURVE_25519_TYPE +import org.matrix.android.sdk.internal.crypto.model.SessionInfo import org.matrix.android.sdk.internal.crypto.model.toRest import org.matrix.android.sdk.internal.crypto.repository.WarnOnUnknownDeviceRepository import org.matrix.android.sdk.internal.crypto.store.IMXCryptoStore @@ -968,7 +969,7 @@ internal class DefaultCryptoService @Inject constructor( eventContent?.historyVisibility?.let { cryptoStore.setShouldEncryptForInvitedMembers(roomId, it != RoomHistoryVisibility.JOINED) cryptoStore.setShouldShareHistory(roomId, it.shouldShareHistory()) - } + } ?: cryptoStore.setShouldShareHistory(roomId, false) } /** @@ -1338,7 +1339,7 @@ internal class DefaultCryptoService @Inject constructor( } } - override fun sendSharedHistoryKeysToLastChunk(roomId: String, userId: String, sessionInfoSet: Set?) { + override fun sendSharedHistoryKeys(roomId: String, userId: String, sessionInfoSet: Set?) { cryptoCoroutineScope.launch(coroutineDispatchers.crypto) { runCatching { deviceListManager.downloadKeys(listOf(userId), false) @@ -1347,11 +1348,11 @@ internal class DefaultCryptoService @Inject constructor( userDevices?.forEach { // Lets share the provided inbound sessions for every user device val deviceId = it.key - sessionInfoSet?.mapNotNull { sessionInfoPair -> + sessionInfoSet?.mapNotNull { sessionInfo -> // Get inbound session from sessionId and sessionKey cryptoStore.getInboundGroupSession( - sessionId = sessionInfoPair.first, - senderKey = sessionInfoPair.second, + sessionId = sessionInfo.sessionId, + senderKey = sessionInfo.senderKey, sharedHistory = true ) }?.filter { inboundGroupSession -> @@ -1362,7 +1363,7 @@ internal class DefaultCryptoService @Inject constructor( val exportedKeys = inboundGroupSession.exportKeys(sharedHistory = true) val algorithm = exportedKeys?.algorithm val decryptor = roomDecryptorProvider.getRoomDecryptor(roomId, algorithm) - decryptor?.shareKeysWithDevice(exportedKeys, deviceId, userId) + decryptor?.shareForwardKeysWithDevice(exportedKeys, deviceId, userId) Timber.i("## CRYPTO | Sharing inbound session") } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/algorithms/IMXDecrypting.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/algorithms/IMXDecrypting.kt index ce445d3ce6..7edae70fd8 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/algorithms/IMXDecrypting.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/algorithms/IMXDecrypting.kt @@ -45,5 +45,5 @@ internal interface IMXDecrypting { */ fun onRoomKeyEvent(event: Event, defaultKeysBackupService: DefaultKeysBackupService) {} - fun shareKeysWithDevice(exportedKeys: MegolmSessionData?, deviceId: String, userId: String) {} + fun shareForwardKeysWithDevice(exportedKeys: MegolmSessionData?, deviceId: String, userId: String) {} } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/model/SessionInfo.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/model/SessionInfo.kt new file mode 100644 index 0000000000..45f0c519d1 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/model/SessionInfo.kt @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.internal.crypto.model + +data class SessionInfo( + val sessionId: String, + val senderKey: String +) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/ChunkEntityHelper.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/ChunkEntityHelper.kt index 73af812271..221abe0df5 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/ChunkEntityHelper.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/ChunkEntityHelper.kt @@ -21,6 +21,7 @@ import io.realm.kotlin.createObject import org.matrix.android.sdk.api.session.events.model.content.EncryptedEventContent import org.matrix.android.sdk.api.session.events.model.toModel import org.matrix.android.sdk.api.session.room.model.RoomMemberContent +import org.matrix.android.sdk.internal.crypto.model.SessionInfo import org.matrix.android.sdk.internal.database.mapper.asDomain import org.matrix.android.sdk.internal.database.model.ChunkEntity import org.matrix.android.sdk.internal.database.model.CurrentStateEventEntityFields @@ -185,13 +186,11 @@ internal fun ChunkEntity.isMoreRecentThan(chunkToCheck: ChunkEntity): Boolean { return false } -internal fun ChunkEntity.Companion.findLatestSessionInfo(realm: Realm, roomId: String): Set? = +internal fun ChunkEntity.Companion.findLatestSessionInfo(realm: Realm, roomId: String): Set? = ChunkEntity.findLastForwardChunkOfRoom(realm, roomId)?.timelineEvents?.mapNotNull { timelineEvent -> timelineEvent?.root?.asDomain()?.content?.toModel()?.let { content -> - content.sessionId ?: return@mapNotNull null - content.senderKey ?: return@mapNotNull null - Pair(content.sessionId, content.senderKey) + content.sessionId ?: return@mapNotNull null + content.senderKey ?: return@mapNotNull null + SessionInfo(content.sessionId, content.senderKey) } }?.toSet() - -internal typealias SessionInfoPair = Pair diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/DefaultMembershipService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/DefaultMembershipService.kt index b6dbaa7be8..0f82872f23 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/DefaultMembershipService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/DefaultMembershipService.kt @@ -17,6 +17,7 @@ package org.matrix.android.sdk.internal.session.room.membership import androidx.lifecycle.LiveData +import com.otaliastudios.opengl.core.use import com.zhuinden.monarchy.Monarchy import dagger.assisted.Assisted import dagger.assisted.AssistedFactory @@ -44,6 +45,7 @@ import org.matrix.android.sdk.internal.session.room.membership.admin.MembershipA import org.matrix.android.sdk.internal.session.room.membership.joining.InviteTask import org.matrix.android.sdk.internal.session.room.membership.threepid.InviteThreePidTask import org.matrix.android.sdk.internal.util.fetchCopied +import timber.log.Timber internal class DefaultMembershipService @AssistedInject constructor( @Assisted private val roomId: String, @@ -143,10 +145,10 @@ internal class DefaultMembershipService @AssistedInject constructor( } override suspend fun invite(userId: String, reason: String?) { - val sessionInfoSet = Realm.getInstance(monarchy.realmConfiguration).use { + val sessionInfo = Realm.getInstance(monarchy.realmConfiguration).use { ChunkEntity.findLatestSessionInfo(it, roomId) } - cryptoService.sendSharedHistoryKeysToLastChunk(roomId, userId, sessionInfoSet) + cryptoService.sendSharedHistoryKeys(roomId, userId, sessionInfo) val params = InviteTask.Params(roomId, userId, reason) inviteTask.execute(params) }