implement decrypting when loading timeline

This commit is contained in:
Bruno Windels 2020-09-04 15:30:06 +02:00
parent 62bcb27784
commit 5a731903da

View file

@ -24,18 +24,41 @@ export class TimelineReader {
this._roomId = roomId; this._roomId = roomId;
this._storage = storage; this._storage = storage;
this._fragmentIdComparer = fragmentIdComparer; this._fragmentIdComparer = fragmentIdComparer;
this._decryptEntries = null;
}
enableEncryption(decryptEntries) {
this._decryptEntries = decryptEntries;
} }
_openTxn() { _openTxn() {
if (this._decryptEntries) {
return this._storage.readWriteTxn([
this._storage.storeNames.timelineEvents,
this._storage.storeNames.timelineFragments,
this._storage.storeNames.inboundGroupSessions,
this._storage.storeNames.groupSessionDecryptions,
]);
} else {
return this._storage.readTxn([ return this._storage.readTxn([
this._storage.storeNames.timelineEvents, this._storage.storeNames.timelineEvents,
this._storage.storeNames.timelineFragments, this._storage.storeNames.timelineFragments,
]); ]);
} }
}
async readFrom(eventKey, direction, amount) { async readFrom(eventKey, direction, amount) {
const txn = await this._openTxn(); const txn = await this._openTxn();
return this._readFrom(eventKey, direction, amount, txn); let entries;
try {
entries = await this._readFrom(eventKey, direction, amount, txn);
} catch (err) {
txn.abort();
throw err;
}
await txn.complete();
return entries;
} }
async _readFrom(eventKey, direction, amount, txn) { async _readFrom(eventKey, direction, amount, txn) {
@ -50,7 +73,10 @@ export class TimelineReader {
} else { } else {
eventsWithinFragment = await timelineStore.eventsBefore(this._roomId, eventKey, amount); eventsWithinFragment = await timelineStore.eventsBefore(this._roomId, eventKey, amount);
} }
const eventEntries = eventsWithinFragment.map(e => new EventEntry(e, this._fragmentIdComparer)); let eventEntries = eventsWithinFragment.map(e => new EventEntry(e, this._fragmentIdComparer));
if (this._decryptEntries) {
eventEntries = await this._decryptEntries(eventEntries, txn);
}
entries = directionalConcat(entries, eventEntries, direction); entries = directionalConcat(entries, eventEntries, direction);
// prepend or append eventsWithinFragment to entries, and wrap them in EventEntry // prepend or append eventsWithinFragment to entries, and wrap them in EventEntry
@ -78,22 +104,24 @@ export class TimelineReader {
async readFromEnd(amount) { async readFromEnd(amount) {
const txn = await this._openTxn(); const txn = await this._openTxn();
let entries;
try {
const liveFragment = await txn.timelineFragments.liveFragment(this._roomId); const liveFragment = await txn.timelineFragments.liveFragment(this._roomId);
// room hasn't been synced yet // room hasn't been synced yet
if (!liveFragment) { if (!liveFragment) {
return []; entries = [];
} } else {
this._fragmentIdComparer.add(liveFragment); this._fragmentIdComparer.add(liveFragment);
const liveFragmentEntry = FragmentBoundaryEntry.end(liveFragment, this._fragmentIdComparer); const liveFragmentEntry = FragmentBoundaryEntry.end(liveFragment, this._fragmentIdComparer);
const eventKey = liveFragmentEntry.asEventKey(); const eventKey = liveFragmentEntry.asEventKey();
const entries = await this._readFrom(eventKey, Direction.Backward, amount, txn); entries = await this._readFrom(eventKey, Direction.Backward, amount, txn);
entries.unshift(liveFragmentEntry); entries.unshift(liveFragmentEntry);
}
} catch (err) {
txn.abort();
throw err;
}
await txn.complete();
return entries; return entries;
} }
// reads distance up and down from eventId
// or just expose eventIdToKey?
readAtEventId(eventId, distance) {
return null;
}
} }