Skip to content

Commit 9f245c4

Browse files
Refactor code structure and improve naming
1 parent 5712c19 commit 9f245c4

File tree

7 files changed

+42
-19
lines changed

7 files changed

+42
-19
lines changed

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/CryptoService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import org.matrix.android.sdk.api.session.crypto.verification.VerificationServic
4141
import org.matrix.android.sdk.api.session.events.model.Content
4242
import org.matrix.android.sdk.api.session.events.model.Event
4343
import org.matrix.android.sdk.api.session.events.model.content.RoomKeyWithHeldContent
44-
import org.matrix.android.sdk.internal.database.helper.SessionInfoPair
44+
import org.matrix.android.sdk.internal.crypto.model.SessionInfo
4545

4646
interface CryptoService {
4747

@@ -167,5 +167,5 @@ interface CryptoService {
167167
/**
168168
* Share all inbound sessions of the last chunk messages to the provided userId devices
169169
*/
170-
fun sendSharedHistoryKeysToLastChunk(roomId: String, userId: String, sessionInfoSet: Set<SessionInfoPair>?)
170+
fun sendSharedHistoryKeys(roomId: String, userId: String, sessionInfoSet: Set<SessionInfo>?)
171171
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ import org.matrix.android.sdk.internal.crypto.algorithms.olm.MXOlmEncryptionFact
8383
import org.matrix.android.sdk.internal.crypto.crosssigning.DefaultCrossSigningService
8484
import org.matrix.android.sdk.internal.crypto.keysbackup.DefaultKeysBackupService
8585
import org.matrix.android.sdk.internal.crypto.model.MXKey.Companion.KEY_SIGNED_CURVE_25519_TYPE
86+
import org.matrix.android.sdk.internal.crypto.model.SessionInfo
8687
import org.matrix.android.sdk.internal.crypto.model.toRest
8788
import org.matrix.android.sdk.internal.crypto.repository.WarnOnUnknownDeviceRepository
8889
import org.matrix.android.sdk.internal.crypto.store.IMXCryptoStore
@@ -92,7 +93,6 @@ import org.matrix.android.sdk.internal.crypto.tasks.GetDevicesTask
9293
import org.matrix.android.sdk.internal.crypto.tasks.SetDeviceNameTask
9394
import org.matrix.android.sdk.internal.crypto.tasks.UploadKeysTask
9495
import org.matrix.android.sdk.internal.crypto.verification.DefaultVerificationService
95-
import org.matrix.android.sdk.internal.database.helper.SessionInfoPair
9696
import org.matrix.android.sdk.internal.di.DeviceId
9797
import org.matrix.android.sdk.internal.di.MoshiProvider
9898
import org.matrix.android.sdk.internal.di.UserId
@@ -958,7 +958,7 @@ internal class DefaultCryptoService @Inject constructor(
958958
eventContent?.historyVisibility?.let {
959959
cryptoStore.setShouldEncryptForInvitedMembers(roomId, it != RoomHistoryVisibility.JOINED)
960960
cryptoStore.setShouldShareHistory(roomId, it.shouldShareHistory())
961-
}
961+
} ?: cryptoStore.setShouldShareHistory(roomId, false)
962962
}
963963

964964
/**
@@ -1376,7 +1376,7 @@ internal class DefaultCryptoService @Inject constructor(
13761376
}
13771377
}
13781378

1379-
override fun sendSharedHistoryKeysToLastChunk(roomId: String, userId: String, sessionInfoSet: Set<SessionInfoPair>?) {
1379+
override fun sendSharedHistoryKeys(roomId: String, userId: String, sessionInfoSet: Set<SessionInfo>?) {
13801380
cryptoCoroutineScope.launch(coroutineDispatchers.crypto) {
13811381
runCatching {
13821382
deviceListManager.downloadKeys(listOf(userId), false)
@@ -1385,11 +1385,11 @@ internal class DefaultCryptoService @Inject constructor(
13851385
userDevices?.forEach {
13861386
// Lets share the provided inbound sessions for every user device
13871387
val deviceId = it.key
1388-
sessionInfoSet?.mapNotNull { sessionInfoPair ->
1388+
sessionInfoSet?.mapNotNull { sessionInfo ->
13891389
// Get inbound session from sessionId and sessionKey
13901390
cryptoStore.getInboundGroupSession(
1391-
sessionId = sessionInfoPair.first,
1392-
senderKey = sessionInfoPair.second,
1391+
sessionId = sessionInfo.sessionId,
1392+
senderKey = sessionInfo.senderKey,
13931393
sharedHistory = true
13941394
)
13951395
}?.filter { inboundGroupSession ->
@@ -1400,7 +1400,7 @@ internal class DefaultCryptoService @Inject constructor(
14001400
val exportedKeys = inboundGroupSession.exportKeys(sharedHistory = true)
14011401
val algorithm = exportedKeys?.algorithm
14021402
val decryptor = roomDecryptorProvider.getRoomDecryptor(roomId, algorithm)
1403-
decryptor?.shareKeysWithDevice(exportedKeys, deviceId, userId)
1403+
decryptor?.shareForwardKeysWithDevice(exportedKeys, deviceId, userId)
14041404
Timber.i("## CRYPTO | Sharing inbound session")
14051405
}
14061406
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/algorithms/IMXDecrypting.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ internal interface IMXDecrypting {
6161
*/
6262
fun shareKeysWithDevice(request: IncomingRoomKeyRequest) {}
6363

64-
fun shareKeysWithDevice(exportedKeys: MegolmSessionData?, deviceId: String, userId: String) {}
64+
fun shareForwardKeysWithDevice(exportedKeys: MegolmSessionData?, deviceId: String, userId: String) {}
6565

6666
fun shareSecretWithDevice(request: IncomingSecretShareRequest, secretValue: String) {}
6767

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/algorithms/megolm/MXMegolmDecryption.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ internal class MXMegolmDecryption(private val userId: String,
342342
return olmDevice.hasInboundSessionKeys(roomId, senderKey, sessionId)
343343
}
344344

345-
override fun shareKeysWithDevice(exportedKeys: MegolmSessionData?, deviceId: String, userId: String) {
345+
override fun shareForwardKeysWithDevice(exportedKeys: MegolmSessionData?, deviceId: String, userId: String) {
346346
exportedKeys ?: return
347347
cryptoCoroutineScope.launch(coroutineDispatchers.crypto) {
348348
runCatching { deviceListManager.downloadKeys(listOf(userId), false) }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2022 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.matrix.android.sdk.internal.crypto.model
18+
19+
data class SessionInfo(
20+
val sessionId: String,
21+
val senderKey: String
22+
)

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/ChunkEntityHelper.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import io.realm.kotlin.createObject
2222
import org.matrix.android.sdk.api.session.events.model.content.EncryptedEventContent
2323
import org.matrix.android.sdk.api.session.events.model.toModel
2424
import org.matrix.android.sdk.api.session.room.model.RoomMemberContent
25+
import org.matrix.android.sdk.internal.crypto.model.SessionInfo
2526
import org.matrix.android.sdk.internal.database.mapper.asDomain
2627
import org.matrix.android.sdk.internal.database.model.ChunkEntity
2728
import org.matrix.android.sdk.internal.database.model.CurrentStateEventEntityFields
@@ -241,13 +242,11 @@ internal fun ChunkEntity.isMoreRecentThan(chunkToCheck: ChunkEntity): Boolean {
241242
return false
242243
}
243244

244-
internal fun ChunkEntity.Companion.findLatestSessionInfo(realm: Realm, roomId: String): Set<SessionInfoPair>? =
245+
internal fun ChunkEntity.Companion.findLatestSessionInfo(realm: Realm, roomId: String): Set<SessionInfo>? =
245246
ChunkEntity.findLastForwardChunkOfRoom(realm, roomId)?.timelineEvents?.mapNotNull { timelineEvent ->
246247
timelineEvent?.root?.asDomain()?.content?.toModel<EncryptedEventContent>()?.let { content ->
247-
content.sessionId ?: return@mapNotNull null
248-
content.senderKey ?: return@mapNotNull null
249-
Pair(content.sessionId, content.senderKey)
248+
content.sessionId ?: return@mapNotNull null
249+
content.senderKey ?: return@mapNotNull null
250+
SessionInfo(content.sessionId, content.senderKey)
250251
}
251252
}?.toSet()
252-
253-
internal typealias SessionInfoPair = Pair<String, String>

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/DefaultMembershipService.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.matrix.android.sdk.internal.session.room.membership
1818

1919
import androidx.lifecycle.LiveData
20+
import com.otaliastudios.opengl.core.use
2021
import com.zhuinden.monarchy.Monarchy
2122
import dagger.assisted.Assisted
2223
import dagger.assisted.AssistedFactory
@@ -42,6 +43,7 @@ import org.matrix.android.sdk.internal.session.room.membership.admin.MembershipA
4243
import org.matrix.android.sdk.internal.session.room.membership.joining.InviteTask
4344
import org.matrix.android.sdk.internal.session.room.membership.threepid.InviteThreePidTask
4445
import org.matrix.android.sdk.internal.util.fetchCopied
46+
import timber.log.Timber
4547

4648
internal class DefaultMembershipService @AssistedInject constructor(
4749
@Assisted private val roomId: String,
@@ -131,10 +133,10 @@ internal class DefaultMembershipService @AssistedInject constructor(
131133
}
132134

133135
override suspend fun invite(userId: String, reason: String?) {
134-
val sessionInfoSet = Realm.getInstance(monarchy.realmConfiguration).use {
136+
val sessionInfo = Realm.getInstance(monarchy.realmConfiguration).use {
135137
ChunkEntity.findLatestSessionInfo(it, roomId)
136138
}
137-
cryptoService.sendSharedHistoryKeysToLastChunk(roomId, userId, sessionInfoSet)
139+
cryptoService.sendSharedHistoryKeys(roomId, userId, sessionInfo)
138140
val params = InviteTask.Params(roomId, userId, reason)
139141
inviteTask.execute(params)
140142
}

0 commit comments

Comments
 (0)