Refactor code structure and improve naming

This commit is contained in:
ariskotsomitopoulos 2022-04-29 14:02:26 +03:00 committed by Valere
parent 497f7cf044
commit 395d48f946
6 changed files with 41 additions and 17 deletions

View File

@ -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<SessionInfoPair>?)
fun sendSharedHistoryKeys(roomId: String, userId: String, sessionInfoSet: Set<SessionInfo>?)
}

View File

@ -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<SessionInfoPair>?) {
override fun sendSharedHistoryKeys(roomId: String, userId: String, sessionInfoSet: Set<SessionInfo>?) {
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")
}
}

View File

@ -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) {}
}

View File

@ -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
)

View File

@ -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<SessionInfoPair>? =
internal fun ChunkEntity.Companion.findLatestSessionInfo(realm: Realm, roomId: String): Set<SessionInfo>? =
ChunkEntity.findLastForwardChunkOfRoom(realm, roomId)?.timelineEvents?.mapNotNull { timelineEvent ->
timelineEvent?.root?.asDomain()?.content?.toModel<EncryptedEventContent>()?.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<String, String>

View File

@ -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)
}