crypto: Move the outgoing request sending logic into separate methods

This commit is contained in:
Damir Jelić 2021-03-25 13:17:06 +01:00
parent 4b157f7915
commit 3812162f4f

View file

@ -748,11 +748,7 @@ internal class DefaultCryptoService @Inject constructor(
// This request can only be a keys claim request. // This request can only be a keys claim request.
when (request) { when (request) {
is Request.KeysClaim -> { is Request.KeysClaim -> {
val claimParams = ClaimOneTimeKeysForUsersDeviceTask.Params(request.oneTimeKeys) claimKeys(request)
val response = oneTimeKeysForUsersDeviceTask.execute(claimParams)
val adapter = MoshiProvider.providesMoshi().adapter<KeysClaimResponse>(KeysClaimResponse::class.java)
val json_response = adapter.toJson(response)!!
olmMachine!!.markRequestAsSent(request.requestId, RequestType.KEYS_CLAIM, json_response)
} }
} }
} }
@ -761,17 +757,7 @@ internal class DefaultCryptoService @Inject constructor(
// This request can only be a to-device request. // This request can only be a to-device request.
when (toDeviceRequest) { when (toDeviceRequest) {
is Request.ToDevice -> { is Request.ToDevice -> {
// TODO this produces floats for the Olm type fields, which sendToDevice(toDeviceRequest)
// are integers originally.
val adapter = MoshiProvider.providesMoshi().adapter<Map<String, HashMap<String, Any>>>(Map::class.java)
val body = adapter.fromJson(toDeviceRequest.body)!!
val userMap = MXUsersDevicesMap<Any>()
userMap.join(body)
val sendToDeviceParams = SendToDeviceTask.Params(toDeviceRequest.eventType, userMap)
sendToDeviceTask.execute(sendToDeviceParams)
olmMachine!!.markRequestAsSent(toDeviceRequest.requestId, RequestType.TO_DEVICE, "{}")
} }
} }
} }
@ -781,22 +767,29 @@ internal class DefaultCryptoService @Inject constructor(
return olmMachine!!.encrypt(roomId, eventType, content) return olmMachine!!.encrypt(roomId, eventType, content)
} }
private suspend fun sendOutgoingRequests() { private suspend fun uploadKeys(outgoingRequest: Request.KeysUpload) {
// TODO these requests should be sent out in parallel val body = MoshiProvider
for (outgoingRequest in olmMachine!!.outgoingRequests()) { .providesMoshi()
when (outgoingRequest) { .adapter<JsonDict>(Map::class.java)
is Request.KeysUpload -> { .fromJson(outgoingRequest.body)!!
Timber.v("HELLO UPLOADING RUSTY KEYS")
val body = MoshiProvider.providesMoshi().adapter<JsonDict>(Map::class.java).fromJson(outgoingRequest.body)!!
val request = UploadKeysTask.Params(body) val request = UploadKeysTask.Params(body)
val response = uploadKeysTask.execute(request) val response = uploadKeysTask.execute(request)
val adapter = MoshiProvider.providesMoshi().adapter<KeysUploadResponse>(KeysUploadResponse::class.java) val adapter = MoshiProvider
.providesMoshi()
.adapter<KeysUploadResponse>(KeysUploadResponse::class.java)
val json_response = adapter.toJson(response)!! val json_response = adapter.toJson(response)!!
olmMachine!!.markRequestAsSent(outgoingRequest.requestId, RequestType.KEYS_UPLOAD, json_response)
Timber.v("HELLO UPLOADED KEYS $response") olmMachine!!.markRequestAsSent(
outgoingRequest.requestId,
RequestType.KEYS_UPLOAD,
json_response
)
} }
is Request.KeysQuery -> {
private suspend fun queryKeys(outgoingRequest: Request.KeysQuery) {
Timber.v("HELLO KEYS QUERY REQUEST ${outgoingRequest.users}") Timber.v("HELLO KEYS QUERY REQUEST ${outgoingRequest.users}")
val params = DownloadKeysForUsersTask.Params(outgoingRequest.users, null) val params = DownloadKeysForUsersTask.Params(outgoingRequest.users, null)
@ -808,8 +801,44 @@ internal class DefaultCryptoService @Inject constructor(
} catch (throwable: Throwable) { } catch (throwable: Throwable) {
Timber.e(throwable, "## CRYPTO | doKeyDownloadForUsers(): error") 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<String, HashMap<String, Any>>>(Map::class.java)
val body = adapter.fromJson(request.body)!!
val userMap = MXUsersDevicesMap<Any>()
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>(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 -> {
uploadKeys(outgoingRequest)
}
is Request.KeysQuery -> {
queryKeys(outgoingRequest)
} }
is Request.ToDevice -> { is Request.ToDevice -> {
// Timber.v("HELLO TO DEVICE REQUEST ${outgoingRequest.body}") // Timber.v("HELLO TO DEVICE REQUEST ${outgoingRequest.body}")