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

View file

@ -61,25 +61,19 @@ internal class RedactionEventProcessor @Inject constructor() : EventInsertLivePr
val isLocalEcho = LocalEcho.isLocalEchoId(redactionEvent.eventId ?: "")
Timber.v("Redact event for ${redactionEvent.redacts} localEcho=$isLocalEcho")
val eventToPrune = EventEntity.where(realm, eventId = redactionEvent.redacts).findFirst()
?: return
val eventToPrune = EventEntity.where(realm, eventId = redactionEvent.redacts).findFirst() ?: return
val typeToPrune = eventToPrune.type
val stateKey = eventToPrune.stateKey
val allowedKeys = computeAllowedKeys(typeToPrune)
if (allowedKeys.isNotEmpty()) {
when {
allowedKeys.isNotEmpty() -> {
val prunedContent = ContentMapper.map(eventToPrune.content)?.filterKeys { key -> allowedKeys.contains(key) }
eventToPrune.content = ContentMapper.map(prunedContent)
} else {
when (typeToPrune) {
EventType.ENCRYPTED,
EventType.MESSAGE,
in EventType.STATE_ROOM_BEACON_INFO.values,
in EventType.BEACON_LOCATION_DATA.values,
in EventType.POLL_START.values -> {
}
canPruneEventType(typeToPrune) -> {
Timber.d("REDACTION for message ${eventToPrune.eventId}")
val unsignedData = EventMapper.map(eventToPrune).unsignedData
?: UnsignedData(null, null)
val unsignedData = EventMapper.map(eventToPrune).unsignedData ?: UnsignedData(null, null)
// was this event a m.replace
// val contentModel = ContentMapper.map(eventToPrune.content)?.toModel<MessageContent>()
@ -88,8 +82,6 @@ internal class RedactionEventProcessor @Inject constructor() : EventInsertLivePr
// }
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
// so there is not much of a problem
eventToPrune.content = ContentMapper.map(emptyMap())
eventToPrune.unsignedData = MoshiProvider.providesMoshi().adapter(UnsignedData::class.java).toJson(modified)
eventToPrune.decryptionResultJson = null
@ -97,10 +89,6 @@ internal class RedactionEventProcessor @Inject constructor() : EventInsertLivePr
handleTimelineThreadSummaryIfNeeded(realm, eventToPrune, isLocalEcho)
}
// EventType.REACTION -> {
// eventRelationsAggregationUpdater.handleReactionRedact(eventToPrune, realm, userId)
// }
}
}
if (typeToPrune == EventType.STATE_ROOM_MEMBER && stateKey != null) {
TimelineEventEntity.findWithSenderMembershipEvent(realm, eventToPrune.eventId).forEach {
@ -167,4 +155,34 @@ internal class RedactionEventProcessor @Inject constructor() : EventInsertLivePr
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
}
}
}