1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-29 16:43:09 +03:00

Add MatrixEvent.attemptDecryption

... and use it from both MatrixClient and the megolm re-decryption code.

This will help us avoid races when decryption is asynchronous.
This commit is contained in:
Richard van der Hoff
2017-07-18 14:41:25 +01:00
parent d37cbb10a5
commit 86f2c86440
3 changed files with 54 additions and 44 deletions

View File

@@ -640,42 +640,6 @@ MatrixClient.prototype.importRoomKeys = async function(keys) {
this._crypto.importRoomKeys(keys);
};
/**
* Decrypt a received event according to the algorithm specified in the event.
*
* @param {MatrixClient} client
* @param {MatrixEvent} event
*/
function _decryptEvent(client, event) {
if (!client._crypto) {
_badEncryptedMessage(event, "Encryption not enabled");
return;
}
try {
client._crypto.decryptEvent(event);
} catch (e) {
console.warn(
`Error decrypting event (id=${event.getId()}): ${e}`,
);
if (e.name !== "DecryptionError") {
throw e;
}
_badEncryptedMessage(event, e.message);
return;
}
}
function _badEncryptedMessage(event, reason) {
event.setClearData({
type: "m.room.message",
content: {
msgtype: "m.bad.encrypted",
body: "** Unable to decrypt: " + reason + " **",
},
});
}
// Room ops
// ========
@@ -3223,7 +3187,7 @@ function _PojoToMatrixEventMapper(client) {
function mapper(plainOldJsObject) {
const event = new MatrixEvent(plainOldJsObject);
if (event.isEncrypted()) {
_decryptEvent(client, event);
event.attemptDecryption(client._crypto);
}
return event;
}