Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit d0d2907

Browse files
committed
Decrypt events ahead of storing them in the index
1 parent 6e3f8d6 commit d0d2907

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

src/indexing/EventIndex.js

+15-22
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ export default class EventIndex extends EventEmitter {
3838
this._eventsPerCrawl = 100;
3939
this._crawler = null;
4040
this._currentCheckpoint = null;
41-
this.liveEventsForIndex = new Set();
4241
}
4342

4443
async init() {
@@ -188,16 +187,11 @@ export default class EventIndex extends EventEmitter {
188187
return;
189188
}
190189

191-
// If the event is not yet decrypted mark it for the
192-
// Event.decrypted callback.
193190
if (ev.isBeingDecrypted()) {
194-
const eventId = ev.getId();
195-
this.liveEventsForIndex.add(eventId);
196-
} else {
197-
// If the event is decrypted or is unencrypted add it to the
198-
// index now.
199-
await this.addLiveEventToIndex(ev);
191+
await ev._decryptionPromise;
200192
}
193+
194+
await this.addLiveEventToIndex(ev);
201195
}
202196

203197
onRoomStateEvent = async (ev, state) => {
@@ -219,7 +213,6 @@ export default class EventIndex extends EventEmitter {
219213
const eventId = ev.getId();
220214

221215
// If the event isn't in our live event set, ignore it.
222-
if (!this.liveEventsForIndex.delete(eventId)) return;
223216
if (err) return;
224217
await this.addLiveEventToIndex(ev);
225218
}
@@ -523,18 +516,18 @@ export default class EventIndex extends EventEmitter {
523516
}
524517
});
525518

526-
const decryptionPromises = [];
527-
528-
matrixEvents.forEach(ev => {
529-
if (ev.isBeingDecrypted() || ev.isDecryptionFailure()) {
530-
// TODO the decryption promise is a private property, this
531-
// should either be made public or we should convert the
532-
// event that gets fired when decryption is done into a
533-
// promise using the once event emitter method:
534-
// https://nodejs.org/api/events.html#events_events_once_emitter_name
535-
decryptionPromises.push(ev._decryptionPromise);
536-
}
537-
});
519+
const decryptionPromises = matrixEvents
520+
.filter(event => event.isEncrypted())
521+
.map(event => {
522+
if (event.shouldAttemptDecryption()) {
523+
return event.attemptDecryption(client._crypto, {
524+
isRetry: true,
525+
emit: false,
526+
});
527+
} else {
528+
return event._decryptionPromise;
529+
}
530+
});
538531

539532
// Let us wait for all the events to get decrypted.
540533
await Promise.all(decryptionPromises);

0 commit comments

Comments
 (0)