hubot-genesys-cloud
Version:
Genesys Cloud adapter for Hubot
145 lines (107 loc) • 5.2 kB
text/coffeescript
Controller = require './framework/Controller.coffee'
module.exports = class ActiveChatController extends Controller
:
setActive: (jid) ->
if not
.push ActiveChatController.expose.setActive.bind @, jid
return
if [jid]?.active is true then return
.debug 'active', 'setting', jid, 'as active'
stanza = new ltx.Element('iq', type:'set', to:"pubsub.#{@realtime.jid.domain}", from:.jid.toString(), id:'activeChatsSet')
stanza.c('pubsub', xmlns:'http://jabber.org/protocol/pubsub')
.c('publish', node:'orgspan:activeChats')
.c('item', id:jid, type:(if jid.match(//) then 'room' else 'person'))
.send stanza
setInactive: (jid) ->
if not
.push ActiveChatController.expose.setInactive.bind @, jid
return
if [jid]?.active is false then return
.debug 'active', 'setting', jid, 'as inactive'
stanza = new ltx.Element('iq', type:'set', to:"pubsub.#{@realtime.jid.domain}", from:.jid.toString(), id:'activeChatsSet')
stanza.c('pubsub', xmlns:'http://jabber.org/protocol/pubsub')
.c('retract', node:'orgspan:activeChats')
.c('item', id:jid, type:(if jid.match(//) then 'room' else 'person'))
.send stanza
getActiveChats: (callback) ->
.debug 'active', 'getActiveChats called'
if then return callback null,
.features['orgspan:activeChats'] = true
.debug 'active', 'waiting for _actives'
'actives', =>
.debug 'active', 'returning _actives'
callback null,
: ['activeChat']
:
ignoredResult: (stanza) -> stanza.is('iq') and stanza.attrs.type is 'result' and stanza.attrs.id in ['activeChatsSet', 'activeChatsSub']
activeChatsResult: (stanza) -> stanza.is('iq') and stanza.attrs.type is 'result' and stanza.getChildByAttr('node', "orgspan:activeChats", null, true)? and stanza.attrs.id isnt 'activeChatsSub'
activeMessage: (stanza) -> stanza.is('message') and stanza.getChild('event', "http://jabber.org/protocol/pubsub#event")?.getChild('items')?.attrs.node is "orgspan:activeChats"
constructor: ->
super
= null
= null
= []
.on 'rosterItem', ({type}) => type
.on 'disconnect', =>
.on 'connect', =>
ignoredResult: ->
onConnect: ->
.getActiveChats (error, actives) =>
if error then return logError 'couldnt get active chats', error
debug '********** actives', actives
for jid, obj of actives
if obj.type is 'room'
.joinRoom jid
onDisconnect: ->
= null
= []
activeChatsResult: (stanza) ->
items = stanza.getChildByAttr('node', "orgspan:activeChats", null, true)
.debug 'active', 'got activeChats result', items.toString()
oldActives = or {}
= {}
for child in items.getChildren('item')
jid = child.attrs.id
active = true
type = child.attrs.type
[child.attrs.id] = {active, type}
'activeChat', {jid, active, type}
for jid, obj of oldActives
unless [jid]
active = false
type = obj.type
'activeChat', {jid, active, type}
work =
= []
work.forEach (update) ->
update()
'actives'
activeMessage: (stanza) ->
.debug 'active', 'activeMessage', stanza.toString()
items = stanza.getChild('event', "http://jabber.org/protocol/pubsub#event")?.getChild('items')
for child in items.children
unless child.name in ['item', 'retract'] then continue
{id, type} = child.attrs
if child.is 'item'
active = true
else if child.is 'retract'
active = false
.debug 'active', 'set', {id, active, type}, child.toString()
?[child.attrs.id] = {active, type}
'activeChat', {jid:id, active, type}
_sendRequest: ->
.debug 'active', 'active _sendRequest', discovered:.discovered
unless .connected then return .once 'connected', =>
if .features['orgspan:actives'] and !.discovered then return
.getRoster =>
.getGroups =>
.debug 'active', 'sending activeChats request'
iq = new ltx.Element 'iq', type:'set', from:.jid.toString(), to:"pubsub.#{@realtime.jid.domain}", id:'activeChatsSub'
iq.c('pubsub', xmlns:'http://jabber.org/protocol/pubsub')
.c('subscribe', node:'orgspan:activeChats', jid:.jid.toString())
.send iq
iq = new ltx.Element 'iq', from:.jid.toString(), type:'get'
iq.c('pubsub', xmlns:'http://jabber.org/protocol/pubsub')
.c 'items', node:'orgspan:activeChats'
.send iq