Extract app-level logic from libtextsecure

Ensure that both tryAgain functions return promises, allowing the
application to take appropriate action in the result of success or
failure. This lets us remove all dependency from libtextsecure on
app-level constructs like message objects/ids and the `extenion.trigger`
function.

Corresponding frontend changes to follow in another commit.
This commit is contained in:
lilia 2015-02-23 15:59:22 -08:00
parent e88d4d8170
commit c642854ddf
5 changed files with 20 additions and 46 deletions

View file

@ -110,7 +110,6 @@
));
}).catch(function(e) {
if (e.name === 'IncomingIdentityKeyError') {
e.args.push(message.id);
message.save({ errors : [e] }).then(function() {
extension.trigger('message', message);
notifyConversation(message);

View file

@ -37819,18 +37819,11 @@ window.axolotl.sessions = {
}
};
var wipeIdentityAndTryMessageAgain = function(from, encodedMessage, message_id) {
var wipeIdentityAndTryMessageAgain = function(from, encodedMessage) {
// Wipe identity key!
textsecure.storage.devices.removeIdentityKeyForNumber(from.split('.')[0]);
//TODO: Probably breaks with a devicecontrol message
return textsecure.protocol_wrapper.handlePreKeyWhisperMessage(from, encodedMessage).then(decodeMessageContents).then(
function(pushMessageContent) {
extension.trigger('message:decrypted', {
message_id : message_id,
data : pushMessageContent
});
}
);
return textsecure.protocol_wrapper.handlePreKeyWhisperMessage(from, encodedMessage).then(decodeMessageContents);
}
textsecure.replay.registerFunction(wipeIdentityAndTryMessageAgain, textsecure.replay.Type.INIT_SESSION);
})();
@ -39451,20 +39444,15 @@ window.textsecure.messaging = function() {
}
}
var tryMessageAgain = function(number, encodedMessage, message_id) {
var message = new Whisper.MessageCollection().add({id: message_id});
message.fetch().then(function() {
//TODO: Encapsuate with the rest of textsecure.storage.devices
textsecure.storage.devices.removeIdentityKeyForNumber(number);
var proto = textsecure.protobuf.PushMessageContent.decode(encodedMessage, 'binary');
sendMessageProto(message.get('sent_at'), [number], proto, function(res) {
var tryMessageAgain = function(number, encodedMessage, timestamp) {
textsecure.storage.devices.removeIdentityKeyForNumber(number);
var proto = textsecure.protobuf.PushMessageContent.decode(encodedMessage, 'binary');
return new Promise(function(resolve, reject) {
sendMessageProto(timestamp, [number], proto, function(res) {
if (res.failure.length > 0)
message.set('errors', res.failure);
reject(res.failure);
else
message.set('errors', []);
message.save().then(function(){
extension.trigger('message', message); // notify frontend listeners
});
resolve();
});
});
};
@ -39525,7 +39513,7 @@ window.textsecure.messaging = function() {
if (error.message !== "Identity key changed")
registerError(number, "Failed to reload device keys", error);
else {
error = new textsecure.OutgoingIdentityKeyError(number, getString(message.encode()));
error = new textsecure.OutgoingIdentityKeyError(number, getString(message.encode()), timestamp);
registerError(number, "Identity key changed", error);
}
});

View file

@ -97,7 +97,6 @@
var keyErrors = [];
_.each(errors, function(e) {
if (e.error.name === 'OutgoingIdentityKeyError') {
e.error.args.push(message.id);
keyErrors.push(e.error);
}
});

View file

@ -119,18 +119,11 @@
}
};
var wipeIdentityAndTryMessageAgain = function(from, encodedMessage, message_id) {
var wipeIdentityAndTryMessageAgain = function(from, encodedMessage) {
// Wipe identity key!
textsecure.storage.devices.removeIdentityKeyForNumber(from.split('.')[0]);
//TODO: Probably breaks with a devicecontrol message
return textsecure.protocol_wrapper.handlePreKeyWhisperMessage(from, encodedMessage).then(decodeMessageContents).then(
function(pushMessageContent) {
extension.trigger('message:decrypted', {
message_id : message_id,
data : pushMessageContent
});
}
);
return textsecure.protocol_wrapper.handlePreKeyWhisperMessage(from, encodedMessage).then(decodeMessageContents);
}
textsecure.replay.registerFunction(wipeIdentityAndTryMessageAgain, textsecure.replay.Type.INIT_SESSION);
})();

View file

@ -127,20 +127,15 @@ window.textsecure.messaging = function() {
}
}
var tryMessageAgain = function(number, encodedMessage, message_id) {
var message = new Whisper.MessageCollection().add({id: message_id});
message.fetch().then(function() {
//TODO: Encapsuate with the rest of textsecure.storage.devices
textsecure.storage.devices.removeIdentityKeyForNumber(number);
var proto = textsecure.protobuf.PushMessageContent.decode(encodedMessage, 'binary');
sendMessageProto(message.get('sent_at'), [number], proto, function(res) {
var tryMessageAgain = function(number, encodedMessage, timestamp) {
textsecure.storage.devices.removeIdentityKeyForNumber(number);
var proto = textsecure.protobuf.PushMessageContent.decode(encodedMessage, 'binary');
return new Promise(function(resolve, reject) {
sendMessageProto(timestamp, [number], proto, function(res) {
if (res.failure.length > 0)
message.set('errors', res.failure);
reject(res.failure);
else
message.set('errors', []);
message.save().then(function(){
extension.trigger('message', message); // notify frontend listeners
});
resolve();
});
});
};
@ -201,7 +196,7 @@ window.textsecure.messaging = function() {
if (error.message !== "Identity key changed")
registerError(number, "Failed to reload device keys", error);
else {
error = new textsecure.OutgoingIdentityKeyError(number, getString(message.encode()));
error = new textsecure.OutgoingIdentityKeyError(number, getString(message.encode()), timestamp);
registerError(number, "Identity key changed", error);
}
});