[local-sync] Fix a couple of multi-send bugs

Summary:
1) Send the custom body, rather than the generic body
2) Extract contacts correctly so that the saved sent message has all the
   participants, rather than just the last one

Test Plan: Tested locally

Reviewers: jackie

Reviewed By: jackie

Differential Revision: https://phab.nylas.com/D3499
This commit is contained in:
Halla Moore 2016-12-12 13:33:34 -08:00
parent 655175d94f
commit 547ff416e7
2 changed files with 16 additions and 6 deletions

View file

@ -3,7 +3,7 @@
const fs = require('fs');
const nodemailer = require('nodemailer');
const mailcomposer = require('mailcomposer');
const {HTTPError} = require('./sending-utils');
const {HTTPError} = require('../shared/errors');
const MAX_RETRIES = 1;
@ -92,7 +92,7 @@ class SendmailClient {
return msgData;
}
_getBodyWithMessageIds(draft) {
_replaceBodyMessageIds(body, id) {
const serverUrl = {
local: 'http:\/\/lvh\.me:5100',
development: 'http:\/\/lvh\.me:5100',
@ -100,8 +100,8 @@ class SendmailClient {
production: 'https:\/\/n1cloud\.nylas\.com',
}[process.env];
const regex = new RegExp(`${serverUrl}\/.+MESSAGE_ID`, 'g')
return draft.body.replace(regex, (match) => {
return match.replace('MESSAGE_ID', draft.id)
return body.replace(regex, (match) => {
return match.replace('MESSAGE_ID', id)
})
}
@ -126,7 +126,7 @@ class SendmailClient {
async sendCustomBody(draft, body, recipients) {
const origBody = draft.body;
draft.body = this._getBodyWithMessageIds(draft);
draft.body = this._replaceBodyMessageIds(body);
const envelope = {};
for (const field of Object.keys(recipients)) {
envelope[field] = recipients[field].map(r => r.email);

View file

@ -11,7 +11,17 @@ const Errors = require('./errors');
const SNIPPET_SIZE = 100;
const SNIPPET_MAX_SIZE = 255;
function extractContacts(values = []) {
// The input is the value of a to/cc/bcc/from header as parsed by the imap
// library we're using, but it currently parses them in a weird format. If an
// email is sent to a@example.com and b@example.com, the parsed output of the
// 'to' header is ['a@example.com, b@example.com']. (Note both emails are in
// the same string.) When fixed, this function will need to update accordingly.
function extractContacts(input) {
if (!input || input.length === 0 || !input[0]) {
return [];
}
const s = `["${input[0].replace(/"/g, '\\"').replace(/, /g, '", "')}"]`;
const values = JSON.parse(s);
return values.map(v => {
const {name, address: email} = mimelib.parseAddresses(v).pop()
return {name, email}