@@ -39,26 +39,43 @@ internal fun isEventRead(realmConfiguration: RealmConfiguration,
39
39
val liveChunk = ChunkEntity .findLastForwardChunkOfRoom(realm, roomId) ? : return @use
40
40
val eventToCheck = liveChunk.timelineEvents.find(eventId)
41
41
isEventRead = when {
42
- eventToCheck == null -> {
43
- // This can happen in case of fast lane Event
44
- false
45
- }
42
+ eventToCheck == null -> hasReadMissingEvent(
43
+ realm = realm,
44
+ latestChunkEntity = liveChunk,
45
+ roomId = roomId,
46
+ userId = userId,
47
+ eventId = eventId
48
+ )
46
49
eventToCheck.root?.sender == userId -> true
47
50
else -> {
48
- val readReceipt = ReadReceiptEntity .where(realm, roomId, userId).findFirst()
49
- ? : return @use
50
- val readReceiptIndex = liveChunk.timelineEvents.find(readReceipt.eventId)?.displayIndex
51
- ? : Int .MIN_VALUE
52
- val eventToCheckIndex = eventToCheck.displayIndex
53
-
54
- eventToCheckIndex <= readReceiptIndex
51
+ val readReceipt = ReadReceiptEntity .where(realm, roomId, userId).findFirst() ? : return @use
52
+ val readReceiptIndex = liveChunk.timelineEvents.find(readReceipt.eventId)?.displayIndex ? : Int .MIN_VALUE
53
+ eventToCheck.displayIndex <= readReceiptIndex
55
54
}
56
55
}
57
56
}
58
57
59
58
return isEventRead
60
59
}
61
60
61
+ /* *
62
+ * Missing events can be caused by the latest timeline chunk no longer contain an older event or
63
+ * by fast lane eagerly displaying events before the database has finished updating
64
+ */
65
+ private fun hasReadMissingEvent (realm : Realm , latestChunkEntity : ChunkEntity , roomId : String , userId : String , eventId : String ): Boolean {
66
+ return realm.doesEventExistInChunkHistory(eventId) && realm.hasReadReceiptInLatestChunk(latestChunkEntity, roomId, userId)
67
+ }
68
+
69
+ private fun Realm.doesEventExistInChunkHistory (eventId : String ): Boolean {
70
+ return ChunkEntity .findIncludingEvent(this , eventId) != null
71
+ }
72
+
73
+ private fun Realm.hasReadReceiptInLatestChunk (latestChunkEntity : ChunkEntity , roomId : String , userId : String ): Boolean {
74
+ return ReadReceiptEntity .where(this , roomId = roomId, userId = userId).findFirst()?.let {
75
+ latestChunkEntity.timelineEvents.find(it.eventId)
76
+ } != null
77
+ }
78
+
62
79
internal fun isReadMarkerMoreRecent (realmConfiguration : RealmConfiguration ,
63
80
roomId : String? ,
64
81
eventId : String? ): Boolean {
0 commit comments