You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-12-19 10:22:30 +03:00
Refactor decryption
Create the MatrixEvent wrapper before decryption, and then pass that into the decryptors, which should update it. Also remove the workaround that sends m.new_device messages when we get an unknown session; it's just a bandaid which is obscuring more meaningful problems.
This commit is contained in:
@@ -463,38 +463,31 @@ MatrixClient.prototype.isRoomEncrypted = function(roomId) {
|
||||
* Decrypt a received event according to the algorithm specified in the event.
|
||||
*
|
||||
* @param {MatrixClient} client
|
||||
* @param {object} raw event
|
||||
*
|
||||
* @return {MatrixEvent}
|
||||
* @param {MatrixEvent} event
|
||||
*/
|
||||
function _decryptEvent(client, event) {
|
||||
if (!client._crypto) {
|
||||
return _badEncryptedMessage(event, "**Encryption not enabled**");
|
||||
_badEncryptedMessage(event, "**Encryption not enabled**");
|
||||
return;
|
||||
}
|
||||
|
||||
var decryptionResult;
|
||||
try {
|
||||
decryptionResult = client._crypto.decryptEvent(event);
|
||||
client._crypto.decryptEvent(event);
|
||||
} catch (e) {
|
||||
if (!(e instanceof Crypto.DecryptionError)) {
|
||||
throw e;
|
||||
}
|
||||
return _badEncryptedMessage(event, "**" + e.message + "**");
|
||||
_badEncryptedMessage(event, "**" + e.message + "**");
|
||||
return;
|
||||
}
|
||||
return new MatrixEvent(
|
||||
event, decryptionResult.payload,
|
||||
decryptionResult.keysProved,
|
||||
decryptionResult.keysClaimed
|
||||
);
|
||||
}
|
||||
|
||||
function _badEncryptedMessage(event, reason) {
|
||||
return new MatrixEvent(event, {
|
||||
event.setClearData({
|
||||
type: "m.room.message",
|
||||
content: {
|
||||
msgtype: "m.bad.encrypted",
|
||||
body: reason,
|
||||
content: event.content,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -2829,10 +2822,11 @@ function _resolve(callback, defer, res) {
|
||||
|
||||
function _PojoToMatrixEventMapper(client) {
|
||||
function mapper(plainOldJsObject) {
|
||||
if (plainOldJsObject.type === "m.room.encrypted") {
|
||||
return _decryptEvent(client, plainOldJsObject);
|
||||
var event = new MatrixEvent(plainOldJsObject);
|
||||
if (event.isEncrypted()) {
|
||||
_decryptEvent(client, event);
|
||||
}
|
||||
return new MatrixEvent(plainOldJsObject);
|
||||
return event;
|
||||
}
|
||||
return mapper;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user