Skip to content

Commit 2ccedc1

Browse files
authored
Merge pull request #913 from vector-im/feature/fga/avoid_spamming_sync_flow
Avoid spamming sync flow by checking item origin
2 parents 8986cf5 + 5f225ce commit 2ccedc1

File tree

5 files changed

+78
-5
lines changed

5 files changed

+78
-5
lines changed

libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/timeline/item/event/EventContent.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ data class MessageContent(
3535

3636

3737
sealed interface InReplyTo {
38+
/** The event details are not loaded yet. We can fetch them. */
3839
data class NotLoaded(val eventId: EventId) : InReplyTo
40+
41+
/** The event details are pending to be fetched. We should **not** fetch them again. */
42+
object Pending : InReplyTo
43+
44+
/** The event details are available. */
3945
data class Ready(
4046
val eventId: EventId,
4147
val content: MessageContent,
@@ -44,6 +50,14 @@ sealed interface InReplyTo {
4450
val senderAvatarUrl: String?,
4551
) : InReplyTo
4652

53+
/**
54+
* Fetching the event details failed.
55+
*
56+
* We can try to fetch them again **with a proper retry strategy**, but not blindly:
57+
*
58+
* If the reason for the failure is consistent on the server, we'd enter a loop
59+
* where we keep trying to fetch the same event.
60+
* */
4761
object Error : InReplyTo
4862
}
4963

libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/timeline/item/event/EventTimelineItem.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ data class EventTimelineItem(
4242
}
4343
fun hasNotLoadedInReplyTo(): Boolean {
4444
val details = inReplyTo()
45-
return details is InReplyTo.NotLoaded || details is InReplyTo.Error
45+
return details is InReplyTo.NotLoaded
4646
}
4747
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import io.element.android.libraries.matrix.impl.media.map
4141
import io.element.android.libraries.matrix.impl.room.location.toInner
4242
import io.element.android.libraries.matrix.impl.timeline.RustMatrixTimeline
4343
import io.element.android.libraries.matrix.impl.timeline.backPaginationStatusFlow
44+
import io.element.android.libraries.matrix.impl.timeline.eventOrigin
4445
import io.element.android.libraries.matrix.impl.timeline.timelineDiffFlow
4546
import io.element.android.libraries.sessionstorage.api.SessionData
4647
import io.element.android.services.toolbox.api.systemclock.SystemClock
@@ -54,6 +55,7 @@ import kotlinx.coroutines.flow.launchIn
5455
import kotlinx.coroutines.flow.onEach
5556
import kotlinx.coroutines.launch
5657
import kotlinx.coroutines.withContext
58+
import org.matrix.rustcomponents.sdk.EventItemOrigin
5759
import org.matrix.rustcomponents.sdk.RequiredState
5860
import org.matrix.rustcomponents.sdk.Room
5961
import org.matrix.rustcomponents.sdk.RoomListItem
@@ -119,9 +121,11 @@ class RustMatrixRoom(
119121
roomCoroutineScope.launch(roomDispatcher) {
120122
innerRoom.timelineDiffFlow { initialList ->
121123
_timeline.postItems(initialList)
122-
}.onEach {
123-
_syncUpdateFlow.value = systemClock.epochMillis()
124-
_timeline.postDiff(it)
124+
}.onEach { diff ->
125+
if (diff.eventOrigin() == EventItemOrigin.SYNC) {
126+
_syncUpdateFlow.value = systemClock.epochMillis()
127+
}
128+
_timeline.postDiff(diff)
125129
}.launchIn(this)
126130

127131
innerRoom.backPaginationStatusFlow()
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2023 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.element.android.libraries.matrix.impl.timeline
18+
19+
import org.matrix.rustcomponents.sdk.EventItemOrigin
20+
import org.matrix.rustcomponents.sdk.TimelineChange
21+
import org.matrix.rustcomponents.sdk.TimelineDiff
22+
import org.matrix.rustcomponents.sdk.TimelineItem
23+
24+
/**
25+
* Tries to get an event origin from the TimelineDiff.
26+
* If there is multiple events in the diff, uses the first one as it should be a good indicator.
27+
*/
28+
internal fun TimelineDiff.eventOrigin(): EventItemOrigin? {
29+
return when (change()) {
30+
TimelineChange.APPEND -> {
31+
append()?.firstOrNull()?.eventOrigin()
32+
}
33+
TimelineChange.PUSH_BACK -> {
34+
pushBack()?.eventOrigin()
35+
}
36+
TimelineChange.PUSH_FRONT -> {
37+
pushFront()?.eventOrigin()
38+
}
39+
TimelineChange.SET -> {
40+
set()?.item?.eventOrigin()
41+
}
42+
TimelineChange.INSERT -> {
43+
insert()?.item?.eventOrigin()
44+
}
45+
TimelineChange.RESET -> {
46+
reset()?.firstOrNull()?.eventOrigin()
47+
}
48+
else -> null
49+
}
50+
}
51+
52+
private fun TimelineItem.eventOrigin(): EventItemOrigin? {
53+
return asEvent()?.origin()
54+
}

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/item/event/EventMessageMapper.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ class EventMessageMapper {
5858
)
5959
}
6060
is RepliedToEventDetails.Error -> InReplyTo.Error
61-
is RepliedToEventDetails.Pending, is RepliedToEventDetails.Unavailable -> InReplyTo.NotLoaded(inReplyToId!!)
61+
is RepliedToEventDetails.Pending -> InReplyTo.Pending
62+
is RepliedToEventDetails.Unavailable -> InReplyTo.NotLoaded(inReplyToId!!)
6263
}
6364
}
6465
MessageContent(

0 commit comments

Comments
 (0)