/**
* Licensed Materials - Property of IBM
* IBM Cognos Products: Collaboration
* (C) Copyright IBM Corp. 2019, 2020
*
* US Government Users Restricted Rights - Use, duplication or disclosure
* restricted by GSA ADP Schedule Contract with IBM Corp.
*/
define([
'../ConnectorBase',
'./EmailClient',
'../../../nls/StringResources'
], function (ConnectorBase, EmailClient, StringMessages) {
'use strict';
var EmailConnector = ConnectorBase.extend( /** @lends EmailConnector */ {
/**
* @desc Constructor for EmailConnector.
* @constructs EmailConnector
* @extends ConnectorBase
* @public
* @param options {Object} Options
* @param options.glassContext {Object} The glass context
*/
init: function (options) {
// base class is keeping for us the glassContext and logger
EmailConnector.inherited('init', this, arguments);
this.client = new EmailClient(options.glassContext);
},
/**
* Configure this connector with some meta info.
* @instance
* @param {object} meta meta data specific to this connector type.
*/
configure: function (meta) {
EmailConnector.inherited('configure', this, arguments);
if (!(meta.features &&
meta.features.messaging && meta.features.messaging.endpoint)) {
throw new Error(StringMessages.get('error_invalid_configuration'));
}
},
/**
* Send an email message.
* @instance
* @param {object} options Options
* @param {string} options.channel Channel name
* @param {string} options.message Message text
* @param {string} options.url URL to CA asset
* @returns {promise}
*/
send: function (data) {
// some validations...
if ((typeof data.recipients !== 'object') || (data.recipients.to === undefined && data.recipients.toAddress === undefined)) {
return Promise
.reject(new Error(StringMessages.get('error_missing_recipient')))
.catch(this.errorHandler);
} else if (!data.message && !data.url) {
return Promise
.reject(new Error(StringMessages.get('error_missing_message')))
.catch(this.errorHandler);
}
const date = new Date();
/* We're using the ISO international standard for the date (YYYY-MM-DD) so that the date format will be standardized across all locales. */
const formattedDate = date.toISOString().split('T')[0];
return this.client.sendMessage({
url: this.meta.features.messaging.endpoint,
imageData: data['dataUrl'],
payload: {
recipients: data.recipients,
subject: data.subject,
message: data.message,
// Optional asset image
asset_image: data['dataUrl'] ? {
filename: `Screenshot-${formattedDate}.png`,
description: StringMessages.get(data.assetTitle && 'ca_image' || 'ca_image_short', data)
} : null,
// Optional asset link
asset_link: data.url ? {
// Per UI design, email link should not include asset title. This is different from Slack.
title: StringMessages.get('ca_link_short', data),
url: data.url
} : null,
assetType: (data.assetType).charAt(0).toLocaleUpperCase(data.language) + data.assetType.slice(1),
assetTitle: data.assetTitle,
assetSubTitleObj: data.assetSubTitleObj,
attachments: data.attachments
}
}).catch(this.errorHandler);
},
/**
* Calls feature 'me' to check if the email provider is ready for use.
* @override
*/
check: function () {
return this.loadFeatures()
.then(function () {
return this.client
.doGet({
url: this.meta.features.me.endpoint
})
.then(function (response) {
this.me = response.data || {};
}.bind(this))
.catch(this.errorHandler);
}.bind(this));
},
/**
* Check if the user has two required capabilities to use email (see Jira COLLAB-63).
* @override
*/
checkUserCapabilities: function() {
return this.glassContext.hasCapability('canEmail') && this.glassContext.hasCapability('canShareUsingEmail');
}
});
return EmailConnector;
});