Prune redacted events which are not explicitly restricted

This commit is contained in:
Florian Renaud 2022-11-17 11:54:00 +01:00
parent eb12b1c99b
commit 0209cc4969
1 changed files with 46 additions and 28 deletions

View File

@ -61,45 +61,33 @@ internal class RedactionEventProcessor @Inject constructor() : EventInsertLivePr
val isLocalEcho = LocalEcho.isLocalEchoId(redactionEvent.eventId ?: "") val isLocalEcho = LocalEcho.isLocalEchoId(redactionEvent.eventId ?: "")
Timber.v("Redact event for ${redactionEvent.redacts} localEcho=$isLocalEcho") Timber.v("Redact event for ${redactionEvent.redacts} localEcho=$isLocalEcho")
val eventToPrune = EventEntity.where(realm, eventId = redactionEvent.redacts).findFirst() val eventToPrune = EventEntity.where(realm, eventId = redactionEvent.redacts).findFirst() ?: return
?: return
val typeToPrune = eventToPrune.type val typeToPrune = eventToPrune.type
val stateKey = eventToPrune.stateKey val stateKey = eventToPrune.stateKey
val allowedKeys = computeAllowedKeys(typeToPrune) val allowedKeys = computeAllowedKeys(typeToPrune)
if (allowedKeys.isNotEmpty()) { when {
val prunedContent = ContentMapper.map(eventToPrune.content)?.filterKeys { key -> allowedKeys.contains(key) } allowedKeys.isNotEmpty() -> {
eventToPrune.content = ContentMapper.map(prunedContent) val prunedContent = ContentMapper.map(eventToPrune.content)?.filterKeys { key -> allowedKeys.contains(key) }
} else { eventToPrune.content = ContentMapper.map(prunedContent)
when (typeToPrune) { }
EventType.ENCRYPTED, canPruneEventType(typeToPrune) -> {
EventType.MESSAGE, Timber.d("REDACTION for message ${eventToPrune.eventId}")
in EventType.STATE_ROOM_BEACON_INFO.values, val unsignedData = EventMapper.map(eventToPrune).unsignedData ?: UnsignedData(null, null)
in EventType.BEACON_LOCATION_DATA.values,
in EventType.POLL_START.values -> {
Timber.d("REDACTION for message ${eventToPrune.eventId}")
val unsignedData = EventMapper.map(eventToPrune).unsignedData
?: UnsignedData(null, null)
// was this event a m.replace // was this event a m.replace
// val contentModel = ContentMapper.map(eventToPrune.content)?.toModel<MessageContent>() // val contentModel = ContentMapper.map(eventToPrune.content)?.toModel<MessageContent>()
// if (RelationType.REPLACE == contentModel?.relatesTo?.type && contentModel.relatesTo?.eventId != null) { // if (RelationType.REPLACE == contentModel?.relatesTo?.type && contentModel.relatesTo?.eventId != null) {
// eventRelationsAggregationUpdater.handleRedactionOfReplace(eventToPrune, contentModel.relatesTo!!.eventId!!, realm) // eventRelationsAggregationUpdater.handleRedactionOfReplace(eventToPrune, contentModel.relatesTo!!.eventId!!, realm)
// } // }
val modified = unsignedData.copy(redactedEvent = redactionEvent) val modified = unsignedData.copy(redactedEvent = redactionEvent)
// Deleting the content of a thread message will result to delete the thread relation, however threads are now dynamic eventToPrune.content = ContentMapper.map(emptyMap())
// so there is not much of a problem eventToPrune.unsignedData = MoshiProvider.providesMoshi().adapter(UnsignedData::class.java).toJson(modified)
eventToPrune.content = ContentMapper.map(emptyMap()) eventToPrune.decryptionResultJson = null
eventToPrune.unsignedData = MoshiProvider.providesMoshi().adapter(UnsignedData::class.java).toJson(modified) eventToPrune.decryptionErrorCode = null
eventToPrune.decryptionResultJson = null
eventToPrune.decryptionErrorCode = null
handleTimelineThreadSummaryIfNeeded(realm, eventToPrune, isLocalEcho) handleTimelineThreadSummaryIfNeeded(realm, eventToPrune, isLocalEcho)
}
// EventType.REACTION -> {
// eventRelationsAggregationUpdater.handleReactionRedact(eventToPrune, realm, userId)
// }
} }
} }
if (typeToPrune == EventType.STATE_ROOM_MEMBER && stateKey != null) { if (typeToPrune == EventType.STATE_ROOM_MEMBER && stateKey != null) {
@ -167,4 +155,34 @@ internal class RedactionEventProcessor @Inject constructor() : EventInsertLivePr
else -> emptyList() else -> emptyList()
} }
} }
private fun canPruneEventType(eventType: String): Boolean {
return when {
EventType.isCallEvent(eventType) -> false
EventType.isVerificationEvent(eventType) -> false
eventType == EventType.ROOM_KEY ||
eventType == EventType.STATE_ROOM_WIDGET_LEGACY ||
eventType == EventType.STATE_ROOM_WIDGET ||
eventType == EventType.STATE_ROOM_NAME ||
eventType == EventType.STATE_ROOM_TOPIC ||
eventType == EventType.STATE_ROOM_AVATAR ||
eventType == EventType.STATE_ROOM_THIRD_PARTY_INVITE ||
eventType == EventType.STATE_ROOM_GUEST_ACCESS ||
eventType == EventType.STATE_SPACE_CHILD ||
eventType == EventType.STATE_SPACE_PARENT ||
eventType == EventType.STATE_ROOM_TOMBSTONE ||
eventType == EventType.STATE_ROOM_HISTORY_VISIBILITY ||
eventType == EventType.STATE_ROOM_RELATED_GROUPS ||
eventType == EventType.STATE_ROOM_PINNED_EVENT ||
eventType == EventType.STATE_ROOM_ENCRYPTION ||
eventType == EventType.STATE_ROOM_SERVER_ACL ||
eventType == EventType.ROOM_KEY_REQUEST ||
eventType == EventType.FORWARDED_ROOM_KEY ||
eventType in EventType.ROOM_KEY_WITHHELD.values ||
eventType == EventType.REQUEST_SECRET ||
eventType == EventType.SEND_SECRET ||
eventType == EventType.REACTION -> false
else -> true
}
}
} }