fix(read-receipts): Always remove own read receipt images

This commit is contained in:
Ben Gotow 2016-05-04 13:44:31 -07:00
parent 0e10ccbde9
commit 7657598f40
6 changed files with 130 additions and 77 deletions

View file

@ -103,24 +103,17 @@ const TrackingBlacklist = [{
name: 'Salesloft',
pattern: 'sdr.salesloft.com/email_trackers',
homepage: 'http://salesloft.com',
}, {
name: 'Nylas',
pattern: 'nylas.com/open',
homepage: 'http://nylas.com/N1',
}]
export function removeTrackingImagesFromBody(body) {
export function rejectImagesInBody(body, callback) {
const spliceRegions = [];
const regex = RegExpUtils.imageTagRegex();
// Identify img tags that should be cut
let result = null;
while ((result = regex.exec(body)) !== null) {
for (const item of TrackingBlacklist) {
if (result[1].indexOf(item.pattern) > 0) {
spliceRegions.push({start: result.index, end: result.index + result[0].length})
continue;
}
if (callback(result[1])) {
spliceRegions.push({start: result.index, end: result.index + result[0].length})
}
}
// Remove them all, from the end of the string to the start
@ -132,17 +125,39 @@ export function removeTrackingImagesFromBody(body) {
return updated;
}
export function removeTrackingPixels(message) {
const isFromMe = message.isFromMe();
message.body = rejectImagesInBody(message.body, (imageURL) => {
if (isFromMe) {
// If the image is sent by the user, remove all forms of tracking pixels.
// They could be viewing an email they sent with Salesloft, etc.
for (const item of TrackingBlacklist) {
if (imageURL.indexOf(item.pattern) >= 0) {
return true;
}
}
}
// Remove Nylas read receipt pixels for the current account. If this is a
// reply, our read receipt could still be in the body and could trigger
// additional opens. (isFromMe is not sufficient!)
if (imageURL.indexOf(`nylas.com/open/${message.accountId}`) >= 0) {
return true;
}
return false;
});
}
class TrackingPixelsMessageExtension extends MessageViewExtension {
static formatMessageBody = ({message}) => {
if (message.isFromMe()) {
message.body = removeTrackingImagesFromBody(message.body);
}
removeTrackingPixels(message);
}
}
class TrackingPixelsComposerExtension extends ComposerExtension {
static prepareNewDraft = ({draft}) => {
draft.body = removeTrackingImagesFromBody(draft.body);
removeTrackingPixels(draft);
}
}

View file

@ -0,0 +1,28 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><p>Hey Ben,</p><p>
I've noticed that we don't yet have an SLA in place with&nbsp;Nylas. Are you the right
person to be speaking with to make sure everything is set up on that end? If not,
could you please put me in touch with them, so that we can get you guys set up
correctly as soon as possible?</p><p>Thanks!</p><p>Gleb Polyakov</p><p>Head of
Business Development and Growth</p>After Pixel
<br><br><signature>Sent from <a href="https://nylas.com/n1?ref=n1">Nylas N1</a>, the extensible, open source mail client.<br/></signature><div class="gmail_quote">
On Apr 28 2016, at 2:14 pm, Ben Gotow (Careless) &lt;careless@foundry376.com&gt; wrote:
<br>
<blockquote class="gmail_quote"
style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<body>nother mailA<br /><br />Sent from <a href="https://link.nylas.com/link/b5djvgcuhj6i3x8nm53d0vnjm/local-a84ad76e-006b/0?redirect=https%3A%2F%2Fnylas.com%2Fn1%3Fref%3Dn1">Nylas N1</a>, the extensible, open source mail client.<br /><img width="0" height="0" style="border:0; width:0; height:0;" src="https://link.nylas.com/open/b5djvgcuhj6i3x8nm53d0vnjm/local-a84ad76e-006b" /><div>
On Apr 28 2016, at 1:46 pm, Ben Gotow (Careless) &lt;careless@foundry376.com&gt; wrote:
<br />
<blockquote style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi Ben this is just a test.<br /><br />Sent from <a href="https://link.nylas.com/link/b5djvgcuhj6i3x8nm53d0vnjm/local-aa39d95b-b883/0?redirect=https%3A%2F%2Fnylas.com%2Fn1%3Fref%3Dn1">Nylas N1</a>, the extensible, open source mail client.<br /><img width="0" height="0" style="border:0; width:0; height:0;" src="https://link.nylas.com/open/b5djvgcuhj6i3x8nm53d0vnjm/local-aa39d95b-b883" /><div>
On Apr 26 2016, at 6:03 pm, Ben Gotow &lt;bengotow@gmail.com&gt; wrote:
<br />
<blockquote style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<p>To test this, send https://www.google.com/search?q=test@example.com to yourself from a client that allows plaintext or html editing.</p>
<p><br />Ben Gotow<br />-----------------------------------<br /><a href="http://www.foundry376.com/">http://www.foundry376.com/</a><br />bengotow@gmail.com<br />540-250-2334</p>
</blockquote>
</div>
</blockquote>
</div></body>
</blockquote>
</div>

View file

@ -0,0 +1,28 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><p>Hey Ben,</p><p>
I've noticed that we don't yet have an SLA in place with&nbsp;Nylas. Are you the right
person to be speaking with to make sure everything is set up on that end? If not,
could you please put me in touch with them, so that we can get you guys set up
correctly as soon as possible?</p><p>Thanks!</p><p>Gleb Polyakov</p><p>Head of
Business Development and Growth</p><img src="https://sdr.salesloft.com/email_trackers/8c8bea88-af43-4f66-bf78-a97ad73d7aec/open.gif" alt="" width="1" height="1">After Pixel
<br><br><signature>Sent from <a href="https://nylas.com/n1?ref=n1">Nylas N1</a>, the extensible, open source mail client.<br/></signature><div class="gmail_quote">
On Apr 28 2016, at 2:14 pm, Ben Gotow (Careless) &lt;careless@foundry376.com&gt; wrote:
<br>
<blockquote class="gmail_quote"
style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<body>nother mailA<br /><br />Sent from <a href="https://link.nylas.com/link/b5djvgcuhj6i3x8nm53d0vnjm/local-a84ad76e-006b/0?redirect=https%3A%2F%2Fnylas.com%2Fn1%3Fref%3Dn1">Nylas N1</a>, the extensible, open source mail client.<br /><img width="0" height="0" style="border:0; width:0; height:0;" src="https://link.nylas.com/open/b5djvgcuhj6i3x8nm53d0vnjm/local-a84ad76e-006b" /><div>
On Apr 28 2016, at 1:46 pm, Ben Gotow (Careless) &lt;careless@foundry376.com&gt; wrote:
<br />
<blockquote style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi Ben this is just a test.<br /><br />Sent from <a href="https://link.nylas.com/link/b5djvgcuhj6i3x8nm53d0vnjm/local-aa39d95b-b883/0?redirect=https%3A%2F%2Fnylas.com%2Fn1%3Fref%3Dn1">Nylas N1</a>, the extensible, open source mail client.<br /><img width="0" height="0" style="border:0; width:0; height:0;" src="https://link.nylas.com/open/b5djvgcuhj6i3x8nm53d0vnjm/local-aa39d95b-b883" /><div>
On Apr 26 2016, at 6:03 pm, Ben Gotow &lt;bengotow@gmail.com&gt; wrote:
<br />
<blockquote style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<p>To test this, send https://www.google.com/search?q=test@example.com to yourself from a client that allows plaintext or html editing.</p>
<p><br />Ben Gotow<br />-----------------------------------<br /><a href="http://www.foundry376.com/">http://www.foundry376.com/</a><br />bengotow@gmail.com<br />540-250-2334</p>
</blockquote>
</div>
</blockquote>
</div></body>
</blockquote>
</div>

View file

@ -0,0 +1,8 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><p>Hey Ben,</p><p>
This is the reply! This tracking pixel should not be removed.
<img src="https://nylas.com/open/abcd/zza1231231" />
<blockquote>
This is the email I sent!
</blockquote>
</div>

View file

@ -0,0 +1,8 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><p>Hey Ben,</p><p>
This is the reply! This tracking pixel should not be removed.
<img src="https://nylas.com/open/abcd/zza1231231" />
<blockquote>
This is the email I sent!
<img src="https://nylas.com/open/1234/12zxczxc123" />
</blockquote>
</div>

View file

@ -1,69 +1,35 @@
/* eslint no-irregular-whitespace: 0 */
import {removeTrackingImagesFromBody} from '../lib/main';
import {removeTrackingPixels} from '../lib/main';
import fs from 'fs';
const testBody = `
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><p>Hey Ben,</p><p>
I've noticed that we don't yet have an SLA in place with&nbsp;Nylas. Are you the right
person to be speaking with to make sure everything is set up on that end? If not,
could you please put me in touch with them, so that we can get you guys set up
correctly as soon as possible?</p><p>Thanks!</p><p>Gleb Polyakov</p><p>Head of
Business Development and Growth</p><img src="https://sdr.salesloft.com/email_trackers/8c8bea88-af43-4f66-bf78-a97ad73d7aec/open.gif" alt="" width="1" height="1">After Pixel
<br><br><signature>Sent from <a href="https://nylas.com/n1?ref=n1">Nylas N1</a>, the extensible, open source mail client.<br/></signature><div class="gmail_quote">
On Apr 28 2016, at 2:14 pm, Ben Gotow (Careless) &lt;careless@foundry376.com&gt; wrote:
<br>
<blockquote class="gmail_quote"
style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<body>nother mailA<br /><br />Sent from <a href="https://link.nylas.com/link/b5djvgcuhj6i3x8nm53d0vnjm/local-a84ad76e-006b/0?redirect=https%3A%2F%2Fnylas.com%2Fn1%3Fref%3Dn1">Nylas N1</a>, the extensible, open source mail client.<br /><img width="0" height="0" style="border:0; width:0; height:0;" src="https://link.nylas.com/open/b5djvgcuhj6i3x8nm53d0vnjm/local-a84ad76e-006b" /><div>
On Apr 28 2016, at 1:46 pm, Ben Gotow (Careless) &lt;careless@foundry376.com&gt; wrote:
<br />
<blockquote style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi Ben this is just a test.<br /><br />Sent from <a href="https://link.nylas.com/link/b5djvgcuhj6i3x8nm53d0vnjm/local-aa39d95b-b883/0?redirect=https%3A%2F%2Fnylas.com%2Fn1%3Fref%3Dn1">Nylas N1</a>, the extensible, open source mail client.<br /><img width="0" height="0" style="border:0; width:0; height:0;" src="https://link.nylas.com/open/b5djvgcuhj6i3x8nm53d0vnjm/local-aa39d95b-b883" /><div>
On Apr 26 2016, at 6:03 pm, Ben Gotow &lt;bengotow@gmail.com&gt; wrote:
<br />
<blockquote style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<p>To test this, send https://www.google.com/search?q=test@example.com to yourself from a client that allows plaintext or html editing.</p>
<p><br />Ben Gotow<br />-----------------------------------<br /><a href="http://www.foundry376.com/">http://www.foundry376.com/</a><br />bengotow@gmail.com<br />540-250-2334</p>
</blockquote>
</div>
</blockquote>
</div></body>
</blockquote>
</div>
`
const testBodyProcessed = `
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><p>Hey Ben,</p><p>
I've noticed that we don't yet have an SLA in place with&nbsp;Nylas. Are you the right
person to be speaking with to make sure everything is set up on that end? If not,
could you please put me in touch with them, so that we can get you guys set up
correctly as soon as possible?</p><p>Thanks!</p><p>Gleb Polyakov</p><p>Head of
Business Development and Growth</p>After Pixel
<br><br><signature>Sent from <a href="https://nylas.com/n1?ref=n1">Nylas N1</a>, the extensible, open source mail client.<br/></signature><div class="gmail_quote">
On Apr 28 2016, at 2:14 pm, Ben Gotow (Careless) &lt;careless@foundry376.com&gt; wrote:
<br>
<blockquote class="gmail_quote"
style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<body>nother mailA<br /><br />Sent from <a href="https://link.nylas.com/link/b5djvgcuhj6i3x8nm53d0vnjm/local-a84ad76e-006b/0?redirect=https%3A%2F%2Fnylas.com%2Fn1%3Fref%3Dn1">Nylas N1</a>, the extensible, open source mail client.<br /><div>
On Apr 28 2016, at 1:46 pm, Ben Gotow (Careless) &lt;careless@foundry376.com&gt; wrote:
<br />
<blockquote style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi Ben this is just a test.<br /><br />Sent from <a href="https://link.nylas.com/link/b5djvgcuhj6i3x8nm53d0vnjm/local-aa39d95b-b883/0?redirect=https%3A%2F%2Fnylas.com%2Fn1%3Fref%3Dn1">Nylas N1</a>, the extensible, open source mail client.<br /><div>
On Apr 26 2016, at 6:03 pm, Ben Gotow &lt;bengotow@gmail.com&gt; wrote:
<br />
<blockquote style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<p>To test this, send https://www.google.com/search?q=test@example.com to yourself from a client that allows plaintext or html editing.</p>
<p><br />Ben Gotow<br />-----------------------------------<br /><a href="http://www.foundry376.com/">http://www.foundry376.com/</a><br />bengotow@gmail.com<br />540-250-2334</p>
</blockquote>
</div>
</blockquote>
</div></body>
</blockquote>
</div>
`
const readFixture = (name) => {
return fs.readFileSync(`${__dirname}/fixtures/${name}`).toString().trim()
}
describe("TrackingPixelsExtension", () => {
it("should splice tracking pixels", () => {
expect(removeTrackingImagesFromBody(testBody)).toEqual(testBodyProcessed);
it("should splice all tracking pixels from emails I've sent", () => {
const before = readFixture('a-before.txt');
const expected = readFixture('a-after.txt');
const message = {
body: before,
accountId: '1234',
isFromMe: () => true,
}
removeTrackingPixels(message);
expect(message.body).toEqual(expected);
});
it("should always splice Nylas read receipts for the current account id ", () => {
const before = readFixture('b-before.txt');
const expected = readFixture('b-after.txt');
const message = {
body: before,
accountId: '1234',
isFromMe: () => false,
}
removeTrackingPixels(message);
expect(message.body).toEqual(expected);
});
});