hubot-genesys-cloud
Version:
Genesys Cloud adapter for Hubot
185 lines (144 loc) • 7.2 kB
text/coffeescript
Controller = require './framework/Controller.coffee'
module.exports = class RoomController extends Controller
:
createRoom: (callback) -> #(err,roomJid) ->
.debug 'room', 'createRoom'
room = "#{uuid.generate()}-z@conference.#{@realtime.jid.domain}"
return callback null, room
inviteToRoom: (roomJid, userJid) ->
.debug 'room', 'inviteToRoom', roomJid, userJid
stanza = new ltx.Element 'message', from:.jid.toString(), to:userJid
stanza.c 'x', xmlns:'jabber:x:conference', jid:roomJid
.send stanza
joinRoom: _.throttle (roomJid, callback) ->
unless roomJid then return console?.error('joinRoom called with no jid.')
roomJid = roomJid.toString().split('@')[0]+'@conference.'+.jid.domain
.debug 'room', 'joining', roomJid
roomJid
'join:'+roomJid.split('@')[0], =>
# .sendMessage roomJid, 'Hello!'
if typeof callback is 'function' then callback()
, 20
leaveRoom: (roomJid) ->
unless roomJid then return console?.error('leaveRoom called with no jid.')
roomJid = roomJid.toString().split('@')[0]+'@conference.'+.jid.domain
.debug 'room', 'joining', roomJid
stanza = new ltx.Element 'presence', from:.jid.toString(), to:"#{roomJid}/#{@_nick}", id:uuid.generate(), type:'unavailable'
delete [roomJid]
.send stanza
._clearMessages roomJid
setRoomSubject: (roomJid, subject, callback = ->) ->
roomJid = roomJid.toString().split('@')[0]+'@conference.'+.jid.domain
stanza = new ltx.Element 'message', from:.jid.toString(), to:roomJid, type:'groupchat'
stanza.c('subject').t(subject)
.send stanza
setTimeout (=>
'subjectChange', {room:roomJid, subject}
callback()
), 500
getRoomInfo: (roomJid, callback) ->
roomJid = roomJid.toString().split('@')[0]+'@conference.'+.jid.domain
id = uuid.generate()
stanza = new ltx.Element 'iq', id:id, to:roomJid, type:'get'
stanza.c('query', xmlns:'http://jabber.org/protocol/disco#info')
.send stanza
'roomInfo:'+id, (obj) =>
return callback null, obj
setAttributes: (roomJid, attributes) ->
roomJid = roomJid.toString().split('@')[0]+'@conference.'+.jid.domain
stanza = new ltx.Element 'message', to:roomJid, type:'groupchat'
[roomJid] ?= {}
if _.isEqual [roomJid], attributes then return
[roomJid] = attributes
stanza.c('attributes').t(JSON.stringify(attributes))
.send stanza
: ['invite', 'subjectChange', 'data', 'occupantChange']
:
handleInvite: (stanza) -> stanza.is('message') and stanza.getChild('x', 'jabber:x:conference')?.attrs.jid?
handleSubject: (stanza) -> stanza.is('message') and stanza.getChild('subject')?.text().length > 0
handleData: (stanza) -> stanza.is('message') and stanza.attrs.type is 'groupchat' and stanza.getChild('data')?.text().length > 0
handleInfo: (stanza) -> stanza.is('iq') and stanza.attrs.type is 'result' and stanza.attrs.from?.match(//) and stanza.getChild('query', 'http://jabber.org/protocol/disco#info')?
handlePresence: (stanza) -> stanza.is('presence') and stanza.attrs.from isnt .jid?.bare().toString() and stanza.attrs.type in [undefined, 'unavailable'] and stanza.attrs.from.match(//)
constructor: ->
super
= {}
= {}
= {}
= {}
.on 'connect', =>
oldRooms =
= {}
= {}
= {}
for jid of oldRooms then jid
= .jid.bare().toString().replace('@', '#')
handleInfo: (stanza) ->
query = stanza.getChild('query')
attributes = {}
for child in query.getChild('attributes')?.children
if child.name
attributes[child.name] = child.text()
participants = []
for child in query.getChild('participants')?.getChildren('participant')
participants.push child.text()
occupants = []
for child in query.getChild('occupants')?.getChildren('occupant')
occupants.push child.text()
subject = query.getChild('subject')?.text()
'roomInfo:'+stanza.attrs.id, {subject, occupants, participants, attributes}
handleSubject: (stanza) ->
out = {room:new JID(stanza.attrs.from).bare().toString(), from:stanza.attrs.ofrom, subject:stanza.getChild('subject').text()}
.debug 'room', 'subjectChange', out
.debug 'subject', 'subjectChange', out
'subjectChange', out
handleInvite: (stanza) ->
.debug 'room', 'got invite', stanza.toString()
room = if stanza.getChild('x','http://jabber.org/protocol/muc#user')?.getChild('invite')? then stanza.attrs.to else stanza.getChild('x', 'jabber:x:conference').attrs.jid
room
.sendMessage room, 'Hello!'
handleData: (stanza) ->
.debug 'data', 'handleData', stanza.toString()
data = JSON.parse stanza.getChild('data').text()
.debug 'data', 'handleData', 'data', data
'data', {data, room:stanza.attrs.from, from:stanza.attrs.ofrom}
handlePresence: (stanza) ->
[from] = stanza.attrs.ofrom.split('/')
[room] = stanza.attrs.from.split('/')
[to] = stanza.attrs.to.split('/')
[room] or= {}
[room] or= {}
if stanza.attrs.type is 'unavailable'
type = 'leave'
unless [room][from] then return
delete [room][from]
delete [room][from]
else
type = 'join'
if [room][from] then return
[room][from] = true
[room][from] = true
if from is .jid.bare().toString()
'join:'+room.split('@')[0]
.debug 'room', 'occupantChange', {room, from, type}
'occupantChange', {room, from, type}
if from is to
stanza
_handleJoinRoomConfirmation: (stanza)->
[room] = stanza.attrs.from.split('/')
debug 'room', 'join room confirmation'
.getActiveChats (err, actives) =>
if actives and !actives[room]
debug 'room', "I'm not even supposed to be here today!", room, actives
.leaveRoom room
else
if [room] and [room]
for from of [room]
unless [room][from]
'occupantChange', {room, from, type: 'leave'}
[room] = [room]
_joinRoom: (roomJid) ->
if [roomJid] then return
[roomJid] = true
stanza = new ltx.Element 'presence', from:.jid.toString(), to:"#{roomJid}/#{@_nick}", id:uuid.generate()
stanza.c 'x', xmlns:'http://jabber.org/protocol/muc'
.send stanza