Move Session storage to axolotlstore
This commit is contained in:
parent
9de1572ba6
commit
f38b18ef63
4 changed files with 80 additions and 104 deletions
|
@ -169,16 +169,52 @@
|
|||
});
|
||||
},
|
||||
|
||||
getSession: function(identifier) {
|
||||
if (identifier === null || identifier === undefined)
|
||||
getSession: function(encodedNumber) {
|
||||
if (encodedNumber === null || encodedNumber === undefined)
|
||||
throw new Error("Tried to get session for undefined/null key");
|
||||
return textsecure.storage.sessions.getSessionsForNumber(identifier);
|
||||
return Promise.resolve((function() {
|
||||
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;
|
||||
|
||||
return sessions[deviceId];
|
||||
})());
|
||||
},
|
||||
putSession: function(identifier, record) {
|
||||
if (identifier === null || identifier === undefined)
|
||||
putSession: function(encodedNumber, record) {
|
||||
if (encodedNumber === null || encodedNumber === undefined)
|
||||
throw new Error("Tried to put session for undefined/null key");
|
||||
return textsecure.storage.sessions.putSessionsForDevice(identifier, record);
|
||||
}
|
||||
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.devices.getIdentityKeyForNumber(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));
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
window.AxolotlStore = AxolotlStore;
|
||||
|
|
|
@ -38094,52 +38094,6 @@ axolotlInternal.RecipientRecord = function() {
|
|||
window.textsecure = window.textsecure || {};
|
||||
window.textsecure.storage = window.textsecure.storage || {};
|
||||
|
||||
window.textsecure.storage.sessions = {
|
||||
getSessionsForNumber: function(encodedNumber) {
|
||||
return Promise.resolve((function() {
|
||||
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;
|
||||
|
||||
return sessions[deviceId];
|
||||
})());
|
||||
},
|
||||
|
||||
putSessionsForDevice: function(encodedNumber, record) {
|
||||
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.devices.getIdentityKeyForNumber(number).then(function(identityKey) {
|
||||
device = { encodedNumber: encodedNumber,
|
||||
//TODO: Remove this duplication
|
||||
identityKey: identityKey
|
||||
};
|
||||
return textsecure.storage.devices.saveDeviceObject(device);
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// Use textsecure.storage.devices.removeIdentityKeyForNumber (which calls this) instead
|
||||
_removeIdentityKeyForNumber: function(number) {
|
||||
return Promise.resolve(textsecure.storage.remove("sessions" + number));
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
window.textsecure.storage.devices = {
|
||||
saveDeviceObject: function(deviceObject) {
|
||||
return internalSaveDeviceObject(deviceObject, false);
|
||||
|
@ -38195,7 +38149,7 @@ axolotlInternal.RecipientRecord = function() {
|
|||
if (map === undefined)
|
||||
throw new Error("Tried to remove identity for unknown number");
|
||||
textsecure.storage.remove("devices" + number);
|
||||
return textsecure.storage.sessions._removeIdentityKeyForNumber(number);
|
||||
return textsecure.storage.axolotl.removeAllSessions(number);
|
||||
})());
|
||||
},
|
||||
|
||||
|
|
|
@ -23,52 +23,6 @@
|
|||
window.textsecure = window.textsecure || {};
|
||||
window.textsecure.storage = window.textsecure.storage || {};
|
||||
|
||||
window.textsecure.storage.sessions = {
|
||||
getSessionsForNumber: function(encodedNumber) {
|
||||
return Promise.resolve((function() {
|
||||
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;
|
||||
|
||||
return sessions[deviceId];
|
||||
})());
|
||||
},
|
||||
|
||||
putSessionsForDevice: function(encodedNumber, record) {
|
||||
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.devices.getIdentityKeyForNumber(number).then(function(identityKey) {
|
||||
device = { encodedNumber: encodedNumber,
|
||||
//TODO: Remove this duplication
|
||||
identityKey: identityKey
|
||||
};
|
||||
return textsecure.storage.devices.saveDeviceObject(device);
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// Use textsecure.storage.devices.removeIdentityKeyForNumber (which calls this) instead
|
||||
_removeIdentityKeyForNumber: function(number) {
|
||||
return Promise.resolve(textsecure.storage.remove("sessions" + number));
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
window.textsecure.storage.devices = {
|
||||
saveDeviceObject: function(deviceObject) {
|
||||
return internalSaveDeviceObject(deviceObject, false);
|
||||
|
@ -124,7 +78,7 @@
|
|||
if (map === undefined)
|
||||
throw new Error("Tried to remove identity for unknown number");
|
||||
textsecure.storage.remove("devices" + number);
|
||||
return textsecure.storage.sessions._removeIdentityKeyForNumber(number);
|
||||
return textsecure.storage.axolotl.removeAllSessions(number);
|
||||
})());
|
||||
},
|
||||
|
||||
|
|
|
@ -84,9 +84,41 @@ describe("AxolotlStore", function() {
|
|||
});
|
||||
it('stores sessions', function(done) {
|
||||
var testRecord = "an opaque string";
|
||||
store.putSession(identifier + '.1', testRecord).then(function() {
|
||||
return store.getSession(identifier + '.1').then(function(record) {
|
||||
assert.deepEqual(record, testRecord);
|
||||
var devices = [1, 2, 3].map(function(deviceId) {
|
||||
return [identifier, deviceId].join('.');
|
||||
});
|
||||
var promise = Promise.resolve();
|
||||
devices.forEach(function(encodedNumber) {
|
||||
promise = promise.then(function() {
|
||||
return store.putSession(encodedNumber, testRecord + encodedNumber)
|
||||
});
|
||||
});
|
||||
promise.then(function() {
|
||||
return Promise.all(devices.map(store.getSession)).then(function(records) {
|
||||
for (var i in records) {
|
||||
assert.strictEqual(records[i], testRecord + devices[i]);
|
||||
};
|
||||
});
|
||||
}).then(done,done);
|
||||
});
|
||||
it('removes all sessions for a number', function(done) {
|
||||
var testRecord = "an opaque string";
|
||||
var devices = [1, 2, 3].map(function(deviceId) {
|
||||
return [identifier, deviceId].join('.');
|
||||
});
|
||||
var promise = Promise.resolve();
|
||||
devices.forEach(function(encodedNumber) {
|
||||
promise = promise.then(function() {
|
||||
return store.putSession(encodedNumber, testRecord + encodedNumber)
|
||||
});
|
||||
});
|
||||
promise.then(function() {
|
||||
return store.removeAllSessions(identifier).then(function(record) {
|
||||
return Promise.all(devices.map(store.getSession)).then(function(records) {
|
||||
for (var i in records) {
|
||||
assert.isUndefined(records[i]);
|
||||
};
|
||||
});
|
||||
});
|
||||
}).then(done,done);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue