From 3feb0037e5e7bdde3f156d6e9865ae633d76f654 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Fri, 21 Jun 2019 14:06:37 -0700 Subject: [PATCH] Ensure that long message attachments don't show in media gallery --- js/views/conversation_view.js | 24 ++++--- .../initializeAttachmentMetadata_test.ts | 68 +++++++++++++++++++ ts/types/MIME.ts | 1 + .../message/initializeAttachmentMetadata.ts | 13 ++-- 4 files changed, 92 insertions(+), 14 deletions(-) diff --git a/js/views/conversation_view.js b/js/views/conversation_view.js index 3c535f2bfd1..dfcdb155aaa 100644 --- a/js/views/conversation_view.js +++ b/js/views/conversation_view.js @@ -936,16 +936,20 @@ ); // Unlike visual media, only one non-image attachment is supported - const documents = rawDocuments.map(message => { - const attachments = message.attachments || []; - const attachment = attachments[0]; - return { - contentType: attachment.contentType, - index: 0, - attachment, - message, - }; - }); + const documents = rawDocuments + .filter(message => + Boolean(message.attachments && message.attachments.length) + ) + .map(message => { + const attachments = message.attachments || []; + const attachment = attachments[0]; + return { + contentType: attachment.contentType, + index: 0, + attachment, + message, + }; + }); const saveAttachment = async ({ attachment, message } = {}) => { const timestamp = message.received_at; diff --git a/ts/test/types/message/initializeAttachmentMetadata_test.ts b/ts/test/types/message/initializeAttachmentMetadata_test.ts index 207012db4cc..b74e04dde6b 100644 --- a/ts/test/types/message/initializeAttachmentMetadata_test.ts +++ b/ts/test/types/message/initializeAttachmentMetadata_test.ts @@ -133,5 +133,73 @@ describe('Message', () => { const actual = await Message.initializeAttachmentMetadata(input); assert.deepEqual(actual, expected); }); + + it('does not include long message attachments', async () => { + const input: IncomingMessage = { + type: 'incoming', + conversationId: 'foo', + id: '11111111-1111-1111-1111-111111111111', + timestamp: 1523317140899, + received_at: 1523317140899, + sent_at: 1523317140800, + attachments: [ + { + contentType: MIME.LONG_MESSAGE, + data: stringToArrayBuffer('foo'), + fileName: 'message.txt', + size: 1111, + }, + ], + }; + const expected: IncomingMessage = { + type: 'incoming', + conversationId: 'foo', + id: '11111111-1111-1111-1111-111111111111', + timestamp: 1523317140899, + received_at: 1523317140899, + sent_at: 1523317140800, + attachments: [ + { + contentType: MIME.LONG_MESSAGE, + data: stringToArrayBuffer('foo'), + fileName: 'message.txt', + size: 1111, + }, + ], + hasAttachments: 0, + hasVisualMediaAttachments: undefined, + hasFileAttachments: undefined, + }; + + const actual = await Message.initializeAttachmentMetadata(input); + assert.deepEqual(actual, expected); + }); + + it('handles not attachments', async () => { + const input: IncomingMessage = { + type: 'incoming', + conversationId: 'foo', + id: '11111111-1111-1111-1111-111111111111', + timestamp: 1523317140899, + received_at: 1523317140899, + sent_at: 1523317140800, + attachments: [], + }; + const expected: IncomingMessage = { + type: 'incoming', + conversationId: 'foo', + id: '11111111-1111-1111-1111-111111111111', + timestamp: 1523317140899, + received_at: 1523317140899, + sent_at: 1523317140800, + attachments: [], + hasAttachments: 0, + hasVisualMediaAttachments: undefined, + hasFileAttachments: undefined, + }; + + const actual = await Message.initializeAttachmentMetadata(input); + assert.deepEqual(actual, expected); + }); }); }); diff --git a/ts/types/MIME.ts b/ts/types/MIME.ts index dcf968138ce..72caf9b59b1 100644 --- a/ts/types/MIME.ts +++ b/ts/types/MIME.ts @@ -9,6 +9,7 @@ export const IMAGE_JPEG = 'image/jpeg' as MIMEType; export const IMAGE_WEBP = 'image/webp' as MIMEType; export const VIDEO_MP4 = 'video/mp4' as MIMEType; export const VIDEO_QUICKTIME = 'video/quicktime' as MIMEType; +export const LONG_MESSAGE = 'text/x-signal-plain' as MIMEType; export const isJPEG = (value: MIMEType): boolean => value === 'image/jpeg'; export const isImage = (value: MIMEType): boolean => diff --git a/ts/types/message/initializeAttachmentMetadata.ts b/ts/types/message/initializeAttachmentMetadata.ts index 3ab02722e4b..30d679479cb 100644 --- a/ts/types/message/initializeAttachmentMetadata.ts +++ b/ts/types/message/initializeAttachmentMetadata.ts @@ -17,12 +17,17 @@ export const initializeAttachmentMetadata = async ( return message; } - const hasAttachments = IndexedDB.toIndexableBoolean( - message.attachments.length > 0 + const attachments = message.attachments.filter( + (attachment: Attachment.Attachment) => + attachment.contentType !== 'text/x-signal-plain' ); + const hasAttachments = IndexedDB.toIndexableBoolean(attachments.length > 0); - const hasFileAttachments = hasFileAttachment(message); - const hasVisualMediaAttachments = hasVisualMediaAttachment(message); + const hasFileAttachments = hasFileAttachment({ ...message, attachments }); + const hasVisualMediaAttachments = hasVisualMediaAttachment({ + ...message, + attachments, + }); return { ...message,