diff --git a/lib/client.js b/lib/client.js index b6ea2bead..7aae83b5d 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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: *