Decrypt events ahead of storing them in the index

This commit is contained in:
Germain Souquet 2021-05-10 15:19:46 +01:00
parent 6e3f8d6a0a
commit d0d2907a07

View file

@ -38,7 +38,6 @@ export default class EventIndex extends EventEmitter {
this._eventsPerCrawl = 100; this._eventsPerCrawl = 100;
this._crawler = null; this._crawler = null;
this._currentCheckpoint = null; this._currentCheckpoint = null;
this.liveEventsForIndex = new Set();
} }
async init() { async init() {
@ -188,16 +187,11 @@ export default class EventIndex extends EventEmitter {
return; return;
} }
// If the event is not yet decrypted mark it for the
// Event.decrypted callback.
if (ev.isBeingDecrypted()) { if (ev.isBeingDecrypted()) {
const eventId = ev.getId(); await ev._decryptionPromise;
this.liveEventsForIndex.add(eventId);
} else {
// If the event is decrypted or is unencrypted add it to the
// index now.
await this.addLiveEventToIndex(ev);
} }
await this.addLiveEventToIndex(ev);
} }
onRoomStateEvent = async (ev, state) => { onRoomStateEvent = async (ev, state) => {
@ -219,7 +213,6 @@ export default class EventIndex extends EventEmitter {
const eventId = ev.getId(); const eventId = ev.getId();
// If the event isn't in our live event set, ignore it. // If the event isn't in our live event set, ignore it.
if (!this.liveEventsForIndex.delete(eventId)) return;
if (err) return; if (err) return;
await this.addLiveEventToIndex(ev); await this.addLiveEventToIndex(ev);
} }
@ -523,18 +516,18 @@ export default class EventIndex extends EventEmitter {
} }
}); });
const decryptionPromises = []; const decryptionPromises = matrixEvents
.filter(event => event.isEncrypted())
matrixEvents.forEach(ev => { .map(event => {
if (ev.isBeingDecrypted() || ev.isDecryptionFailure()) { if (event.shouldAttemptDecryption()) {
// TODO the decryption promise is a private property, this return event.attemptDecryption(client._crypto, {
// should either be made public or we should convert the isRetry: true,
// event that gets fired when decryption is done into a emit: false,
// promise using the once event emitter method: });
// https://nodejs.org/api/events.html#events_events_once_emitter_name } else {
decryptionPromises.push(ev._decryptionPromise); return event._decryptionPromise;
} }
}); });
// 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);