crypto: Move the request sending logic into the sender

This commit is contained in:
Damir Jelić 2021-06-30 16:22:17 +02:00
parent 2c1dc053ed
commit cd5aad9a31

View file

@ -45,7 +45,6 @@ import org.matrix.android.sdk.api.session.crypto.verification.VerificationServic
import org.matrix.android.sdk.api.session.events.model.Content import org.matrix.android.sdk.api.session.events.model.Content
import org.matrix.android.sdk.api.session.events.model.Event import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.events.model.EventType import org.matrix.android.sdk.api.session.events.model.EventType
import org.matrix.android.sdk.api.session.events.model.LocalEcho
import org.matrix.android.sdk.api.session.events.model.toModel import org.matrix.android.sdk.api.session.events.model.toModel
import org.matrix.android.sdk.api.session.room.model.Membership import org.matrix.android.sdk.api.session.room.model.Membership
import org.matrix.android.sdk.api.session.room.model.RoomHistoryVisibility import org.matrix.android.sdk.api.session.room.model.RoomHistoryVisibility
@ -107,9 +106,38 @@ import kotlin.math.max
internal class RequestSender @Inject constructor( internal class RequestSender @Inject constructor(
private val sendToDeviceTask: SendToDeviceTask, private val sendToDeviceTask: SendToDeviceTask,
private val oneTimeKeysForUsersDeviceTask: ClaimOneTimeKeysForUsersDeviceTask,
private val uploadKeysTask: UploadKeysTask,
private val downloadKeysForUsersTask: DownloadKeysForUsersTask,
private val sendVerificationMessageTask: Lazy<DefaultSendVerificationMessageTask>, private val sendVerificationMessageTask: Lazy<DefaultSendVerificationMessageTask>,
) { ) {
suspend fun claimKeys(request: Request.KeysClaim): String {
val claimParams = ClaimOneTimeKeysForUsersDeviceTask.Params(request.oneTimeKeys)
val response = oneTimeKeysForUsersDeviceTask.execute(claimParams)
val adapter = MoshiProvider
.providesMoshi()
.adapter(KeysClaimResponse::class.java)
return adapter.toJson(response)!!
}
suspend fun queryKeys(request: Request.KeysQuery): String {
val params = DownloadKeysForUsersTask.Params(request.users, null)
val response = downloadKeysForUsersTask.execute(params)
val adapter = MoshiProvider.providesMoshi().adapter(KeysQueryResponse::class.java)
return adapter.toJson(response)!!
}
suspend fun uploadKeys(request: Request.KeysUpload): String {
val body = MoshiProvider.providesMoshi().adapter<JsonDict>(Map::class.java).fromJson(request.body)!!
val params = UploadKeysTask.Params(body)
val response = uploadKeysTask.execute(params)
val adapter = MoshiProvider.providesMoshi().adapter(KeysUploadResponse::class.java)
return adapter.toJson(response)!!
}
suspend fun sendVerificationRequest(request: OutgoingVerificationRequest) { suspend fun sendVerificationRequest(request: OutgoingVerificationRequest) {
when (request) { when (request) {
is OutgoingVerificationRequest.InRoom -> sendRoomMessage(request) is OutgoingVerificationRequest.InRoom -> sendRoomMessage(request)
@ -142,8 +170,7 @@ internal class RequestSender @Inject constructor(
} }
suspend fun sendToDevice(eventType: String, body: String) { suspend fun sendToDevice(eventType: String, body: String) {
// TODO this produces floats for the Olm type fields, which // TODO this produces floats for the Olm type fields, which are integers originally.
// are integers originally.
val adapter = MoshiProvider val adapter = MoshiProvider
.providesMoshi() .providesMoshi()
.adapter<Map<String, HashMap<String, Any>>>(Map::class.java) .adapter<Map<String, HashMap<String, Any>>>(Map::class.java)
@ -187,12 +214,8 @@ internal class DefaultCryptoService @Inject constructor(
// Tasks // Tasks
private val deleteDeviceTask: DeleteDeviceTask, private val deleteDeviceTask: DeleteDeviceTask,
private val getDevicesTask: GetDevicesTask, private val getDevicesTask: GetDevicesTask,
private val oneTimeKeysForUsersDeviceTask: ClaimOneTimeKeysForUsersDeviceTask,
private val sendToDeviceTask: SendToDeviceTask,
private val downloadKeysForUsersTask: DownloadKeysForUsersTask,
private val getDeviceInfoTask: GetDeviceInfoTask, private val getDeviceInfoTask: GetDeviceInfoTask,
private val setDeviceNameTask: SetDeviceNameTask, private val setDeviceNameTask: SetDeviceNameTask,
private val uploadKeysTask: UploadKeysTask,
private val loadRoomMembersTask: LoadRoomMembersTask, private val loadRoomMembersTask: LoadRoomMembersTask,
private val cryptoSessionInfoProvider: CryptoSessionInfoProvider, private val cryptoSessionInfoProvider: CryptoSessionInfoProvider,
private val coroutineDispatchers: MatrixCoroutineDispatchers, private val coroutineDispatchers: MatrixCoroutineDispatchers,
@ -809,36 +832,15 @@ internal class DefaultCryptoService @Inject constructor(
return olmMachine!!.encrypt(roomId, eventType, content) return olmMachine!!.encrypt(roomId, eventType, content)
} }
private suspend fun uploadKeys(outgoingRequest: Request.KeysUpload) { private suspend fun uploadKeys(request: Request.KeysUpload) {
val body = MoshiProvider val response = this.sender.uploadKeys(request)
.providesMoshi() this.olmMachine!!.markRequestAsSent(request.requestId, RequestType.KEYS_UPLOAD, response)
.adapter<JsonDict>(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 jsonResponse = adapter.toJson(response)!!
this.olmMachine!!.markRequestAsSent(
outgoingRequest.requestId,
RequestType.KEYS_UPLOAD,
jsonResponse
)
} }
private suspend fun queryKeys(outgoingRequest: Request.KeysQuery) { private suspend fun queryKeys(request: Request.KeysQuery) {
val params = DownloadKeysForUsersTask.Params(outgoingRequest.users, null)
try { try {
val response = downloadKeysForUsersTask.execute(params) val response = this.sender.queryKeys(request)
val adapter = MoshiProvider.providesMoshi().adapter(KeysQueryResponse::class.java) this.olmMachine!!.markRequestAsSent(request.requestId, RequestType.KEYS_QUERY, response)
val jsonResponse = adapter.toJson(response)!!
this.olmMachine!!.markRequestAsSent(outgoingRequest.requestId, RequestType.KEYS_QUERY, jsonResponse)
} catch (throwable: Throwable) { } catch (throwable: Throwable) {
Timber.e(throwable, "## CRYPTO | doKeyDownloadForUsers(): error") Timber.e(throwable, "## CRYPTO | doKeyDownloadForUsers(): error")
} }
@ -850,14 +852,8 @@ internal class DefaultCryptoService @Inject constructor(
} }
private suspend fun claimKeys(request: Request.KeysClaim) { private suspend fun claimKeys(request: Request.KeysClaim) {
val claimParams = ClaimOneTimeKeysForUsersDeviceTask.Params(request.oneTimeKeys) val response = this.sender.claimKeys(request)
val response = oneTimeKeysForUsersDeviceTask.execute(claimParams) olmMachine!!.markRequestAsSent(request.requestId, RequestType.KEYS_CLAIM, response)
val adapter = MoshiProvider
.providesMoshi()
.adapter(KeysClaimResponse::class.java)
val jsonResponse = adapter.toJson(response)!!
olmMachine!!.markRequestAsSent(request.requestId, RequestType.KEYS_CLAIM, jsonResponse)
} }
private suspend fun sendOutgoingRequests() { private suspend fun sendOutgoingRequests() {