1
0
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:
Richard van der Hoff
2016-09-07 22:10:52 +01:00
parent d706b57fe9
commit 6fe4dfcad0
2 changed files with 58 additions and 41 deletions

View File

@@ -151,12 +151,10 @@ function MatrixClient(opts) {
if (CRYPTO_ENABLED && opts.sessionStore !== null &&
userId !== null && this.deviceId !== null) {
this._crypto = new Crypto(
this,
this, this,
opts.sessionStore,
userId, this.deviceId
);
setupCryptoEventHandler(this);
}
}
utils.inherits(MatrixClient, EventEmitter);
@@ -408,41 +406,6 @@ MatrixClient.prototype.isEventSenderVerified = function(event) {
);
};
/**
* Register a listener for m.room.encryption events which will enable encryption
* for a room.
*
* @param {MatrixClient} client
*/
function setupCryptoEventHandler(client) {
client.on("event", function(event) {
if (!event.isState() || event.getType() != "m.room.encryption") {
return;
}
onCryptoEvent(client, event);
});
client.on("RoomMember.membership",
client._crypto.onRoomMembership.bind(client._crypto));
client.on("toDeviceEvent", function(event) {
if (event.getType() == "m.room_key") {
client._crypto.onRoomKeyEvent(event);
}
});
}
function onCryptoEvent(client, event) {
var roomId = event.getRoomId();
var content = event.getContent();
try {
client.setRoomEncryption(roomId, content).done();
} catch (e) {
console.error("Error configuring encryption in room " + roomId +
":", e);
}
}
/**
* Enable end-to-end encryption for a room.
* @param {string} roomId The room ID to enable encryption in.
@@ -2694,6 +2657,16 @@ module.exports.CRYPTO_ENABLED = CRYPTO_ENABLED;
* });
*/
/**
* Fires whenever the SDK receives a new to-device event.
* @event module:client~MatrixClient#"toDeviceEvent"
* @param {MatrixEvent} event The matrix event which caused this event to fire.
* @example
* matrixClient.on("toDeviceEvent", function(event){
* var sender = event.getSender();
* });
*/
/**
* Fires whenever the SDK's syncing state is updated. The state can be one of:
* <ul>