@@ -23,6 +23,7 @@ import io.realm.RealmConfiguration
23
23
import io.realm.RealmResults
24
24
import io.realm.kotlin.createObject
25
25
import kotlinx.coroutines.CompletableDeferred
26
+ import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
26
27
import org.matrix.android.sdk.api.extensions.orFalse
27
28
import org.matrix.android.sdk.api.failure.Failure
28
29
import org.matrix.android.sdk.api.failure.MatrixError
@@ -41,6 +42,7 @@ import org.matrix.android.sdk.internal.database.query.findAllIncludingEvents
41
42
import org.matrix.android.sdk.internal.database.query.findLastForwardChunkOfThread
42
43
import org.matrix.android.sdk.internal.database.query.where
43
44
import org.matrix.android.sdk.internal.session.room.relation.threads.FetchThreadTimelineTask
45
+ import org.matrix.android.sdk.internal.session.room.state.StateEventDataSource
44
46
import org.matrix.android.sdk.internal.session.sync.handler.room.ThreadsAwarenessHandler
45
47
import org.matrix.android.sdk.internal.util.time.Clock
46
48
import timber.log.Timber
@@ -100,7 +102,9 @@ internal class LoadTimelineStrategy constructor(
100
102
val onEventsUpdated : (Boolean ) -> Unit ,
101
103
val onEventsDeleted : () -> Unit ,
102
104
val onLimitedTimeline : () -> Unit ,
103
- val onNewTimelineEvents : (List <String >) -> Unit
105
+ val onNewTimelineEvents : (List <String >) -> Unit ,
106
+ val stateEventDataSource : StateEventDataSource ,
107
+ val matrixCoroutineDispatchers : MatrixCoroutineDispatchers ,
104
108
)
105
109
106
110
private var getContextLatch: CompletableDeferred <Unit >? = null
@@ -165,7 +169,13 @@ internal class LoadTimelineStrategy constructor(
165
169
onEventsUpdated = dependencies.onEventsUpdated
166
170
)
167
171
168
- fun onStart () {
172
+ private val liveRoomStateListener = LiveRoomStateListener (
173
+ roomId,
174
+ dependencies.stateEventDataSource,
175
+ dependencies.matrixCoroutineDispatchers.main
176
+ )
177
+
178
+ suspend fun onStart () {
169
179
dependencies.eventDecryptor.start()
170
180
dependencies.timelineInput.listeners.add(timelineInputListener)
171
181
val realm = dependencies.realm.get()
@@ -174,9 +184,13 @@ internal class LoadTimelineStrategy constructor(
174
184
it.addChangeListener(chunkEntityListener)
175
185
timelineChunk = it.createTimelineChunk()
176
186
}
187
+
188
+ if (dependencies.timelineSettings.useLiveSenderInfo) {
189
+ liveRoomStateListener.start()
190
+ }
177
191
}
178
192
179
- fun onStop () {
193
+ suspend fun onStop () {
180
194
dependencies.eventDecryptor.destroy()
181
195
dependencies.timelineInput.listeners.remove(timelineInputListener)
182
196
chunkEntity?.removeChangeListener(chunkEntityListener)
@@ -188,6 +202,9 @@ internal class LoadTimelineStrategy constructor(
188
202
if (mode is Mode .Thread ) {
189
203
clearThreadChunkEntity(dependencies.realm.get(), mode.rootThreadEventId)
190
204
}
205
+ if (dependencies.timelineSettings.useLiveSenderInfo) {
206
+ liveRoomStateListener.stop()
207
+ }
191
208
}
192
209
193
210
suspend fun loadMore (count : Int , direction : Timeline .Direction , fetchOnServerIfNeeded : Boolean = true): LoadMoreResult {
@@ -222,7 +239,22 @@ internal class LoadTimelineStrategy constructor(
222
239
}
223
240
224
241
fun buildSnapshot (): List <TimelineEvent > {
225
- return buildSendingEvents() + timelineChunk?.builtItems(includesNext = true , includesPrev = true ).orEmpty()
242
+ val events = buildSendingEvents() + timelineChunk?.builtItems(includesNext = true , includesPrev = true ).orEmpty()
243
+ return if (dependencies.timelineSettings.useLiveSenderInfo) {
244
+ events.map(this ::applyLiveRoomState)
245
+ } else {
246
+ events
247
+ }
248
+ }
249
+
250
+ private fun applyLiveRoomState (event : TimelineEvent ): TimelineEvent {
251
+ val updatedState = liveRoomStateListener.getLiveState(event.senderInfo.userId)
252
+ return if (updatedState != null ) {
253
+ val updatedSenderInfo = event.senderInfo.copy(avatarUrl = updatedState.avatarUrl, displayName = updatedState.displayName)
254
+ event.copy(senderInfo = updatedSenderInfo)
255
+ } else {
256
+ event
257
+ }
226
258
}
227
259
228
260
private fun buildSendingEvents (): List <TimelineEvent > {
0 commit comments