From 3812162f4f5def6d18d8f58b78d4566ed0efaa76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 25 Mar 2021 13:17:06 +0100 Subject: [PATCH] crypto: Move the outgoing request sending logic into separate methods --- .../internal/crypto/DefaultCryptoService.kt | 105 +++++++++++------- 1 file changed, 67 insertions(+), 38 deletions(-) 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 8cb5fdbe82..a04fba6239 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 @@ -748,11 +748,7 @@ internal class DefaultCryptoService @Inject constructor( // This request can only be a keys claim request. when (request) { is Request.KeysClaim -> { - val claimParams = ClaimOneTimeKeysForUsersDeviceTask.Params(request.oneTimeKeys) - val response = oneTimeKeysForUsersDeviceTask.execute(claimParams) - val adapter = MoshiProvider.providesMoshi().adapter(KeysClaimResponse::class.java) - val json_response = adapter.toJson(response)!! - olmMachine!!.markRequestAsSent(request.requestId, RequestType.KEYS_CLAIM, json_response) + claimKeys(request) } } } @@ -761,17 +757,7 @@ internal class DefaultCryptoService @Inject constructor( // This request can only be a to-device request. when (toDeviceRequest) { is Request.ToDevice -> { - // TODO this produces floats for the Olm type fields, which - // are integers originally. - val adapter = MoshiProvider.providesMoshi().adapter>>(Map::class.java) - val body = adapter.fromJson(toDeviceRequest.body)!! - - val userMap = MXUsersDevicesMap() - userMap.join(body) - - val sendToDeviceParams = SendToDeviceTask.Params(toDeviceRequest.eventType, userMap) - sendToDeviceTask.execute(sendToDeviceParams) - olmMachine!!.markRequestAsSent(toDeviceRequest.requestId, RequestType.TO_DEVICE, "{}") + sendToDevice(toDeviceRequest) } } } @@ -781,35 +767,78 @@ internal class DefaultCryptoService @Inject constructor( return olmMachine!!.encrypt(roomId, eventType, content) } + private suspend fun uploadKeys(outgoingRequest: Request.KeysUpload) { + val body = MoshiProvider + .providesMoshi() + .adapter(Map::class.java) + .fromJson(outgoingRequest.body)!! + + val request = UploadKeysTask.Params(body) + + val response = uploadKeysTask.execute(request) + val adapter = MoshiProvider + .providesMoshi() + .adapter(KeysUploadResponse::class.java) + + val json_response = adapter.toJson(response)!! + + olmMachine!!.markRequestAsSent( + outgoingRequest.requestId, + RequestType.KEYS_UPLOAD, + json_response + ) + } + + private suspend fun queryKeys(outgoingRequest: Request.KeysQuery) { + Timber.v("HELLO KEYS QUERY REQUEST ${outgoingRequest.users}") + val params = DownloadKeysForUsersTask.Params(outgoingRequest.users, null) + + try { + val response = downloadKeysForUsersTask.execute(params) + val adapter = MoshiProvider.providesMoshi().adapter(KeysQueryResponse::class.java) + val json_response = adapter.toJson(response)!! + olmMachine!!.markRequestAsSent(outgoingRequest.requestId, RequestType.KEYS_QUERY, json_response) + } catch (throwable: Throwable) { + Timber.e(throwable, "## CRYPTO | doKeyDownloadForUsers(): error") + } + } + + private suspend fun sendToDevice(request: Request.ToDevice) { + // TODO this produces floats for the Olm type fields, which + // are integers originally. + val adapter = MoshiProvider + .providesMoshi() + .adapter>>(Map::class.java) + val body = adapter.fromJson(request.body)!! + + val userMap = MXUsersDevicesMap() + userMap.join(body) + + val sendToDeviceParams = SendToDeviceTask.Params(request.eventType, userMap) + sendToDeviceTask.execute(sendToDeviceParams) + olmMachine!!.markRequestAsSent(request.requestId, RequestType.TO_DEVICE, "{}") + } + + private suspend fun claimKeys(request: Request.KeysClaim) { + val claimParams = ClaimOneTimeKeysForUsersDeviceTask.Params(request.oneTimeKeys) + val response = oneTimeKeysForUsersDeviceTask.execute(claimParams) + val adapter = MoshiProvider + .providesMoshi() + .adapter(KeysClaimResponse::class.java) + val json_response = adapter.toJson(response)!! + + olmMachine!!.markRequestAsSent(request.requestId, RequestType.KEYS_CLAIM, json_response) + } + private suspend fun sendOutgoingRequests() { // TODO these requests should be sent out in parallel for (outgoingRequest in olmMachine!!.outgoingRequests()) { when (outgoingRequest) { is Request.KeysUpload -> { - Timber.v("HELLO UPLOADING RUSTY KEYS") - val body = MoshiProvider.providesMoshi().adapter(Map::class.java).fromJson(outgoingRequest.body)!! - val request = UploadKeysTask.Params(body) - - val response = uploadKeysTask.execute(request) - val adapter = MoshiProvider.providesMoshi().adapter(KeysUploadResponse::class.java) - val json_response = adapter.toJson(response)!! - olmMachine!!.markRequestAsSent(outgoingRequest.requestId, RequestType.KEYS_UPLOAD, json_response) - Timber.v("HELLO UPLOADED KEYS $response") + uploadKeys(outgoingRequest) } is Request.KeysQuery -> { - Timber.v("HELLO KEYS QUERY REQUEST ${outgoingRequest.users}") - val params = DownloadKeysForUsersTask.Params(outgoingRequest.users, null) - - try { - val response = downloadKeysForUsersTask.execute(params) - val adapter = MoshiProvider.providesMoshi().adapter(KeysQueryResponse::class.java) - val json_response = adapter.toJson(response)!! - olmMachine!!.markRequestAsSent(outgoingRequest.requestId, RequestType.KEYS_QUERY, json_response) - } catch (throwable: Throwable) { - Timber.e(throwable, "## CRYPTO | doKeyDownloadForUsers(): error") - } - - + queryKeys(outgoingRequest) } is Request.ToDevice -> { // Timber.v("HELLO TO DEVICE REQUEST ${outgoingRequest.body}")