Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Crypto] Improve Event.getClearContent() and fix assignement issue. #8744

Merged
merged 5 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ouch :/

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
Loading