Skip to content

Commit f9dd3b9

Browse files
committed
Stop using workers for interactive verification
1 parent 54fb4ae commit f9dd3b9

12 files changed

+146
-324
lines changed

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ import org.matrix.android.sdk.internal.crypto.tasks.GetDevicesTask
9797
import org.matrix.android.sdk.internal.crypto.tasks.SetDeviceNameTask
9898
import org.matrix.android.sdk.internal.crypto.tasks.UploadKeysTask
9999
import org.matrix.android.sdk.internal.crypto.verification.DefaultVerificationService
100+
import org.matrix.android.sdk.internal.crypto.verification.VerificationMessageProcessor
100101
import org.matrix.android.sdk.internal.di.DeviceId
101102
import org.matrix.android.sdk.internal.di.MoshiProvider
102103
import org.matrix.android.sdk.internal.di.UserId
@@ -183,6 +184,7 @@ internal class DefaultCryptoService @Inject constructor(
183184
private val taskExecutor: TaskExecutor,
184185
private val cryptoCoroutineScope: CoroutineScope,
185186
private val eventDecryptor: EventDecryptor,
187+
private val verificationMessageProcessor: VerificationMessageProcessor,
186188
private val liveEventManager: Lazy<StreamEventsManager>
187189
) : CryptoService {
188190

@@ -197,7 +199,7 @@ internal class DefaultCryptoService @Inject constructor(
197199
}
198200
}
199201

200-
fun onLiveEvent(roomId: String, event: Event) {
202+
fun onLiveEvent(roomId: String, event: Event, isInitialSync: Boolean) {
201203
// handle state events
202204
if (event.isStateEvent()) {
203205
when (event.type) {
@@ -206,6 +208,15 @@ internal class DefaultCryptoService @Inject constructor(
206208
EventType.STATE_ROOM_HISTORY_VISIBILITY -> onRoomHistoryVisibilityEvent(roomId, event)
207209
}
208210
}
211+
212+
// handle verification
213+
if (!isInitialSync) {
214+
if (event.type != null && verificationMessageProcessor.shouldProcess(event.type)) {
215+
cryptoCoroutineScope.launch(coroutineDispatchers.dmVerif) {
216+
verificationMessageProcessor.process(event)
217+
}
218+
}
219+
}
209220
}
210221

211222
// val gossipingBuffer = mutableListOf<Event>()

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.matrix.android.sdk.internal.crypto.tasks
1717

1818
import org.matrix.android.sdk.api.crypto.MXCRYPTO_ALGORITHM_MEGOLM
19+
import dagger.Lazy
1920
import org.matrix.android.sdk.api.session.crypto.CryptoService
2021
import org.matrix.android.sdk.api.session.crypto.model.MXEncryptEventContentResult
2122
import org.matrix.android.sdk.api.session.crypto.model.MXEventDecryptionResult
@@ -39,7 +40,7 @@ internal interface EncryptEventTask : Task<EncryptEventTask.Params, Event> {
3940

4041
internal class DefaultEncryptEventTask @Inject constructor(
4142
private val localEchoRepository: LocalEchoRepository,
42-
private val cryptoService: CryptoService
43+
private val cryptoService: Lazy<CryptoService>
4344
) : EncryptEventTask {
4445
override suspend fun execute(params: EncryptEventTask.Params): Event {
4546
// don't want to wait for any query
@@ -59,7 +60,7 @@ internal class DefaultEncryptEventTask @Inject constructor(
5960
// try {
6061
// let it throws
6162
awaitCallback<MXEncryptEventContentResult> {
62-
cryptoService.encryptEventContent(localMutableContent, localEvent.type, params.roomId, it)
63+
cryptoService.get().encryptEventContent(localMutableContent, localEvent.type, params.roomId, it)
6364
}.let { result ->
6465
val modifiedContent = HashMap(result.eventContent)
6566
params.keepKeys?.forEach { toKeep ->
@@ -80,7 +81,7 @@ internal class DefaultEncryptEventTask @Inject constructor(
8081
).toContent(),
8182
forwardingCurve25519KeyChain = emptyList(),
8283
senderCurve25519Key = result.eventContent["sender_key"] as? String,
83-
claimedEd25519Key = cryptoService.getMyDevice().fingerprint()
84+
claimedEd25519Key = cryptoService.get().getMyDevice().fingerprint()
8485
)
8586
} else {
8687
null

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ internal class DefaultSendVerificationMessageTask @Inject constructor(
4747
localEchoRepository.updateSendState(localId, event.roomId, SendState.SENDING)
4848
val response = executeRequest(globalErrorReceiver) {
4949
roomAPI.send(
50-
localId,
50+
txId = localId,
5151
roomId = event.roomId ?: "",
5252
content = event.content,
5353
eventType = event.type ?: ""

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -764,8 +764,15 @@ internal class DefaultVerificationService @Inject constructor(
764764
return
765765
}
766766

767+
val roomId = event.roomId
768+
if (roomId == null) {
769+
Timber.e("## SAS Verification missing roomId for event")
770+
// TODO cancel?
771+
return
772+
}
773+
767774
handleReadyReceived(event.senderId, readyReq) {
768-
verificationTransportRoomMessageFactory.createTransport(event.roomId!!, it)
775+
verificationTransportRoomMessageFactory.createTransport(roomId, it)
769776
}
770777
}
771778

@@ -1171,6 +1178,7 @@ internal class DefaultVerificationService @Inject constructor(
11711178
}
11721179
.distinct()
11731180

1181+
requestsForUser.add(verificationRequest)
11741182
transport.sendVerificationRequest(methodValues, validLocalId, otherUserId, roomId, null) { syncedId, info ->
11751183
// We need to update with the syncedID
11761184
updatePendingRequest(verificationRequest.copy(
@@ -1180,7 +1188,6 @@ internal class DefaultVerificationService @Inject constructor(
11801188
))
11811189
}
11821190

1183-
requestsForUser.add(verificationRequest)
11841191
dispatchRequestAdded(verificationRequest)
11851192

11861193
return verificationRequest

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

-88
This file was deleted.

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

+9-37
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,26 @@
1515
*/
1616
package org.matrix.android.sdk.internal.crypto.verification
1717

18-
import io.realm.Realm
19-
import org.matrix.android.sdk.api.session.crypto.MXCryptoError
20-
import org.matrix.android.sdk.api.session.crypto.model.OlmDecryptionResult
2118
import org.matrix.android.sdk.api.session.crypto.verification.VerificationService
2219
import org.matrix.android.sdk.api.session.events.model.Event
2320
import org.matrix.android.sdk.api.session.events.model.EventType
24-
import org.matrix.android.sdk.api.session.events.model.LocalEcho
2521
import org.matrix.android.sdk.api.session.events.model.toModel
2622
import org.matrix.android.sdk.api.session.room.model.message.MessageContent
2723
import org.matrix.android.sdk.api.session.room.model.message.MessageRelationContent
2824
import org.matrix.android.sdk.api.session.room.model.message.MessageType
2925
import org.matrix.android.sdk.api.session.room.model.message.MessageVerificationReadyContent
3026
import org.matrix.android.sdk.api.session.room.model.message.MessageVerificationRequestContent
3127
import org.matrix.android.sdk.api.session.room.model.message.MessageVerificationStartContent
32-
import org.matrix.android.sdk.internal.crypto.EventDecryptor
33-
import org.matrix.android.sdk.internal.database.model.EventInsertType
3428
import org.matrix.android.sdk.internal.di.DeviceId
3529
import org.matrix.android.sdk.internal.di.UserId
36-
import org.matrix.android.sdk.internal.session.EventInsertLiveProcessor
3730
import timber.log.Timber
3831
import javax.inject.Inject
3932

4033
internal class VerificationMessageProcessor @Inject constructor(
41-
private val eventDecryptor: EventDecryptor,
4234
private val verificationService: DefaultVerificationService,
4335
@UserId private val userId: String,
4436
@DeviceId private val deviceId: String?
45-
) : EventInsertLiveProcessor {
37+
) {
4638

4739
private val transactionsHandledByOtherDevice = ArrayList<String>()
4840

@@ -58,41 +50,20 @@ internal class VerificationMessageProcessor @Inject constructor(
5850
EventType.ENCRYPTED
5951
)
6052

61-
override fun shouldProcess(eventId: String, eventType: String, insertType: EventInsertType): Boolean {
62-
if (insertType != EventInsertType.INCREMENTAL_SYNC) {
63-
return false
64-
}
65-
return allowedTypes.contains(eventType) && !LocalEcho.isLocalEchoId(eventId)
53+
fun shouldProcess(eventType: String): Boolean {
54+
return allowedTypes.contains(eventType)
6655
}
6756

68-
override suspend fun process(realm: Realm, event: Event) {
69-
Timber.v("## SAS Verification live observer: received msgId: ${event.eventId} msgtype: ${event.type} from ${event.senderId}")
57+
suspend fun process(event: Event) {
58+
Timber.v("## SAS Verification live observer: received msgId: ${event.eventId} msgtype: ${event.getClearType()} from ${event.senderId}")
7059

7160
// If the request is in the future by more than 5 minutes or more than 10 minutes in the past,
7261
// the message should be ignored by the receiver.
7362

74-
if (!VerificationService.isValidRequest(event.ageLocalTs
75-
?: event.originServerTs)) return Unit.also {
76-
Timber.d("## SAS Verification live observer: msgId: ${event.eventId} is outdated")
63+
if (event.ageLocalTs != null && !VerificationService.isValidRequest(event.ageLocalTs)) return Unit.also {
64+
Timber.d("## SAS Verification live observer: msgId: ${event.eventId} is outdated age:$event.ageLocalTs ms")
7765
}
7866

79-
// decrypt if needed?
80-
if (event.isEncrypted() && event.mxDecryptionResult == null) {
81-
// TODO use a global event decryptor? attache to session and that listen to new sessionId?
82-
// for now decrypt sync
83-
try {
84-
val result = eventDecryptor.decryptEvent(event, "")
85-
event.mxDecryptionResult = OlmDecryptionResult(
86-
payload = result.clearEvent,
87-
senderKey = result.senderCurve25519Key,
88-
keysClaimed = result.claimedEd25519Key?.let { mapOf("ed25519" to it) },
89-
forwardingCurve25519KeyChain = result.forwardingCurve25519KeyChain
90-
)
91-
} catch (e: MXCryptoError) {
92-
Timber.e("## SAS Failed to decrypt event: ${event.eventId}")
93-
verificationService.onPotentiallyInterestingEventRoomFailToDecrypt(event)
94-
}
95-
}
9667
Timber.v("## SAS Verification live observer: received msgId: ${event.eventId} type: ${event.getClearType()}")
9768

9869
// Relates to is not encrypted
@@ -101,7 +72,6 @@ internal class VerificationMessageProcessor @Inject constructor(
10172
if (event.senderId == userId) {
10273
// If it's send from me, we need to keep track of Requests or Start
10374
// done from another device of mine
104-
10575
if (EventType.MESSAGE == event.getClearType()) {
10676
val msgType = event.getClearContent().toModel<MessageContent>()?.msgType
10777
if (MessageType.MSGTYPE_VERIFICATION_REQUEST == msgType) {
@@ -136,6 +106,8 @@ internal class VerificationMessageProcessor @Inject constructor(
136106
transactionsHandledByOtherDevice.remove(it)
137107
verificationService.onRoomRequestHandledByOtherDevice(event)
138108
}
109+
} else if (EventType.ENCRYPTED == event.getClearType()) {
110+
verificationService.onPotentiallyInterestingEventRoomFailToDecrypt(event)
139111
}
140112

141113
Timber.v("## SAS Verification ignoring message sent by me: ${event.eventId} type: ${event.getClearType()}")

0 commit comments

Comments
 (0)