Remove duplicated code by reusing RedactEventTask

This commit is contained in:
Florian Renaud 2023-01-24 16:12:01 +01:00
parent 300e167577
commit a3f65b9bcb

View file

@ -19,13 +19,11 @@ import android.content.Context
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import com.squareup.moshi.JsonClass import com.squareup.moshi.JsonClass
import org.matrix.android.sdk.api.failure.Failure import org.matrix.android.sdk.api.failure.Failure
import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilitiesService
import org.matrix.android.sdk.internal.SessionManager import org.matrix.android.sdk.internal.SessionManager
import org.matrix.android.sdk.internal.crypto.tasks.RedactEventTask
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.session.SessionComponent import org.matrix.android.sdk.internal.session.SessionComponent
import org.matrix.android.sdk.internal.session.room.RoomAPI import org.matrix.android.sdk.internal.session.room.RoomAPI
import org.matrix.android.sdk.internal.session.room.send.model.EventRedactBody
import org.matrix.android.sdk.internal.worker.SessionSafeCoroutineWorker import org.matrix.android.sdk.internal.worker.SessionSafeCoroutineWorker
import org.matrix.android.sdk.internal.worker.SessionWorkerParams import org.matrix.android.sdk.internal.worker.SessionWorkerParams
import org.matrix.android.sdk.internal.worker.WorkerParamsFactory import org.matrix.android.sdk.internal.worker.WorkerParamsFactory
@ -51,34 +49,23 @@ internal class RedactEventWorker(context: Context, params: WorkerParameters, ses
@Inject lateinit var roomAPI: RoomAPI @Inject lateinit var roomAPI: RoomAPI
@Inject lateinit var globalErrorReceiver: GlobalErrorReceiver @Inject lateinit var globalErrorReceiver: GlobalErrorReceiver
@Inject lateinit var homeServerCapabilitiesService: HomeServerCapabilitiesService @Inject lateinit var redactEventTask: RedactEventTask
override fun injectWith(injector: SessionComponent) { override fun injectWith(injector: SessionComponent) {
injector.inject(this) injector.inject(this)
} }
override suspend fun doSafeWork(params: Params): Result { override suspend fun doSafeWork(params: Params): Result {
val eventId = params.eventId
val withRelations = if (homeServerCapabilitiesService.getHomeServerCapabilities().canRedactEventWithRelations &&
!params.withRelations.isNullOrEmpty()) {
params.withRelations
} else {
null
}
return runCatching { return runCatching {
executeRequest(globalErrorReceiver) { redactEventTask.execute(
roomAPI.redactEvent( RedactEventTask.Params(
txId = params.txID, txID = params.txID,
roomId = params.roomId, roomId = params.roomId,
eventId = eventId, eventId = params.eventId,
body = EventRedactBody( reason = params.reason,
reason = params.reason, withRelations = params.withRelations,
withRelations = withRelations, )
withRelationsUnstable = withRelations, )
)
)
}
}.fold( }.fold(
{ {
Result.success() Result.success()