Skip to content

Commit ee3cbd9

Browse files
committed
Filter in only encrypted events with relatesTo content
1 parent d1ce15b commit ee3cbd9

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
package org.matrix.android.sdk.internal.database
1818

1919
import com.zhuinden.monarchy.Monarchy
20+
import io.realm.Realm
2021
import io.realm.RealmConfiguration
2122
import io.realm.RealmResults
2223
import kotlinx.coroutines.launch
2324
import kotlinx.coroutines.sync.Mutex
2425
import kotlinx.coroutines.sync.withLock
26+
import org.matrix.android.sdk.api.session.events.model.Event
2527
import org.matrix.android.sdk.api.session.events.model.EventType
28+
import org.matrix.android.sdk.api.session.events.model.content.EncryptedEventContent
29+
import org.matrix.android.sdk.api.session.events.model.toModel
2630
import org.matrix.android.sdk.internal.database.mapper.asDomain
2731
import org.matrix.android.sdk.internal.database.model.EventEntity
2832
import org.matrix.android.sdk.internal.database.model.EventInsertEntity
@@ -76,17 +80,17 @@ internal class EventInsertLiveObserver @Inject constructor(
7680
Timber.v("##Transaction: There are ${filteredEvents.size} events to process ")
7781
filteredEvents.forEach { eventInsert ->
7882
val eventId = eventInsert.eventId
79-
val event = EventEntity.where(realm, eventId).findFirst()
80-
if (event == null) {
81-
Timber.v("Event $eventId not found")
83+
val event = getEvent(realm, eventId)
84+
if (event != null && canProcessEvent(event)) {
85+
processors.filter {
86+
it.shouldProcess(eventId, event.getClearType(), eventInsert.insertType)
87+
}.forEach {
88+
it.process(realm, event)
89+
}
90+
} else {
91+
Timber.v("Cannot process event with id $eventId")
8292
return@forEach
8393
}
84-
val domainEvent = event.asDomain()
85-
processors.filter {
86-
it.shouldProcess(eventId, domainEvent.getClearType(), eventInsert.insertType)
87-
}.forEach {
88-
it.process(realm, domainEvent)
89-
}
9094
}
9195
realm.where(EventInsertEntity::class.java)
9296
.`in`(EventInsertEntityFields.EVENT_ID, idsToDeleteAfterProcess.toTypedArray())
@@ -104,6 +108,20 @@ internal class EventInsertLiveObserver @Inject constructor(
104108
}
105109
}
106110

111+
private fun getEvent(realm: Realm, eventId: String): Event? {
112+
val event = EventEntity.where(realm, eventId).findFirst()
113+
if (event == null) {
114+
Timber.v("Event $eventId not found")
115+
}
116+
return event?.asDomain()
117+
}
118+
119+
private fun canProcessEvent(event: Event): Boolean {
120+
// event should be either not encrypted or if encrypted it should contain relatesTo content
121+
return event.getClearType() != EventType.ENCRYPTED ||
122+
event.content.toModel<EncryptedEventContent>()?.relatesTo != null
123+
}
124+
107125
private fun shouldProcess(eventInsertEntity: EventInsertEntity): Boolean {
108126
return processors.any {
109127
it.shouldProcess(eventInsertEntity.eventId, eventInsertEntity.eventType, eventInsertEntity.insertType)

0 commit comments

Comments
 (0)