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

Commit

Permalink
Decrypt events ahead of storing them in the index
Browse files Browse the repository at this point in the history
  • Loading branch information
germain-gg committed May 10, 2021
1 parent 6e3f8d6 commit d0d2907
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions src/indexing/EventIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export default class EventIndex extends EventEmitter {
this._eventsPerCrawl = 100;
this._crawler = null;
this._currentCheckpoint = null;
this.liveEventsForIndex = new Set();
}

async init() {
Expand Down Expand Up @@ -188,16 +187,11 @@ export default class EventIndex extends EventEmitter {
return;
}

// If the event is not yet decrypted mark it for the
// Event.decrypted callback.
if (ev.isBeingDecrypted()) {
const eventId = ev.getId();
this.liveEventsForIndex.add(eventId);
} else {
// If the event is decrypted or is unencrypted add it to the
// index now.
await this.addLiveEventToIndex(ev);
await ev._decryptionPromise;
}

await this.addLiveEventToIndex(ev);
}

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

// If the event isn't in our live event set, ignore it.
if (!this.liveEventsForIndex.delete(eventId)) return;
if (err) return;
await this.addLiveEventToIndex(ev);
}
Expand Down Expand Up @@ -523,18 +516,18 @@ export default class EventIndex extends EventEmitter {
}
});

const decryptionPromises = [];

matrixEvents.forEach(ev => {
if (ev.isBeingDecrypted() || ev.isDecryptionFailure()) {
// TODO the decryption promise is a private property, this
// should either be made public or we should convert the
// event that gets fired when decryption is done into a
// promise using the once event emitter method:
// https://nodejs.org/api/events.html#events_events_once_emitter_name
decryptionPromises.push(ev._decryptionPromise);
}
});
const decryptionPromises = matrixEvents
.filter(event => event.isEncrypted())
.map(event => {
if (event.shouldAttemptDecryption()) {
return event.attemptDecryption(client._crypto, {
isRetry: true,
emit: false,
});
} else {
return event._decryptionPromise;
}
});

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

0 comments on commit d0d2907

Please sign in to comment.