From c3bbdb393f25d47f1bbfa7fed96c70db4d235c4f Mon Sep 17 00:00:00 2001 From: lilia Date: Wed, 24 Feb 2016 15:22:51 -0800 Subject: [PATCH] Refactor media uploading step Move this to its own function which encapsulates the error handling for it. // FREEBIE --- js/libtextsecure.js | 27 ++++++++++++++++----------- libtextsecure/sendmessage.js | 27 ++++++++++++++++----------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/js/libtextsecure.js b/js/libtextsecure.js index f7bac4656..1a5ffaa57 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -37519,7 +37519,7 @@ Message.prototype = { } var proto = new textsecure.protobuf.DataMessage(); proto.body = this.body; - proto.attachments = this.attachments; + proto.attachments = this.attachmentPointers; if (this.flags) { proto.flags = this.flags; } @@ -37581,12 +37581,23 @@ MessageSender.prototype = { }.bind(this)); }, - sendMessage: function(attrs) { - var message = new Message(attrs); + uploadMedia: function(message) { return Promise.all( message.attachments.map(this.makeAttachmentPointer.bind(this)) ).then(function(attachmentPointers) { - message.attachments = attachmentPointers; + message.attachmentPointers = attachmentPointers; + }).catch(function(error) { + if (error instanceof Error && error.name === 'HTTPError') { + throw new textsecure.MessageError(message, error); + } else { + throw error; + } + }); + }, + + sendMessage: function(attrs) { + var message = new Message(attrs); + return this.uploadMedia(message).then(function() { return new Promise(function(resolve, reject) { this.sendMessageProto( message.timestamp, @@ -37602,13 +37613,7 @@ MessageSender.prototype = { } ); }.bind(this)); - }.bind(this)).catch(function(error) { - if (error instanceof Error && error.name === 'HTTPError') { - throw new textsecure.MessageError(attrs, error); - } else { - throw error; - } - }); + }.bind(this)); }, sendMessageProto: function(timestamp, numbers, message, callback) { var outgoing = new OutgoingMessage(this.server, timestamp, numbers, message, callback); diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index f69f81e53..35b7a70aa 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -62,7 +62,7 @@ Message.prototype = { } var proto = new textsecure.protobuf.DataMessage(); proto.body = this.body; - proto.attachments = this.attachments; + proto.attachments = this.attachmentPointers; if (this.flags) { proto.flags = this.flags; } @@ -124,12 +124,23 @@ MessageSender.prototype = { }.bind(this)); }, - sendMessage: function(attrs) { - var message = new Message(attrs); + uploadMedia: function(message) { return Promise.all( message.attachments.map(this.makeAttachmentPointer.bind(this)) ).then(function(attachmentPointers) { - message.attachments = attachmentPointers; + message.attachmentPointers = attachmentPointers; + }).catch(function(error) { + if (error instanceof Error && error.name === 'HTTPError') { + throw new textsecure.MessageError(message, error); + } else { + throw error; + } + }); + }, + + sendMessage: function(attrs) { + var message = new Message(attrs); + return this.uploadMedia(message).then(function() { return new Promise(function(resolve, reject) { this.sendMessageProto( message.timestamp, @@ -145,13 +156,7 @@ MessageSender.prototype = { } ); }.bind(this)); - }.bind(this)).catch(function(error) { - if (error instanceof Error && error.name === 'HTTPError') { - throw new textsecure.MessageError(attrs, error); - } else { - throw error; - } - }); + }.bind(this)); }, sendMessageProto: function(timestamp, numbers, message, callback) { var outgoing = new OutgoingMessage(this.server, timestamp, numbers, message, callback);