Fix modifying the wrong events in TimelineChunk

I was observing cases where builtEvents[modificationIndex] was not
having the same eventId as the udpatedEntity in handleDatabaseChangeSet.

In particular, I observed both cases that
- there was no item in the list yet with the same eventId as the updated
  one
- there was an item with the same eventId already in the list, but at a
  different position.

Whenever this happened, the timeline would render missing, duplicated,
or swapped messages in the timeline.

Instead of relying on the modificationIndex to be the same for both the
change set and builtEvents, look up the proper index by eventId.
This commit is contained in:
SpiritCroc 2022-03-12 09:32:25 +01:00
parent acfeb7ff65
commit cd45248f40
2 changed files with 6 additions and 1 deletions

1
changelog.d/5528.bugfix Normal file
View File

@ -0,0 +1 @@
Fix cases of missing, swapped, or duplicated messages

View File

@ -447,8 +447,12 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity,
for (range in modifications) {
for (modificationIndex in (range.startIndex until range.startIndex + range.length)) {
val updatedEntity = results[modificationIndex] ?: continue
val displayIndex = builtEventsIndexes.getOrDefault(updatedEntity.eventId, null)
if (displayIndex == null) {
continue
}
try {
builtEvents[modificationIndex] = updatedEntity.buildAndDecryptIfNeeded()
builtEvents[displayIndex] = updatedEntity.buildAndDecryptIfNeeded()
} catch (failure: Throwable) {
Timber.v("Fail to update items at index: $modificationIndex")
}