UNPKG

node-ciscospark

Version:

Super-Simple Lightweight Javascript wrapper for Cisco Spark API

188 lines (157 loc) 7.7 kB
'use strict'; /** @ignore */ 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 _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var CiscoSpark = require('./CiscoSpark'); /** * Spark Messages * @see https://developer.ciscospark.com/resource-messages.html */ var Messages = function (_CiscoSpark) { _inherits(Messages, _CiscoSpark); /** * @constructor * @param {string} [accessToken] - Your Cisco Spark accesstoken * @param {string} [userAgent] - User Agent request header */ function Messages(accessToken, userAgent) { _classCallCheck(this, Messages); /** @private */ var _this = _possibleConstructorReturn(this, (Messages.__proto__ || Object.getPrototypeOf(Messages)).call(this, accessToken, userAgent, 'https://api.ciscospark.com/v1/messages')); _this.idName = 'messageId'; return _this; } /** * List Messages * Lists all messages in a room. Each message will include content attachments if present. * The list sorts the messages in descending order by creation date. * * @override * @param {MessageListParams} params - see https://developer.ciscospark.com/endpoint-messages-get.html * @param {requestCallback} callback */ _createClass(Messages, [{ key: 'list', value: function list(params, callback) { if (!params || !params.roomId) { if (typeof params === 'string') { params = { roomId: params }; } else { return callback(new Error('Invalid Params. Require roomId')); } } return _get(Messages.prototype.__proto__ || Object.getPrototypeOf(Messages.prototype), 'list', this).call(this, params, callback); } /** * Create a Message * Posts a plain text message, and optionally, a media content attachment, to a room. * * @override * @param {MessageCreateParams} params - see https://developer.ciscospark.com/endpoint-messages-post.html * @param {requestCallback} callback */ }, { key: 'create', value: function create(params, callback) { if (params && (params.roomId || params.toPersonId || params.toPersonEmail)) { return _get(Messages.prototype.__proto__ || Object.getPrototypeOf(Messages.prototype), 'create', this).call(this, params, callback); } else { return callback(new Error('Invalid Params. Require roomId, toPersonId or toPersonEmail')); } } /** * Create a Message to a Room * * @param {string} roomId - Spark Room ID * @param {string|MessageCreateParams} params - Markdown formatted message string or Request parameters object * @param {requestCallback} callback */ }, { key: 'createToRoom', value: function createToRoom(roomId, params, callback) { if (typeof params === 'string') params = { markdown: params }; params.roomId = roomId; return this.create(params, callback); } /** * Create a Message for a Person * * @param {string} personId - Spark Person ID * @param {string|MessageCreateParams} params - Markdown formatted message string or Request parameters object * @param {requestCallback} callback */ }, { key: 'createToPersonId', value: function createToPersonId(personId, params, callback) { if (typeof params === 'string') params = { markdown: params }; params.toPersonId = personId; return this.create(params, callback); } /** * Create a Message to a person via email * * @param {string} email - Email address * @param {string|MessageCreateParams} params - Markdown formatted message string or Request parameters object * @param {requestCallback} callback */ }, { key: 'createToPersonEmail', value: function createToPersonEmail(email, params, callback) { if (typeof params === 'string') params = { markdown: params }; params.toPersonEmail = email; return this.create(params, callback); } /** * Get a message * * @override * @param {string} messageId - Spark Message ID * @param {requestCallback} callback */ }, { key: 'get', value: function get(messageId, callback) { if (!messageId || typeof messageId !== 'string') { return callback(new Error('Message ID is missing or in the wrong format')); } return _get(Messages.prototype.__proto__ || Object.getPrototypeOf(Messages.prototype), 'get', this).call(this, messageId, callback); } /** * Delete a message * * @override * @param {string} messageId - Spark Message ID * @param {requestCallback} callback */ }, { key: 'delete', value: function _delete(messageId, callback) { if (!messageId || typeof messageId !== 'string') { return callback(new Error('Message ID is missing or in the wrong format')); } return _get(Messages.prototype.__proto__ || Object.getPrototypeOf(Messages.prototype), 'delete', this).call(this, messageId, callback); } }]); return Messages; }(CiscoSpark); module.exports = Messages; /** * @typedef {Object} MessageListParams * @property {string} roomId - The Room ID * @property {string} [mentionedPeople] - filter messages which has mentioned Person ID * @property {string} [before] - list Messages before time in ISO8601 format * @property {string} [beforeMessage] - list Messages before Message ID * @property {number} [max] - Limit maximum messages in response */ /** * @typedef {Object} MessageCreateParams * @property {string} roomId - The Room ID * @property {string} toPersonId - Person ID * @property {string} toPersonEmail - Email Address * @property {string} text - Plain Message text * @property {string} markdown - Markdown formatted Message text * @property {Array(string)} files - Public URL link to the files */