Refactor and fixup key requests

Fix a bad loop scope bug in getKeysForNumber by using forEach.
Refactor the initial process of establishing key material for devices
that do not have open sessions.

// FREEBIE
This commit is contained in:
lilia 2015-11-03 16:13:19 -08:00
parent 6717390e83
commit a52d35bb1b
3 changed files with 52 additions and 76 deletions

View file

@ -38140,16 +38140,25 @@ axolotlInternal.RecipientRecord = function() {
delete tempKeys[encodedNumber];
return Promise.resolve();
},
needKeysForDevice: function(encodedNumber) {
if (tempKeys[encodedNumber] !== undefined) {
return Promise.resolve(false);
} else {
return textsecure.protocol_wrapper.hasOpenSession(encodedNumber).then(function(result) {
return !result;
});
}
},
getStaleDeviceIdsForNumber: function(number) {
return textsecure.storage.axolotl.getDeviceIds(number).then(function(deviceIds) {
if (deviceIds.length === 0) {
return [1];
}
var updateDevices = [];
return Promise.all(deviceIds.map(function(deviceId) {
var encodedNumber = number + '.' + deviceId;
return textsecure.protocol_wrapper.hasOpenSession(encodedNumber).then(function(hasSession) {
if (!hasSession && !tempKeys[encodedNumber]) {
updateDevices.push(deviceId);
}
});
})).then(function() {
return updateDevices;
});
});
},
getDeviceObjectsForNumber: function(number) {
return textsecure.storage.axolotl.getIdentityKey(number).then(function(identityKey) {
if (identityKey === undefined) {
@ -39767,11 +39776,11 @@ OutgoingMessage.prototype = {
return this.server.getKeysForNumber(number).then(handleResult);
} else {
var promise = Promise.resolve();
for (var i in updateDevices) {
updateDevices.forEach(function(device) {
promise = promise.then(function() {
return this.server.getKeysForNumber(number, updateDevices[i]).then(handleResult);
return this.server.getKeysForNumber(number, device).then(handleResult);
}.bind(this));
}
}.bind(this));
return promise;
}
@ -39812,33 +39821,12 @@ OutgoingMessage.prototype = {
},
sendToNumber: function(number) {
return textsecure.storage.devices.getDeviceObjectsForNumber(number).then(function(devicesForNumber) {
return Promise.all(devicesForNumber.map(function(device) {
return textsecure.storage.devices.needKeysForDevice(device.encodedNumber).then(function(result) {
if (result) {
return this.getKeysForNumber(number,
[parseInt(textsecure.utils.unencodeNumber(device.encodedNumber)[1])]
).catch(function(error) {
if (error.name !== 'OutgoingIdentityKeyError') {
this.registerError(number, "Failed to retreive new device keys for " + device.encodedNumber, error);
}
return Promise.reject();
}.bind(this));
}
return textsecure.storage.devices.getStaleDeviceIdsForNumber(number).then(function(updateDevices) {
return this.getKeysForNumber(number, updateDevices)
.then(this.reloadDevicesAndSend(number, true))
.catch(function(error) {
this.registerError(number, "Failed to retreive new device keys for number " + number, error);
}.bind(this));
}.bind(this))).then(function() {
return textsecure.storage.devices.getDeviceObjectsForNumber(number).then(function(devicesForNumber) {
if (devicesForNumber.length == 0) {
return this.getKeysForNumber(number, [1])
.then(this.reloadDevicesAndSend(number, true))
.catch(function(error) {
this.registerError(number, "Failed to retreive new device keys for number " + number, error);
}.bind(this));
} else {
return this.doSendMessage(number, devicesForNumber, true);
}
}.bind(this));
}.bind(this));
}.bind(this));
},

View file

@ -69,11 +69,11 @@ OutgoingMessage.prototype = {
return this.server.getKeysForNumber(number).then(handleResult);
} else {
var promise = Promise.resolve();
for (var i in updateDevices) {
updateDevices.forEach(function(device) {
promise = promise.then(function() {
return this.server.getKeysForNumber(number, updateDevices[i]).then(handleResult);
return this.server.getKeysForNumber(number, device).then(handleResult);
}.bind(this));
}
}.bind(this));
return promise;
}
@ -114,33 +114,12 @@ OutgoingMessage.prototype = {
},
sendToNumber: function(number) {
return textsecure.storage.devices.getDeviceObjectsForNumber(number).then(function(devicesForNumber) {
return Promise.all(devicesForNumber.map(function(device) {
return textsecure.storage.devices.needKeysForDevice(device.encodedNumber).then(function(result) {
if (result) {
return this.getKeysForNumber(number,
[parseInt(textsecure.utils.unencodeNumber(device.encodedNumber)[1])]
).catch(function(error) {
if (error.name !== 'OutgoingIdentityKeyError') {
this.registerError(number, "Failed to retreive new device keys for " + device.encodedNumber, error);
}
return Promise.reject();
}.bind(this));
}
return textsecure.storage.devices.getStaleDeviceIdsForNumber(number).then(function(updateDevices) {
return this.getKeysForNumber(number, updateDevices)
.then(this.reloadDevicesAndSend(number, true))
.catch(function(error) {
this.registerError(number, "Failed to retreive new device keys for number " + number, error);
}.bind(this));
}.bind(this))).then(function() {
return textsecure.storage.devices.getDeviceObjectsForNumber(number).then(function(devicesForNumber) {
if (devicesForNumber.length == 0) {
return this.getKeysForNumber(number, [1])
.then(this.reloadDevicesAndSend(number, true))
.catch(function(error) {
this.registerError(number, "Failed to retreive new device keys for number " + number, error);
}.bind(this));
} else {
return this.doSendMessage(number, devicesForNumber, true);
}
}.bind(this));
}.bind(this));
}.bind(this));
},

View file

@ -40,16 +40,25 @@
delete tempKeys[encodedNumber];
return Promise.resolve();
},
needKeysForDevice: function(encodedNumber) {
if (tempKeys[encodedNumber] !== undefined) {
return Promise.resolve(false);
} else {
return textsecure.protocol_wrapper.hasOpenSession(encodedNumber).then(function(result) {
return !result;
});
}
},
getStaleDeviceIdsForNumber: function(number) {
return textsecure.storage.axolotl.getDeviceIds(number).then(function(deviceIds) {
if (deviceIds.length === 0) {
return [1];
}
var updateDevices = [];
return Promise.all(deviceIds.map(function(deviceId) {
var encodedNumber = number + '.' + deviceId;
return textsecure.protocol_wrapper.hasOpenSession(encodedNumber).then(function(hasSession) {
if (!hasSession && !tempKeys[encodedNumber]) {
updateDevices.push(deviceId);
}
});
})).then(function() {
return updateDevices;
});
});
},
getDeviceObjectsForNumber: function(number) {
return textsecure.storage.axolotl.getIdentityKey(number).then(function(identityKey) {
if (identityKey === undefined) {