diff --git a/js/axolotl_store.js b/js/axolotl_store.js index dd273251..c9ab1ec3 100644 --- a/js/axolotl_store.js +++ b/js/axolotl_store.js @@ -172,9 +172,7 @@ getSession: function(identifier) { if (identifier === null || identifier === undefined) throw new Error("Tried to get session for undefined/null key"); - return new Promise(function(resolve) { - resolve(textsecure.storage.sessions.getSessionsForNumber(identifier)); - }); + return textsecure.storage.sessions.getSessionsForNumber(identifier); }, putSession: function(identifier, record) { if (identifier === null || identifier === undefined) diff --git a/js/libtextsecure.js b/js/libtextsecure.js index f7a0e36f..4ed14167 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -38096,16 +38096,18 @@ axolotlInternal.RecipientRecord = function() { window.textsecure.storage.sessions = { getSessionsForNumber: function(encodedNumber) { - var number = textsecure.utils.unencodeNumber(encodedNumber)[0]; - var deviceId = textsecure.utils.unencodeNumber(encodedNumber)[1]; + 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; + var sessions = textsecure.storage.get("sessions" + number); + if (sessions === undefined) + return undefined; + if (sessions[deviceId] === undefined) + return undefined; - return sessions[deviceId]; + return sessions[deviceId]; + })()); }, putSessionsForDevice: function(encodedNumber, record) { @@ -39807,12 +39809,12 @@ window.textsecure.messaging = function() { var getDevicesAndSendToNumber = function(number) { var devicesForNumber = textsecure.storage.devices.getDeviceObjectsForNumber(number); - var promises = []; - for (var j in devicesForNumber) - if (!textsecure.protocol_wrapper.hasOpenSession(devicesForNumber[j].encodedNumber)) - promises[promises.length] = getKeysForNumber(number, [parseInt(textsecure.utils.unencodeNumber(devicesForNumber[j].encodedNumber)[1])]); - - Promise.all(promises).then(function() { + return Promise.all(devicesForNumber.map(function(device) { + return textsecure.protocol_wrapper.hasOpenSession(device.encodedNumber).then(function(result) { + if (!result) + return getKeysForNumber(number, [parseInt(textsecure.utils.unencodeNumber(device.encodedNumber)[1])]); + }); + })).then(function() { devicesForNumber = textsecure.storage.devices.getDeviceObjectsForNumber(number); if (devicesForNumber.length == 0) { diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index f3668b4f..8ad61965 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -210,12 +210,12 @@ window.textsecure.messaging = function() { var getDevicesAndSendToNumber = function(number) { var devicesForNumber = textsecure.storage.devices.getDeviceObjectsForNumber(number); - var promises = []; - for (var j in devicesForNumber) - if (!textsecure.protocol_wrapper.hasOpenSession(devicesForNumber[j].encodedNumber)) - promises[promises.length] = getKeysForNumber(number, [parseInt(textsecure.utils.unencodeNumber(devicesForNumber[j].encodedNumber)[1])]); - - Promise.all(promises).then(function() { + return Promise.all(devicesForNumber.map(function(device) { + return textsecure.protocol_wrapper.hasOpenSession(device.encodedNumber).then(function(result) { + if (!result) + return getKeysForNumber(number, [parseInt(textsecure.utils.unencodeNumber(device.encodedNumber)[1])]); + }); + })).then(function() { devicesForNumber = textsecure.storage.devices.getDeviceObjectsForNumber(number); if (devicesForNumber.length == 0) { diff --git a/libtextsecure/storage/devices.js b/libtextsecure/storage/devices.js index 44da7264..3bdaeed2 100644 --- a/libtextsecure/storage/devices.js +++ b/libtextsecure/storage/devices.js @@ -25,16 +25,18 @@ window.textsecure.storage.sessions = { getSessionsForNumber: function(encodedNumber) { - var number = textsecure.utils.unencodeNumber(encodedNumber)[0]; - var deviceId = textsecure.utils.unencodeNumber(encodedNumber)[1]; + 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; + var sessions = textsecure.storage.get("sessions" + number); + if (sessions === undefined) + return undefined; + if (sessions[deviceId] === undefined) + return undefined; - return sessions[deviceId]; + return sessions[deviceId]; + })()); }, putSessionsForDevice: function(encodedNumber, record) {