Source: messaging/connectors/ConnectorBase.js

  1. /**
  2. * Licensed Materials - Property of IBM
  3. * IBM Cognos Products: Collaboration
  4. * (C) Copyright IBM Corp. 2017, 2019
  5. *
  6. * US Government Users Restricted Rights - Use, duplication or disclosure
  7. * restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define([
  10. 'underscore',
  11. '../../lib/@waca/core-client/js/core-client/ui/core/Class',
  12. '../../nls/StringResources'
  13. ], function (_, Class, StringResources) {
  14. 'use strict';
  15. var ConnectorBase = Class.extend(/** @lends ConnectorBase */ {
  16. /**
  17. * @desc Constructor for ConnectorBase.
  18. * @constructs ConnectorBase
  19. * @extends Class
  20. * @public
  21. * @param {object} options Options
  22. * @param {object} options.glassContext The glass context
  23. * @param {function} options.errorHandler Function to call when an error occurs
  24. */
  25. init: function (options) {
  26. ConnectorBase.inherited('init', this, arguments);
  27. this.glassContext = options.glassContext;
  28. if (!this.glassContext) {
  29. throw Error('Missing glassContext in options');
  30. }
  31. this.logger = options.glassContext.getCoreSvc('.Logger') || {
  32. log: console.log,
  33. error: console.error
  34. };
  35. this._setMeta(options.meta);
  36. this.me = options.me || {};
  37. this.errorHandler = function (error) {
  38. error.connector = this;
  39. if (options.errorHandler) {
  40. return options.errorHandler(error);
  41. }
  42. throw error;
  43. }.bind(this);
  44. },
  45. loadFeatures: function () {
  46. if (this.meta.features) {
  47. return Promise.resolve();
  48. }
  49. return this.client
  50. .doGet({
  51. url: this.meta.endpoint
  52. })
  53. .then(function (result) {
  54. this.configure(result.data);
  55. }.bind(this))
  56. .catch(this.errorHandler);
  57. },
  58. /**
  59. * Configure this connector with some meta info.
  60. * @instance
  61. * @param {object} meta meta data specific to this connector type.
  62. */
  63. configure: function (meta) {
  64. this._setMeta(meta);
  65. },
  66. /**
  67. * Get this connector's type.
  68. * @instance
  69. * @returns {string} the type
  70. */
  71. getType: function () {
  72. return this.meta.type;
  73. },
  74. /**
  75. * Get this connector's label.
  76. * @instance
  77. * @returns {string} the label
  78. */
  79. getLabel: function () {
  80. if (this.meta.workspace) {
  81. return StringResources.get('composite_provider_name', {
  82. providerLabel: this.meta.label,
  83. workspaceLabel: this.meta.workspace.label
  84. });
  85. }
  86. if (this.meta.type === 'email') {
  87. // Email provider name returned from the server is not localized. Use client-side localized name.
  88. // We only support one email provider, so duplicate names are not an issue.
  89. return StringResources.get('link_email');
  90. }
  91. return this.meta.label;
  92. },
  93. /**
  94. * Authenticate API.
  95. * @instance
  96. * @abstract
  97. * @returns {promise}
  98. */
  99. authenticate: function (options) {
  100. void(options);
  101. throw new Error('Missing implementation (authentication).');
  102. },
  103. /**
  104. * Send a message API.
  105. * @instance
  106. * @param {object} options Options
  107. * @param {string} options.channel channel name
  108. * @param {string} options.message message text
  109. * @param {string} options.url url to CA asset
  110. * @abstract
  111. * @returns {promise}
  112. */
  113. send: function (options) {
  114. void(options);
  115. throw new Error('Missing implementation (send).');
  116. },
  117. /**
  118. * Indicates if this provider supports image attachments.
  119. * @instance
  120. * @returns {boolean} True if images are supported by this message platform, false otherwise.
  121. */
  122. isImageSupported: function() {
  123. return true;
  124. },
  125. /**
  126. * Checks if this provider is available. Subclasses should override this method as desired.
  127. * @return {promise}
  128. */
  129. check: function () {
  130. return Promise.resolve();
  131. },
  132. /**
  133. * Checks if the current user has the required capabilities to use this provider.
  134. * Subclasses should override this method as desired.
  135. *
  136. * @return true if the user has the capabilities to use this provider, false otherwise.
  137. */
  138. checkUserCapabilities: function() {
  139. return true;
  140. },
  141. _setMeta: function(meta) {
  142. this.meta = this.meta ? _.extend(this.meta, meta) : (meta || {});
  143. this.meta.errors = this.meta.errors || [];
  144. }
  145. });
  146. return ConnectorBase;
  147. });