Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/3347.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes notifications not dismissing when reading messages on other devices
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import org.matrix.android.sdk.internal.database.model.TimelineEventEntity
import io.realm.Realm
import io.realm.RealmConfiguration

private const val MARK_OLD_EVENT_AS_READ = true
private const val MARK_UNREAD_DUE_TO_FASTLANE = false

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure declaring const for boolean returned value increases the readability of the code. Maybe a good comment is better?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I think this is a sign that the handleMissingEvent is the wrong name, renaming to hasReadMissingEvent allows us to infer the return result is has read or has not read

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


internal fun isEventRead(realmConfiguration: RealmConfiguration,
userId: String?,
roomId: String?,
Expand All @@ -39,10 +42,7 @@ internal fun isEventRead(realmConfiguration: RealmConfiguration,
val liveChunk = ChunkEntity.findLastForwardChunkOfRoom(realm, roomId) ?: return@use
val eventToCheck = liveChunk.timelineEvents.find(eventId)
isEventRead = when {
eventToCheck == null -> {
// This can happen in case of fast lane Event
false
}
eventToCheck == null -> handleMissingEvent(realm, liveChunk, roomId, userId, eventId)
eventToCheck.root?.sender == userId -> true
else -> {
val readReceipt = ReadReceiptEntity.where(realm, roomId, userId).findFirst()
Expand All @@ -59,6 +59,25 @@ internal fun isEventRead(realmConfiguration: RealmConfiguration,
return isEventRead
}

private fun handleMissingEvent(realm: Realm, latestChunkEntity: ChunkEntity, roomId: String, userId: String, eventId: String): Boolean {
return if (realm.doesEventExistInChunkHistory(eventId) && realm.hasReadReceiptInLatestChunk(latestChunkEntity, roomId, userId)) {
MARK_OLD_EVENT_AS_READ
} else {
// This can happen when fast lane events are displayed before the database finishes updating
MARK_UNREAD_DUE_TO_FASTLANE
}
}

private fun Realm.doesEventExistInChunkHistory(eventId: String): Boolean {
return ChunkEntity.findIncludingEvent(this, eventId) != null
}

private fun Realm.hasReadReceiptInLatestChunk(latestChunkEntity: ChunkEntity, roomId: String, userId: String) : Boolean {
return ReadReceiptEntity.where(this, roomId, userId).findFirst()?.let {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: The code is OK, but maybe use named arguments for roomId and userId to avoid confusion, since the types are the same (String)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 will do

latestChunkEntity.timelineEvents.find(it.eventId)
} != null
}

internal fun isReadMarkerMoreRecent(realmConfiguration: RealmConfiguration,
roomId: String?,
eventId: String?): Boolean {
Expand Down