Do not change txnId it in case of retry, if not provided in the params

Also create txnId using UUID.randomUUID() instead of Random.nextInt(Integer.MAX_VALUE) for coherency
This commit is contained in:
Benoit Marty 2021-07-21 11:22:35 +02:00
parent 98720ce4a5
commit 49a44bd042

View file

@ -22,8 +22,8 @@ import org.matrix.android.sdk.internal.crypto.model.rest.SendToDeviceBody
import org.matrix.android.sdk.internal.network.GlobalErrorReceiver import org.matrix.android.sdk.internal.network.GlobalErrorReceiver
import org.matrix.android.sdk.internal.network.executeRequest import org.matrix.android.sdk.internal.network.executeRequest
import org.matrix.android.sdk.internal.task.Task import org.matrix.android.sdk.internal.task.Task
import java.util.UUID
import javax.inject.Inject import javax.inject.Inject
import kotlin.random.Random
internal interface SendToDeviceTask : Task<SendToDeviceTask.Params, Unit> { internal interface SendToDeviceTask : Task<SendToDeviceTask.Params, Unit> {
data class Params( data class Params(
@ -31,7 +31,7 @@ internal interface SendToDeviceTask : Task<SendToDeviceTask.Params, Unit> {
val eventType: String, val eventType: String,
// the content to send. Map from user_id to device_id to content dictionary. // the content to send. Map from user_id to device_id to content dictionary.
val contentMap: MXUsersDevicesMap<Any>, val contentMap: MXUsersDevicesMap<Any>,
// the transactionId // the transactionId. If not provided, a transactionId will be created by the task
val transactionId: String? = null val transactionId: String? = null
) )
} }
@ -46,16 +46,21 @@ internal class DefaultSendToDeviceTask @Inject constructor(
messages = params.contentMap.map messages = params.contentMap.map
) )
// Create a unique txnId first, to use the same value if the request is retried
val txnId = params.transactionId ?: createUniqueTxnId()
return executeRequest( return executeRequest(
globalErrorReceiver, globalErrorReceiver,
canRetry = true, canRetry = true,
maxRetriesCount = 3 maxRetriesCount = 3
) { ) {
cryptoApi.sendToDevice( cryptoApi.sendToDevice(
params.eventType, eventType = params.eventType,
params.transactionId ?: Random.nextInt(Integer.MAX_VALUE).toString(), transactionId = txnId,
sendToDeviceBody body = sendToDeviceBody
) )
} }
} }
} }
internal fun createUniqueTxnId() = UUID.randomUUID().toString()