diff --git a/js/modules/types/contact.js b/js/modules/types/contact.js new file mode 100644 index 000000000..561a1f23f --- /dev/null +++ b/js/modules/types/contact.js @@ -0,0 +1,121 @@ +const { omit, compact, map } = require('lodash'); + +exports.parseAndWriteContactAvatar = upgradeAttachment => async ( + contact, + context = {} +) => { + const { message } = context; + const { avatar } = contact; + const contactWithUpdatedAvatar = + avatar && avatar.avatar + ? Object.assign({}, contact, { + avatar: Object.assign({}, avatar, { + avatar: await upgradeAttachment(avatar.avatar, context), + }), + }) + : omit(contact, ['avatar']); + + // eliminates empty numbers, emails, and addresses; adds type if not provided + const contactWithCleanedElements = parseContact(contactWithUpdatedAvatar); + + // We'll log if the contact is invalid, leave everything as-is + validateContact(contactWithCleanedElements, { + messageId: idForLogging(message), + }); + + return contactWithCleanedElements; +}; + +function parseContact(contact) { + return Object.assign( + {}, + omit(contact, ['avatar', 'number', 'email', 'address']), + cleanAvatar(contact.avatar), + addArrayKey('number', compact(map(contact.number, cleanBasicItem))), + addArrayKey('email', compact(map(contact.email, cleanBasicItem))), + addArrayKey('address', compact(map(contact.address, cleanAddress))) + ); +} + +function idForLogging(message) { + return `${message.source}.${message.sourceDevice} ${message.sent_at}`; +} + +function validateContact(contact, options = {}) { + const { messageId } = options; + const { name, number, email, address, organization } = contact; + + if ((!name || !name.displayName) && !organization) { + console.log( + `Message ${messageId}: Contact had neither 'displayName' nor 'organization'` + ); + return false; + } + + if ( + (!number || !number.length) && + (!email || !email.length) && + (!address || !address.length) + ) { + console.log( + `Message ${messageId}: Contact had no included numbers, email or addresses` + ); + return false; + } + + return true; +} + +function cleanBasicItem(item) { + if (!item.value) { + return null; + } + + return Object.assign({}, item, { + type: item.type || 1, + }); +} + +function cleanAddress(address) { + if (!address) { + return null; + } + + if ( + !address.street && + !address.pobox && + !address.neighborhood && + !address.city && + !address.region && + !address.postcode && + !address.country + ) { + return null; + } + + return Object.assign({}, address, { + type: address.type || 1, + }); +} + +function cleanAvatar(avatar) { + if (!avatar) { + return null; + } + + return { + avatar: Object.assign({}, avatar, { + isProfile: avatar.isProfile || false, + }), + }; +} + +function addArrayKey(key, array) { + if (!array || !array.length) { + return null; + } + + return { + [key]: array, + }; +} diff --git a/js/modules/types/message.js b/js/modules/types/message.js index 624c6b52e..437a25379 100644 --- a/js/modules/types/message.js +++ b/js/modules/types/message.js @@ -1,5 +1,6 @@ -const { isFunction, isString, omit, compact, map } = require('lodash'); +const { isFunction, isString, omit } = require('lodash'); +const Contact = require('./contact'); const Attachment = require('./attachment'); const Errors = require('./errors'); const SchemaVersion = require('./schema_version'); @@ -210,126 +211,6 @@ exports._mapQuotedAttachments = upgradeAttachment => async ( }); }; -function validateContact(contact, options = {}) { - const { messageId } = options; - const { name, number, email, address, organization } = contact; - - if ((!name || !name.displayName) && !organization) { - console.log( - `Message ${messageId}: Contact had neither 'displayName' nor 'organization'` - ); - return false; - } - - if ( - (!number || !number.length) && - (!email || !email.length) && - (!address || !address.length) - ) { - console.log( - `Message ${messageId}: Contact had no included numbers, email or addresses` - ); - return false; - } - - return true; -} - -function cleanContact(contact) { - function cleanBasicItem(item) { - if (!item.value) { - return null; - } - - return Object.assign({}, item, { - type: item.type || 1, - }); - } - - function cleanAddress(address) { - if (!address) { - return null; - } - - if ( - !address.street && - !address.pobox && - !address.neighborhood && - !address.city && - !address.region && - !address.postcode && - !address.country - ) { - return null; - } - - return Object.assign({}, address, { - type: address.type || 1, - }); - } - - function cleanAvatar(avatar) { - if (!avatar) { - return null; - } - - return { - avatar: Object.assign({}, avatar, { - isProfile: avatar.isProfile || false, - }), - }; - } - - function addArrayKey(key, array) { - if (!array || !array.length) { - return null; - } - - return { - [key]: array, - }; - } - - return Object.assign( - {}, - omit(contact, ['avatar', 'number', 'email', 'address']), - cleanAvatar(contact.avatar), - addArrayKey('number', compact(map(contact.number, cleanBasicItem))), - addArrayKey('email', compact(map(contact.email, cleanBasicItem))), - addArrayKey('address', compact(map(contact.address, cleanAddress))) - ); -} - -function idForLogging(message) { - return `${message.source}.${message.sourceDevice} ${message.sent_at}`; -} - -exports._cleanAndWriteContactAvatar = upgradeAttachment => async ( - contact, - context = {} -) => { - const { message } = context; - const { avatar } = contact; - const contactWithUpdatedAvatar = - avatar && avatar.avatar - ? Object.assign({}, contact, { - avatar: Object.assign({}, avatar, { - avatar: await upgradeAttachment(avatar.avatar, context), - }), - }) - : omit(contact, ['avatar']); - - // eliminates empty numbers, emails, and addresses; adds type if not provided - const contactWithCleanedElements = cleanContact(contactWithUpdatedAvatar); - - // We'll log if the contact is invalid, leave everything as-is - validateContact(contactWithCleanedElements, { - messageId: idForLogging(message), - }); - - return contactWithCleanedElements; -}; - const toVersion0 = async message => exports.initializeSchemaVersion(message); const toVersion1 = exports._withSchemaVersion( @@ -353,7 +234,7 @@ const toVersion5 = exports._withSchemaVersion(5, initializeAttachmentMetadata); const toVersion6 = exports._withSchemaVersion( 6, exports._mapContact( - exports._cleanAndWriteContactAvatar(Attachment.migrateDataToFileSystem) + Contact.parseAndWriteContactAvatar(Attachment.migrateDataToFileSystem) ) ); diff --git a/test/modules/types/contact_test.js b/test/modules/types/contact_test.js new file mode 100644 index 000000000..c195e62e4 --- /dev/null +++ b/test/modules/types/contact_test.js @@ -0,0 +1,356 @@ +const { assert } = require('chai'); +const sinon = require('sinon'); + +const Contact = require('../../../js/modules/types/contact'); +const { + stringToArrayBuffer, +} = require('../../../js/modules/string_to_array_buffer'); + +describe('Contact', () => { + describe('parseAndWriteContactAvatar', () => { + const NUMBER = '+12025550099'; + + it('handles message with no avatar in contact', async () => { + const upgradeAttachment = sinon + .stub() + .throws(new Error("Shouldn't be called")); + const upgradeVersion = Contact.parseAndWriteContactAvatar( + upgradeAttachment + ); + + const message = { + body: 'hey there!', + contact: [ + { + name: { + displayName: 'Someone Somewhere', + }, + number: [ + { + type: 1, + value: NUMBER, + }, + ], + }, + ], + }; + const result = await upgradeVersion(message.contact[0], { message }); + assert.deepEqual(result, message.contact[0]); + }); + + it('removes contact avatar if it has no sub-avatar', async () => { + const upgradeAttachment = sinon + .stub() + .throws(new Error("Shouldn't be called")); + const upgradeVersion = Contact.parseAndWriteContactAvatar( + upgradeAttachment + ); + + const message = { + body: 'hey there!', + contact: [ + { + name: { + displayName: 'Someone Somewhere', + }, + number: [ + { + type: 1, + value: NUMBER, + }, + ], + avatar: { + isProfile: true, + }, + }, + ], + }; + const expected = { + name: { + displayName: 'Someone Somewhere', + }, + number: [ + { + type: 1, + value: NUMBER, + }, + ], + }; + const result = await upgradeVersion(message.contact[0], { message }); + assert.deepEqual(result, expected); + }); + + it('writes avatar to disk', async () => { + const upgradeAttachment = async () => { + return { + path: 'abc/abcdefg', + }; + }; + const upgradeVersion = Contact.parseAndWriteContactAvatar( + upgradeAttachment + ); + + const message = { + body: 'hey there!', + contact: [ + { + name: { + displayName: 'Someone Somewhere', + }, + number: [ + { + type: 1, + value: NUMBER, + }, + ], + email: [ + { + type: 2, + value: 'someone@somewhere.com', + }, + ], + address: [ + { + type: 1, + street: '5 Somewhere Ave.', + }, + ], + avatar: { + otherKey: 'otherValue', + avatar: { + contentType: 'image/png', + data: stringToArrayBuffer('It’s easy if you try'), + }, + }, + }, + ], + }; + const expected = { + name: { + displayName: 'Someone Somewhere', + }, + number: [ + { + type: 1, + value: NUMBER, + }, + ], + email: [ + { + type: 2, + value: 'someone@somewhere.com', + }, + ], + address: [ + { + type: 1, + street: '5 Somewhere Ave.', + }, + ], + avatar: { + otherKey: 'otherValue', + isProfile: false, + avatar: { + path: 'abc/abcdefg', + }, + }, + }; + + const result = await upgradeVersion(message.contact[0], { message }); + assert.deepEqual(result, expected); + }); + + it('removes number element if it ends up with no value', async () => { + const upgradeAttachment = sinon + .stub() + .throws(new Error("Shouldn't be called")); + const upgradeVersion = Contact.parseAndWriteContactAvatar( + upgradeAttachment + ); + + const message = { + body: 'hey there!', + contact: [ + { + name: { + displayName: 'Someone Somewhere', + }, + number: [ + { + type: 1, + }, + ], + email: [ + { + value: 'someone@somewhere.com', + }, + ], + }, + ], + }; + const expected = { + name: { + displayName: 'Someone Somewhere', + }, + email: [ + { + type: 1, + value: 'someone@somewhere.com', + }, + ], + }; + const result = await upgradeVersion(message.contact[0], { message }); + assert.deepEqual(result, expected); + }); + + it('drops address if it has no real values', async () => { + const upgradeAttachment = sinon + .stub() + .throws(new Error("Shouldn't be called")); + const upgradeVersion = Contact.parseAndWriteContactAvatar( + upgradeAttachment + ); + + const message = { + body: 'hey there!', + contact: [ + { + name: { + displayName: 'Someone Somewhere', + }, + number: [ + { + value: NUMBER, + }, + ], + address: [ + { + type: 1, + }, + ], + }, + ], + }; + const expected = { + name: { + displayName: 'Someone Somewhere', + }, + number: [ + { + value: NUMBER, + type: 1, + }, + ], + }; + const result = await upgradeVersion(message.contact[0], { message }); + assert.deepEqual(result, expected); + }); + + it('logs if contact has no name.displayName or organization', async () => { + const upgradeAttachment = sinon + .stub() + .throws(new Error("Shouldn't be called")); + const upgradeVersion = Contact.parseAndWriteContactAvatar( + upgradeAttachment + ); + + const message = { + body: 'hey there!', + source: NUMBER, + sourceDevice: '1', + sent_at: 1232132, + contact: [ + { + name: { + name: 'Someone', + }, + number: [ + { + type: 1, + value: NUMBER, + }, + ], + }, + ], + }; + const expected = { + name: { + name: 'Someone', + }, + number: [ + { + type: 1, + value: NUMBER, + }, + ], + }; + const result = await upgradeVersion(message.contact[0], { message }); + assert.deepEqual(result, expected); + }); + + it('removes invalid elements then logs if no values remain in contact', async () => { + const upgradeAttachment = sinon + .stub() + .throws(new Error("Shouldn't be called")); + const upgradeVersion = Contact.parseAndWriteContactAvatar( + upgradeAttachment + ); + + const message = { + body: 'hey there!', + source: NUMBER, + sourceDevice: '1', + sent_at: 1232132, + contact: [ + { + name: { + displayName: 'Someone Somewhere', + }, + number: [ + { + type: 1, + }, + ], + email: [ + { + type: 1, + }, + ], + }, + ], + }; + const expected = { + name: { + displayName: 'Someone Somewhere', + }, + }; + const result = await upgradeVersion(message.contact[0], { message }); + assert.deepEqual(result, expected); + }); + + it('handles a contact with just organization', async () => { + const upgradeAttachment = sinon + .stub() + .throws(new Error("Shouldn't be called")); + const upgradeVersion = Contact.parseAndWriteContactAvatar( + upgradeAttachment + ); + + const message = { + contact: [ + { + organization: 'Somewhere Consulting', + number: [ + { + type: 1, + value: NUMBER, + }, + ], + }, + ], + }; + const result = await upgradeVersion(message.contact[0], { message }); + assert.deepEqual(result, message.contact[0]); + }); + }); +}); diff --git a/test/modules/types/message_test.js b/test/modules/types/message_test.js index 9c507c3cd..2343bd803 100644 --- a/test/modules/types/message_test.js +++ b/test/modules/types/message_test.js @@ -630,351 +630,4 @@ describe('Message', () => { assert.deepEqual(result, expected); }); }); - - describe('_cleanAndWriteContactAvatar', () => { - const NUMBER = '+12025550099'; - - it('handles message with no avatar in contact', async () => { - const upgradeAttachment = sinon - .stub() - .throws(new Error("Shouldn't be called")); - const upgradeVersion = Message._cleanAndWriteContactAvatar( - upgradeAttachment - ); - - const message = { - body: 'hey there!', - contact: [ - { - name: { - displayName: 'Someone Somewhere', - }, - number: [ - { - type: 1, - value: NUMBER, - }, - ], - }, - ], - }; - const result = await upgradeVersion(message.contact[0], { message }); - assert.deepEqual(result, message.contact[0]); - }); - - it('removes contact avatar if it has no sub-avatar', async () => { - const upgradeAttachment = sinon - .stub() - .throws(new Error("Shouldn't be called")); - const upgradeVersion = Message._cleanAndWriteContactAvatar( - upgradeAttachment - ); - - const message = { - body: 'hey there!', - contact: [ - { - name: { - displayName: 'Someone Somewhere', - }, - number: [ - { - type: 1, - value: NUMBER, - }, - ], - avatar: { - isProfile: true, - }, - }, - ], - }; - const expected = { - name: { - displayName: 'Someone Somewhere', - }, - number: [ - { - type: 1, - value: NUMBER, - }, - ], - }; - const result = await upgradeVersion(message.contact[0], { message }); - assert.deepEqual(result, expected); - }); - - it('writes avatar to disk', async () => { - const upgradeAttachment = async () => { - return { - path: 'abc/abcdefg', - }; - }; - const upgradeVersion = Message._cleanAndWriteContactAvatar( - upgradeAttachment - ); - - const message = { - body: 'hey there!', - contact: [ - { - name: { - displayName: 'Someone Somewhere', - }, - number: [ - { - type: 1, - value: NUMBER, - }, - ], - email: [ - { - type: 2, - value: 'someone@somewhere.com', - }, - ], - address: [ - { - type: 1, - street: '5 Somewhere Ave.', - }, - ], - avatar: { - otherKey: 'otherValue', - avatar: { - contentType: 'image/png', - data: stringToArrayBuffer('It’s easy if you try'), - }, - }, - }, - ], - }; - const expected = { - name: { - displayName: 'Someone Somewhere', - }, - number: [ - { - type: 1, - value: NUMBER, - }, - ], - email: [ - { - type: 2, - value: 'someone@somewhere.com', - }, - ], - address: [ - { - type: 1, - street: '5 Somewhere Ave.', - }, - ], - avatar: { - otherKey: 'otherValue', - isProfile: false, - avatar: { - path: 'abc/abcdefg', - }, - }, - }; - - const result = await upgradeVersion(message.contact[0], { message }); - assert.deepEqual(result, expected); - }); - - it('removes number element if it ends up with no value', async () => { - const upgradeAttachment = sinon - .stub() - .throws(new Error("Shouldn't be called")); - const upgradeVersion = Message._cleanAndWriteContactAvatar( - upgradeAttachment - ); - - const message = { - body: 'hey there!', - contact: [ - { - name: { - displayName: 'Someone Somewhere', - }, - number: [ - { - type: 1, - }, - ], - email: [ - { - value: 'someone@somewhere.com', - }, - ], - }, - ], - }; - const expected = { - name: { - displayName: 'Someone Somewhere', - }, - email: [ - { - type: 1, - value: 'someone@somewhere.com', - }, - ], - }; - const result = await upgradeVersion(message.contact[0], { message }); - assert.deepEqual(result, expected); - }); - - it('drops address if it has no real values', async () => { - const upgradeAttachment = sinon - .stub() - .throws(new Error("Shouldn't be called")); - const upgradeVersion = Message._cleanAndWriteContactAvatar( - upgradeAttachment - ); - - const message = { - body: 'hey there!', - contact: [ - { - name: { - displayName: 'Someone Somewhere', - }, - number: [ - { - value: NUMBER, - }, - ], - address: [ - { - type: 1, - }, - ], - }, - ], - }; - const expected = { - name: { - displayName: 'Someone Somewhere', - }, - number: [ - { - value: NUMBER, - type: 1, - }, - ], - }; - const result = await upgradeVersion(message.contact[0], { message }); - assert.deepEqual(result, expected); - }); - - it('logs if contact has no name.displayName or organization', async () => { - const upgradeAttachment = sinon - .stub() - .throws(new Error("Shouldn't be called")); - const upgradeVersion = Message._cleanAndWriteContactAvatar( - upgradeAttachment - ); - - const message = { - body: 'hey there!', - source: NUMBER, - sourceDevice: '1', - sent_at: 1232132, - contact: [ - { - name: { - name: 'Someone', - }, - number: [ - { - type: 1, - value: NUMBER, - }, - ], - }, - ], - }; - const expected = { - name: { - name: 'Someone', - }, - number: [ - { - type: 1, - value: NUMBER, - }, - ], - }; - const result = await upgradeVersion(message.contact[0], { message }); - assert.deepEqual(result, expected); - }); - - it('removes invalid elements then logs if no values remain in contact', async () => { - const upgradeAttachment = sinon - .stub() - .throws(new Error("Shouldn't be called")); - const upgradeVersion = Message._cleanAndWriteContactAvatar( - upgradeAttachment - ); - - const message = { - body: 'hey there!', - source: NUMBER, - sourceDevice: '1', - sent_at: 1232132, - contact: [ - { - name: { - displayName: 'Someone Somewhere', - }, - number: [ - { - type: 1, - }, - ], - email: [ - { - type: 1, - }, - ], - }, - ], - }; - const expected = { - name: { - displayName: 'Someone Somewhere', - }, - }; - const result = await upgradeVersion(message.contact[0], { message }); - assert.deepEqual(result, expected); - }); - - it('handles a contact with just organization', async () => { - const upgradeAttachment = sinon - .stub() - .throws(new Error("Shouldn't be called")); - const upgradeVersion = Message._cleanAndWriteContactAvatar( - upgradeAttachment - ); - - const message = { - contact: [ - { - organization: 'Somewhere Consulting', - number: [ - { - type: 1, - value: NUMBER, - }, - ], - }, - ], - }; - const result = await upgradeVersion(message.contact[0], { message }); - assert.deepEqual(result, message.contact[0]); - }); - }); });