Skip to content

Commit

Permalink
Merge pull request #29 from gitaboard/feature/support-for-multiple-rooms
Browse files Browse the repository at this point in the history
Add Support For Multiple Rooms
  • Loading branch information
geekgonecrazy committed Aug 28, 2015
2 parents cca5f73 + 9760baa commit fb96cb9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
"email": "support@rocket.chat"
},
"devDependencies": {
"coffee-script": "~1.7.1",
"hubot": "~2.11"
"coffee-script": "~1.7.1"
},
"main": "./src/rocketchat",
"dependencies": {
"asteroid": "^0.6.1"
"asteroid": "^0.6.1",
"parent-require": "^1.0.0"
}
}
42 changes: 25 additions & 17 deletions src/rocketchat.coffee
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
{Adapter, TextMessage} = require "../../hubot"
try
{Robot,Adapter,TextMessage,User} = require 'hubot'
catch
prequire = require('parent-require')
{Robot,Adapter,TextMessage,User} = prequire 'hubot'
Chatdriver = require './rocketchat_driver'

RocketChatURL = process.env.ROCKETCHAT_URL or "192.168.88.107:3000"
RocketChatRoom = process.env.ROCKETCHAT_ROOM or "thhPNzZhi2MHd23pZ"
RocketChatURL = process.env.ROCKETCHAT_URL or "rocketchat.vm:80"
RocketChatRoom = process.env.ROCKETCHAT_ROOM or "u6rMNkRMwzxW7AuGu,GENERAL"
RocketChatUser = process.env.ROCKETCHAT_USER or "hubot"
RocketChatPassword = process.env.ROCKETCHAT_PASSWORD or "abc123"
RocketChatPassword = process.env.ROCKETCHAT_PASSWORD or "password"

class RocketChatBotAdapter extends Adapter

Expand All @@ -18,21 +22,25 @@ class RocketChatBotAdapter extends Adapter
@robot.logger.info "running rocketchat"
@lastts = new Date()
@chatdriver = new Chatdriver RocketChatURL, @robot.logger
rooms = RocketChatRoom.split(',')
@robot.logger.info "first room #{rooms[0]}"
@chatdriver.login(RocketChatUser, RocketChatPassword).then (userid) =>
@robot.logger.info "logged in"
@chatdriver.joinRoom userid, RocketChatUser, RocketChatRoom
@chatdriver.prepMeteorSubscriptions({uid: userid, roomid: RocketChatRoom}).then (arg) =>
@robot.logger.info "subscription ready"
@chatdriver.setupReactiveMessageList (newmsg) =>
if newmsg.u._id isnt userid
curts = new Date(newmsg.ts.$date)
@robot.logger.info "message receive callback id " + newmsg._id + " ts " + curts
@robot.logger.info " text is " + newmsg.msg
if curts > @lastts
@lastts = curts
user = @robot.brain.userForId newmsg.u._id, name: newmsg.u.username, room: newmsg.rid
text = new TextMessage(user, newmsg.msg, newmsg._id)
@robot.receive text
for room in rooms
do(room) =>
@chatdriver.joinRoom userid, RocketChatUser, room
@chatdriver.prepMeteorSubscriptions({uid: userid, roomid: room}).then (arg) =>
@robot.logger.info "subscription ready"
@chatdriver.setupReactiveMessageList (newmsg) =>
if newmsg.u._id isnt userid
curts = new Date(newmsg.ts.$date)
@robot.logger.info "message receive callback id " + newmsg._id + " ts " + curts
@robot.logger.info " text is " + newmsg.msg
if curts > @lastts
@lastts = curts
user = @robot.brain.userForId newmsg.u._id, name: newmsg.u.username, room: newmsg.rid
text = new TextMessage(user, newmsg.msg, newmsg._id)
@robot.receive text
@emit 'connected'

send: (envelope, strings...) =>
Expand Down
2 changes: 1 addition & 1 deletion src/rocketchat_driver.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class RocketChatDriver
# return promise
@logger.info "prepare meteor subscriptions"
msgsub = @asteroid.subscribe _msgsubtopic, data.roomid, _msgsublimit
@logger.info "data.roomid == #{data.roomid}"
return msgsub.ready

setupReactiveMessageList: (receiveMessageCallback) =>
Expand All @@ -50,4 +51,3 @@ class RocketChatDriver
receiveMessageCallback changedMsg

module.exports = RocketChatDriver

0 comments on commit fb96cb9

Please sign in to comment.