meses-messaging
Version:
Meses messaging SDK in JavaScript
499 lines (459 loc) • 16.5 kB
JavaScript
import HttpClient from './util/HttpClient'
class MesesMessagingService {
constructor(uri, applicationId) {
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
*/
getEntity(entityName) {
const GET_ENTITY_PATH = '/entity'
let apiUrl = this._uri + this._MESSAGING_API_PATH + GET_ENTITY_PATH
let params = {
applicationId: this._applicationId,
entityName: entityName
}
let context = {}
return new Promise(function(resolve, reject) {
const callback = function(err, result) {
if (err) reject(err)
else resolve(result)
}
HttpClient.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
*/
checkEntityNotExists(entityName) {
const GET_ENTITY_PATH = '/entity'
let apiUrl = this._uri + this._MESSAGING_API_PATH + GET_ENTITY_PATH
let params = {
applicationId: this._applicationId,
entityName: entityName
}
let context = {}
return new Promise(function(resolve, reject) {
const callback = function(err, result) {
if (err) resolve()
else reject('ENTITY_ALREADY_EXISTS')
}
HttpClient.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
*/
createEntity(entityName, displayName, properties) {
const CREATE_ENTITY_PATH = '/entity'
let apiUrl = this._uri + this._MESSAGING_API_PATH + CREATE_ENTITY_PATH
let spec = Object.assign({
applicationId: this._applicationId,
entityName: entityName,
displayName: displayName
}, properties)
let context = {}
return new Promise(function(resolve, reject) {
const callback = function(err, result) {
if (err) reject(err)
else resolve(result)
}
HttpClient.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
*/
upsertEntityProperties(entityName, properties) {
const UPSERT_ENTITY_PROPERTIES_PATH = '/entity/properties'
let apiUrl = this._uri + this._MESSAGING_API_PATH + UPSERT_ENTITY_PROPERTIES_PATH
let spec = Object.assign({
applicationId: this._applicationId,
entityName: entityName
}, properties)
let context = {}
return new Promise(function(resolve, reject) {
const callback = function(err, result) {
if (err) reject(err)
else resolve(result)
}
HttpClient.executePost(apiUrl, spec, context, callback)
})
}
/**
* Get conversation properties
*
* @param {String} conversationName
* @return {Promise} properties of specified conversation if fulfilled
*/
getConversation(conversationName) {
const GET_CONVERSATION_PATH = '/conversation'
let apiUrl = this._uri + this._MESSAGING_API_PATH + GET_CONVERSATION_PATH
let params = {
applicationId: this._applicationId,
conversationName: conversationName
}
let context = {}
return new Promise(function(resolve, reject) {
const callback = function(err, result) {
if (err) reject(err)
else resolve(result)
}
HttpClient.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
*/
checkConversationNotExists(conversationName) {
const GET_CONVERSATION_PATH = '/conversation'
let apiUrl = this._uri + this._MESSAGING_API_PATH + GET_CONVERSATION_PATH
let params = {
applicationId: this._applicationId,
conversationName: conversationName
}
let context = {}
return new Promise(function(resolve, reject) {
const callback = function(err, result) {
if (err) resolve()
else reject('CONVERSATION_ALREADY_EXISTS')
}
HttpClient.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
*/
createConversation(conversationName, properties) {
const CREATE_CONVERSATION_PATH = '/conversation'
let apiUrl = this._uri + this._MESSAGING_API_PATH + CREATE_CONVERSATION_PATH
let spec = Object.assign({
applicationId: this._applicationId,
conversationName: conversationName,
title: title
}, properties)
let context = {}
return new Promise(function(resolve, reject) {
const callback = function(err, result) {
if (err) reject(err)
else resolve(result)
}
HttpClient.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
*/
upsertConversationProperties(conversationName, properties) {
const UPSERT_CONVERSATION_PROPERTIES_PATH = '/conversation/properties'
let apiUrl = this._uri + this._MESSAGING_API_PATH + UPSERT_CONVERSATION_PROPERTIES_PATH
let spec = Object.assign({
applicationId: this._applicationId,
conversationName: conversationName
}, properties)
let context = {}
return new Promise(function(resolve, reject) {
const callback = function(err, result) {
if (err) reject(err)
else resolve(result)
}
HttpClient.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
*/
createSubscription(conversationName, subscriberName) {
const CREATE_SUBSCRIPTION_PATH = '/subscribe'
let apiUrl = this._uri + this._MESSAGING_API_PATH + CREATE_SUBSCRIPTION_PATH
let spec = {
applicationId: this._applicationId,
conversationName: conversationName,
subscriberName: subscriberName
}
let context = {}
return new Promise(function(resolve, reject) {
const callback = function(err, result) {
if (err) reject(err)
else resolve(result)
}
HttpClient.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.
*/
createSubscriptions(conversationName, subscribers) {
const CREATE_BATCH_SUBSCRIPTION = '/subscribe/batch'
let apiUrl = this._uri + this._MESSAGING_API_PATH + CREATE_BATCH_SUBSCRIPTION
subscribers = subscribers.map(x => {
return {
applicationId: this._applicationId,
conversationName: conversationName,
subscriberName: x
}
})
let spec = { requestPayloads: subscribers }
let context = {}
return new Promise(function(resolve, reject) {
const callback = function(err, result) {
if (err) reject(err)
else resolve(result)
}
HttpClient.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
*/
createMessage(conversationName, creatorName, message, properties) {
const CREATE_MESSAGE_PATH = '/message'
let apiUrl = this._uri + this._MESSAGING_API_PATH + CREATE_MESSAGE_PATH
let spec = Object.assign({
applicationId: this._applicationId,
conversationName: conversationName,
creatorName: creatorName,
message: message
}, properties)
let context = {}
return new Promise(function(resolve, reject) {
const callback = function(err, result) {
if (err) reject(err)
else resolve(result)
}
HttpClient.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
*/
getSubscriptions(subscriberName) {
const GET_SUBSCRIPTIONS_PATH = '/subscription/list'
let apiUrl = this._uri + this._MESSAGING_API_PATH + GET_SUBSCRIPTIONS_PATH
let params = {
applicationId: this._applicationId,
subscriberName: subscriberName
}
let context = {}
return new Promise(function(resolve, reject) {
const callback = function(err, result) {
if (err) reject(err)
else resolve(result)
}
HttpClient.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
*/
getConversations(subscriberName, startConversationTime, limit) {
const GET_CONVERSATIONS_PATH = '/conversation/list'
let apiUrl = this._uri + this._MESSAGING_API_PATH + GET_CONVERSATIONS_PATH
let params = {
applicationId: this._applicationId,
subscriberName: subscriberName,
startConversationTime: startConversationTime,
limit: limit
}
let context = {}
return new Promise(function(resolve, reject) {
const callback = function(err, result) {
if (err) reject(err)
else resolve(result)
}
HttpClient.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
*/
getMessages(conversationName, startMessageId, limit) {
const GET_MESSAGES_PATH = '/message/list'
let apiUrl = this._uri + this._MESSAGING_API_PATH + GET_MESSAGES_PATH
let params = {
applicationId: this._applicationId,
conversationName: conversationName,
startMessageId: startMessageId,
limit: limit
}
let context = {}
return new Promise(function(resolve, reject) {
const callback = function(err, result) {
if (err) reject(err)
else resolve(result)
}
HttpClient.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.
*/
getUpdatedConversations(subscriberName, startTime, endTime) {
const GET_UPDATED_CONVERSATIONS_PATH = '/conversation/interval'
let apiUrl = this._uri + this._MESSAGING_API_PATH + GET_UPDATED_CONVERSATIONS_PATH
let params = {
applicationId: this._applicationId,
subscriberName: subscriberName,
startTime: startTime,
endTime: endTime
}
let context = {}
return new Promise(function(resolve, reject) {
const callback = function(err, result) {
if (err) reject(err)
else resolve(result)
}
HttpClient.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.
*/
getMessagesByInterval(conversationName, startTime, endTime) {
const GET_MESSAGES_BY_INTERVAL_PATH = '/message/interval'
let apiUrl = this._uri + this._MESSAGING_API_PATH + GET_MESSAGES_BY_INTERVAL_PATH
let params = {
applicationId: this._applicationId,
conversationName: conversationName,
startTime: startTime,
endTime: endTime
}
let context = {}
return new Promise(function(resolve, reject) {
const callback = function(err, result) {
if (err) reject(err)
else resolve(result)
}
HttpClient.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
*/
unsubscribeConversation(conversationName, subscriberName) {
const UNSUBSCRIBE_CONVERSATION_PATH = '/unsubscribe'
let apiUrl = this._uri + this._MESSAGING_API_PATH + UNSUBSCRIBE_CONVERSATION_PATH
let spec = {
applicationId: this._applicationId,
conversationName: conversationName,
subscriberName: subscriberName
}
let context = {}
return new Promise(function(resolve, reject) {
const callback = function(err, result) {
if (err) reject(err)
else resolve(result)
}
HttpClient.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
*/
markConversationRead(conversationName, subscriberName) {
const MARK_CONVERSATION_READ_PATH = '/read'
let apiUrl = this._uri + this._MESSAGING_API_PATH + MARK_CONVERSATION_READ_PATH
let spec = {
applicationId: this._applicationId,
conversationName: conversationName,
subscriberName: subscriberName
}
let context = {}
return new Promise(function(resolve, reject) {
const callback = function(err, result) {
if (err) reject(err)
else resolve(result)
}
HttpClient.executePost(apiUrl, spec, context, callback)
})
}
}
export default MesesMessagingService