Refactoring / PruneWorker should be a task not a worker

This commit is contained in:
Valere 2019-05-19 12:31:10 +02:00
parent 71e364b42f
commit 64c307077f
4 changed files with 28 additions and 53 deletions

View File

@ -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<LiveEntityObserver>(groupSummaryUpdater, eventsPruner, userEntityUpdater)
}

View File

@ -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
}
}
}

View File

@ -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<EventEntity>(monarchy) {
override val query = Monarchy.Query<EventEntity> { 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<PruneEventWorker>()
.setInputData(workData)
.build()
pruneEventTask.configureWith(params)
.executeBy(taskExecutor)
WorkManager.getInstance()
.beginUniqueWork(PRUNE_EVENT_WORKER, ExistingWorkPolicy.APPEND, sendWork)
.enqueue()
}
}

View File

@ -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<PruneEventTask.Params, Unit> {
data class Params(
val redactionEvents: List<Event>,
val userId: String
)
private val monarchy by inject<Monarchy>()
}
override fun doWork(): Result {
val params = WorkerParamsFactory.fromData<Params>(inputData)
?: return Result.failure()
internal class DefaultPruneEventTask(private val monarchy: Monarchy) : PruneEventTask {
val result = monarchy.tryTransactionSync { realm ->
override fun execute(params: PruneEventTask.Params): Try<Unit> {
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()
}
}
}