From 44a2febce9c39d44a7dc409107a3fbacf255cb4e Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 6 Nov 2020 23:43:02 +0100 Subject: [PATCH] hook it up --- src/domain/session/room/RoomViewModel.js | 13 +++++++++++-- src/matrix/e2ee/RoomEncryption.js | 3 ++- src/matrix/room/Room.js | 4 ++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/domain/session/room/RoomViewModel.js b/src/domain/session/room/RoomViewModel.js index 5aa20a2a..c872ca0f 100644 --- a/src/domain/session/room/RoomViewModel.js +++ b/src/domain/session/room/RoomViewModel.js @@ -169,6 +169,7 @@ class ComposerViewModel extends ViewModel { super(); this._roomVM = roomVM; this._isEmpty = true; + this._ensureKeyPromise = null; } get isEncrypted() { @@ -188,8 +189,16 @@ class ComposerViewModel extends ViewModel { return !this._isEmpty; } - setInput(text) { + async setInput(text) { + const wasEmpty = this._isEmpty; this._isEmpty = text.length === 0; - this.emitChange("canSend"); + if (wasEmpty && !this._isEmpty && !this._ensureKeyPromise) { + this._ensureKeyPromise = this._roomVM._room.ensureMessageKeyIsShared().then(() => { + this._ensureKeyPromise = null; + }); + } + if (wasEmpty !== this._isEmpty) { + this.emitChange("canSend"); + } } } diff --git a/src/matrix/e2ee/RoomEncryption.js b/src/matrix/e2ee/RoomEncryption.js index f10e3f07..94522e60 100644 --- a/src/matrix/e2ee/RoomEncryption.js +++ b/src/matrix/e2ee/RoomEncryption.js @@ -247,9 +247,10 @@ export class RoomEncryption { } /** shares the encryption key for the next message if needed */ - async ensureNextMessageKeyIsShared(hsApi) { + async ensureMessageKeyIsShared(hsApi) { const roomKeyMessage = await this._megolmEncryption.ensureOutboundSession(this._room.id, this._encryptionParams); if (roomKeyMessage) { + await this._deviceTracker.trackRoom(this._room); await this._shareNewRoomKey(roomKeyMessage, hsApi); } } diff --git a/src/matrix/room/Room.js b/src/matrix/room/Room.js index b2d2b635..8427239c 100644 --- a/src/matrix/room/Room.js +++ b/src/matrix/room/Room.js @@ -354,6 +354,10 @@ export class Room extends EventEmitter { return this._sendQueue.enqueueEvent(eventType, content); } + async ensureMessageKeyIsShared() { + return this._roomEncryption?.ensureMessageKeyIsShared(this._hsApi); + } + /** @public */ async loadMemberList() { if (this._memberList) {