EventIndex: Check if a newly encrypted room is indexed before adding a checkpoint.

This commit is contained in:
Damir Jelić 2020-06-08 16:43:20 +02:00
parent ea35fc2881
commit 7a2bb4b112
2 changed files with 38 additions and 8 deletions

View file

@ -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<boolean>} Returns true if the index contains events for
* the given room, false otherwise.
*/
isRoomIndexed(roomId: string): Promise<boolean> {
throw new Error("Unimplemented");
}
/**
* Get statistical information of the index.
*

View file

@ -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<boolean>} 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.
*