From 73edabfb1727d0f268a4f4b30a4b42e61a3fba1b Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Thu, 19 Apr 2018 11:19:23 -0700 Subject: [PATCH] Full pipeline to send quotes, including thumbnail upload --- js/models/conversations.js | 4 ++- js/views/conversation_view.js | 4 +-- libtextsecure/sendmessage.js | 62 ++++++++++++++++++++++++++++++++--- 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index 69066f0872..6b9e99ae00 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -663,7 +663,7 @@ }; }, - sendMessage(body, attachments) { + sendMessage(body, attachments, quote) { this.queueJob(async () => { const now = Date.now(); @@ -678,6 +678,7 @@ type: 'outgoing', body, conversationId: this.id, + quote, attachments, sent_at: now, received_at: now, @@ -719,6 +720,7 @@ this.get('id'), body, attachmentsWithData, + quote, now, this.get('expireTimer'), profileKey diff --git a/js/views/conversation_view.js b/js/views/conversation_view.js index 7081551ef3..bf611347c1 100644 --- a/js/views/conversation_view.js +++ b/js/views/conversation_view.js @@ -1157,13 +1157,13 @@ } const attachments = await this.fileInput.getFiles(); - const sendDelta = Date.now() - this.sendStart; console.log('Send pre-checks took', sendDelta, 'milliseconds'); - this.model.sendMessage(message, attachments); + this.model.sendMessage(message, attachments, this.quote); input.val(''); + this.setQuoteMessage(null); this.focusMessageFieldAndClearDisabled(); this.forceUpdateMessageFieldSize(e); this.fileInput.deleteFiles(); diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index 507029355c..b8a0c02a5d 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -17,6 +17,7 @@ function stringToArrayBuffer(str) { function Message(options) { this.body = options.body; this.attachments = options.attachments || []; + this.quote = options.quote; this.group = options.group; this.flags = options.flags; this.recipients = options.recipients; @@ -93,6 +94,29 @@ Message.prototype = { proto.group.id = stringToArrayBuffer(this.group.id); proto.group.type = this.group.type } + if (this.quote) { + var QuotedAttachment = textsecure.protobuf.DataMessage.Quote.QuotedAttachment; + var Quote = textsecure.protobuf.DataMessage.Quote; + console.log(this.quote); + + proto.quote = new Quote(); + + var quote = proto.quote; + quote.id = this.quote.id; + quote.author = this.quote.author; + quote.text = this.quote.text; + quote.attachments = (this.quote.attachments || []).map(function(attachment) { + var quotedAttachment = new QuotedAttachment(); + + quotedAttachment.contentType = attachment.contentType; + quotedAttachment.fileName = attachment.fileName; + if (attachment.attachmentPointer) { + quotedAttachment.thumbnail = attachment.attachmentPointer; + } + + return quotedAttachment; + }); + } if (this.expireTimer) { proto.expireTimer = this.expireTimer; } @@ -223,7 +247,7 @@ MessageSender.prototype = { }.bind(this)); }, - uploadMedia: function(message) { + uploadAttachments: function(message) { return Promise.all( message.attachments.map(this.makeAttachmentPointer.bind(this)) ).then(function(attachmentPointers) { @@ -237,9 +261,37 @@ MessageSender.prototype = { }); }, + uploadThumbnails: function(message) { + var makePointer = this.makeAttachmentPointer.bind(this); + var quote = message.quote; + + if (!quote || !quote.attachments || quote.attachments.length === 0) { + return Promise.resolve(); + } + + return Promise.all(quote.attachments.map(function(attachment) { + const thumbnail = attachment.thumbnail; + if (!thumbnail) { + return; + } + + return makePointer(thumbnail).then(function(pointer) { + attachment.attachmentPointer = pointer; + }); + })).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 this.uploadAttachments(message).then(function() { + return this.uploadThumbnails(message); + }.bind(this)).then(function() { return new Promise(function(resolve, reject) { this.sendMessageProto( message.timestamp, @@ -494,12 +546,13 @@ MessageSender.prototype = { }.bind(this)); }, - sendMessageToNumber: function(number, messageText, attachments, timestamp, expireTimer, profileKey) { + sendMessageToNumber: function(number, messageText, attachments, quote, timestamp, expireTimer, profileKey) { return this.sendMessage({ recipients : [number], body : messageText, timestamp : timestamp, attachments : attachments, + quote : quote, needsSync : true, expireTimer : expireTimer, profileKey : profileKey @@ -558,7 +611,7 @@ MessageSender.prototype = { ]); }, - sendMessageToGroup: function(groupId, messageText, attachments, timestamp, expireTimer, profileKey) { + sendMessageToGroup: function(groupId, messageText, attachments, quote, timestamp, expireTimer, profileKey) { return textsecure.storage.groups.getNumbers(groupId).then(function(numbers) { if (numbers === undefined) return Promise.reject(new Error("Unknown Group")); @@ -574,6 +627,7 @@ MessageSender.prototype = { body : messageText, timestamp : timestamp, attachments : attachments, + quote : quote, needsSync : true, expireTimer : expireTimer, profileKey : profileKey,