Still need to link empty chunks to glue everything together

Change-Id: Ic037db94d044c7271fe2f016a58c153945b73615
This commit is contained in:
SpiritCroc 2022-05-03 22:48:03 +02:00
parent 099bf5a016
commit 8158fff0f7
1 changed files with 29 additions and 7 deletions

View File

@ -182,6 +182,7 @@ internal class TokenChunkEventPersistor @Inject constructor(
} }
val optimizedThreadSummaryMap = hashMapOf<String, EventEntity>() val optimizedThreadSummaryMap = hashMapOf<String, EventEntity>()
var hasNewEvents = false var hasNewEvents = false
var existingChunkToLink: ChunkEntity? = null
run processTimelineEvents@{ run processTimelineEvents@{
eventList.forEach { event -> eventList.forEach { event ->
if (event.eventId == null || event.senderId == null) { if (event.eventId == null || event.senderId == null) {
@ -196,13 +197,6 @@ internal class TokenChunkEventPersistor @Inject constructor(
// If it exists, we want to stop here, just link the prevChunk // If it exists, we want to stop here, just link the prevChunk
val existingChunk = existingTimelineEvent?.chunk?.firstOrNull() val existingChunk = existingTimelineEvent?.chunk?.firstOrNull()
if (existingChunk != null) { if (existingChunk != null) {
// If we haven't found a single new event yet, we don't want to link in the pagination direction, as that might cause a
// timeline loop if the other chunk is in the other direction.
if (!hasNewEvents) {
Timber.i("Skip adding event $eventId, already exists")
// Only skip this event, but still process other events
return@forEach
}
val alreadyLinkedNext = currentChunk.doesNextChunksVerifyCondition { it == existingChunk } val alreadyLinkedNext = currentChunk.doesNextChunksVerifyCondition { it == existingChunk }
val alreadyLinkedPrev = currentChunk.doesPrevChunksVerifyCondition { it == existingChunk } val alreadyLinkedPrev = currentChunk.doesPrevChunksVerifyCondition { it == existingChunk }
if (alreadyLinkedNext || alreadyLinkedPrev) { if (alreadyLinkedNext || alreadyLinkedPrev) {
@ -217,6 +211,18 @@ internal class TokenChunkEventPersistor @Inject constructor(
// Stop processing here // Stop processing here
return@processTimelineEvents return@processTimelineEvents
} }
// If we haven't found a single new event yet, we don't want to link in the pagination direction, as that might cause a
// timeline loop if the other chunk is in the other direction.
if (!hasNewEvents) {
Timber.i("Skip adding event $eventId, already exists")
// Only skip this event, but still process other events.
// Remember this chunk, since in case we don't find any new events, we still want to link this in pagination direction
// in order to link a chunk to the /sync chunk
if (existingChunkToLink == null) {
existingChunkToLink = existingChunk
}
return@forEach
}
when (direction) { when (direction) {
PaginationDirection.BACKWARDS -> { PaginationDirection.BACKWARDS -> {
Timber.i("Backwards insert chunk: ${existingChunk.identifier()} -> ${currentChunk.identifier()}") Timber.i("Backwards insert chunk: ${existingChunk.identifier()} -> ${currentChunk.identifier()}")
@ -263,6 +269,22 @@ internal class TokenChunkEventPersistor @Inject constructor(
} }
} }
} }
val existingChunk = existingChunkToLink
if (!hasNewEvents && existingChunk != null) {
when (direction) {
PaginationDirection.BACKWARDS -> {
Timber.i("Backwards insert chunk: ${existingChunk.identifier()} -> ${currentChunk.identifier()}")
currentChunk.prevChunk = existingChunk
existingChunk.nextChunk = currentChunk
}
PaginationDirection.FORWARDS -> {
Timber.i("Forward insert chunk: ${currentChunk.identifier()} -> ${existingChunk.identifier()}")
currentChunk.nextChunk = existingChunk
existingChunk.prevChunk = currentChunk
}
}
}
if (currentChunk.isValid) { if (currentChunk.isValid) {
RoomEntity.where(realm, roomId).findFirst()?.addIfNecessary(currentChunk) RoomEntity.where(realm, roomId).findFirst()?.addIfNecessary(currentChunk)
} }