From b1226d9220ecc0f53d7b27fe28dac993b666a0ab Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 3 Sep 2020 15:36:48 +0200 Subject: [PATCH] add infrastructure to encrypt while sending --- src/matrix/SendScheduler.js | 2 +- src/matrix/room/Room.js | 2 ++ src/matrix/room/sending/PendingEvent.js | 7 +++++++ src/matrix/room/sending/SendQueue.js | 15 ++++++++++++++- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/matrix/SendScheduler.js b/src/matrix/SendScheduler.js index 6e6196d3..e7627c81 100644 --- a/src/matrix/SendScheduler.js +++ b/src/matrix/SendScheduler.js @@ -121,7 +121,7 @@ export class SendScheduler { } this._sendRequests = []; } - console.error("error for request", request); + console.error("error for request", err); request.reject(err); break; } diff --git a/src/matrix/room/Room.js b/src/matrix/room/Room.js index dbf1e5e3..e8315538 100644 --- a/src/matrix/room/Room.js +++ b/src/matrix/room/Room.js @@ -88,6 +88,7 @@ export class Room extends EventEmitter { // encryption got enabled if (!this._summary.encryption && summaryChanges.encryption && !this._roomEncryption) { this._roomEncryption = this._createRoomEncryption(this, summaryChanges.encryption); + this._sendQueue.enableEncryption(this._roomEncryption); } if (memberChanges.size) { if (this._changedMembersDuringSync) { @@ -137,6 +138,7 @@ export class Room extends EventEmitter { this._summary.load(summary); if (this._summary.encryption) { this._roomEncryption = this._createRoomEncryption(this, this._summary.encryption); + this._sendQueue.enableEncryption(this._roomEncryption); } // need to load members for name? if (this._summary.needsHeroes) { diff --git a/src/matrix/room/sending/PendingEvent.js b/src/matrix/room/sending/PendingEvent.js index 2b4f7477..fb2d1a47 100644 --- a/src/matrix/room/sending/PendingEvent.js +++ b/src/matrix/room/sending/PendingEvent.js @@ -26,5 +26,12 @@ export class PendingEvent { get remoteId() { return this._data.remoteId; } set remoteId(value) { this._data.remoteId = value; } get content() { return this._data.content; } + get needsEncryption() { return this._data.needsEncryption; } get data() { return this._data; } + + setEncrypted(type, content) { + this._data.eventType = type; + this._data.content = content; + this._data.needsEncryption = false; + } } diff --git a/src/matrix/room/sending/SendQueue.js b/src/matrix/room/sending/SendQueue.js index 9a094798..fe7afe77 100644 --- a/src/matrix/room/sending/SendQueue.js +++ b/src/matrix/room/sending/SendQueue.js @@ -33,6 +33,11 @@ export class SendQueue { this._isSending = false; this._offline = false; this._amountSent = 0; + this._roomEncryption = null; + } + + enableEncryption(roomEncryption) { + this._roomEncryption = roomEncryption; } async _sendLoop() { @@ -45,6 +50,13 @@ export class SendQueue { if (pendingEvent.remoteId) { continue; } + if (pendingEvent.needsEncryption) { + const {type, content} = await this._sendScheduler.request(async hsApi => { + return await this._roomEncryption.encrypt(pendingEvent.eventType, pendingEvent.content, hsApi); + }); + pendingEvent.setEncrypted(type, content); + await this._tryUpdateEvent(pendingEvent); + } console.log("really sending now"); const response = await this._sendScheduler.request(hsApi => { console.log("got sendScheduler slot"); @@ -156,7 +168,8 @@ export class SendQueue { queueIndex, eventType, content, - txnId: makeTxnId() + txnId: makeTxnId(), + needsEncryption: !!this._roomEncryption }); console.log("_createAndStoreEvent: adding to pendingEventsStore"); pendingEventsStore.add(pendingEvent.data);