Source: messaging/connectors/ConnectorBase.js

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

define([
	'underscore',
	'../../lib/@waca/core-client/js/core-client/ui/core/Class',
	'../../nls/StringResources'
], function (_, Class, StringResources) {
	'use strict';

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

		/**
		 * @desc Constructor for ConnectorBase.
		 * @constructs ConnectorBase
		 * @extends Class
		 * @public
		 * @param {object} options Options
		 * @param {object} options.glassContext The glass context
		 * @param {function} options.errorHandler Function to call when an error occurs
		 */
		init: function (options) {
			ConnectorBase.inherited('init', this, arguments);
			this.glassContext = options.glassContext;
			if (!this.glassContext) {
				throw Error('Missing glassContext in options');
			}
			this.logger = options.glassContext.getCoreSvc('.Logger') || {
				log: console.log,
				error: console.error
			};
			this._setMeta(options.meta);
			this.me = options.me || {};
			this.errorHandler = function (error) {
				error.connector = this;
				if (options.errorHandler) {
					return options.errorHandler(error);
				}
				throw error;
			}.bind(this);
		},

		loadFeatures: function () {
			if (this.meta.features) {
				return Promise.resolve();
			}
			return this.client
				.doGet({
					url: this.meta.endpoint
				})
				.then(function (result) {
					this.configure(result.data);
				}.bind(this))
				.catch(this.errorHandler);
		},

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

		/**
		 * Get this connector's type.
		 * @instance
		 * @returns {string} the type
		 */
		getType: function () {
			return this.meta.type;
		},

		/**
		 * Get this connector's label.
		 * @instance
		 * @returns {string} the label
		 */
		getLabel: function () {
			if (this.meta.workspace) {
				return StringResources.get('composite_provider_name', {
					providerLabel: this.meta.label,
					workspaceLabel: this.meta.workspace.label
				});
			}
			if (this.meta.type === 'email') {
				// Email provider name returned from the server is not localized. Use client-side localized name.
				// We only support one email provider, so duplicate names are not an issue.
				return StringResources.get('link_email');
			}
			return this.meta.label;
		},

		/**
		 * Authenticate API.
		 * @instance
		 * @abstract
		 * @returns {promise}
		 */
		authenticate: function (options) {
			void(options);
			throw new Error('Missing implementation (authentication).');
		},

		/**
		 * Send a message API.
		 * @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
		 * @abstract
		 * @returns {promise}
		 */
		send: function (options) {
			void(options);
			throw new Error('Missing implementation (send).');
		},

		/**
		 * Indicates if this provider supports image attachments.
		 * @instance
		 * @returns {boolean} True if images are supported by this message platform, false otherwise.
		 */
		isImageSupported: function() {
			return true;
		},

		/**
		 * Checks if this provider is available. Subclasses should override this method as desired.
		 * @return {promise}
		 */
		check: function () {
			return Promise.resolve();
		},

		/**
		 * Checks if the current user has the required capabilities to use this provider.
		 * Subclasses should override this method as desired.
		 * 
		 * @return true if the user has the capabilities to use this provider, false otherwise.
		 */
		checkUserCapabilities: function() {
			return true;
		},

		_setMeta: function(meta) {
			this.meta = this.meta ? _.extend(this.meta, meta) : (meta || {});
			this.meta.errors = this.meta.errors || [];
		}
	});
	return ConnectorBase;
});