meses-messaging
Version:
Meses messaging SDK in JavaScript
310 lines (260 loc) • 10.6 kB
JavaScript
'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 _MesesMessagingService = require('./services/MesesMessagingService');
var _MesesMessagingService2 = _interopRequireDefault(_MesesMessagingService);
var _Conversation = require('./classes/Conversation');
var _Conversation2 = _interopRequireDefault(_Conversation);
var _PreviousConversationListQuery = require('./classes/PreviousConversationListQuery');
var _PreviousConversationListQuery2 = _interopRequireDefault(_PreviousConversationListQuery);
var _ConversationUpdateHandler = require('./classes/ConversationUpdateHandler');
var _ConversationUpdateHandler2 = _interopRequireDefault(_ConversationUpdateHandler);
var _NewMessageUpdateHandler = require('./classes/NewMessageUpdateHandler');
var _NewMessageUpdateHandler2 = _interopRequireDefault(_NewMessageUpdateHandler);
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 MesesMessagingApp = function () {
function MesesMessagingApp(uri, applicationId) {
_classCallCheck(this, MesesMessagingApp);
this._service = new _MesesMessagingService2.default(uri, applicationId);
this._user = null;
this._updateHandlers = [];
}
_createClass(MesesMessagingApp, [{
key: 'onBeforeDisconnect',
value: function onBeforeDisconnect() {}
/**
* Connect client app with a user
*
* @param {String} username - an entity name
* @return {Promise} user and its properties if connect succeeded (fulfilled).
*/
}, {
key: 'connect',
value: function connect(username) {
if (this._user && this._user.username != username) {
this.onBeforeDisconnect();
this.detachAllUpdateHandlers();
}
var service = this._service;
return new Promise(function (resolve, reject) {
service.getEntity(username).then(function (result) {
this._user = Object.assign({ username: username }, result);
resolve(this._user);
}.bind(this)).catch(function (err) {
return reject(err);
});
}.bind(this));
}
/**
* Disconnect client app from the connected user
* This method will also detach all update handlers
*
* @return {Promise} fulfilled if disconnect succeeded.
*/
}, {
key: 'disconnect',
value: function disconnect() {
return new Promise(function (resolve, reject) {
if (this._user == null) reject('USER_NOT_CONNECTED');else {
this.onBeforeDisconnect();
this.detachAllUpdateHandlers();
this._user = null;resolve();
}
}.bind(this));
}
/**
* Create a new entity with the specified properties
*
* @param {String} username - entity name of the new user
* @param {String} displayName - name that will be displayed in messaging UI
* @param {Object} properties
* @return {Promise} new user and its properties if user creation succeeded (fulfilled).
*/
}, {
key: 'createUser',
value: function createUser(username, displayName, properties) {
var service = this._service;
return new Promise(function (resolve, reject) {
service.checkEntityNotExists(username).then(function (result) {
service.createEntity(username, displayName, properties);
}).then(function () {
resolve(Object.assign({ username: username }, properties));
}).catch(function (err) {
return reject(err);
});
});
}
/**
* Append/update properties of the currently connected user.
* This method will also update the connected user with new properties.
*
* @param {Object} properties
* @return {Promise} new user state if update user succeeded (fulfilled).
*/
}, {
key: 'updateUserInfo',
value: function updateUserInfo(properties) {
return new Promise(function (resolve, reject) {
if (this._user == null) {
reject('USER_NOT_CONNECTED');
} else {
var username = this._user.username;
var service = this._service;
service.upsertEntityProperties(username, properties).then(function () {
this._user = Object.assign(this._user, properties);
resolve(this._user);
}.bind(this)).catch(function (err) {
return reject(err);
});
}
}.bind(this));
}
/**
* Retrieve a conversation object of the specified conversation name.
*
* @param {String} conversationName
* @return {Promise} Conversation and its properties if retrieval succeeded (fulfilled).
*/
}, {
key: 'getConversation',
value: function getConversation(conversationName) {
if (this._user == null) return new Promise(function (resolve, reject) {
reject('USER_NOT_CONNECTED');
});
var username = this._user.username;
var service = this._service;
return new Promise(function (resolve, reject) {
service.getConversation(conversationName).then(function (properties) {
return resolve(new _Conversation2.default(service, conversationName, username, properties));
}).catch(function (err) {
return reject(err);
});
});
}
/**
* Create a new conversation with specified properties and subscribers
*
* @param {String} conversationName
* @param {String} title
* @param {Object} properties
* @param {Array.<String>} subscribers - array of subscriber names
* @return {Promise} Conversation and its properties if creation succeeded (fulfilled).
*/
}, {
key: 'createConversation',
value: function createConversation(conversationName, title, properties, subscribers) {
if (this._user == null) return new Promise(function (resolve, reject) {
reject('USER_NOT_CONNECTED');
});
var username = this._user.username;
var service = this._service;
return new Promise(function (resolve, reject) {
service.checkConversationNotExists(conversationName).then(function () {
return service.createConversation(conversationName, title, properties);
}).then(function () {
var found = subscribers.find(function (name) {
return name === username;
});
if (!found) subscribers.push(username);
return service.createSubscriptions(conversationName, subscribers);
}).then(function () {
return service.getConversation(conversationName);
}).then(function (result) {
resolve(new _Conversation2.default(service, conversationName, username, result));
}).catch(function (err) {
return reject(err);
});
});
}
/**
* Create a previous conversation list query.
*
* @param {Number} start - query param for first conversation's message time
* @return {PreviousConversationListQuery}
* @throws {Error} if user is not connected
*/
}, {
key: 'createPreviousConversationListQuery',
value: function createPreviousConversationListQuery(start) {
if (this._user == null) throw new Error('USER_NOT_CONNECTED');
start = start ? start : null;
var service = this._service;
var username = this._user.username;
return new _PreviousConversationListQuery2.default(service, username, start);
}
/**
* Create a conversation update handler.
*
* @param {String} identifier - unique identifier of update handler
* @param {Function(err, result)} callback function for conversation updates
* @param {Number} timeout - refresh rate (in milliseconds)
* @return {ConversationUpdateHandler}
* @throws {Error} if user is not connected
*/
}, {
key: 'createConversationUpdateHandler',
value: function createConversationUpdateHandler(identifier, callback, timeout) {
if (this._user == null) throw new Error('USER_NOT_CONNECTED');
var service = this._service;
var username = this._user.username;
return new _ConversationUpdateHandler2.default(service, username, identifier, callback, timeout);
}
/**
* Attach an update handler
*
* @param {ConversationUpdateHandler | NewMessageUpdateHandler} handler - update handler
* @return {Void}
* @throws {Error} if invalid handler type
*/
}, {
key: 'attachUpdateHandler',
value: function attachUpdateHandler(handler) {
if (handler instanceof _ConversationUpdateHandler2.default || handler instanceof _NewMessageUpdateHandler2.default) {
var found = this._updateHandlers.find(function (x) {
return x._identifier === handler._identifier && x.constructor.name === handler.constructor.name;
});
if (found == null) {
this._updateHandlers.push(handler);
handler.activate();
}
} else throw new Error('INVALID_HANDLER_TYPE');
}
/**
* Detach all update handlers
*
* @return {Void}
*/
}, {
key: 'detachAllUpdateHandlers',
value: function detachAllUpdateHandlers() {
this._updateHandlers.map(function (x) {
return x.destroy();
});this._updateHandlers = [];
}
/**
* Detach update handler with specified identifier
*
* @param {String} identifier
* @return {Void}
*/
}, {
key: 'detachUpdateHandler',
value: function detachUpdateHandler(identifier) {
var indexes = [];
this._updateHandlers.map(function (x, i) {
if (x._identifier === identifier) {
x.destroy();indexes.push(i);
}
});
indexes.reverse();
indexes.map(function (x) {
this._updateHandlers.splice(x, 1);
}.bind(this));
}
}]);
return MesesMessagingApp;
}();
exports.default = MesesMessagingApp;