2016-02-24 14:55:47 +08:00
|
|
|
import request from 'request';
|
2016-02-26 04:48:05 +08:00
|
|
|
import {ComponentRegistry, ExtensionRegistry, Actions} from 'nylas-exports';
|
2016-02-20 04:30:24 +08:00
|
|
|
import OpenTrackingButton from './open-tracking-button';
|
|
|
|
import OpenTrackingIcon from './open-tracking-icon';
|
2016-02-24 07:22:55 +08:00
|
|
|
import OpenTrackingMessageStatus from './open-tracking-message-status';
|
2016-02-20 04:30:24 +08:00
|
|
|
import OpenTrackingComposerExtension from './open-tracking-composer-extension';
|
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
|
|
|
|
|
|
|
const post = Promise.promisify(request.post, {multiArgs: true});
|
2016-02-24 14:55:47 +08:00
|
|
|
|
2016-02-20 04:30:24 +08:00
|
|
|
|
2016-02-26 04:48:05 +08:00
|
|
|
function afterDraftSend({message}) {
|
2016-02-20 04:30:24 +08:00
|
|
|
// only run this handler in the main window
|
|
|
|
if (!NylasEnv.isMainWindow()) return;
|
|
|
|
|
2016-02-26 04:48:05 +08:00
|
|
|
// 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, thread_id: 1};
|
|
|
|
const serverUrl = `${PLUGIN_URL}/plugins/register-message`;
|
|
|
|
|
|
|
|
post({
|
|
|
|
url: serverUrl,
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
}).then(([response, responseBody]) => {
|
|
|
|
if (response.statusCode !== 200) {
|
2016-02-26 05:32:51 +08:00
|
|
|
throw new Error(`Server error ${response.statusCode} at ${serverUrl}: ${responseBody}`);
|
2016-02-26 04:48:05 +08:00
|
|
|
}
|
|
|
|
}).catch(error => {
|
2016-02-26 05:32:51 +08:00
|
|
|
NylasEnv.showErrorDialog(`There was a problem saving your read receipt settings. This message will not have a read receipt. ${error.message}`);
|
2016-02-26 04:48:05 +08:00
|
|
|
Promise.reject(error);
|
|
|
|
});
|
|
|
|
}
|
2016-02-20 04:30:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function activate() {
|
|
|
|
ComponentRegistry.register(OpenTrackingButton, {role: 'Composer:ActionButton'});
|
|
|
|
ComponentRegistry.register(OpenTrackingIcon, {role: 'ThreadListIcon'});
|
2016-02-24 07:22:55 +08:00
|
|
|
ComponentRegistry.register(OpenTrackingMessageStatus, {role: 'MessageHeaderStatus'});
|
2016-02-20 04:30:24 +08:00
|
|
|
ExtensionRegistry.Composer.register(OpenTrackingComposerExtension);
|
|
|
|
this._unlistenSendDraftSuccess = Actions.sendDraftSuccess.listen(afterDraftSend);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function serialize() {}
|
|
|
|
|
|
|
|
export function deactivate() {
|
|
|
|
ComponentRegistry.unregister(OpenTrackingButton);
|
|
|
|
ComponentRegistry.unregister(OpenTrackingIcon);
|
2016-02-24 17:04:57 +08:00
|
|
|
ComponentRegistry.unregister(OpenTrackingMessageStatus);
|
2016-02-20 04:30:24 +08:00
|
|
|
ExtensionRegistry.Composer.unregister(OpenTrackingComposerExtension);
|
|
|
|
this._unlistenSendDraftSuccess()
|
|
|
|
}
|