From 12739ac82dc2d392ae85c782fe51a2a41658a61a Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Fri, 8 Feb 2019 18:45:59 -0800 Subject: [PATCH] Ensure we don't add empty attachments to quote --- js/models/conversations.js | 87 +++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 30 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index 580bb7a1c..afde991fe 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -755,41 +755,20 @@ return _.without(this.get('members'), me); }, - async makeQuote(quotedMessage) { - const { getName } = Contact; - const contact = quotedMessage.getContact(); - const attachments = quotedMessage.get('attachments'); - const preview = quotedMessage.get('preview'); - - const body = quotedMessage.get('body'); - const embeddedContact = quotedMessage.get('contact'); - const embeddedContactName = - embeddedContact && embeddedContact.length > 0 - ? getName(embeddedContact[0]) - : ''; - - const media = - attachments && attachments.length ? attachments : preview || []; - - return { - author: contact.id, - id: quotedMessage.get('sent_at'), - text: body || embeddedContactName, - attachments: await Promise.all( - media + async getQuoteAttachment(attachments, preview) { + if (attachments && attachments.length) { + return Promise.all( + attachments .filter( attachment => attachment && - (attachment.image || (!attachment.pending && !attachment.error)) + attachment.contentType && + !attachment.pending && + !attachment.error ) .slice(0, 1) .map(async attachment => { - const { fileName } = attachment; - - const thumbnail = attachment.thumbnail || attachment.image; - const contentType = - attachment.contentType || - (attachment.image && attachment.image.contentType); + const { fileName, thumbnail, contentType } = attachment; return { contentType, @@ -804,7 +783,55 @@ : null, }; }) - ), + ); + } + + if (preview && preview.length) { + return Promise.all( + preview + .filter(item => item && item.image) + .slice(0, 1) + .map(async attachment => { + const { image } = attachment; + const { contentType } = image; + + return { + contentType, + // Our protos library complains about this field being undefined, so we + // force it to null + fileName: null, + thumbnail: image + ? { + ...(await loadAttachmentData(image)), + objectUrl: getAbsoluteAttachmentPath(image.path), + } + : null, + }; + }) + ); + } + + return []; + }, + + async makeQuote(quotedMessage) { + const { getName } = Contact; + const contact = quotedMessage.getContact(); + const attachments = quotedMessage.get('attachments'); + const preview = quotedMessage.get('preview'); + + const body = quotedMessage.get('body'); + const embeddedContact = quotedMessage.get('contact'); + const embeddedContactName = + embeddedContact && embeddedContact.length > 0 + ? getName(embeddedContact[0]) + : ''; + + return { + author: contact.id, + id: quotedMessage.get('sent_at'), + text: body || embeddedContactName, + attachments: await this.getQuoteAttachment(attachments, preview), }; },