From 9ad55c803fa5131fd3a81ed014935b759a7f0287 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Fri, 13 Apr 2018 15:45:37 -0700 Subject: [PATCH] Fix handling attachment thumbnails using thumbnail key --- js/modules/types/message.js | 13 +++++++++++-- test/modules/types/message_test.js | 23 +++++++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/js/modules/types/message.js b/js/modules/types/message.js index 6a6cc64ca..5df1bebcf 100644 --- a/js/modules/types/message.js +++ b/js/modules/types/message.js @@ -157,8 +157,17 @@ exports._mapQuotedAttachments = upgradeAttachment => async (message, context) => return message; } - const upgradeWithContext = attachment => - upgradeAttachment(attachment, context); + const upgradeWithContext = async (attachment) => { + if (!attachment || !attachment.thumbnail) { + return attachment; + } + + const thumbnail = await upgradeAttachment(attachment.thumbnail, context); + return Object.assign({}, attachment, { + thumbnail, + }); + }; + const quotedAttachments = (message.quote && message.quote.attachments) || []; const attachments = await Promise.all(quotedAttachments.map(upgradeWithContext)); diff --git a/test/modules/types/message_test.js b/test/modules/types/message_test.js index 138ad46f1..7a40dfcd9 100644 --- a/test/modules/types/message_test.js +++ b/test/modules/types/message_test.js @@ -358,6 +358,21 @@ describe('Message', () => { assert.deepEqual(result, message); }); + it('handles attachments with no thumbnail', async () => { + const upgradeAttachment = sinon.stub().throws(new Error("Shouldn't be called")); + const upgradeVersion = Message._mapQuotedAttachments(upgradeAttachment); + + const message = { + body: 'hey there!', + quote: { + text: 'hey!', + attachments: [], + }, + }; + const result = await upgradeVersion(message); + assert.deepEqual(result, message); + }); + it('calls provided async function for each quoted attachment', async () => { const upgradeAttachment = sinon.stub().resolves({ path: '/new/path/on/disk', @@ -369,7 +384,9 @@ describe('Message', () => { quote: { text: 'hey!', attachments: [{ - data: 'data is here', + thumbnail: { + data: 'data is here', + }, }], }, }; @@ -378,7 +395,9 @@ describe('Message', () => { quote: { text: 'hey!', attachments: [{ - path: '/new/path/on/disk', + thumbnail: { + path: '/new/path/on/disk', + }, }], }, };