From 8453424ebdcf1724c586a64fd7b76447884524f1 Mon Sep 17 00:00:00 2001 From: lilia Date: Mon, 28 Sep 2015 13:33:26 -0700 Subject: [PATCH] Fix stuck pending messages state Refactor outgoing message error handling to use the same success and error handlers. This creates a somewhat strange pattern, where we call send and pass in the promise that resolves when sending is complete, but there's enough variety in the libtextsecure syntax for different message sending routines that it belongs at the conversation level and only the post-processing stuff is really shared by all messages. // FREEBIE --- js/models/conversations.js | 54 ++++++-------------------------------- js/models/messages.js | 33 +++++++++++++++++++++++ 2 files changed, 41 insertions(+), 46 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index 735194a1..9ae7e511 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -98,50 +98,20 @@ else { sendFunc = textsecure.messaging.sendMessageToGroup; } - sendFunc(this.get('id'), body, attachments, now).then(function() { - message.save({'sent': true}); - }.bind(this)).catch(function(errors) { - if (errors instanceof Error) { - errors = [errors]; - } - var keyErrors = []; - _.each(errors, function(e) { - console.log(e); - console.log(e.stack); - if (e.error.name === 'OutgoingIdentityKeyError') { - keyErrors.push(e.error); - } - }); - if (keyErrors.length) { - message.save({ errors : keyErrors }); - } else { - if (!(errors instanceof Array)) { - errors = [errors]; - } - errors.map(function(e) { - if (e.error && e.error.stack) { - console.error(e.error.stack); - } - }); - throw errors; - } - }); + message.send(sendFunc(this.get('id'), body, attachments, now)); }, endSession: function() { if (this.isPrivate()) { var now = Date.now(); - var message = this.messageCollection.add({ + var message = this.messageCollection.create({ conversationId : this.id, type : 'outgoing', sent_at : now, received_at : now, flags : textsecure.protobuf.DataMessage.Flags.END_SESSION }); - message.save(); - textsecure.messaging.closeSession(this.id).then(function() { - message.save({sent: true}); - }); + message.send(textsecure.messaging.closeSession(this.id)); } }, @@ -154,40 +124,32 @@ group_update = this.pick(['name', 'avatar', 'members']); } var now = Date.now(); - var message = this.messageCollection.add({ + var message = this.messageCollection.create({ conversationId : this.id, type : 'outgoing', sent_at : now, received_at : now, group_update : group_update }); - message.save(); - textsecure.messaging.updateGroup( + message.send(textsecure.messaging.updateGroup( this.id, this.get('name'), this.get('avatar'), this.get('members') - ).catch(function(errors) { - message.save({errors: errors.map(function(e){return e.error;})}); - }).then(function() { - message.save({sent: true}); - }); + )); }, leaveGroup: function() { var now = Date.now(); if (this.get('type') === 'group') { - var message = this.messageCollection.add({ + var message = this.messageCollection.create({ group_update: { left: 'You' }, conversationId : this.id, type : 'outgoing', sent_at : now, received_at : now }); - message.save(); - textsecure.messaging.leaveGroup(this.id).then(function() { - message.save({sent: true}); - }); + message.send(textsecure.messaging.leaveGroup(this.id)); } }, diff --git a/js/models/messages.js b/js/models/messages.js index 806f9d3e..da99fc39 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -131,6 +131,39 @@ e.number === number; }); }, + + send: function(promise) { + return promise.then(function() { + this.save({sent: true}); + }.bind(this)).catch(function(errors) { + this.save({sent: true}); + if (errors instanceof Error) { + errors = [errors]; + } + var keyErrors = []; + _.each(errors, function(e) { + console.log(e); + console.log(e.stack); + if (e.error.name === 'OutgoingIdentityKeyError') { + keyErrors.push(e.error); + } + }); + if (keyErrors.length) { + message.save({ errors : keyErrors }); + } else { + if (!(errors instanceof Array)) { + errors = [errors]; + } + errors.map(function(e) { + if (e.error && e.error.stack) { + console.error(e.error.stack); + } + }); + throw errors; + } + }.bind(this)); + }, + resolveConflict: function(number) { var error = this.getKeyConflict(number); if (error) {