From 64c307077feaab3d53f83081c2a2a8985f3ecee6 Mon Sep 17 00:00:00 2001 From: Valere Date: Sun, 19 May 2019 12:31:10 +0200 Subject: [PATCH] Refactoring / PruneWorker should be a task not a worker --- .../android/internal/session/SessionModule.kt | 2 +- .../internal/session/room/RoomModule.kt | 19 ++++------- .../session/room/prune/EventsPruner.kt | 26 +++++++------- ...{PruneEventWorker.kt => PruneEventTask.kt} | 34 +++++-------------- 4 files changed, 28 insertions(+), 53 deletions(-) rename matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/{PruneEventWorker.kt => PruneEventTask.kt} (87%) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt index 1a5594f2..6640cb2a 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt @@ -154,7 +154,7 @@ internal class SessionModule(private val sessionParams: SessionParams) { scope(DefaultSession.SCOPE) { val groupSummaryUpdater = GroupSummaryUpdater(get()) - val eventsPruner = EventsPruner(get(), get()) + val eventsPruner = EventsPruner(get(), get(), get(), get()) val userEntityUpdater = UserEntityUpdater(get(), get(), get()) listOf(groupSummaryUpdater, eventsPruner, userEntityUpdater) } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomModule.kt index c7df624c..d3abeb81 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomModule.kt @@ -21,24 +21,14 @@ import im.vector.matrix.android.internal.session.room.annotation.DefaultFindReac import im.vector.matrix.android.internal.session.room.annotation.FindReactionEventForUndoTask import im.vector.matrix.android.internal.session.room.create.CreateRoomTask import im.vector.matrix.android.internal.session.room.create.DefaultCreateRoomTask -import im.vector.matrix.android.internal.session.room.membership.DefaultLoadRoomMembersTask -import im.vector.matrix.android.internal.session.room.membership.LoadRoomMembersTask -import im.vector.matrix.android.internal.session.room.membership.joining.DefaultInviteTask -import im.vector.matrix.android.internal.session.room.membership.joining.DefaultJoinRoomTask -import im.vector.matrix.android.internal.session.room.membership.joining.InviteTask -import im.vector.matrix.android.internal.session.room.membership.joining.JoinRoomTask -import im.vector.matrix.android.internal.session.room.membership.leaving.DefaultLeaveRoomTask -import im.vector.matrix.android.internal.session.room.membership.leaving.LeaveRoomTask +import im.vector.matrix.android.internal.session.room.prune.PruneEventTask +import im.vector.matrix.android.internal.session.room.prune.DefaultPruneEventTask import im.vector.matrix.android.internal.session.room.read.DefaultSetReadMarkersTask import im.vector.matrix.android.internal.session.room.read.SetReadMarkersTask import im.vector.matrix.android.internal.session.room.send.LocalEchoEventFactory import im.vector.matrix.android.internal.session.room.state.DefaultSendStateTask import im.vector.matrix.android.internal.session.room.state.SendStateTask -import im.vector.matrix.android.internal.session.room.timeline.DefaultGetContextOfEventTask -import im.vector.matrix.android.internal.session.room.timeline.DefaultPaginationTask -import im.vector.matrix.android.internal.session.room.timeline.GetContextOfEventTask -import im.vector.matrix.android.internal.session.room.timeline.PaginationTask -import im.vector.matrix.android.internal.session.room.timeline.TokenChunkEventPersistor +import im.vector.matrix.android.internal.session.room.timeline.* import org.koin.dsl.module.module import retrofit2.Retrofit @@ -104,6 +94,9 @@ class RoomModule { DefaultFindReactionEventForUndoTask(get()) as FindReactionEventForUndoTask } + scope(DefaultSession.SCOPE) { + DefaultPruneEventTask(get()) as PruneEventTask + } } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/EventsPruner.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/EventsPruner.kt index 36e280bb..cb7a3818 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/EventsPruner.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/EventsPruner.kt @@ -16,9 +16,6 @@ package im.vector.matrix.android.internal.session.room.prune -import androidx.work.ExistingWorkPolicy -import androidx.work.OneTimeWorkRequestBuilder -import androidx.work.WorkManager import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.api.auth.data.Credentials import im.vector.matrix.android.api.session.events.model.EventType @@ -26,11 +23,14 @@ import im.vector.matrix.android.internal.database.RealmLiveEntityObserver import im.vector.matrix.android.internal.database.mapper.asDomain import im.vector.matrix.android.internal.database.model.EventEntity import im.vector.matrix.android.internal.database.query.where -import im.vector.matrix.android.internal.util.WorkerParamsFactory +import im.vector.matrix.android.internal.task.TaskExecutor +import im.vector.matrix.android.internal.task.configureWith -private const val PRUNE_EVENT_WORKER = "PRUNE_EVENT_WORKER" -internal class EventsPruner(monarchy: Monarchy, private val credentials: Credentials) : +internal class EventsPruner(monarchy: Monarchy, + private val credentials: Credentials, + private val pruneEventTask: PruneEventTask, + private val taskExecutor: TaskExecutor) : RealmLiveEntityObserver(monarchy) { override val query = Monarchy.Query { EventEntity.where(it, type = EventType.REDACTION) } @@ -39,16 +39,14 @@ internal class EventsPruner(monarchy: Monarchy, private val credentials: Credent val redactionEvents = inserted .mapNotNull { it.asDomain() } - val pruneEventWorkerParams = PruneEventWorker.Params(redactionEvents, credentials.userId) - val workData = WorkerParamsFactory.toData(pruneEventWorkerParams) + val params = PruneEventTask.Params( + redactionEvents, + credentials.userId + ) - val sendWork = OneTimeWorkRequestBuilder() - .setInputData(workData) - .build() + pruneEventTask.configureWith(params) + .executeBy(taskExecutor) - WorkManager.getInstance() - .beginUniqueWork(PRUNE_EVENT_WORKER, ExistingWorkPolicy.APPEND, sendWork) - .enqueue() } } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/PruneEventWorker.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/PruneEventTask.kt similarity index 87% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/PruneEventWorker.kt rename to matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/PruneEventTask.kt index 08a1491e..82949b41 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/PruneEventWorker.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/PruneEventTask.kt @@ -13,13 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package im.vector.matrix.android.internal.session.room.prune -import android.content.Context -import androidx.work.Worker -import androidx.work.WorkerParameters -import com.squareup.moshi.JsonClass +import arrow.core.Try import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.api.session.events.model.Event import im.vector.matrix.android.api.session.events.model.EventType @@ -32,41 +28,30 @@ import im.vector.matrix.android.internal.database.model.EventAnnotationsSummaryE import im.vector.matrix.android.internal.database.model.EventEntity import im.vector.matrix.android.internal.database.model.ReactionAggregatedSummaryEntityFields import im.vector.matrix.android.internal.database.query.where -import im.vector.matrix.android.internal.di.MatrixKoinComponent import im.vector.matrix.android.internal.di.MoshiProvider -import im.vector.matrix.android.internal.util.WorkerParamsFactory +import im.vector.matrix.android.internal.task.Task import im.vector.matrix.android.internal.util.tryTransactionSync import io.realm.Realm -import org.koin.standalone.inject import timber.log.Timber -//TODO should be a task instead of worker -internal class PruneEventWorker(context: Context, - workerParameters: WorkerParameters -) : Worker(context, workerParameters), MatrixKoinComponent { - @JsonClass(generateAdapter = true) - internal class Params( +internal interface PruneEventTask : Task { + + data class Params( val redactionEvents: List, val userId: String ) - private val monarchy by inject() +} - override fun doWork(): Result { - val params = WorkerParamsFactory.fromData(inputData) - ?: return Result.failure() +internal class DefaultPruneEventTask(private val monarchy: Monarchy) : PruneEventTask { - val result = monarchy.tryTransactionSync { realm -> + override fun execute(params: PruneEventTask.Params): Try { + return monarchy.tryTransactionSync { realm -> params.redactionEvents.forEach { event -> pruneEvent(realm, event, params.userId) } } - return result.fold({ - Result.retry() - }, { - Result.success() - }) } private fun pruneEvent(realm: Realm, redactionEvent: Event, userId: String) { @@ -154,5 +139,4 @@ internal class PruneEventWorker(context: Context, else -> emptyList() } } - } \ No newline at end of file