2016-02-26 04:48:05 +08:00
|
|
|
import {ComposerExtension, QuotedHTMLTransformer} from 'nylas-exports';
|
2016-02-24 14:55:47 +08:00
|
|
|
import {PLUGIN_ID, PLUGIN_URL} from './open-tracking-constants';
|
2016-02-20 04:30:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
class DraftBody {
|
|
|
|
constructor(draft) {this._body = draft.body}
|
|
|
|
get unquoted() {return QuotedHTMLTransformer.removeQuotedHTML(this._body);}
|
|
|
|
set unquoted(text) {this._body = QuotedHTMLTransformer.appendQuotedHTML(text, this._body);}
|
|
|
|
get body() {return this._body}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class OpenTrackingComposerExtension extends ComposerExtension {
|
|
|
|
|
2016-03-17 10:27:12 +08:00
|
|
|
static applyTransformsToDraft({draft}) {
|
2016-02-20 04:30:24 +08:00
|
|
|
// grab message metadata, if any
|
2016-03-17 10:27:12 +08:00
|
|
|
const nextDraft = draft.clone();
|
2016-02-20 04:30:24 +08:00
|
|
|
const metadata = draft.metadataForPluginId(PLUGIN_ID);
|
2016-03-08 12:54:43 +08:00
|
|
|
if (!metadata) {
|
2016-03-17 10:27:12 +08:00
|
|
|
return nextDraft;
|
2016-03-08 12:54:43 +08:00
|
|
|
}
|
2016-02-20 04:30:24 +08:00
|
|
|
|
2016-03-08 12:54:43 +08:00
|
|
|
if (!metadata.uid) {
|
|
|
|
NylasEnv.reportError(new Error("Open tracking composer extension could not find 'uid' in metadata!"));
|
2016-03-17 10:27:12 +08:00
|
|
|
return nextDraft;
|
2016-02-20 04:30:24 +08:00
|
|
|
}
|
2016-03-08 12:54:43 +08:00
|
|
|
|
|
|
|
// insert a tracking pixel <img> into the message
|
|
|
|
const serverUrl = `${PLUGIN_URL}/open/${draft.accountId}/${metadata.uid}`;
|
2016-03-17 10:27:12 +08:00
|
|
|
const img = `<img class="n1-open" width="0" height="0" style="border:0; width:0; height:0;" src="${serverUrl}">`;
|
2016-03-08 12:54:43 +08:00
|
|
|
const draftBody = new DraftBody(draft);
|
|
|
|
|
2016-03-17 10:27:12 +08:00
|
|
|
draftBody.unquoted = `${draftBody.unquoted}${img}`;
|
|
|
|
nextDraft.body = draftBody.body;
|
|
|
|
return nextDraft;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unapplyTransformsToDraft({draft}) {
|
|
|
|
const nextDraft = draft.clone();
|
|
|
|
nextDraft.body = draft.body.replace(/<img class="n1-open"[^>]*>/g, '');
|
|
|
|
return nextDraft;
|
2016-02-20 04:30:24 +08:00
|
|
|
}
|
|
|
|
}
|