Rework edition of event management

This commit is contained in:
Benoit Marty 2021-03-01 15:03:47 +01:00 committed by Benoit Marty
parent c7e7bf4d2c
commit bdec23f740
2 changed files with 31 additions and 15 deletions

View File

@ -50,5 +50,5 @@ internal fun EventAnnotationsSummaryEntity.Companion.create(realm: Realm, roomId
internal fun EventAnnotationsSummaryEntity.Companion.getOrCreate(realm: Realm, roomId: String, eventId: String): EventAnnotationsSummaryEntity { internal fun EventAnnotationsSummaryEntity.Companion.getOrCreate(realm: Realm, roomId: String, eventId: String): EventAnnotationsSummaryEntity {
return EventAnnotationsSummaryEntity.where(realm, eventId).findFirst() return EventAnnotationsSummaryEntity.where(realm, eventId).findFirst()
?: EventAnnotationsSummaryEntity.create(realm, roomId, eventId).apply { this.roomId = roomId } ?: EventAnnotationsSummaryEntity.create(realm, roomId, eventId)
} }

View File

@ -191,29 +191,45 @@ internal class EventRelationsAggregationProcessor @Inject constructor(@UserId pr
// OPT OUT serer aggregation until API mature enough // OPT OUT serer aggregation until API mature enough
private val SHOULD_HANDLE_SERVER_AGREGGATION = false private val SHOULD_HANDLE_SERVER_AGREGGATION = false
private fun handleReplace(realm: Realm, event: Event, content: MessageContent, roomId: String, isLocalEcho: Boolean, relatedEventId: String? = null) { private fun handleReplace(realm: Realm,
event: Event,
content: MessageContent,
roomId: String,
isLocalEcho: Boolean,
relatedEventId: String? = null) {
val eventId = event.eventId ?: return val eventId = event.eventId ?: return
val targetEventId = relatedEventId ?: content.relatesTo?.eventId ?: return val targetEventId = relatedEventId ?: content.relatesTo?.eventId ?: return
val newContent = content.newContent ?: return val newContent = content.newContent ?: return
// Check that the sender is the same
val editedEvent = EventEntity.where(realm, targetEventId).findFirst()
if (editedEvent == null) {
// We do not know yet about the edited event
} else if (editedEvent.sender != event.senderId) {
// Edited by someone else, ignore
Timber.w("Ignore edition by someone else")
return
}
// ok, this is a replace // ok, this is a replace
val existing = EventAnnotationsSummaryEntity.getOrCreate(realm, roomId, targetEventId) val eventAnnotationsSummaryEntity = EventAnnotationsSummaryEntity.getOrCreate(realm, roomId, targetEventId)
// we have it // we have it
val existingSummary = existing.editSummary val existingSummary = eventAnnotationsSummaryEntity.editSummary
if (existingSummary == null) { if (existingSummary == null) {
Timber.v("###REPLACE new edit summary for $targetEventId, creating one (localEcho:$isLocalEcho)") Timber.v("###REPLACE new edit summary for $targetEventId, creating one (localEcho:$isLocalEcho)")
// create the edit summary // create the edit summary
val editSummary = realm.createObject(EditAggregatedSummaryEntity::class.java) eventAnnotationsSummaryEntity.editSummary = realm.createObject(EditAggregatedSummaryEntity::class.java)
editSummary.aggregatedContent = ContentMapper.map(newContent) .also { editSummary ->
if (isLocalEcho) { editSummary.aggregatedContent = ContentMapper.map(newContent)
editSummary.lastEditTs = 0 if (isLocalEcho) {
editSummary.sourceLocalEchoEvents.add(eventId) editSummary.lastEditTs = 0
} else { editSummary.sourceLocalEchoEvents.add(eventId)
editSummary.lastEditTs = event.originServerTs ?: 0 } else {
editSummary.sourceEvents.add(eventId) editSummary.lastEditTs = event.originServerTs ?: 0
} editSummary.sourceEvents.add(eventId)
}
existing.editSummary = editSummary }
} else { } else {
if (existingSummary.sourceEvents.contains(eventId)) { if (existingSummary.sourceEvents.contains(eventId)) {
// ignore this event, we already know it (??) // ignore this event, we already know it (??)