Skip to content

Commit 3886048

Browse files
authored
Merge pull request #1825 from vector-im/feature/fga/suppress_remove_timeline_usage
Suppress usage of removeTimeline method (#1824)
2 parents ff3ee5a + d36943c commit 3886048

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

changelog.d/1824.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Suppress usage of removeTimeline method.

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RoomContentForwarder.kt

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ import io.element.android.libraries.matrix.api.core.EventId
2121
import io.element.android.libraries.matrix.api.core.RoomId
2222
import io.element.android.libraries.matrix.api.room.ForwardEventException
2323
import io.element.android.libraries.matrix.impl.roomlist.roomOrNull
24+
import io.element.android.libraries.matrix.impl.timeline.runWithTimelineListenerRegistered
2425
import kotlinx.coroutines.CancellationException
2526
import kotlinx.coroutines.withTimeout
2627
import org.matrix.rustcomponents.sdk.Room
2728
import org.matrix.rustcomponents.sdk.RoomListService
28-
import org.matrix.rustcomponents.sdk.TimelineDiff
29-
import org.matrix.rustcomponents.sdk.TimelineListener
3029
import kotlin.time.Duration.Companion.milliseconds
3130

3231
/**
@@ -56,16 +55,14 @@ class RoomContentForwarder(
5655
val failedForwardingTo = mutableSetOf<RoomId>()
5756
targetRooms.parallelMap { room ->
5857
room.use { targetRoom ->
59-
val result = runCatching {
58+
runCatching {
6059
// Sending a message requires a registered timeline listener
61-
targetRoom.addTimelineListener(NoOpTimelineListener)
62-
withTimeout(timeoutMs.milliseconds) {
63-
targetRoom.send(content)
60+
targetRoom.runWithTimelineListenerRegistered {
61+
withTimeout(timeoutMs.milliseconds) {
62+
targetRoom.send(content)
63+
}
6464
}
6565
}
66-
// After sending, we remove the timeline
67-
targetRoom.removeTimeline()
68-
result
6966
}.onFailure {
7067
failedForwardingTo.add(RoomId(room.id()))
7168
if (it is CancellationException) {
@@ -78,8 +75,4 @@ class RoomContentForwarder(
7875
throw ForwardEventException(toRoomIds.toList())
7976
}
8077
}
81-
82-
private object NoOpTimelineListener : TimelineListener {
83-
override fun onUpdate(diff: List<TimelineDiff>) = Unit
84-
}
8578
}

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RoomTimelineExtensions.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,17 @@ internal fun Room.backPaginationStatusFlow(): Flow<BackPaginationStatus> =
7070
subscribeToBackPaginationStatus(listener)
7171
}
7272
}.buffer(Channel.UNLIMITED)
73+
74+
internal suspend fun Room.runWithTimelineListenerRegistered(action: suspend () -> Unit) {
75+
val result = addTimelineListener(NoOpTimelineListener)
76+
try {
77+
action()
78+
} finally {
79+
result.itemsStream.cancelAndDestroy()
80+
result.items.destroyAll()
81+
}
82+
}
83+
84+
private object NoOpTimelineListener : TimelineListener {
85+
override fun onUpdate(diff: List<TimelineDiff>) = Unit
86+
}

0 commit comments

Comments
 (0)