UNPKG

node-jivesbs-rest

Version:

REST API and Feed Wrapper for Jive SBS 4.5.

353 lines (239 loc) 8.55 kB
class JiveContainer # Requre Username and Password constructor: (@config)-> if !@config.username || !@config.password throw new Error("HubContainer must have username and password in config object") @config.url = @config.proto + @config.url @request = require 'request' @xml2js = require 'xml2js' getGroups: (cb)-> groupsUrl = "/rpc/rest/socialGroupService/socialGroups" groupsFn = (err, response, body)=> if !@__checkResponseBody(body, cb) return if err cb(err, null) return parseString = @xml2js.parseString parseString body, (err, result)-> if err cb(err, null) return if !result['ns2:getSocialGroupsResponse'] cb("no valid response", null) return cb(err, result['ns2:getSocialGroupsResponse']['return'] || null) return @request.get(@config.url + groupsUrl, groupsFn).auth(@config.username, @config.password, true) return @ getProjects: (cb)-> projectsUrl = "/rpc/rest/projectService/projects" projectsFn = (err, response, body)=> if !@__checkResponseBody(body, cb) return if err cb(err, null) return parseString = @xml2js.parseString parseString body, (err, result)-> if err cb(err, null) return if !result['ns2:getProjectsResponse'] cb("no valid response", null) return cb(err, result['ns2:getProjectsResponse']['return'] || null) return @request.get(@config.url + projectsUrl, projectsFn).auth(@config.username, @config.password, true) return @ getPersonalDocuments: (person, cb)-> if !person || !cb console.log "getPersonalDocuments needs (person, cb)" return rssUrl = "/groups/feeds/documents?targetUser=#{person}&numItems=100000" groupDocumentsFn = (err, response, body)=> if err cb(err, null) return if !@__checkResponseBody(body, cb) return parseString = @xml2js.parseString parseString body, (err, result)-> if err cb(err, null) return if !result.rss cb("no valid response", null) return if !result.rss.channel[0].item || result.rss.channel[0].item.length <= 0 cb(null, []) return cb(null, result.rss.channel[0].item) return @request.get(@config.url + rssUrl, groupDocumentsFn).auth(@config.username, @config.password, true) return @ getGroupDocuments: (groupId, cb)-> if !groupId || ! cb cb("No group Id or callback provided for getting documents", null) return @ rssUrl = "/community/feeds/documents?containerType=700&container=#{groupId}&numItems=10000" groupDocumentsFn = (err, response, body)=> if err cb(err, null) return if !@__checkResponseBody(body, cb) return parseString = @xml2js.parseString parseString body, (err, result)-> if err cb(err, null) return if !result.rss cb("no valid response", null) return if !result.rss.channel[0].item || result.rss.channel[0].item.length <= 0 cb(null, []) return cb(null, result.rss.channel[0].item) return @request.get(@config.url + rssUrl, groupDocumentsFn).auth(@config.username, @config.password, true) return @ getProjectDocuments: (projectId, cb)-> if !projectId || ! cb cb("No project Id or callback provided for getting documents", null) return @ rssUrl = "/community/feeds/documents?containerType=600&container=#{projectId}&numItems=10000" groupDocumentsFn = (err, response, body)=> if err cb(err, null) return if !@__checkResponseBody(body, cb) return parseString = @xml2js.parseString parseString body, (err, result)-> if err cb(err, null) return if !result.rss cb("no valid response", null) return if !result.rss.channel[0].item || result.rss.channel[0].item.length <= 0 cb(null, []) return cb(null, result.rss.channel[0].item) return @request.get(@config.url + rssUrl, groupDocumentsFn).auth(@config.username, @config.password, true) return @ getCommunities: (cb)-> communitiesUrl = "/rpc/rest/communityService/communities" communitiesFn = (err, response, body)=> if err cb(err, null) return if !@__checkResponseBody(body, cb) return parseString = @xml2js.parseString parseString body, (err, result)-> if err cb(err, null) return if !result['ns2:getRecursiveCommunitiesResponse'] cb("no valid response", null) return cb(null, result['ns2:getRecursiveCommunitiesResponse']['return']) return @request.get(@config.url + communitiesUrl, communitiesFn).auth(@config.username, @config.password, true) return @ getCommunityDocuments: (communityId, cb)-> if !communityId || ! cb cb("No community ID or callback provided for getting documents", null) return @ documentsUrl = "/rpc/rest/documentService/documentsByCommunity/" + communityId self = @ documentsFn = (err, response, body)=> if err cb(err, null) return if !@__checkResponseBody(body, cb) return if body.indexOf("javax.xml.bind.MarshalException") is -1 parser = new self.xml2js.Parser() parser.parseString body, (err, result)-> if err cb(err, null) return if !result['ns2:getDocumentsByCommunityResponse'] cb("no valid response", null) return if result['ns2:getDocumentsByCommunityResponse']['return'].length <= 0 cb(null, []) return cb(null, result['ns2:getDocumentsByCommunityResponse']['return'] || []) return else cb(null, []) return @request.get(@config.url + documentsUrl, documentsFn).auth(@config.username, @config.password, true) return @ getAttachments: (documentId, cb)-> if !documentId || !cb cb("No documentId or callback provided", null) return @ attachmentsUrl = "/rpc/rest/documentService/attachments/DOC-" + documentId self = @ attachmentsFn = (err, response, body)=> if err cb(err, null) return if !@__checkResponseBody(body, cb) return parser = new self.xml2js.Parser() parser.parseString body, (err, result)-> if err cb(err, null) return if !result['ns2:getAttachmentsByDocumentIDResponse'] cb("no valid response", null) return if !result['ns2:getAttachmentsByDocumentIDResponse']['return'] || result['ns2:getAttachmentsByDocumentIDResponse']['return'].length <= 0 cb(null, []) return cb(null, result['ns2:getAttachmentsByDocumentIDResponse']['return']) return @request.get(@config.url + attachmentsUrl, attachmentsFn).auth(@config.username, @config.password, true) return @ getBinaryDocument: (documentId, cb)-> if !documentId || !cb cb("No document ID or cb provided", null) return binaryUrl = "/rpc/rest/documentService/binaryDocument/DOC-" + documentId self = @ binaryFn = (err, response, body)=> if err cb(err, null) return if !@__checkResponseBody(body, cb) return parser = new self.xml2js.Parser() parser.parseString body, (err, result)-> if err cb(err, null) return if !result || !result['ns2:getBinaryDocumentContentResponse'] cb("no valid response", null) return cb(null, result['ns2:getBinaryDocumentContentResponse']['return'][0] || []) return try @request.get(@config.url + binaryUrl, binaryFn).auth(@config.username, @config.password, true) catch e console.log "Request timed out!\n " + e #try again @request.get(@config.url + binaryUrl, binaryFn).auth(@config.username, @config.password, true) return @ __checkResponseBody: (body, cb)-> if body.indexOf("Bad credentials") >= 0 cb(body, null) return false return true module.exports = JiveContainer