finish room encryption part

This commit is contained in:
Bruno Windels 2020-11-06 18:56:32 +01:00
parent d0d1f68a9c
commit c6ff4c2517
2 changed files with 5 additions and 18 deletions

View file

@ -247,22 +247,10 @@ export class RoomEncryption {
} }
/** shares the encryption key for the next message if needed */ /** shares the encryption key for the next message if needed */
async ensureNextMessageEncryptionKeyIsShared(hsApi) { async ensureNextMessageKeyIsShared(hsApi) {
const txn = this._storage.readWriteTxn([ const roomKeyMessage = await this._megolmEncryption.ensureOutboundSession(this._room.id, this._encryptionParams);
this._storage.storeNames.operations,
this._storage.storeNames.outboundGroupSessions,
this._storage.storeNames.inboundGroupSessions,
]);
let roomKeyMessage;
try {
roomKeyMessage = await this._megolmEncryption.ensureOutboundSession(this._room.id, this._encryptionParams, txn);
} catch (err) {
txn.abort();
throw err;
}
// will complete the txn
if (roomKeyMessage) { if (roomKeyMessage) {
await this._shareNewRoomKey(roomKeyMessage, hsApi, txn); await this._shareNewRoomKey(roomKeyMessage, hsApi);
} }
} }
@ -288,12 +276,12 @@ export class RoomEncryption {
return false; return false;
} }
async _shareNewRoomKey(roomKeyMessage, hsApi, txn = null) { async _shareNewRoomKey(roomKeyMessage, hsApi) {
const devices = await this._deviceTracker.devicesForTrackedRoom(this._room.id, hsApi); const devices = await this._deviceTracker.devicesForTrackedRoom(this._room.id, hsApi);
const userIds = Array.from(devices.reduce((set, device) => set.add(device.userId), new Set())); const userIds = Array.from(devices.reduce((set, device) => set.add(device.userId), new Set()));
// store operation for room key share, in case we don't finish here // store operation for room key share, in case we don't finish here
const writeOpTxn = txn || this._storage.readWriteTxn([this._storage.storeNames.operations]); const writeOpTxn = this._storage.readWriteTxn([this._storage.storeNames.operations]);
let operationId; let operationId;
try { try {
operationId = this._writeRoomKeyShareOperation(roomKeyMessage, userIds, writeOpTxn); operationId = this._writeRoomKeyShareOperation(roomKeyMessage, userIds, writeOpTxn);

View file

@ -56,7 +56,6 @@ export class Encryption {
roomKeyMessage = this._readOrCreateSession(session, sessionEntry, roomId, encryptionParams, txn); roomKeyMessage = this._readOrCreateSession(session, sessionEntry, roomId, encryptionParams, txn);
if (roomKeyMessage) { if (roomKeyMessage) {
this._writeSession(sessionEntry, session, roomId, txn); this._writeSession(sessionEntry, session, roomId, txn);
return roomKeyMessage;
} }
} catch (err) { } catch (err) {
txn.abort(); txn.abort();