Skip to content

Commit 1277f6f

Browse files
authored
Merge pull request #8744 from element-hq/feature/bma/usedDecryptedEvent
[Crypto] Improve Event.getClearContent() and fix assignement issue.
2 parents 96648bb + 3611052 commit 1277f6f

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

changelog.d/8744.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve `Event.getClearContent()` and fix assignment issue that may help to decrypt last Event in the room list.

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/events/model/Event.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,16 @@ data class Event(
219219
}
220220

221221
/**
222-
* @return the event content
222+
* @return the event content.
223+
* If the content is encrypted, it will return the decrypted content, or null if the content is not
224+
* decrypted.
223225
*/
224226
fun getClearContent(): Content? {
225-
return getDecryptedContent() ?: content
227+
return if (isEncrypted()) {
228+
getDecryptedContent()
229+
} else {
230+
content
231+
}
226232
}
227233

228234
/**

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustCryptoService.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ internal class RustCryptoService @Inject constructor(
627627
}
628628

629629
private fun notifyRoomKeyReceived(
630-
roomId: String,
630+
roomId: String?,
631631
sessionId: String,
632632
) {
633633
megolmSessionImportManager.dispatchNewSession(roomId, sessionId)
@@ -664,18 +664,18 @@ internal class RustCryptoService @Inject constructor(
664664
when (event.type) {
665665
EventType.ROOM_KEY -> {
666666
val content = event.getClearContent().toModel<RoomKeyContent>() ?: return@forEach
667-
content.sessionKey
668-
val roomId = content.sessionId ?: return@forEach
669-
val sessionId = content.sessionId
667+
668+
val roomId = content.roomId
669+
val sessionId = content.sessionId ?: return@forEach
670670

671671
notifyRoomKeyReceived(roomId, sessionId)
672672
matrixConfiguration.cryptoAnalyticsPlugin?.onRoomKeyImported(sessionId, EventType.ROOM_KEY)
673673
}
674674
EventType.FORWARDED_ROOM_KEY -> {
675675
val content = event.getClearContent().toModel<ForwardedRoomKeyContent>() ?: return@forEach
676676

677-
val roomId = content.sessionId ?: return@forEach
678-
val sessionId = content.sessionId
677+
val roomId = content.roomId
678+
val sessionId = content.sessionId ?: return@forEach
679679

680680
notifyRoomKeyReceived(roomId, sessionId)
681681
matrixConfiguration.cryptoAnalyticsPlugin?.onRoomKeyImported(sessionId, EventType.FORWARDED_ROOM_KEY)

matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/session/event/ValidDecryptedEventTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class ValidDecryptedEventTest {
8989
).toContent()
9090
)
9191

92-
val unValidatedContent = mixedEvent.getClearContent().toModel<MessageTextContent>()
92+
val unValidatedContent = mixedEvent.content.toModel<MessageTextContent>()
9393
unValidatedContent?.body shouldBe "some message"
9494

9595
mixedEvent.toValidDecryptedEvent()?.clearContent?.toModel<MessageTextContent>() shouldBe null

0 commit comments

Comments
 (0)