From 7a2bb4b112daec2392f04b64dfc067aeb35af6bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 8 Jun 2020 16:43:20 +0200 Subject: [PATCH] EventIndex: Check if a newly encrypted room is indexed before adding a checkpoint. --- src/indexing/BaseEventIndexManager.ts | 13 +++++++++++ src/indexing/EventIndex.js | 33 ++++++++++++++++++++------- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/indexing/BaseEventIndexManager.ts b/src/indexing/BaseEventIndexManager.ts index c40d1300ea..32ab3b34fe 100644 --- a/src/indexing/BaseEventIndexManager.ts +++ b/src/indexing/BaseEventIndexManager.ts @@ -134,6 +134,19 @@ export default abstract class BaseEventIndexManager { throw new Error("Unimplemented"); } + /** + * Check if the room with the given id is already indexed. + * + * @param {string} roomId The ID of the room which we want to check if it + * has been already indexed. + * + * @return {Promise} Returns true if the index contains events for + * the given room, false otherwise. + */ + isRoomIndexed(roomId: string): Promise { + throw new Error("Unimplemented"); + } + /** * Get statistical information of the index. * diff --git a/src/indexing/EventIndex.js b/src/indexing/EventIndex.js index f67738ca68..fe7c71cfa6 100644 --- a/src/indexing/EventIndex.js +++ b/src/indexing/EventIndex.js @@ -62,6 +62,7 @@ export default class EventIndex extends EventEmitter { client.on('Event.decrypted', this.onEventDecrypted); client.on('Room.timelineReset', this.onTimelineReset); client.on('Room.redaction', this.onRedaction); + client.on('RoomState.events', this.onRoomStateEvent); } /** @@ -76,6 +77,7 @@ export default class EventIndex extends EventEmitter { client.removeListener('Event.decrypted', this.onEventDecrypted); client.removeListener('Room.timelineReset', this.onTimelineReset); client.removeListener('Room.redaction', this.onRedaction); + client.removeListener('RoomState.events', this.onRoomStateEvent); } /** @@ -182,14 +184,6 @@ export default class EventIndex extends EventEmitter { return; } - if (ev.getType() === "m.room.encryption") { - console.log("EventIndex: Adding checkpoint for newly encrypted room", - room.roomId); - - this.addRoomCheckpoint(room.roomId, true); - return; - } - // If the event is not yet decrypted mark it for the // Event.decrypted callback. if (ev.isBeingDecrypted()) { @@ -202,6 +196,15 @@ export default class EventIndex extends EventEmitter { } } + onRoomStateEvent = async (ev, state) => { + if (!MatrixClientPeg.get().isRoomEncrypted(state.roomId)) return; + + if (ev.getType() === "m.room.encryption" && !await this.isRoomIndexed(state.roomId)) { + console.log("EventIndex: Adding a checkpoint for a newly encrypted room", room.roomId); + this.addRoomCheckpoint(state.roomId, true); + } + } + /* * The Event.decrypted listener. * @@ -847,6 +850,20 @@ export default class EventIndex extends EventEmitter { return indexManager.getStats(); } + /** + * Check if the room with the given id is already indexed. + * + * @param {string} roomId The ID of the room which we want to check if it + * has been already indexed. + * + * @return {Promise} Returns true if the index contains events for + * the given room, false otherwise. + */ + async isRoomIndexed(roomId) { + const indexManager = PlatformPeg.get().getEventIndexingManager(); + return indexManager.isRoomIndexed(roomId); + } + /** * Get the room that we are currently crawling. *