Mailspring/internal_packages/link-tracking/lib/link-tracking-composer-extension.es6
Ben Gotow 552b66fbaf fix(syncback): Bidirectional transforms, ready-to-send saved state
Summary:
This diff replaces "finalizeSessionBeforeSending" with a
plugin hook that is bidirectional and allows us to put the draft in
the "ready to send" state every time we save it, and restore it to
the "ready to edit" state every time a draft session is created to
edit it.

This diff also significantly restructures the draft tasks:

1. SyncbackDraftUploadsTask:
   - ensures that `uploads` are converted to `files` and that any
     existing files on the draft are part of the correct account.

1. SyncbackDraftTask:
   - saves the draft, nothing else.

3. SendDraftTask
   - sends the draft, nothing else.
   - deletes the entire uploads directory for the draft

Test Plan: WIP

Reviewers: juan, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D2753
2016-03-16 19:27:12 -07:00

73 lines
2.4 KiB
JavaScript

import uuid from 'node-uuid';
import {
ComposerExtension,
Actions,
QuotedHTMLTransformer,
RegExpUtils,
} from 'nylas-exports';
import {PLUGIN_ID, PLUGIN_URL} from './link-tracking-constants'
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 LinkTrackingComposerExtension extends ComposerExtension {
static applyTransformsToDraft({draft}) {
// grab message metadata, if any
const nextDraft = draft.clone();
const metadata = nextDraft.metadataForPluginId(PLUGIN_ID);
if (metadata) {
const draftBody = new DraftBody(draft);
const links = [];
const messageUid = uuid.v4().replace(/-/g, "");
// loop through all <a href> elements, replace with redirect links and save
// mappings. The links component of the path is an index of the link array.
draftBody.unquoted = draftBody.unquoted.replace(
RegExpUtils.urlLinkTagRegex(),
(match, prefix, url, suffix, content, closingTag) => {
const encoded = encodeURIComponent(url);
const redirectUrl = `${PLUGIN_URL}/link/${draft.accountId}/${messageUid}/${links.length}?redirect=${encoded}`;
links.push({
url,
click_count: 0,
click_data: [],
redirect_url: redirectUrl,
});
return prefix + redirectUrl + suffix + content + closingTag;
}
);
// save the draft
nextDraft.body = draftBody.body;
// save the link info to draft metadata
metadata.uid = messageUid;
metadata.links = links;
Actions.setMetadata(draft, PLUGIN_ID, metadata);
}
return nextDraft;
}
static unapplyTransformsToDraft({draft}) {
const nextDraft = draft.clone();
const draftBody = new DraftBody(draft);
draftBody.unquoted = draftBody.unquoted.replace(
RegExpUtils.urlLinkTagRegex(),
(match, prefix, url, suffix, content, closingTag) => {
if (url.indexOf(PLUGIN_URL) !== -1) {
const userURLEncoded = url.split('?redirect=')[1];
return prefix + decodeURIComponent(userURLEncoded) + suffix + content + closingTag;
}
return prefix + url + suffix + content + closingTag;
}
)
nextDraft.body = draftBody.body;
return nextDraft;
}
}