_maybeMigrateSession: Directly update cache or save session if needed

This commit is contained in:
Scott Nonnenberg 2021-08-03 15:42:23 -07:00 committed by GitHub
parent a78d30cb5a
commit 0fb3951078
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -807,13 +807,9 @@ export class SignalProtocolStore extends EventsMixin {
return entry.item; return entry.item;
} }
const item = await this._maybeMigrateSession(entry.fromDB); // We'll either just hydrate the item or we'll fully migrate the session
map.set(id, { // and save it to the database.
hydrated: true, return await this._maybeMigrateSession(entry.fromDB, { zone });
item,
fromDB: entry.fromDB,
});
return item;
} catch (error) { } catch (error) {
const errorString = error && error.stack ? error.stack : error; const errorString = error && error.stack ? error.stack : error;
window.log.error( window.log.error(
@ -840,14 +836,30 @@ export class SignalProtocolStore extends EventsMixin {
} }
private async _maybeMigrateSession( private async _maybeMigrateSession(
session: SessionType session: SessionType,
{ zone = GLOBAL_ZONE }: SessionTransactionOptions = {}
): Promise<SessionRecord> { ): Promise<SessionRecord> {
// Already migrated, return record directly if (!this.sessions) {
if (session.version === 2) { throw new Error('_maybeMigrateSession: this.sessions not yet cached!');
return hydrateSession(session);
} }
// Not yet converted, need to translate to new format // Already migrated, hydrate and update cache
if (session.version === 2) {
const item = hydrateSession(session);
const map = this.pendingSessions.has(session.id)
? this.pendingSessions
: this.sessions;
map.set(session.id, {
hydrated: true,
item,
fromDB: session,
});
return item;
}
// Not yet converted, need to translate to new format and save
if (session.version !== undefined) { if (session.version !== undefined) {
throw new Error('_maybeMigrateSession: Unknown session version type!'); throw new Error('_maybeMigrateSession: Unknown session version type!');
} }
@ -874,9 +886,13 @@ export class SignalProtocolStore extends EventsMixin {
JSON.parse(session.record), JSON.parse(session.record),
localUserData localUserData
); );
return SessionRecord.deserialize( const record = SessionRecord.deserialize(
Buffer.from(sessionStructureToArrayBuffer(sessionProto)) Buffer.from(sessionStructureToArrayBuffer(sessionProto))
); );
await this.storeSession(session.id, record, { zone });
return record;
} }
async storeSession( async storeSession(
@ -930,12 +946,13 @@ export class SignalProtocolStore extends EventsMixin {
} }
async getOpenDevices( async getOpenDevices(
identifiers: Array<string> identifiers: Array<string>,
{ zone = GLOBAL_ZONE }: SessionTransactionOptions = {}
): Promise<{ ): Promise<{
devices: Array<DeviceType>; devices: Array<DeviceType>;
emptyIdentifiers: Array<string>; emptyIdentifiers: Array<string>;
}> { }> {
return this.withZone(GLOBAL_ZONE, 'getOpenDevices', async () => { return this.withZone(zone, 'getOpenDevices', async () => {
if (!this.sessions) { if (!this.sessions) {
throw new Error('getOpenDevices: this.sessions not yet cached!'); throw new Error('getOpenDevices: this.sessions not yet cached!');
} }
@ -983,7 +1000,9 @@ export class SignalProtocolStore extends EventsMixin {
return undefined; return undefined;
} }
const record = await this._maybeMigrateSession(entry.fromDB); const record = await this._maybeMigrateSession(entry.fromDB, {
zone,
});
if (record.hasCurrentState()) { if (record.hasCurrentState()) {
return { record, entry }; return { record, entry };
} }
@ -1108,7 +1127,7 @@ export class SignalProtocolStore extends EventsMixin {
async () => { async () => {
const item = entry.hydrated const item = entry.hydrated
? entry.item ? entry.item
: await this._maybeMigrateSession(entry.fromDB); : await this._maybeMigrateSession(entry.fromDB, { zone });
if (!item.hasCurrentState()) { if (!item.hasCurrentState()) {
return; return;