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:
parent
6717390e83
commit
a52d35bb1b
3 changed files with 52 additions and 76 deletions
|
@ -38140,16 +38140,25 @@ axolotlInternal.RecipientRecord = function() {
|
||||||
delete tempKeys[encodedNumber];
|
delete tempKeys[encodedNumber];
|
||||||
return Promise.resolve();
|
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) {
|
getDeviceObjectsForNumber: function(number) {
|
||||||
return textsecure.storage.axolotl.getIdentityKey(number).then(function(identityKey) {
|
return textsecure.storage.axolotl.getIdentityKey(number).then(function(identityKey) {
|
||||||
if (identityKey === undefined) {
|
if (identityKey === undefined) {
|
||||||
|
@ -39767,11 +39776,11 @@ OutgoingMessage.prototype = {
|
||||||
return this.server.getKeysForNumber(number).then(handleResult);
|
return this.server.getKeysForNumber(number).then(handleResult);
|
||||||
} else {
|
} else {
|
||||||
var promise = Promise.resolve();
|
var promise = Promise.resolve();
|
||||||
for (var i in updateDevices) {
|
updateDevices.forEach(function(device) {
|
||||||
promise = promise.then(function() {
|
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));
|
}.bind(this));
|
||||||
}
|
|
||||||
|
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
@ -39812,33 +39821,12 @@ OutgoingMessage.prototype = {
|
||||||
},
|
},
|
||||||
|
|
||||||
sendToNumber: function(number) {
|
sendToNumber: function(number) {
|
||||||
return textsecure.storage.devices.getDeviceObjectsForNumber(number).then(function(devicesForNumber) {
|
return textsecure.storage.devices.getStaleDeviceIdsForNumber(number).then(function(updateDevices) {
|
||||||
return Promise.all(devicesForNumber.map(function(device) {
|
return this.getKeysForNumber(number, updateDevices)
|
||||||
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));
|
|
||||||
}
|
|
||||||
}.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))
|
.then(this.reloadDevicesAndSend(number, true))
|
||||||
.catch(function(error) {
|
.catch(function(error) {
|
||||||
this.registerError(number, "Failed to retreive new device keys for number " + number, error);
|
this.registerError(number, "Failed to retreive new device keys for number " + number, error);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
} else {
|
|
||||||
return this.doSendMessage(number, devicesForNumber, true);
|
|
||||||
}
|
|
||||||
}.bind(this));
|
|
||||||
}.bind(this));
|
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -69,11 +69,11 @@ OutgoingMessage.prototype = {
|
||||||
return this.server.getKeysForNumber(number).then(handleResult);
|
return this.server.getKeysForNumber(number).then(handleResult);
|
||||||
} else {
|
} else {
|
||||||
var promise = Promise.resolve();
|
var promise = Promise.resolve();
|
||||||
for (var i in updateDevices) {
|
updateDevices.forEach(function(device) {
|
||||||
promise = promise.then(function() {
|
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));
|
}.bind(this));
|
||||||
}
|
|
||||||
|
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
@ -114,33 +114,12 @@ OutgoingMessage.prototype = {
|
||||||
},
|
},
|
||||||
|
|
||||||
sendToNumber: function(number) {
|
sendToNumber: function(number) {
|
||||||
return textsecure.storage.devices.getDeviceObjectsForNumber(number).then(function(devicesForNumber) {
|
return textsecure.storage.devices.getStaleDeviceIdsForNumber(number).then(function(updateDevices) {
|
||||||
return Promise.all(devicesForNumber.map(function(device) {
|
return this.getKeysForNumber(number, updateDevices)
|
||||||
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));
|
|
||||||
}
|
|
||||||
}.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))
|
.then(this.reloadDevicesAndSend(number, true))
|
||||||
.catch(function(error) {
|
.catch(function(error) {
|
||||||
this.registerError(number, "Failed to retreive new device keys for number " + number, error);
|
this.registerError(number, "Failed to retreive new device keys for number " + number, error);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
} else {
|
|
||||||
return this.doSendMessage(number, devicesForNumber, true);
|
|
||||||
}
|
|
||||||
}.bind(this));
|
|
||||||
}.bind(this));
|
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -40,16 +40,25 @@
|
||||||
delete tempKeys[encodedNumber];
|
delete tempKeys[encodedNumber];
|
||||||
return Promise.resolve();
|
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) {
|
getDeviceObjectsForNumber: function(number) {
|
||||||
return textsecure.storage.axolotl.getIdentityKey(number).then(function(identityKey) {
|
return textsecure.storage.axolotl.getIdentityKey(number).then(function(identityKey) {
|
||||||
if (identityKey === undefined) {
|
if (identityKey === undefined) {
|
||||||
|
|
Loading…
Reference in a new issue