Skip to content

Commit 2366227

Browse files
authored
Merge pull request #4129 from vector-im/feature/adm/sync-read-notifications
Fixing notifications not being dismissed when read from other devices
2 parents 587c634 + c72f668 commit 2366227

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

changelog.d/3347.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes notifications not dismissing when reading messages on other devices

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/query/ReadQueries.kt

+28-11
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,43 @@ internal fun isEventRead(realmConfiguration: RealmConfiguration,
3939
val liveChunk = ChunkEntity.findLastForwardChunkOfRoom(realm, roomId) ?: return@use
4040
val eventToCheck = liveChunk.timelineEvents.find(eventId)
4141
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+
)
4649
eventToCheck.root?.sender == userId -> true
4750
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
5554
}
5655
}
5756
}
5857

5958
return isEventRead
6059
}
6160

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+
6279
internal fun isReadMarkerMoreRecent(realmConfiguration: RealmConfiguration,
6380
roomId: String?,
6481
eventId: String?): Boolean {

0 commit comments

Comments
 (0)