Let rust encrypt method handle unencrypted content ( like relates_to)

This commit is contained in:
ganfra 2022-04-22 12:14:04 +02:00
parent 39755b08ee
commit 5581b82ab4
2 changed files with 6 additions and 24 deletions

View file

@ -29,9 +29,7 @@ import javax.inject.Inject
internal interface EncryptEventTask : Task<EncryptEventTask.Params, Event> { internal interface EncryptEventTask : Task<EncryptEventTask.Params, Event> {
data class Params(val roomId: String, data class Params(val roomId: String,
val event: Event, val event: Event
/**Do not encrypt these keys, keep them as is in encrypted content (e.g. m.relates_to)*/
val keepKeys: List<String>? = null
) )
} }
@ -49,22 +47,8 @@ internal class DefaultEncryptEventTask @Inject constructor(
localEchoRepository.updateSendState(localEvent.eventId, localEvent.roomId, SendState.ENCRYPTING) localEchoRepository.updateSendState(localEvent.eventId, localEvent.roomId, SendState.ENCRYPTING)
val localMutableContent = localEvent.content?.toMutableMap() ?: mutableMapOf()
params.keepKeys?.forEach {
localMutableContent.remove(it)
}
// let it throws // let it throws
val result = cryptoService.encryptEventContent(localMutableContent, localEvent.type, params.roomId) val result = cryptoService.encryptEventContent(localEvent.content ?: emptyMap(), localEvent.type, params.roomId)
val modifiedContent = HashMap(result.eventContent)
params.keepKeys?.forEach { toKeep ->
localEvent.content?.get(toKeep)?.let {
// put it back in the encrypted thing
modifiedContent[toKeep] = it
}
}
val safeResult = result.copy(eventContent = modifiedContent)
// Better handling of local echo, to avoid decrypting transition on remote echo // Better handling of local echo, to avoid decrypting transition on remote echo
// Should I only do it for text messages? // Should I only do it for text messages?
val decryptionLocalEcho = if (result.eventContent["algorithm"] == MXCRYPTO_ALGORITHM_MEGOLM) { val decryptionLocalEcho = if (result.eventContent["algorithm"] == MXCRYPTO_ALGORITHM_MEGOLM) {
@ -81,17 +65,16 @@ internal class DefaultEncryptEventTask @Inject constructor(
} else { } else {
null null
} }
localEchoRepository.updateEcho(localEvent.eventId) { _, localEcho -> localEchoRepository.updateEcho(localEvent.eventId) { _, localEcho ->
localEcho.type = EventType.ENCRYPTED localEcho.type = EventType.ENCRYPTED
localEcho.content = ContentMapper.map(modifiedContent) localEcho.content = ContentMapper.map(result.eventContent)
decryptionLocalEcho?.also { decryptionLocalEcho?.also {
localEcho.setDecryptionResult(it) localEcho.setDecryptionResult(it)
} }
} }
return localEvent.copy( return localEvent.copy(
type = safeResult.eventType, type = result.eventType,
content = safeResult.eventContent content = result.eventContent
) )
} }
} }

View file

@ -76,8 +76,7 @@ internal class DefaultSendEventTask @Inject constructor(
if (params.encrypt && !params.event.isEncrypted()) { if (params.encrypt && !params.event.isEncrypted()) {
return encryptEventTask.execute(EncryptEventTask.Params( return encryptEventTask.execute(EncryptEventTask.Params(
params.event.roomId ?: "", params.event.roomId ?: "",
params.event, params.event
listOf("m.relates_to")
)) ))
} }
return params.event return params.event