2016-02-24 14:55:47 +08:00
|
|
|
import request from 'request';
|
2016-02-20 04:30:24 +08:00
|
|
|
import {ComponentRegistry, DatabaseStore, Message, ExtensionRegistry, Actions} from 'nylas-exports';
|
|
|
|
import LinkTrackingButton from './link-tracking-button';
|
2016-02-24 14:55:47 +08:00
|
|
|
// TODO what's up with these components?
|
2016-02-24 10:51:05 +08:00
|
|
|
// import LinkTrackingIcon from './link-tracking-icon';
|
2016-02-24 14:55:47 +08:00
|
|
|
// import LinkTrackingPanel from './link-tracking-panel';
|
2016-02-20 04:30:24 +08:00
|
|
|
import LinkTrackingComposerExtension from './link-tracking-composer-extension';
|
2016-02-24 10:20:26 +08:00
|
|
|
import LinkTrackingMessageExtension from './link-tracking-message-extension';
|
2016-02-24 14:55:47 +08:00
|
|
|
import {PLUGIN_ID, PLUGIN_URL} from './link-tracking-constants';
|
2016-02-20 04:30:24 +08:00
|
|
|
|
|
|
|
const post = Promise.promisify(request.post, {multiArgs: true});
|
2016-02-24 14:55:47 +08:00
|
|
|
|
2016-02-20 04:30:24 +08:00
|
|
|
|
|
|
|
function afterDraftSend({draftClientId}) {
|
|
|
|
// only run this handler in the main window
|
|
|
|
if (!NylasEnv.isMainWindow()) return;
|
|
|
|
|
|
|
|
// query for the message
|
|
|
|
DatabaseStore.findBy(Message, {clientId: draftClientId}).then((message) => {
|
|
|
|
// grab message metadata, if any
|
|
|
|
const metadata = message.metadataForPluginId(PLUGIN_ID);
|
|
|
|
// get the uid from the metadata, if present
|
|
|
|
if (metadata) {
|
|
|
|
const uid = metadata.uid;
|
|
|
|
|
|
|
|
// post the uid and message id pair to the plugin server
|
|
|
|
const data = {uid: uid, message_id: message.id};
|
2016-02-24 11:19:43 +08:00
|
|
|
const serverUrl = `${PLUGIN_URL}/register-message`;
|
2016-02-20 04:30:24 +08:00
|
|
|
return post({
|
|
|
|
url: serverUrl,
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
}).then( ([response, responseBody]) => {
|
|
|
|
if (response.statusCode !== 200) {
|
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
return responseBody;
|
|
|
|
}).catch(error => {
|
|
|
|
NylasEnv.showErrorDialog("There was a problem contacting the Link Tracking server! This message will not have link tracking");
|
|
|
|
Promise.reject(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function activate() {
|
|
|
|
ComponentRegistry.register(LinkTrackingButton, {role: 'Composer:ActionButton'});
|
2016-02-24 10:51:05 +08:00
|
|
|
// ComponentRegistry.register(LinkTrackingIcon, {role: 'ThreadListIcon'});
|
2016-02-24 10:20:26 +08:00
|
|
|
// ComponentRegistry.register(LinkTrackingPanel, {role: 'message:BodyHeader'});
|
2016-02-20 04:30:24 +08:00
|
|
|
ExtensionRegistry.Composer.register(LinkTrackingComposerExtension);
|
2016-02-24 10:20:26 +08:00
|
|
|
ExtensionRegistry.MessageView.register(LinkTrackingMessageExtension);
|
2016-02-20 04:30:24 +08:00
|
|
|
this._unlistenSendDraftSuccess = Actions.sendDraftSuccess.listen(afterDraftSend);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function serialize() {}
|
|
|
|
|
|
|
|
export function deactivate() {
|
|
|
|
ComponentRegistry.unregister(LinkTrackingButton);
|
2016-02-24 10:51:05 +08:00
|
|
|
// ComponentRegistry.unregister(LinkTrackingIcon);
|
2016-02-24 10:20:26 +08:00
|
|
|
// ComponentRegistry.unregister(LinkTrackingPanel);
|
2016-02-20 04:30:24 +08:00
|
|
|
ExtensionRegistry.Composer.unregister(LinkTrackingComposerExtension);
|
2016-02-24 10:20:26 +08:00
|
|
|
ExtensionRegistry.MessageView.unregister(LinkTrackingMessageExtension);
|
2016-02-20 04:30:24 +08:00
|
|
|
this._unlistenSendDraftSuccess()
|
|
|
|
}
|