UNPKG

jive-sdk

Version:

Node.js SDK for Jive Software to assist with the development of add-ons

425 lines (329 loc) 12.8 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Jive SDK Source: jive-sdk-api/lib/tile/comments.js</title> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/sunlight.default.css"> <link type="text/css" rel="stylesheet" href="styles/site.spacelab.css"> </head> <body> <div class="container-fluid"> <div class="navbar navbar-fixed-top navbar-inverse"> <div class="navbar-inner"> <a class="brand" href="index.html">Jive SDK</a> <ul class="nav"> <li class="dropdown"> <a href="modules.list.html" class="dropdown-toggle" data-toggle="dropdown">Modules<b class="caret"></b></a> <ul class="dropdown-menu "> <li> <a href="module-abstractDefinitions.html">abstractDefinitions</a> </li> <li> <a href="module-abstractInstances.html">abstractInstances</a> </li> <li> <a href="module-addOnRoutes.html">addOnRoutes</a> </li> <li> <a href="module-api.html">api</a> </li> <li> <a href="module-community.html">community</a> </li> <li> <a href="module-constants.html">constants</a> </li> <li> <a href="module-devRoutes.html">devRoutes</a> </li> <li> <a href="module-events.html">events</a> </li> <li> <a href="module-extensions.html">extensions</a> </li> <li> <a href="module-extstreamsInstances.html">extstreamsInstances</a> </li> <li> <a href="module-jiveutil.html">jiveutil</a> </li> <li> <a href="module-oauthRoutes.html">oauthRoutes</a> </li> <li> <a href="module-request.html">request</a> </li> <li> <a href="module-security.html">security</a> </li> <li> <a href="module-service.html">service</a> </li> <li> <a href="module-tasks.html">tasks</a> </li> <li> <a href="module-tileInstances.html">tileInstances</a> </li> <li> <a href="module-tileRoutes.html">tileRoutes</a> </li> <li> <a href="module-tilesDefinitions.html">tilesDefinitions</a> </li> <li> <a href="module-webhooks.html">webhooks</a> </li> </ul> </li> <li class="dropdown"> <a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b class="caret"></b></a> <ul class="dropdown-menu "> <li> <a href="extstreamsDefinitions.html">extstreamsDefinitions</a> </li> <li> <a href="filePersistence.html">filePersistence</a> </li> <li> <a href="memoryPersistence.html">memoryPersistence</a> </li> <li> <a href="memoryScheduler.html">memoryScheduler</a> </li> <li> <a href="oauthHandler.html">oauthHandler</a> </li> </ul> </li> </ul> </div> </div> <div class="row-fluid"> <div class="span12"> <div id="main"> <h1 class="page-title">Source: jive-sdk-api/lib/tile/comments.js</h1> <section> <article> <pre class="sunlight-highlight-javascript linenums">/* * Copyright 2013 Jive Software * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Private library for comments management. * @module comments * @private */ var jive = require('../../api'); var q = require('q'); /** * Comment on a jive activity. * @param jiveActivity * @param comment * @return {Promise} Promise */ exports.commentOnActivity = function(jiveActivity, comment) { if (!(jiveActivity &amp;&amp; jiveActivity.resources &amp;&amp; jiveActivity.resources.comments &amp;&amp; jiveActivity.resources.comments.ref) || !jiveActivity.parent) { return q.reject({ 'err' : 'Error in jive.extstreams.commentOnActivity: input activity is not a valid Jive object.' + 'It is missing the resources.comments.ref field or parent field.'} ); } var commentsURL = jiveActivity.resources.comments.ref; var parentInstanceURL = jiveActivity.parent + '/activities'; return jive.extstreams.findByURL(parentInstanceURL).then(function(extstream) { if (!comment.externalID){ comment.externalID = extstream.name + '_' + 'comment' + '_' + String(new Date().getTime()); jive.logger.warn(jive.util.format('No externalID field given when creating new comment. Assigning ID %s', comment.externalID )); } // schedule the comment push return jive.context.scheduler.schedule(jive.constants.tileEventNames.PUSH_COMMENT_TO_JIVE, { 'tileInstance' : extstream, 'comment' : comment, 'commentsURL' : commentsURL } ); }); }; /** * Comment on jive activity via its external activity ID, eg. the ID by which that jive activity * is known in an external system such as salesforce. * @param extstream * @param externalActivityID * @param comment * @return {Promise} Promise */ exports.commentOnActivityByExternalID = function(extstream, externalActivityID, comment) { var dataURL = extstream['url']; var commentsURL = dataURL.replace(/activities$/, 'extactivities/') + externalActivityID + '/comments'; if (!comment.externalID){ comment.externalID = extstream.name + '_' + 'comment' + '_' + String(new Date().getTime()); jive.logger.warn(jive.util.format('No externalID field given when creating new comment. Assigning ID %s', comment.externalID )); } // schedule the comment push return jive.context.scheduler.schedule(jive.constants.tileEventNames.PUSH_COMMENT_TO_JIVE, { 'tileInstance' : extstream, 'comment' : comment, 'commentsURL' : commentsURL } ); }; /** * Fetches all comments associated with a jive activity item. Can be filtered for only Jive originated comments, * or for those originating outside of Jive (external comments). * @param jiveActivity * @param opts commentSourceType can be JIVE or EXTERNAL. * @return {Promise} Promise */ exports.fetchCommentsOnActivity = function(jiveActivity, opts) { if (!(jiveActivity &amp;&amp; jiveActivity.resources &amp;&amp; jiveActivity.resources.comments &amp;&amp; jiveActivity.resources.comments.ref) || !jiveActivity.parent) { return q.reject({ 'err' : 'Error in jive.extstreams.fetchCommentsOnActivity: ' + 'input activity is not a valid Jive object. It is missing the resources.comments.ref field or parent field.'} ); } var commentsURL = jiveActivity.resources.comments.ref; commentsURL += buildQueryString(opts['fieldList'], opts['itemsPerPage'], opts['commentSourceType']); var parentInstanceURL = jiveActivity.parent + '/activities'; return jive.extstreams.findByURL(parentInstanceURL).then( function(extstream) { return jive.context.scheduler.schedule(jive.constants.tileEventNames.GET_PAGINATED_RESULTS, { 'extstream' : extstream, 'commentsURL' : commentsURL } ); }).then(function(response){ if (response.entity &amp;&amp; response.entity.list) { response.entity.list = filterComments(response.entity.list, opts['commentSourceType'], opts['publishedAfter']); } return response; }); }; /** * @param extstream * @param opts * @return {Promise} Promise */ exports.fetchAllCommentsForExtstream = function(extstream, opts) { var dataURL = extstream['url']; var commentsURL = commentsUrlFromDataUrl(dataURL); commentsURL += buildQueryString(opts['fieldList'], opts['itemsPerPage'], opts['commentSourceType']); var promise = jive.context.scheduler.schedule(jive.constants.tileEventNames.GET_PAGINATED_RESULTS, { 'extstream' : extstream, 'commentsURL' : commentsURL } ); return promise.then(function(response) { if (response.entity &amp;&amp; response.entity.list) { response.entity.list = filterComments(response.entity.list, opts['commentSourceType'], opts['publishedAfter']); } return response; }); }; function buildQueryString(optionalFieldList, optionalItemsPerPage, commentSourceType) { var queryStr = ''; if (optionalFieldList || optionalItemsPerPage || (commentSourceType &amp;&amp; commentSourceType.toUpperCase() === 'JIVE')) { queryStr += '?'; var q = false; if (optionalFieldList) { if (optionalFieldList.indexOf('externalID') &lt; 0) { //Must return the externalID to be able to filter properly by comment source optionalFieldList.push('externalID'); } if (optionalFieldList.indexOf('published') &lt; 0) { //Need publish date for filtering optionalFieldList.push('published'); } queryStr += 'fields=' + encodeURIComponent(optionalFieldList.join(',')); q = true; } if (optionalItemsPerPage) { queryStr += (q ? '&amp;' : '') + 'count=' + optionalItemsPerPage; q = true; } if (commentSourceType &amp;&amp; commentSourceType.toUpperCase() === 'JIVE') { queryStr += (q ? '&amp;' : '') + 'filter=omitExternal'; //For efficiency add filter that omits external comments q = true; } } return queryStr; } //Sort of a hack to build the comments URL, because we are not storing the "resources" JSON from a Jive external stream object currently function commentsUrlFromDataUrl(dataURL) { return dataURL.slice(0, dataURL.indexOf('activities')) + 'comments'; } //Helper to filter comments based on whether the externalID field is present function filterComments(list, commentSourceType, publishedAfter) { if (commentSourceType &amp;&amp; commentSourceType.toUpperCase() != 'ALL') { if (commentSourceType.toUpperCase() == 'JIVE') { list = list.filter(function(comment) { return comment.externalID == undefined; }) ; } else if (commentSourceType.toUpperCase() == 'EXTERNAL') { list = list.filter(function(comment) { return comment.externalID != undefined; }) ; } } if (publishedAfter) { list = list.filter(function(comment) { var published = new Date(comment['published']); if (isNaN(published.getTime())) { return true; } return published.getTime() > publishedAfter; }); } return list; }</pre> </article> </section> </div> <div class="clearfix"></div> <footer> <span class="copyright"> Jive Software, Inc </span> <br /> <span class="jsdoc-message"> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-dev</a> on Wed Jan 22 2014 12:29:37 GMT-0800 (PST) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>. </span> </footer> </div> <br clear="both"> </div> </div> <script src="scripts/sunlight.js"></script> <script src="scripts/sunlight.javascript.js"></script> <script src="scripts/sunlight-plugin.doclinks.js"></script> <script src="scripts/sunlight-plugin.linenumbers.js"></script> <script src="scripts/sunlight-plugin.menu.js"></script> <script src="scripts/jquery.min.js"></script> <script src="scripts/jquery.scrollTo.js"></script> <script src="scripts/jquery.localScroll.js"></script> <script src="scripts/bootstrap-dropdown.js"></script> <script src="scripts/toc.js"></script> <script> Sunlight.highlightAll({lineNumbers:true, showMenu: true, enableDoclinks :true}); </script> <script> $( function () { $( "#toc" ).toc( { selectors : "h1,h2,h3,h4", showAndHide : false, scrollTo : 60 } ); $( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" ); $( "#main span[id^='toc']" ).addClass( "toc-shim" ); } ); </script> </body> </html>