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
Refactor event handling in Crypto
Move the event-handler registration from client.js into crypto.js
This commit is contained in:
@@ -39,6 +39,9 @@ var DeviceVerification = DeviceInfo.DeviceVerification;
|
||||
*
|
||||
* @param {module:base-apis~MatrixBaseApis} baseApis base matrix api interface
|
||||
*
|
||||
* @param {external:EventEmitter} eventEmitter event source where we can register
|
||||
* for event notifications
|
||||
*
|
||||
* @param {module:store/session/webstorage~WebStorageSessionStore} sessionStore
|
||||
* Store to be used for end-to-end crypto session data
|
||||
*
|
||||
@@ -46,7 +49,7 @@ var DeviceVerification = DeviceInfo.DeviceVerification;
|
||||
*
|
||||
* @param {string} deviceId The identifier for this device.
|
||||
*/
|
||||
function Crypto(baseApis, sessionStore, userId, deviceId) {
|
||||
function Crypto(baseApis, eventEmitter, sessionStore, userId, deviceId) {
|
||||
this._baseApis = baseApis;
|
||||
this._sessionStore = sessionStore;
|
||||
this._userId = userId;
|
||||
@@ -81,6 +84,28 @@ function Crypto(baseApis, sessionStore, userId, deviceId) {
|
||||
this._sessionStore.storeEndToEndDevicesForUser(
|
||||
this._userId, myDevices
|
||||
);
|
||||
|
||||
_registerEventHandlers(this, eventEmitter);
|
||||
}
|
||||
|
||||
function _registerEventHandlers(crypto, eventEmitter) {
|
||||
eventEmitter.on(
|
||||
"RoomMember.membership",
|
||||
crypto._onRoomMembership.bind(crypto)
|
||||
);
|
||||
|
||||
eventEmitter.on("toDeviceEvent", function(event) {
|
||||
if (event.getType() == "m.room_key") {
|
||||
crypto._onRoomKeyEvent(event);
|
||||
}
|
||||
});
|
||||
|
||||
eventEmitter.on("event", function(event) {
|
||||
if (!event.isState() || event.getType() != "m.room.encryption") {
|
||||
return;
|
||||
}
|
||||
crypto._onCryptoEvent(event);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -772,13 +797,31 @@ Crypto.prototype.decryptEvent = function(event) {
|
||||
return alg.decryptEvent(event);
|
||||
};
|
||||
|
||||
/**
|
||||
* handle an m.room.encryption event
|
||||
*
|
||||
* @private
|
||||
* @param {module:models/event.MatrixEvent} event encryption event
|
||||
*/
|
||||
Crypto.prototype._onCryptoEvent = function(event) {
|
||||
var roomId = event.getRoomId();
|
||||
var content = event.getContent();
|
||||
|
||||
try {
|
||||
this.setRoomEncryption(roomId, content);
|
||||
} catch (e) {
|
||||
console.error("Error configuring encryption in room " + roomId +
|
||||
":", e);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle a key event
|
||||
*
|
||||
* @private
|
||||
* @param {module:models/event.MatrixEvent} event key event
|
||||
*/
|
||||
Crypto.prototype.onRoomKeyEvent = function(event) {
|
||||
Crypto.prototype._onRoomKeyEvent = function(event) {
|
||||
var content = event.getContent();
|
||||
var AlgClass = algorithms.DECRYPTION_CLASSES[content.algorithm];
|
||||
if (!AlgClass) {
|
||||
@@ -795,10 +838,11 @@ Crypto.prototype.onRoomKeyEvent = function(event) {
|
||||
/**
|
||||
* Handle a change in the membership state of a member of a room
|
||||
*
|
||||
* @private
|
||||
* @param {module:models/event.MatrixEvent} event event causing the change
|
||||
* @param {module:models/room-member} member user whose membership changed
|
||||
*/
|
||||
Crypto.prototype.onRoomMembership = function(event, member) {
|
||||
Crypto.prototype._onRoomMembership = function(event, member) {
|
||||
|
||||
// this event handler is registered on the *client* (as opposed to the
|
||||
// room member itself), which means it is only called on changes to the
|
||||
|
||||
Reference in New Issue
Block a user