diff --git a/js/models/conversations.js b/js/models/conversations.js index 406f3d451..0fb49dc15 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -42,28 +42,40 @@ if (missing.length) { return "Conversation must have " + missing; } }, - sendMessage: function(message, attachments) { + sendMessage: function(body, attachments) { var now = Date.now(); - this.messageCollection.add({ - body : message, + var message = this.messageCollection.add({ + body : body, conversationId : this.id, type : 'outgoing', attachments : attachments, sent_at : now, received_at : now - }).save(); + }); + message.save(); this.save({ unreadCount : 0, active_at : now }); + var sendFunc; if (this.get('type') == 'private') { - return textsecure.messaging.sendMessageToNumber(this.get('id'), message, attachments); + sendFunc = textsecure.messaging.sendMessageToNumber; } else { - return textsecure.messaging.sendMessageToGroup(this.get('groupId'), message, attachments); + sendFunc = textsecure.messaging.sendMessageToGroup; } + sendFunc(this.get('id'), body, attachments).catch(function(e) { + if (e.name === 'OutgoingIdentityKeyError') { + e.args.push(message.id); + message.save({ errors : [e] }).then(function() { + extension.trigger('message', message); // notify frontend listeners + }); + } else { + throw e; + } + }); }, receiveMessage: function(decrypted) { diff --git a/js/sendmessage.js b/js/sendmessage.js index 9db62f6d2..519b643e6 100644 --- a/js/sendmessage.js +++ b/js/sendmessage.js @@ -114,12 +114,25 @@ window.textsecure.messaging = function() { return Promise.all(promises); } - var tryMessageAgain = function(number, encodedMessage, callback) { - //TODO: Wipe identity key! - refreshGroups(number).then(function() { - var message = textsecure.protobuf.PushMessageContent.decode(encodedMessage); - textsecure.sendMessageProto([number], message, callback); - }); + var tryMessageAgain = function(number, encodedMessage, message_id) { + var message = new Whisper.MessageCollection().add({id: message_id}); + message.fetch().then(function() { + textsecure.storage.removeEncrypted("devices" + number); + refreshGroups(number).then(function() { + var proto = textsecure.protobuf.PushMessageContent.decode(encodedMessage, 'binary'); + sendMessageProto([number], proto, function(res) { + if (res.failure.length > 0) { + message.set('errors', []); + } + else { + message.set('errors', res.failures); + } + message.save().then(function(){ + extension.trigger('message', message); // notify frontend listeners + }); + }); + }); + }); }; textsecure.replay.registerFunction(tryMessageAgain, textsecure.replay.Type.SEND_MESSAGE); @@ -175,7 +188,7 @@ window.textsecure.messaging = function() { if (error.message !== "Identity key changed") registerError(number, "Failed to reload device keys", error); else { - error = new textsecure.OutgoingIdentityKeyError(encodedNumber, message.encode()); + error = new textsecure.OutgoingIdentityKeyError(number, getString(message.encode())); registerError(number, "Identity key changed", error); } });