From a1716907132b5d0767a504d7d55102e35de48207 Mon Sep 17 00:00:00 2001 From: ganfra Date: Thu, 16 Nov 2023 14:11:00 +0100 Subject: [PATCH] Suppress usage of removeTimeline method (#1824) --- changelog.d/1824.misc | 1 + .../matrix/impl/room/RoomContentForwarder.kt | 19 ++++++------------- .../impl/timeline/RoomTimelineExtensions.kt | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 13 deletions(-) create mode 100644 changelog.d/1824.misc diff --git a/changelog.d/1824.misc b/changelog.d/1824.misc new file mode 100644 index 0000000000..16cdc522ed --- /dev/null +++ b/changelog.d/1824.misc @@ -0,0 +1 @@ +Suppress usage of removeTimeline method. diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RoomContentForwarder.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RoomContentForwarder.kt index d539ec6a53..1c203e30d6 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RoomContentForwarder.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RoomContentForwarder.kt @@ -21,12 +21,11 @@ import io.element.android.libraries.matrix.api.core.EventId import io.element.android.libraries.matrix.api.core.RoomId import io.element.android.libraries.matrix.api.room.ForwardEventException import io.element.android.libraries.matrix.impl.roomlist.roomOrNull +import io.element.android.libraries.matrix.impl.timeline.runWithTimelineListenerRegistered import kotlinx.coroutines.CancellationException import kotlinx.coroutines.withTimeout import org.matrix.rustcomponents.sdk.Room import org.matrix.rustcomponents.sdk.RoomListService -import org.matrix.rustcomponents.sdk.TimelineDiff -import org.matrix.rustcomponents.sdk.TimelineListener import kotlin.time.Duration.Companion.milliseconds /** @@ -56,16 +55,14 @@ class RoomContentForwarder( val failedForwardingTo = mutableSetOf() targetRooms.parallelMap { room -> room.use { targetRoom -> - val result = runCatching { + runCatching { // Sending a message requires a registered timeline listener - targetRoom.addTimelineListener(NoOpTimelineListener) - withTimeout(timeoutMs.milliseconds) { - targetRoom.send(content) + targetRoom.runWithTimelineListenerRegistered { + withTimeout(timeoutMs.milliseconds) { + targetRoom.send(content) + } } } - // After sending, we remove the timeline - targetRoom.removeTimeline() - result }.onFailure { failedForwardingTo.add(RoomId(room.id())) if (it is CancellationException) { @@ -78,8 +75,4 @@ class RoomContentForwarder( throw ForwardEventException(toRoomIds.toList()) } } - - private object NoOpTimelineListener : TimelineListener { - override fun onUpdate(diff: List) = Unit - } } diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RoomTimelineExtensions.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RoomTimelineExtensions.kt index bddd2bc872..e87ae74f30 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RoomTimelineExtensions.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RoomTimelineExtensions.kt @@ -70,3 +70,17 @@ internal fun Room.backPaginationStatusFlow(): Flow = subscribeToBackPaginationStatus(listener) } }.buffer(Channel.UNLIMITED) + +internal suspend fun Room.runWithTimelineListenerRegistered(action: suspend () -> Unit) { + val result = addTimelineListener(NoOpTimelineListener) + try { + action() + } finally { + result.itemsStream.cancelAndDestroy() + result.items.destroyAll() + } +} + +private object NoOpTimelineListener : TimelineListener { + override fun onUpdate(diff: List) = Unit +}