From d2b39e5cb86ac29bb87c559273d9e73cdebd2b69 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 2 Mar 2021 10:52:03 +0100 Subject: [PATCH] Convert RelationService to suspend (#2449) --- .../room/model/relation/RelationService.kt | 2 +- .../room/relation/DefaultRelationService.kt | 9 +-- .../edithistory/ViewEditHistoryViewModel.kt | 70 ++++++++++--------- 3 files changed, 39 insertions(+), 42 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/relation/RelationService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/relation/RelationService.kt index 49aa95924c..4638dd204a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/relation/RelationService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/relation/RelationService.kt @@ -93,7 +93,7 @@ interface RelationService { /** * Get the edit history of the given event */ - fun fetchEditHistory(eventId: String, callback: MatrixCallback>) + suspend fun fetchEditHistory(eventId: String): List /** * Reply to an event in the timeline (must be in same room) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/relation/DefaultRelationService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/relation/DefaultRelationService.kt index bbcad3a4fe..da0cf45946 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/relation/DefaultRelationService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/relation/DefaultRelationService.kt @@ -140,13 +140,8 @@ internal class DefaultRelationService @AssistedInject constructor( return eventSenderProcessor.postEvent(event, cryptoSessionInfoProvider.isRoomEncrypted(roomId)) } - override fun fetchEditHistory(eventId: String, callback: MatrixCallback>) { - val params = FetchEditHistoryTask.Params(roomId, eventId) - fetchEditHistoryTask - .configureWith(params) { - this.callback = callback - } - .executeBy(taskExecutor) + override suspend fun fetchEditHistory(eventId: String): List { + return fetchEditHistoryTask.execute(FetchEditHistoryTask.Params(roomId, eventId)) } override fun replyToMessage(eventReplied: TimelineEvent, replyText: CharSequence, autoMarkdown: Boolean): Cancelable? { diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/edithistory/ViewEditHistoryViewModel.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/edithistory/ViewEditHistoryViewModel.kt index 02fb8c66f2..061743e163 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/edithistory/ViewEditHistoryViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/edithistory/ViewEditHistoryViewModel.kt @@ -15,6 +15,7 @@ */ package im.vector.app.features.home.room.detail.timeline.edithistory +import androidx.lifecycle.viewModelScope import com.airbnb.mvrx.Fail import com.airbnb.mvrx.FragmentViewModelContext import com.airbnb.mvrx.Loading @@ -28,10 +29,9 @@ import im.vector.app.core.date.VectorDateFormatter import im.vector.app.core.platform.EmptyAction import im.vector.app.core.platform.EmptyViewEvents import im.vector.app.core.platform.VectorViewModel -import org.matrix.android.sdk.api.MatrixCallback +import kotlinx.coroutines.launch import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.crypto.MXCryptoError -import org.matrix.android.sdk.api.session.events.model.Event import org.matrix.android.sdk.api.session.events.model.isReply import org.matrix.android.sdk.internal.crypto.algorithms.olm.OlmDecryptionResult import timber.log.Timber @@ -68,48 +68,50 @@ class ViewEditHistoryViewModel @AssistedInject constructor(@Assisted private fun loadHistory() { setState { copy(editList = Loading()) } - room.fetchEditHistory(eventId, object : MatrixCallback> { - override fun onFailure(failure: Throwable) { + + viewModelScope.launch { + val data = try { + room.fetchEditHistory(eventId) + } catch (failure: Throwable) { setState { copy(editList = Fail(failure)) } + return@launch } - override fun onSuccess(data: List) { - var originalIsReply = false + var originalIsReply = false - val events = data.map { event -> - val timelineID = event.roomId + UUID.randomUUID().toString() - event.also { - // We need to check encryption - if (it.isEncrypted() && it.mxDecryptionResult == null) { - // for now decrypt sync - try { - val result = session.cryptoService().decryptEvent(it, timelineID) - it.mxDecryptionResult = OlmDecryptionResult( - payload = result.clearEvent, - senderKey = result.senderCurve25519Key, - keysClaimed = result.claimedEd25519Key?.let { k -> mapOf("ed25519" to k) }, - forwardingCurve25519KeyChain = result.forwardingCurve25519KeyChain - ) - } catch (e: MXCryptoError) { - Timber.w("Failed to decrypt event in history") - } - } - - if (event.eventId == it.eventId) { - originalIsReply = it.isReply() + val events = data.map { event -> + val timelineID = event.roomId + UUID.randomUUID().toString() + event.also { + // We need to check encryption + if (it.isEncrypted() && it.mxDecryptionResult == null) { + // for now decrypt sync + try { + val result = session.cryptoService().decryptEvent(it, timelineID) + it.mxDecryptionResult = OlmDecryptionResult( + payload = result.clearEvent, + senderKey = result.senderCurve25519Key, + keysClaimed = result.claimedEd25519Key?.let { k -> mapOf("ed25519" to k) }, + forwardingCurve25519KeyChain = result.forwardingCurve25519KeyChain + ) + } catch (e: MXCryptoError) { + Timber.w("Failed to decrypt event in history") } } - } - setState { - copy( - editList = Success(events), - isOriginalAReply = originalIsReply - ) + + if (event.eventId == it.eventId) { + originalIsReply = it.isReply() + } } } - }) + setState { + copy( + editList = Success(events), + isOriginalAReply = originalIsReply + ) + } + } } override fun handle(action: EmptyAction) {