Merge pull request #8744 from element-hq/feature/bma/usedDecryptedEvent

[Crypto] Improve Event.getClearContent() and fix assignement issue.
This commit is contained in:
Benoit Marty 2024-02-02 09:47:22 +01:00 committed by GitHub
commit 1277f6fdd9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 16 additions and 9 deletions

1
changelog.d/8744.bugfix Normal file
View file

@ -0,0 +1 @@
Improve `Event.getClearContent()` and fix assignment issue that may help to decrypt last Event in the room list.

View file

@ -219,10 +219,16 @@ data class Event(
}
/**
* @return the event content
* @return the event content.
* If the content is encrypted, it will return the decrypted content, or null if the content is not
* decrypted.
*/
fun getClearContent(): Content? {
return getDecryptedContent() ?: content
return if (isEncrypted()) {
getDecryptedContent()
} else {
content
}
}
/**

View file

@ -627,7 +627,7 @@ internal class RustCryptoService @Inject constructor(
}
private fun notifyRoomKeyReceived(
roomId: String,
roomId: String?,
sessionId: String,
) {
megolmSessionImportManager.dispatchNewSession(roomId, sessionId)
@ -664,9 +664,9 @@ internal class RustCryptoService @Inject constructor(
when (event.type) {
EventType.ROOM_KEY -> {
val content = event.getClearContent().toModel<RoomKeyContent>() ?: return@forEach
content.sessionKey
val roomId = content.sessionId ?: return@forEach
val sessionId = content.sessionId
val roomId = content.roomId
val sessionId = content.sessionId ?: return@forEach
notifyRoomKeyReceived(roomId, sessionId)
matrixConfiguration.cryptoAnalyticsPlugin?.onRoomKeyImported(sessionId, EventType.ROOM_KEY)
@ -674,8 +674,8 @@ internal class RustCryptoService @Inject constructor(
EventType.FORWARDED_ROOM_KEY -> {
val content = event.getClearContent().toModel<ForwardedRoomKeyContent>() ?: return@forEach
val roomId = content.sessionId ?: return@forEach
val sessionId = content.sessionId
val roomId = content.roomId
val sessionId = content.sessionId ?: return@forEach
notifyRoomKeyReceived(roomId, sessionId)
matrixConfiguration.cryptoAnalyticsPlugin?.onRoomKeyImported(sessionId, EventType.FORWARDED_ROOM_KEY)

View file

@ -89,7 +89,7 @@ class ValidDecryptedEventTest {
).toContent()
)
val unValidatedContent = mixedEvent.getClearContent().toModel<MessageTextContent>()
val unValidatedContent = mixedEvent.content.toModel<MessageTextContent>()
unValidatedContent?.body shouldBe "some message"
mixedEvent.toValidDecryptedEvent()?.clearContent?.toModel<MessageTextContent>() shouldBe null