diff --git a/packages/local-sync/spec/message-factory-spec.js b/packages/local-sync/spec/message-factory-spec.js index c5776242c..36d85f47c 100644 --- a/packages/local-sync/spec/message-factory-spec.js +++ b/packages/local-sync/spec/message-factory-spec.js @@ -1,5 +1,5 @@ const LocalDatabaseConnector = require('../src/shared/local-database-connector'); -const {parseFromImap, extractSnippet} = require('../src/shared/message-factory'); +const {parseFromImap, extractSnippet, extractContacts} = require('../src/shared/message-factory'); const {forEachJSONFixture, forEachHTMLAndTXTFixture, ACCOUNT_ID} = require('./helpers'); xdescribe('MessageFactory', function MessageFactorySpecs() { @@ -85,6 +85,21 @@ const snippetTestCases = [{ }, ] +const contactsTestCases = [{ + purpose: "not erroneously split contact names on commas", + // NOTE: inputs must be in same format as output by mimelib.parseHeader + input: ['"Little Bo Peep, The Hill" '], + output: [{name: "Little Bo Peep, The Hill", email: "bopeep@example.com"}], +}, { + purpose: "extract two separate contacts, removing quotes properly & respecing unicode", + input: ['AppleBees Zé , "Tiger Zen" b@example.com'], + output: [ + {name: 'AppleBees Zé', email: 'a@example.com'}, + {name: 'Tiger Zen', email: 'b@example.com'}, + ], +}, +] + describe('MessageFactoryHelpers', function MessageFactoryHelperSpecs() { describe('extractSnippet (basic)', () => { snippetTestCases.forEach(({purpose, plainBody, htmlBody, snippet}) => { @@ -102,4 +117,12 @@ describe('MessageFactoryHelpers', function MessageFactoryHelperSpecs() { }); }); }); + describe('extractContacts (basic)', () => { + contactsTestCases.forEach(({purpose, input, output}) => { + it(`should ${purpose}`, () => { + const parsedContacts = extractContacts(input); + expect(parsedContacts).toEqual(output); + }); + }); + }); }); diff --git a/packages/local-sync/src/shared/message-factory.js b/packages/local-sync/src/shared/message-factory.js index fad9553e6..a407592fc 100644 --- a/packages/local-sync/src/shared/message-factory.js +++ b/packages/local-sync/src/shared/message-factory.js @@ -23,14 +23,20 @@ function extractContacts(input) { if (!input || input.length === 0 || !input[0]) { return []; } - const s = `["${input[0].replace(/"/g, '\\"').replace(/, /g, '", "')}"]`; - const values = JSON.parse(s); + const values = mimelib.parseAddresses(input[0]); + if (!values || values.length === 0 || !input[0]) { + return []; + } return values.map(v => { - const parsed = mimelib.parseAddresses(v) - if (!parsed || parsed.length === 0) { + if (!v || v.length === 0) { return null } - const {name, address: email} = parsed.pop() + const {name, address: email} = v; + // contacts without an email address are worthless, especially when + // extracted from emails + if (!email) { + return null; + } return {name, email} }) .filter(c => c != null) @@ -319,6 +325,7 @@ module.exports = { buildForSend, parseFromImap, extractSnippet, + extractContacts, stripTrackingLinksFromBody, buildTrackingBodyForRecipient, replaceMessageIdInBodyTrackingLinks,