17
17
package org.matrix.android.sdk.internal.database
18
18
19
19
import com.zhuinden.monarchy.Monarchy
20
+ import io.realm.Realm
20
21
import io.realm.RealmConfiguration
21
22
import io.realm.RealmResults
22
23
import kotlinx.coroutines.launch
23
24
import kotlinx.coroutines.sync.Mutex
24
25
import kotlinx.coroutines.sync.withLock
26
+ import org.matrix.android.sdk.api.session.events.model.Event
25
27
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
26
30
import org.matrix.android.sdk.internal.database.mapper.asDomain
27
31
import org.matrix.android.sdk.internal.database.model.EventEntity
28
32
import org.matrix.android.sdk.internal.database.model.EventInsertEntity
@@ -76,17 +80,17 @@ internal class EventInsertLiveObserver @Inject constructor(
76
80
Timber .v(" ##Transaction: There are ${filteredEvents.size} events to process " )
77
81
filteredEvents.forEach { eventInsert ->
78
82
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 " )
82
92
return @forEach
83
93
}
84
- val domainEvent = event.asDomain()
85
- processors.filter {
86
- it.shouldProcess(eventId, domainEvent.getClearType(), eventInsert.insertType)
87
- }.forEach {
88
- it.process(realm, domainEvent)
89
- }
90
94
}
91
95
realm.where(EventInsertEntity ::class .java)
92
96
.`in `(EventInsertEntityFields .EVENT_ID , idsToDeleteAfterProcess.toTypedArray())
@@ -104,6 +108,20 @@ internal class EventInsertLiveObserver @Inject constructor(
104
108
}
105
109
}
106
110
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
+
107
125
private fun shouldProcess (eventInsertEntity : EventInsertEntity ): Boolean {
108
126
return processors.any {
109
127
it.shouldProcess(eventInsertEntity.eventId, eventInsertEntity.eventType, eventInsertEntity.insertType)
0 commit comments