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,35 +767,78 @@ internal class DefaultCryptoService @Inject constructor(
return olmMachine!!.encrypt(roomId, eventType, content) return olmMachine!!.encrypt(roomId, eventType, content)
} }
private suspend fun uploadKeys(outgoingRequest: Request.KeysUpload) {
val body = MoshiProvider
.providesMoshi()
.adapter<JsonDict>(Map::class.java)
.fromJson(outgoingRequest.body)!!
val request = UploadKeysTask.Params(body)
val response = uploadKeysTask.execute(request)
val adapter = MoshiProvider
.providesMoshi()
.adapter<KeysUploadResponse>(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>(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<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() { private suspend fun sendOutgoingRequests() {
// TODO these requests should be sent out in parallel // TODO these requests should be sent out in parallel
for (outgoingRequest in olmMachine!!.outgoingRequests()) { for (outgoingRequest in olmMachine!!.outgoingRequests()) {
when (outgoingRequest) { when (outgoingRequest) {
is Request.KeysUpload -> { is Request.KeysUpload -> {
Timber.v("HELLO UPLOADING RUSTY KEYS") uploadKeys(outgoingRequest)
val body = MoshiProvider.providesMoshi().adapter<JsonDict>(Map::class.java).fromJson(outgoingRequest.body)!!
val request = UploadKeysTask.Params(body)
val response = uploadKeysTask.execute(request)
val adapter = MoshiProvider.providesMoshi().adapter<KeysUploadResponse>(KeysUploadResponse::class.java)
val json_response = adapter.toJson(response)!!
olmMachine!!.markRequestAsSent(outgoingRequest.requestId, RequestType.KEYS_UPLOAD, json_response)
Timber.v("HELLO UPLOADED KEYS $response")
} }
is Request.KeysQuery -> { is Request.KeysQuery -> {
Timber.v("HELLO KEYS QUERY REQUEST ${outgoingRequest.users}") queryKeys(outgoingRequest)
val params = DownloadKeysForUsersTask.Params(outgoingRequest.users, null)
try {
val response = downloadKeysForUsersTask.execute(params)
val adapter = MoshiProvider.providesMoshi().adapter<KeysQueryResponse>(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")
}
} }
is Request.ToDevice -> { is Request.ToDevice -> {
// Timber.v("HELLO TO DEVICE REQUEST ${outgoingRequest.body}") // Timber.v("HELLO TO DEVICE REQUEST ${outgoingRequest.body}")