Skip to content

Commit

Permalink
Merge pull request #8744 from element-hq/feature/bma/usedDecryptedEvent
Browse files Browse the repository at this point in the history
[Crypto] Improve Event.getClearContent() and fix assignement issue.
  • Loading branch information
bmarty authored Feb 2, 2024
2 parents 96648bb + 3611052 commit 1277f6f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
1 change: 1 addition & 0 deletions changelog.d/8744.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve `Event.getClearContent()` and fix assignment issue that may help to decrypt last Event in the room list.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ internal class RustCryptoService @Inject constructor(
}

private fun notifyRoomKeyReceived(
roomId: String,
roomId: String?,
sessionId: String,
) {
megolmSessionImportManager.dispatchNewSession(roomId, sessionId)
Expand Down Expand Up @@ -664,18 +664,18 @@ 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)
}
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1277f6f

Please sign in to comment.