You've already forked matrix-js-sdk
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:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user