make a method to determine only the retry entries rather

This commit is contained in:
Bruno Windels 2021-03-03 13:53:52 +01:00
parent 404dbcd065
commit 6771303086

View File

@ -156,40 +156,24 @@ export class Room extends EventEmitter {
return request; return request;
} }
async _prepareSyncDecryption(events, newKeys, roomEncryption, txn, log) { async _getSyncRetryDecryptEntries(newKeys, txn) {
let retryEntries; const entriesPerKey = await Promise.all(newKeys.map(key => this._getRetryDecryptEntriesForKey(key, txn)));
let decryptPreparation; let retryEntries = entriesPerKey.reduce((allEntries, entries) => allEntries.concat(entries), []);
// when new keys arrive, also see if any events can now be retried to decrypt // If we have the timeline open, see if there are more entries for the new keys
if (newKeys) { // as we only store missing session information for synced events, not backfilled.
const entriesPerKey = await Promise.all(newKeys.map(key => this._getRetryDecryptEntriesForKey(key, txn))); // We want to decrypt all events we can though if the user is looking
retryEntries = entriesPerKey.reduce((allEntries, entries) => allEntries.concat(entries), []); // at them when the timeline is open
// If we have the timeline open, see if there are more entries for the new keys if (this._timeline) {
// as we only store missing session information for synced events, not backfilled. let retryTimelineEntries = this._roomEncryption.filterUndecryptedEventEntriesForKeys(this._timeline.remoteEntries, newKeys);
// We want to decrypt all events we can though if the user is looking // filter out any entries already in retryEntries so we don't decrypt them twice
// at them when the timeline is open const existingIds = retryEntries.reduce((ids, e) => {ids.add(e.id); return ids;}, new Set());
if (this._timeline) { retryTimelineEntries = retryTimelineEntries.filter(e => !existingIds.has(e.id));
let retryTimelineEntries = this._roomEncryption.filterUndecryptedEventEntriesForKeys(this._timeline.remoteEntries, newKeys); // make copies so we don't modify the original entry in writeSync, before the afterSync stage
// filter out any entries already in retryEntries so we don't decrypt them twice const retryTimelineEntriesCopies = retryTimelineEntries.map(e => e.clone());
const existingIds = retryEntries.reduce((ids, e) => {ids.add(e.id); return ids;}, new Set()); // add to other retry entries
retryTimelineEntries = retryTimelineEntries.filter(e => !existingIds.has(e.id)); retryEntries = retryEntries.concat(retryTimelineEntriesCopies);
// make copies so we don't modify the original entry in writeSync, before the afterSync stage
const retryTimelineEntriesCopies = retryTimelineEntries.map(e => e.clone());
// add to other retry entries
retryEntries = retryEntries.concat(retryTimelineEntriesCopies);
}
if (retryEntries.length) {
log.set("retry", retryEntries.length);
events = events.concat(retryEntries.map(entry => entry.event));
}
} }
const eventsToDecrypt = events.filter(event => { return retryEntries;
return event?.type === EVENT_ENCRYPTED_TYPE;
});
if (eventsToDecrypt.length) {
decryptPreparation = await roomEncryption.prepareDecryptAll(
eventsToDecrypt, newKeys, DecryptionSource.Sync, txn);
}
return {retryEntries, decryptPreparation};
} }
async prepareSync(roomResponse, membership, newKeys, txn, log) { async prepareSync(roomResponse, membership, newKeys, txn, log) {
@ -208,10 +192,22 @@ export class Room extends EventEmitter {
let retryEntries; let retryEntries;
let decryptPreparation; let decryptPreparation;
if (roomEncryption) { if (roomEncryption) {
const events = roomResponse?.timeline?.events || []; let eventsToDecrypt = roomResponse?.timeline?.events || [];
const result = await this._prepareSyncDecryption(events, newKeys, roomEncryption, txn, log); // when new keys arrive, also see if any older events can now be retried to decrypt
retryEntries = result.retryEntries; if (newKeys) {
decryptPreparation = result.decryptPreparation; retryEntries = await this._getSyncRetryDecryptEntries(newKeys, txn);
if (retryEntries.length) {
log.set("retry", retryEntries.length);
eventsToDecrypt = eventsToDecrypt.concat(retryEntries.map(entry => entry.event));
}
}
eventsToDecrypt = eventsToDecrypt.filter(event => {
return event?.type === EVENT_ENCRYPTED_TYPE;
});
if (eventsToDecrypt.length) {
decryptPreparation = await roomEncryption.prepareDecryptAll(
eventsToDecrypt, newKeys, DecryptionSource.Sync, txn);
}
} }
return { return {