From 7f1cdf68419c5f6c74ad778b1065d0b5c42a06d3 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Tue, 2 Mar 2021 23:20:33 +0100 Subject: [PATCH] also decrypt all matching timeline entries when new key --- src/matrix/room/Room.js | 16 ++++++++++++++-- src/matrix/room/timeline/entries/EventEntry.js | 7 +++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/matrix/room/Room.js b/src/matrix/room/Room.js index a5781de0..e8fda52e 100644 --- a/src/matrix/room/Room.js +++ b/src/matrix/room/Room.js @@ -176,8 +176,20 @@ export class Room extends EventEmitter { let events = roomResponse?.timeline?.events || []; // when new keys arrive, also see if any events that can now be retried to decrypt if (newKeys) { - const nestedEntries = await Promise.all(newKeys.map(key => this._getRetryDecryptEntriesForKey(key, txn))); - retryEntries = nestedEntries.reduce((allEntries, entries) => allEntries.concat(entries), []); + const entriesPerKey = await Promise.all(newKeys.map(key => this._getRetryDecryptEntriesForKey(key, txn))); + retryEntries = entriesPerKey.reduce((allEntries, entries) => allEntries.concat(entries), []); + // If we have the timeline open, see if there are more entries for the new keys + // as we only store missing session for synced and not backfilled events. + // We want to decrypt all events we can though if the user is looking + // at them given the timeline is open + if (this._timeline) { + let retryTimelineEntries = this._roomEncryption.filterEventEntriesForKeys(this._timeline.remoteEntries, newKeys); + // filter out any entries already in retryEntries so we don't decrypt them twice + const existingIds = new Set(retryEntries.map(e => e.id)); + retryTimelineEntries = retryTimelineEntries.filter(e => !existingIds.has(e.id)) + // make copies so we don't modify the original entry before the afterSync stage + retryEntries = retryTimelineEntries.map(e => e.clone()); + } if (retryEntries.length) { log.set("retry", retryEntries.length); events = events.concat(retryEntries.map(entry => entry.event)); diff --git a/src/matrix/room/timeline/entries/EventEntry.js b/src/matrix/room/timeline/entries/EventEntry.js index 410c3d63..88a0aa5e 100644 --- a/src/matrix/room/timeline/entries/EventEntry.js +++ b/src/matrix/room/timeline/entries/EventEntry.js @@ -25,6 +25,13 @@ export class EventEntry extends BaseEntry { this._decryptionResult = null; } + clone() { + const clone = new EventEntry(this._eventEntry, this._fragmentIdComparer); + clone._decryptionResult = this._decryptionResult; + clone._decryptionError = this._decryptionError; + return clone; + } + get event() { return this._eventEntry.event; }