Source: messaging/connectors/msteams/MSTeamsConnector.js

/**
 * Licensed Materials - Property of IBM
 * IBM Cognos Products: Collaboration
 * (C) Copyright IBM Corp. 2017, 2018
 *
 * US Government Users Restricted Rights - Use, duplication or disclosure
 * restricted by GSA ADP Schedule Contract with IBM Corp.
 */

define([
	'../ConnectorBase',
	'./MSTeamsClient',
	'../../../nls/StringResources'
], function (ConnectorBase, MSTeamsClient, StringMessages) {
	'use strict';

	var MSTeamsConnector = ConnectorBase.extend( /** @lends MSTeamsConnector */ {

		/**
		 * @desc Constructor for MSTeamsConnector.
		 * @constructs MSTeamsConnector
		 * @extends ConnectorBase
		 * @public
		 * @param options {Object} Options
		 * @param options.glassContext {Object} The glass context
		 */
		init: function (options) {
			MSTeamsConnector.inherited('init', this, arguments);
			this.client = new MSTeamsClient(options.glassContext);
		},

		/**
		 * Configure this connector with some meta info.
		 * @instance
		 * @param {object} meta meta data specific to this connector type.
		 */
		configure: function (meta) {
			MSTeamsConnector.inherited('configure', this, arguments);

			var isValid = meta.features &&
				meta.features.teams && meta.features.teams.endpoint &&
				meta.features.channels && meta.features.channels.endpoint &&
				meta.features.messaging && meta.features.messaging.endpoint;

			if (!isValid) {
				throw new Error(StringMessages.get('error_invalid_configuration'));
			}
		},

		/**
		 * Authenticate to MS Teams.
		 * @instance
		 * @param {object} options options are not used at the moment.
		 * @returns {promise}
		 */
		authenticate: function (options) {
			void(options);
			return this.client
				.signIn()
				.catch(this.errorHandler);
		},

		/**
		 * Send a message to MS Teams.
		 * @instance
		 * @param {object} options Options
		 * @param {string} options.teamId Team ID
		 * @param {string} options.channelId {string} Channel ID
		 * @param {string} options.message {string} Message text
		 * @param {string} options.url {string} URL to CA asset
		 * @returns {promise}
		 */
		send: function (data) {
			// some validations...
			if (!data.teamId) {
				return Promise
					.reject(new Error(StringMessages.get('error_missing_team_id')))
					.catch(this.errorHandler);
			} else if (!data.channelId) {
				return Promise
					.reject(new Error(StringMessages.get('error_missing_channel_id')))
					.catch(this.errorHandler);
			} else if (!data.message) {
				return Promise
					.reject(new Error(StringMessages.get('error_missing_message')))
					.catch(this.errorHandler);
			}

			return this.client
				.sendMessage({
					url: this.meta.features.messaging.endpoint,
					data: {
						team: data.teamId,
						channel: data.channelId,
						contentType: 'HTML',
						content: this._generateMessageContent(data)
					}
				})
				.catch(this.errorHandler);
		},

		/**
		 * Generate HTML content of the message to send.
		 * @instance
		 * @private
		 * @param {object} data
		 */
		_generateMessageContent: function (data) {
			var content = '<p>';
			// Split the message into lines, including empty lines.
			var lines = data.message.split(/\r?\n/);
			// Insert <br> tag after each line to preserve message formatting.
			for (var i = 0; i < lines.length; i++) {
				content = content + lines[i] + '<br>';
			}
			content = content + '</p>';
			if (data.url) {
				var caLink = '<p> <a href=\'' + data.url + '\'>' + StringMessages.get('ca_link') + '</a> </p>';
				content = content + caLink;
			}
			return content;
		},

		/**
		 * Retrieve current user's joined teams from MS Teams.
		 * @instance
		 * @returns {promise}
		 */
		getJoinedTeams: function () {
			return this.loadFeatures().then(this._getJoinedTeams.bind(this));
		},

		_getJoinedTeams: function () {
			return this.client
				.doGet({
					url: this.meta.features.teams.endpoint
				})
				.then(function (result) {
					return result && result.data && result.data.value || [];
				})
				.catch(this.errorHandler);
		},

		/**
		 * Retrieve channels for the given team.
		 * @instance
		 * @param team {Object} Team object.
		 * @returns {promise}
		 */
		getTeamChannels: function (team) {
			var url = this.meta.features.channels.endpoint.replace('{teamId}', team.id);
			return this.client
				.doGet({
					url: url
				})
				.then(function (result) {
					return result && result.data && result.data.value || [];
				})
				.catch(this.errorHandler);
		}
	});

	return MSTeamsConnector;
});