Skip to content

Commit

Permalink
Merge pull request #585 from matrix-org/dbkr/count_sessions_before_mi…
Browse files Browse the repository at this point in the history
…grate

Check for sessions in indexeddb before migrating
  • Loading branch information
dbkr authored Dec 8, 2017
2 parents d397c5a + 3d71bee commit da90a3c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/crypto/OlmDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ OlmDevice.prototype._migrateFromSessionStore = async function() {
this._cryptoStore.storeAccount(txn, pickledAccount);
}
}
},
);
});
});
},
);

// remove the old account now the transaction has completed. Either we've
// migrated it or decided not to, either way we want to blow away the old data.
Expand All @@ -190,16 +190,27 @@ OlmDevice.prototype._migrateFromSessionStore = async function() {
if (Object.keys(sessions).length > 0) {
await this._cryptoStore.doTxn(
'readwrite', [IndexedDBCryptoStore.STORE_SESSIONS], (txn) => {
let numSessions = 0;
for (const deviceKey of Object.keys(sessions)) {
for (const sessionId of Object.keys(sessions[deviceKey])) {
numSessions++;
this._cryptoStore.storeEndToEndSession(
deviceKey, sessionId, sessions[deviceKey][sessionId], txn,
);
// Don't migrate sessions from localstorage if we already have sessions
// in indexeddb, since this means we've already migrated and an old version
// has run against the same localstorage and created some spurious sessions.
this._cryptoStore.countEndToEndSessions(txn, (count) => {
if (count) {
console.log("Crypto store already has sessions: not migrating");
return;
}
}
console.log("Migrating " + numSessions + " sessions from session store");
let numSessions = 0;
for (const deviceKey of Object.keys(sessions)) {
for (const sessionId of Object.keys(sessions[deviceKey])) {
numSessions++;
this._cryptoStore.storeEndToEndSession(
deviceKey, sessionId, sessions[deviceKey][sessionId], txn,
);
}
}
console.log(
"Migrating " + numSessions + " sessions from session store",
);
});
},
);

Expand Down
8 changes: 8 additions & 0 deletions src/crypto/store/indexeddb-crypto-store-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@ export class Backend {
objectStore.put(newData, "-");
}

countEndToEndSessions(txn, func) {
const objectStore = txn.objectStore("sessions");
const countReq = objectStore.count();
countReq.onsuccess = function() {
func(countReq.result);
};
}

getEndToEndSessions(deviceKey, txn, func) {
const objectStore = txn.objectStore("sessions");
const idx = objectStore.index("deviceKey");
Expand Down
9 changes: 9 additions & 0 deletions src/crypto/store/indexeddb-crypto-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ export default class IndexedDBCryptoStore {
this._backendPromise.value().storeAccount(txn, newData);
}

/**
* Returns the number of end-to-end sessions in the store
* @param {*} txn An active transaction. See doTxn().
* @param {function(int)} func Called with the count of sessions
*/
countEndToEndSessions(txn, func) {
this._backendPromise.value().countEndToEndSessions(txn, func);
}

/**
* Retrieve a specific end-to-end session between the logged-in user
* and another device.
Expand Down
8 changes: 8 additions & 0 deletions src/crypto/store/localStorage-crypto-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ export default class LocalStorageCryptoStore extends MemoryCryptoStore {
this.store = global.localStorage;
}

countEndToEndSessions(txn, func) {
let count = 0;
for (let i = 0; i < this.store.length; ++i) {
if (this.store.key(i).startsWith(keyEndToEndSessions(''))) ++count;
}
func(count);
}

_getEndToEndSessions(deviceKey, txn, func) {
return getJsonItem(this.store, keyEndToEndSessions(deviceKey));
}
Expand Down
4 changes: 4 additions & 0 deletions src/crypto/store/memory-crypto-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ export default class MemoryCryptoStore {
this._account = newData;
}

countEndToEndSessions(txn, func) {
return Object.keys(this._sessions).length;
}

getEndToEndSession(deviceKey, sessionId, txn, func) {
const deviceSessions = this._sessions[deviceKey] || {};
func(deviceSessions[sessionId] || null);
Expand Down

0 comments on commit da90a3c

Please sign in to comment.