EventIndex: Add support to delete events from the index.

pull/21833/head
Damir Jelić 2020-03-12 11:42:02 +01:00
parent b1ad0b8d77
commit cd515f1922
2 changed files with 34 additions and 1 deletions

View File

@ -123,6 +123,10 @@ export default class BaseEventIndexManager {
throw new Error("Unimplemented"); throw new Error("Unimplemented");
} }
async deleteEvent(eventId: string): Promise<boolean> {
throw new Error("Unimplemented");
}
/** /**
* Check if our event index is empty. * Check if our event index is empty.
*/ */

View File

@ -61,6 +61,7 @@ export default class EventIndex extends EventEmitter {
client.on('Room.timeline', this.onRoomTimeline); client.on('Room.timeline', this.onRoomTimeline);
client.on('Event.decrypted', this.onEventDecrypted); client.on('Event.decrypted', this.onEventDecrypted);
client.on('Room.timelineReset', this.onTimelineReset); client.on('Room.timelineReset', this.onTimelineReset);
client.on('Room.redaction', this.onRedaction);
} }
/** /**
@ -74,6 +75,7 @@ export default class EventIndex extends EventEmitter {
client.removeListener('Room.timeline', this.onRoomTimeline); client.removeListener('Room.timeline', this.onRoomTimeline);
client.removeListener('Event.decrypted', this.onEventDecrypted); client.removeListener('Event.decrypted', this.onEventDecrypted);
client.removeListener('Room.timelineReset', this.onTimelineReset); client.removeListener('Room.timelineReset', this.onTimelineReset);
client.removeListener('Room.redaction', this.onRedaction);
} }
/** /**
@ -210,6 +212,23 @@ export default class EventIndex extends EventEmitter {
await this.addLiveEventToIndex(ev); await this.addLiveEventToIndex(ev);
} }
/*
* The Room.redaction listener.
*
* Removes a redacted event from our event index.
*/
onRedaction = async (ev, room) => {
// We only index encrypted rooms locally.
if (!MatrixClientPeg.get().isRoomEncrypted(room.roomId)) return;
const indexManager = PlatformPeg.get().getEventIndexingManager();
try {
await indexManager.deleteEvent(ev.getAssociatedId());
} catch (e) {
console.log("EventIndex: Error deleting event from index", e);
}
}
/* /*
* The Room.timelineReset listener. * The Room.timelineReset listener.
* *
@ -431,12 +450,17 @@ export default class EventIndex extends EventEmitter {
// Let us wait for all the events to get decrypted. // Let us wait for all the events to get decrypted.
await Promise.all(decryptionPromises); await Promise.all(decryptionPromises);
// TODO if there are no events at this point we're missing a lot // TODO if there are no events at this point we're missing a lot
// decryption keys, do we want to retry this checkpoint at a later // decryption keys, do we want to retry this checkpoint at a later
// stage? // stage?
const filteredEvents = matrixEvents.filter(this.isValidEvent); const filteredEvents = matrixEvents.filter(this.isValidEvent);
// Collect the redaction events so we can delete the redacted events
// from the index.
const redactionEvents = matrixEvents.filter((ev) => {
return ev.getType() === "m.room.redaction";
});
// Let us convert the events back into a format that EventIndex can // Let us convert the events back into a format that EventIndex can
// consume. // consume.
const events = filteredEvents.map((ev) => { const events = filteredEvents.map((ev) => {
@ -468,6 +492,11 @@ export default class EventIndex extends EventEmitter {
); );
try { try {
for (var i = 0; i < redactionEvents.length; i++) {
let ev = redactionEvents[i];
await indexManager.deleteEvent(ev.getAssociatedId());
}
const eventsAlreadyAdded = await indexManager.addHistoricEvents( const eventsAlreadyAdded = await indexManager.addHistoricEvents(
events, newCheckpoint, checkpoint); events, newCheckpoint, checkpoint);
// If all events were already indexed we assume that we catched // If all events were already indexed we assume that we catched