Move Session Storage to indexedDB
This commit is contained in:
parent
20ebc3f890
commit
7eda48f755
3 changed files with 37 additions and 26 deletions
|
@ -58,6 +58,7 @@
|
|||
var Model = Backbone.Model.extend({ database: Whisper.Database });
|
||||
var PreKey = Model.extend({ storeName: 'preKeys' });
|
||||
var SignedPreKey = Model.extend({ storeName: 'signedPreKeys' });
|
||||
var Contact = Model.extend({ storeName: 'contacts' });
|
||||
|
||||
function AxolotlStore() {}
|
||||
|
||||
|
@ -161,47 +162,54 @@
|
|||
getSession: function(encodedNumber) {
|
||||
if (encodedNumber === null || encodedNumber === undefined)
|
||||
throw new Error("Tried to get session for undefined/null key");
|
||||
return Promise.resolve((function() {
|
||||
return new Promise(function(resolve) {
|
||||
var number = textsecure.utils.unencodeNumber(encodedNumber)[0];
|
||||
var deviceId = textsecure.utils.unencodeNumber(encodedNumber)[1];
|
||||
|
||||
var sessions = textsecure.storage.get("sessions" + number);
|
||||
if (sessions === undefined)
|
||||
return undefined;
|
||||
if (sessions[deviceId] === undefined)
|
||||
return undefined;
|
||||
var contact = new Contact({id: number});
|
||||
contact.fetch().always(function() {
|
||||
var sessions = contact.get('sessions') || {};
|
||||
resolve(sessions[deviceId]);
|
||||
});
|
||||
|
||||
return sessions[deviceId];
|
||||
})());
|
||||
});
|
||||
},
|
||||
putSession: function(encodedNumber, record) {
|
||||
if (encodedNumber === null || encodedNumber === undefined)
|
||||
throw new Error("Tried to put session for undefined/null key");
|
||||
var number = textsecure.utils.unencodeNumber(encodedNumber)[0];
|
||||
var deviceId = textsecure.utils.unencodeNumber(encodedNumber)[1];
|
||||
return new Promise(function(resolve) {
|
||||
var number = textsecure.utils.unencodeNumber(encodedNumber)[0];
|
||||
var deviceId = textsecure.utils.unencodeNumber(encodedNumber)[1];
|
||||
|
||||
var sessions = textsecure.storage.get("sessions" + number);
|
||||
if (sessions === undefined)
|
||||
sessions = {};
|
||||
sessions[deviceId] = record;
|
||||
textsecure.storage.put("sessions" + number, sessions);
|
||||
|
||||
return textsecure.storage.devices.getDeviceObject(encodedNumber).then(function(device) {
|
||||
if (device === undefined) {
|
||||
return textsecure.storage.axolotl.getIdentityKey(number).then(function(identityKey) {
|
||||
device = { encodedNumber: encodedNumber,
|
||||
//TODO: Remove this duplication
|
||||
identityKey: identityKey
|
||||
};
|
||||
return textsecure.storage.devices.saveDeviceObject(device);
|
||||
var contact = new Contact({id: number});
|
||||
contact.fetch().always(function() {
|
||||
var sessions = contact.get('sessions') || {};
|
||||
sessions[deviceId] = record;
|
||||
contact.save({sessions: sessions}).always(function() {
|
||||
resolve(textsecure.storage.devices.getDeviceObject(encodedNumber).then(function(device) {
|
||||
if (device === undefined) {
|
||||
return textsecure.storage.axolotl.getIdentityKey(number).then(function(identityKey) {
|
||||
device = { encodedNumber: encodedNumber,
|
||||
//TODO: Remove this duplication
|
||||
identityKey: identityKey
|
||||
};
|
||||
return textsecure.storage.devices.saveDeviceObject(device);
|
||||
});
|
||||
}
|
||||
}));
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
removeAllSessions: function(number) {
|
||||
if (number === null || number === undefined)
|
||||
throw new Error("Tried to put session for undefined/null key");
|
||||
return Promise.resolve(textsecure.storage.remove("sessions" + number));
|
||||
return new Promise(function(resolve) {
|
||||
var contact = new Contact({id: number});
|
||||
contact.fetch().then(function() {
|
||||
contact.save({sessions: {}}).always(resolve);
|
||||
});
|
||||
});
|
||||
},
|
||||
getIdentityKey: function(identifier) {
|
||||
if (identifier === null || identifier === undefined)
|
||||
|
|
|
@ -34,6 +34,8 @@
|
|||
conversations.createIndex("group", "members", { unique: false, multiEntry: true });
|
||||
conversations.createIndex("type", "type", { unique: false });
|
||||
|
||||
var contacts = transaction.db.createObjectStore('contacts');
|
||||
|
||||
var preKeys = transaction.db.createObjectStore("preKeys");
|
||||
var signedPreKeys = transaction.db.createObjectStore("signedPreKeys");
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@ describe("AxolotlStore", function() {
|
|||
before(function() { localStorage.clear(); });
|
||||
var store = textsecure.storage.axolotl;
|
||||
var identifier = '+5558675309';
|
||||
var another_identifier = '+5555590210';
|
||||
var identityKey = {
|
||||
pubKey: textsecure.crypto.getRandomBytes(33),
|
||||
privKey: textsecure.crypto.getRandomBytes(32),
|
||||
|
|
Loading…
Reference in a new issue