From 53918d68de334362faf782288b7a87039f118a9d Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Mon, 7 May 2018 15:24:46 -0400 Subject: [PATCH] Add `Attachment.isFile` definition --- ts/test/types/Attachment_test.ts | 39 ++++++++++++++++++++++++++++++++ ts/types/Attachment.ts | 18 +++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/ts/test/types/Attachment_test.ts b/ts/test/types/Attachment_test.ts index 14b0cdcee..23fa40995 100644 --- a/ts/test/types/Attachment_test.ts +++ b/ts/test/types/Attachment_test.ts @@ -59,6 +59,45 @@ describe('Attachment', () => { }); }); + describe('isFile', () => { + it('should return true for JSON', () => { + const attachment: Attachment.Attachment = { + fileName: 'foo.json', + data: stringToArrayBuffer('{"foo": "bar"}'), + contentType: MIME.APPLICATION_JSON, + }; + assert.isTrue(Attachment.isFile(attachment)); + }); + + it('should return false for images', () => { + const attachment: Attachment.Attachment = { + fileName: 'meme.gif', + data: stringToArrayBuffer('gif'), + contentType: MIME.IMAGE_GIF, + }; + assert.isFalse(Attachment.isFile(attachment)); + }); + + it('should return false for videos', () => { + const attachment: Attachment.Attachment = { + fileName: 'meme.mp4', + data: stringToArrayBuffer('mp4'), + contentType: MIME.VIDEO_MP4, + }; + assert.isFalse(Attachment.isFile(attachment)); + }); + + it('should return false for voice message attachment', () => { + const attachment: Attachment.Attachment = { + fileName: 'Voice Message.aac', + flags: SignalService.AttachmentPointer.Flags.VOICE_MESSAGE, + data: stringToArrayBuffer('voice message'), + contentType: MIME.AUDIO_AAC, + }; + assert.isFalse(Attachment.isFile(attachment)); + }); + }); + describe('isVoiceMessage', () => { it('should return true for voice message attachment', () => { const attachment: Attachment.Attachment = { diff --git a/ts/types/Attachment.ts b/ts/types/Attachment.ts index 414bce856..3599f184f 100644 --- a/ts/types/Attachment.ts +++ b/ts/types/Attachment.ts @@ -41,6 +41,24 @@ export const isVisualMedia = (attachment: Attachment): boolean => { return MIME.isImage(contentType) || MIME.isVideo(contentType); }; +export const isFile = (attachment: Attachment): boolean => { + const { contentType } = attachment; + + if (is.undefined(contentType)) { + return false; + } + + if (isVisualMedia(attachment)) { + return false; + } + + if (isVoiceMessage(attachment)) { + return false; + } + + return true; +}; + export const isVoiceMessage = (attachment: Attachment): boolean => { const flag = SignalService.AttachmentPointer.Flags.VOICE_MESSAGE; const hasFlag =