UNPKG

meses-messaging

Version:

Meses messaging SDK in JavaScript

556 lines (489 loc) 19.8 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _HttpClient = require('./util/HttpClient'); var _HttpClient2 = _interopRequireDefault(_HttpClient); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var MesesMessagingService = function () { function MesesMessagingService(uri, applicationId) { _classCallCheck(this, MesesMessagingService); this._VERSION = '1.0.0'; this._USER_AGENT = 'Meses-Client/' + this._VERSION; this._MESSAGING_API_PATH = '/v1/messaging'; this._uri = uri; this._applicationId = applicationId; } /** * Get entity properties of the specified entity * * @param {String} entityName * @return {Promise} properties of the specified entity if fulfilled */ _createClass(MesesMessagingService, [{ key: 'getEntity', value: function getEntity(entityName) { var GET_ENTITY_PATH = '/entity'; var apiUrl = this._uri + this._MESSAGING_API_PATH + GET_ENTITY_PATH; var params = { applicationId: this._applicationId, entityName: entityName }; var context = {}; return new Promise(function (resolve, reject) { var callback = function callback(err, result) { if (err) reject(err);else resolve(result); }; _HttpClient2.default.executeGet(apiUrl, params, context, callback); }); } /** * Check if the specified entity name does not exist * * @param {String} entityName * @return {Promise} fulfilled iff the specified entity name does not exist */ }, { key: 'checkEntityNotExists', value: function checkEntityNotExists(entityName) { var GET_ENTITY_PATH = '/entity'; var apiUrl = this._uri + this._MESSAGING_API_PATH + GET_ENTITY_PATH; var params = { applicationId: this._applicationId, entityName: entityName }; var context = {}; return new Promise(function (resolve, reject) { var callback = function callback(err, result) { if (err) resolve();else reject('ENTITY_ALREADY_EXISTS'); }; _HttpClient2.default.executeGet(apiUrl, params, context, callback); }); } /** * Create a new entity. This method will upsert the previous entry * if entity with same entityName already exists. * * @param {String} entityName (can be NULL) * @param {String} displayName (can be NULL) * @param {Object} properties * @return {Promise} the corresponding entityName of the specified entity * if fulfilled */ }, { key: 'createEntity', value: function createEntity(entityName, displayName, properties) { var CREATE_ENTITY_PATH = '/entity'; var apiUrl = this._uri + this._MESSAGING_API_PATH + CREATE_ENTITY_PATH; var spec = Object.assign({ applicationId: this._applicationId, entityName: entityName, displayName: displayName }, properties); var context = {}; return new Promise(function (resolve, reject) { var callback = function callback(err, result) { if (err) reject(err);else resolve(result); }; _HttpClient2.default.executePost(apiUrl, spec, context, callback); }); } /** * Update entity properties. This method will append the previous entry * 'properties' field with the specified properties. * * @param {String} entityName * @param {Object} properties * @return {Promise} fulfilled iff the specified entity name does exist */ }, { key: 'upsertEntityProperties', value: function upsertEntityProperties(entityName, properties) { var UPSERT_ENTITY_PROPERTIES_PATH = '/entity/properties'; var apiUrl = this._uri + this._MESSAGING_API_PATH + UPSERT_ENTITY_PROPERTIES_PATH; var spec = Object.assign({ applicationId: this._applicationId, entityName: entityName }, properties); var context = {}; return new Promise(function (resolve, reject) { var callback = function callback(err, result) { if (err) reject(err);else resolve(result); }; _HttpClient2.default.executePost(apiUrl, spec, context, callback); }); } /** * Get conversation properties * * @param {String} conversationName * @return {Promise} properties of specified conversation if fulfilled */ }, { key: 'getConversation', value: function getConversation(conversationName) { var GET_CONVERSATION_PATH = '/conversation'; var apiUrl = this._uri + this._MESSAGING_API_PATH + GET_CONVERSATION_PATH; var params = { applicationId: this._applicationId, conversationName: conversationName }; var context = {}; return new Promise(function (resolve, reject) { var callback = function callback(err, result) { if (err) reject(err);else resolve(result); }; _HttpClient2.default.executeGet(apiUrl, params, context, callback); }); } /** * Check if the specified conversation name does not exist * * @param {String} conversationName * @return {Promise} fulfilled iff the specified conversation name * does not exist */ }, { key: 'checkConversationNotExists', value: function checkConversationNotExists(conversationName) { var GET_CONVERSATION_PATH = '/conversation'; var apiUrl = this._uri + this._MESSAGING_API_PATH + GET_CONVERSATION_PATH; var params = { applicationId: this._applicationId, conversationName: conversationName }; var context = {}; return new Promise(function (resolve, reject) { var callback = function callback(err, result) { if (err) resolve();else reject('CONVERSATION_ALREADY_EXISTS'); }; _HttpClient2.default.executeGet(apiUrl, params, context, callback); }); } /** * Create a new conversation. This method will upsert the previous * entry if conversation with same conversationName already exists. * * @param {String} conversationName (can be NULL) * @param {String} title (can be NULL) * @param {Object} properties * @return {Promise} the corresponding conversationName of the specified * conversation if fulfilled */ }, { key: 'createConversation', value: function createConversation(conversationName, properties) { var CREATE_CONVERSATION_PATH = '/conversation'; var apiUrl = this._uri + this._MESSAGING_API_PATH + CREATE_CONVERSATION_PATH; var spec = Object.assign({ applicationId: this._applicationId, conversationName: conversationName, title: title }, properties); var context = {}; return new Promise(function (resolve, reject) { var callback = function callback(err, result) { if (err) reject(err);else resolve(result); }; _HttpClient2.default.executePost(apiUrl, spec, context, callback); }); } /** * Update conversation properties. This method will append the previous * entry 'properties' field with the specified properties. * * @param {String} conversationName * @param {Object} properties * @return {Promise} fulfilled iff the specified conversation name does exist */ }, { key: 'upsertConversationProperties', value: function upsertConversationProperties(conversationName, properties) { var UPSERT_CONVERSATION_PROPERTIES_PATH = '/conversation/properties'; var apiUrl = this._uri + this._MESSAGING_API_PATH + UPSERT_CONVERSATION_PROPERTIES_PATH; var spec = Object.assign({ applicationId: this._applicationId, conversationName: conversationName }, properties); var context = {}; return new Promise(function (resolve, reject) { var callback = function callback(err, result) { if (err) reject(err);else resolve(result); }; _HttpClient2.default.executePost(apiUrl, spec, context, callback); }); } /** * Register a new subscription entry. This method will not register a new subscription * if the specified subscriber has already subscribed the specified conversation. * * @param {String} conversationName * @param {String} subscriberName - name of entity which will subscribe * @return {Promise} fulfilled iff registration succeeded */ }, { key: 'createSubscription', value: function createSubscription(conversationName, subscriberName) { var CREATE_SUBSCRIPTION_PATH = '/subscribe'; var apiUrl = this._uri + this._MESSAGING_API_PATH + CREATE_SUBSCRIPTION_PATH; var spec = { applicationId: this._applicationId, conversationName: conversationName, subscriberName: subscriberName }; var context = {}; return new Promise(function (resolve, reject) { var callback = function callback(err, result) { if (err) reject(err);else resolve(result); }; _HttpClient2.default.executePost(apiUrl, spec, context, callback); }); } /** * Register list of subscribers to the specified conversation. * * @param {String} conversationName * @param {Array.<String>} subscribers - array of entityNames which will subscribe * @return {Promise} fulfilled iff all registration succeeded. */ }, { key: 'createSubscriptions', value: function createSubscriptions(conversationName, subscribers) { var _this = this; var CREATE_BATCH_SUBSCRIPTION = '/subscribe/batch'; var apiUrl = this._uri + this._MESSAGING_API_PATH + CREATE_BATCH_SUBSCRIPTION; subscribers = subscribers.map(function (x) { return { applicationId: _this._applicationId, conversationName: conversationName, subscriberName: x }; }); var spec = { requestPayloads: subscribers }; var context = {}; return new Promise(function (resolve, reject) { var callback = function callback(err, result) { if (err) reject(err);else resolve(result); }; _HttpClient2.default.executePost(apiUrl, spec, context, callback); }); } /** * Create a new message. * * @param {String} conversationName * @param {String} creatorName - the entityName of the creator * @param {String} message * @param {Object} properties * @return {Promise} messageId (message timestamp) if fulfilled */ }, { key: 'createMessage', value: function createMessage(conversationName, creatorName, message, properties) { var CREATE_MESSAGE_PATH = '/message'; var apiUrl = this._uri + this._MESSAGING_API_PATH + CREATE_MESSAGE_PATH; var spec = Object.assign({ applicationId: this._applicationId, conversationName: conversationName, creatorName: creatorName, message: message }, properties); var context = {}; return new Promise(function (resolve, reject) { var callback = function callback(err, result) { if (err) reject(err);else resolve(result); }; _HttpClient2.default.executePost(apiUrl, spec, context, callback); }); } /** * Get a list of conversation names subscribed by the specified entity * * @param {String} subscriberName - the specified entity name * @return {Promise} subscriptions - Array of conversationNames which the entity subscribes */ }, { key: 'getSubscriptions', value: function getSubscriptions(subscriberName) { var GET_SUBSCRIPTIONS_PATH = '/subscription/list'; var apiUrl = this._uri + this._MESSAGING_API_PATH + GET_SUBSCRIPTIONS_PATH; var params = { applicationId: this._applicationId, subscriberName: subscriberName }; var context = {}; return new Promise(function (resolve, reject) { var callback = function callback(err, result) { if (err) reject(err);else resolve(result); }; _HttpClient2.default.executeGet(apiUrl, params, context, callback); }); } /** * Get a list of conversations (with properties) subscribed by the specified entity * ordered descending by lastMessageTime. The result will also contain 'lastReadTime' * and 'unreadCount' attribute. * * @param {String} subscriberName - the specified entity name * @param {Number} startConversationTime - offset for retrieving the list * @param {Number} limit - desired list size * @return {Promise} conversationList - Array of conversations subscribed */ }, { key: 'getConversations', value: function getConversations(subscriberName, startConversationTime, limit) { var GET_CONVERSATIONS_PATH = '/conversation/list'; var apiUrl = this._uri + this._MESSAGING_API_PATH + GET_CONVERSATIONS_PATH; var params = { applicationId: this._applicationId, subscriberName: subscriberName, startConversationTime: startConversationTime, limit: limit }; var context = {}; return new Promise(function (resolve, reject) { var callback = function callback(err, result) { if (err) reject(err);else resolve(result); }; _HttpClient2.default.executeGet(apiUrl, params, context, callback); }); } /** * Get a list of messages in an conversation ordered descending by messageId (timestamp) * * @param {String} conversationName * @param {Number} startMessageId - offset for retrieving the list * @param {Number} limit - desired list size * @return {Promise} messages - Array of messages in the specified conversation */ }, { key: 'getMessages', value: function getMessages(conversationName, startMessageId, limit) { var GET_MESSAGES_PATH = '/message/list'; var apiUrl = this._uri + this._MESSAGING_API_PATH + GET_MESSAGES_PATH; var params = { applicationId: this._applicationId, conversationName: conversationName, startMessageId: startMessageId, limit: limit }; var context = {}; return new Promise(function (resolve, reject) { var callback = function callback(err, result) { if (err) reject(err);else resolve(result); }; _HttpClient2.default.executeGet(apiUrl, params, context, callback); }); } /** * Get a list of conversation with lastMessageTime between a specific interval, which subscribed by the * specified entity. Return an empty array if no new message. * * @param {String} subscriberName - the specified entity name * @param {Number} startTime * @param {Number} endTime * @return {Promise} updatedConversationPayloads - array of conversations. */ }, { key: 'getUpdatedConversations', value: function getUpdatedConversations(subscriberName, startTime, endTime) { var GET_UPDATED_CONVERSATIONS_PATH = '/conversation/interval'; var apiUrl = this._uri + this._MESSAGING_API_PATH + GET_UPDATED_CONVERSATIONS_PATH; var params = { applicationId: this._applicationId, subscriberName: subscriberName, startTime: startTime, endTime: endTime }; var context = {}; return new Promise(function (resolve, reject) { var callback = function callback(err, result) { if (err) reject(err);else resolve(result); }; _HttpClient2.default.executeGet(apiUrl, params, context, callback); }); } /** * Get a list of new messages between a specific interval. Return an empty array if no new message. * * @param {String} conversationName * @param {Number} startTime * @param {Number} endTime * @return {Promise} messages - array of messages. */ }, { key: 'getMessagesByInterval', value: function getMessagesByInterval(conversationName, startTime, endTime) { var GET_MESSAGES_BY_INTERVAL_PATH = '/message/interval'; var apiUrl = this._uri + this._MESSAGING_API_PATH + GET_MESSAGES_BY_INTERVAL_PATH; var params = { applicationId: this._applicationId, conversationName: conversationName, startTime: startTime, endTime: endTime }; var context = {}; return new Promise(function (resolve, reject) { var callback = function callback(err, result) { if (err) reject(err);else resolve(result); }; _HttpClient2.default.executeGet(apiUrl, params, context, callback); }); } /** * Unregister a subscription entry. This method will do nothing if the specified * subscription entry already not exist. * * @param {String} conversationName * @param {String} subscriberName - name of entity which will unsubscribe * @return {Promise} fulfilled iff unregister succeeded */ }, { key: 'unsubscribeConversation', value: function unsubscribeConversation(conversationName, subscriberName) { var UNSUBSCRIBE_CONVERSATION_PATH = '/unsubscribe'; var apiUrl = this._uri + this._MESSAGING_API_PATH + UNSUBSCRIBE_CONVERSATION_PATH; var spec = { applicationId: this._applicationId, conversationName: conversationName, subscriberName: subscriberName }; var context = {}; return new Promise(function (resolve, reject) { var callback = function callback(err, result) { if (err) reject(err);else resolve(result); }; _HttpClient2.default.executePost(apiUrl, spec, context, callback); }); } /** * Update the conversation read time by the specified subscriber with current millis. * * @param {String} conversationName * @param {String} subscriberName - the entity that subscribes the conversation * @return {Promise} fulfilled iff mark as read succeeded */ }, { key: 'markConversationRead', value: function markConversationRead(conversationName, subscriberName) { var MARK_CONVERSATION_READ_PATH = '/read'; var apiUrl = this._uri + this._MESSAGING_API_PATH + MARK_CONVERSATION_READ_PATH; var spec = { applicationId: this._applicationId, conversationName: conversationName, subscriberName: subscriberName }; var context = {}; return new Promise(function (resolve, reject) { var callback = function callback(err, result) { if (err) reject(err);else resolve(result); }; _HttpClient2.default.executePost(apiUrl, spec, context, callback); }); } }]); return MesesMessagingService; }(); exports.default = MesesMessagingService;