From 39bcd8d56d1e51e62c4559daae97a867bd887741 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 21 May 2020 10:10:15 +0200 Subject: [PATCH] EventIndex: Add a checkpoint if a room turns into a encrypted one. --- src/indexing/EventIndex.js | 69 +++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 15 deletions(-) diff --git a/src/indexing/EventIndex.js b/src/indexing/EventIndex.js index fac7c92b65..3f08574d7a 100644 --- a/src/indexing/EventIndex.js +++ b/src/indexing/EventIndex.js @@ -182,6 +182,14 @@ 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()) { @@ -234,26 +242,12 @@ export default class EventIndex extends EventEmitter { */ onTimelineReset = async (room, timelineSet, resetAllTimelines) => { if (room === null) return; - - const indexManager = PlatformPeg.get().getEventIndexingManager(); if (!MatrixClientPeg.get().isRoomEncrypted(room.roomId)) return; - const timeline = room.getLiveTimeline(); - const token = timeline.getPaginationToken("b"); - - const backwardsCheckpoint = { - roomId: room.roomId, - token: token, - fullCrawl: false, - direction: "b", - }; - console.log("EventIndex: Added checkpoint because of a limited timeline", backwardsCheckpoint); - await indexManager.addCrawlerCheckpoint(backwardsCheckpoint); - - this.crawlerCheckpoints.push(backwardsCheckpoint); + this.addRoomCheckpoint(room.roomId, false); } /** @@ -319,6 +313,51 @@ export default class EventIndex extends EventEmitter { this.emit("changedCheckpoint", this.currentRoom()); } + async addEventsFromLiveTimeline(timeline) { + let events = timeline.getEvents(); + + for (let i = 0; i < events.length; i++) { + const ev = events[i]; + await this.addLiveEventToIndex(ev); + } + } + + async addRoomCheckpoint(roomId, fullCrawl = false) { + const indexManager = PlatformPeg.get().getEventIndexingManager(); + const client = MatrixClientPeg.get(); + const room = client.getRoom(roomId); + + if (!room) return; + + const timeline = room.getLiveTimeline(); + let token = timeline.getPaginationToken("b"); + + if(!token) { + // The room doesn't contain any tokens, meaning the live timeline + // contains all the events, add those to the index. + await this.addEventsFromLiveTimeline(timeline); + return; + } + + const checkpoint = { + roomId: room.roomId, + token: token, + fullCrawl: fullCrawl, + direction: "b", + }; + + console.log("EventIndex: Adding checkpoint", checkpoint); + + try{ + await indexManager.addCrawlerCheckpoint(checkpoint); + } catch (e) { + console.log("EventIndex: Error adding new checkpoint for room", + room.roomId, checkpoint, e); + } + + this.crawlerCheckpoints.push(checkpoint); + } + /** * The main crawler loop. *