Skip to content

Commit

Permalink
get room name
Browse files Browse the repository at this point in the history
  • Loading branch information
Qi Yin committed May 26, 2017
1 parent aaf71bf commit f84f504
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
13 changes: 9 additions & 4 deletions src/rocketchat.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,12 @@ class RocketChatBotAdapter extends Adapter
@lastts = curts

user = @robot.brain.userForId newmsg.u._id, name: newmsg.u.username, alias: newmsg.alias
user.room = newmsg.rid

if newmsg.t is 'uj'
@robot.receive new EnterMessage user, null, newmsg._id
else
@chatdriver.getRommName(newmsg.rid).then((roomName)=>
user.room = roomName
if newmsg.t is 'uj'
@robot.receive new EnterMessage user, null, newmsg._id
else
# check for the presence of attachments in the message
if newmsg.file? and newmsg.attachments.length
attachment = newmsg.attachments[0]
Expand All @@ -184,6 +185,10 @@ class RocketChatBotAdapter extends Adapter
message.text = "#{ @robot.name } #{ message.text }"
@robot.receive message
@robot.logger.info "Message sent to hubot brain."
).catch((roomErr) =>
@robot.logger.error "Unable to get room name: #{JSON.stringify(roomErr)} Reason: #{roomErr.reason}"
throw roomErr
)
)
.then(() =>
@emit 'connected'
Expand Down
32 changes: 18 additions & 14 deletions src/rocketchat_driver.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@ LRU = require('lru-cache')
# TODO: need to grab these values from process.env[]

_msgsubtopic = 'stream-room-messages' # 'messages'
_msgsublimit = 10 # this is not actually used right now
_msgsublimit = 10 # this is not actually used right now
_messageCollection = 'stream-room-messages'

# room id cache
_roomCacheSize = parseInt(process.env.ROOM_ID_CACHE_SIZE) || 10
_directMessageRoomCacheSize = parseInt(process.env.DM_ROOM_ID_CACHE_SIZE) || 100
_cacheMaxAge = parseInt(process.env.ROOM_ID_CACHE_MAX_AGE) || 300
_roomIdCache = LRU( max: _roomCacheSize, maxAge: 1000 * _cacheMaxAge )
_directMessageRoomIdCache = LRU( max: _directMessageRoomCacheSize, maxAge: 1000 * _cacheMaxAge )
_roomIdCache = LRU(max: _roomCacheSize, maxAge: 1000 * _cacheMaxAge)
_directMessageRoomIdCache = LRU(max: _directMessageRoomCacheSize, maxAge: 1000 * _cacheMaxAge)
_roomNameCache = LRU(max: _roomCacheSize, maxAge: 1000 * _cacheMaxAge)

# driver specific to Rocketchat hubot integration
# plugs into generic rocketchatbotadapter

class RocketChatDriver
constructor: (url, ssl, @logger, cb) ->
if ssl is 'true'
if ssl is 'true'
sslenable = true
else
sslenable = false
Expand All @@ -39,6 +40,9 @@ class RocketChatDriver
getRoomId: (room) =>
@tryCache _roomIdCache, 'getRoomIdByNameOrId', room, 'Room ID'

getRommName: (room) =>
@tryCache _roomNameCache, 'getRoomNameById', room, 'Room Name'

getDirectMessageRoomId: (username) =>
@tryCache _directMessageRoomIdCache, 'createDirectMessage', username, 'DM Room ID'

Expand Down Expand Up @@ -80,9 +84,9 @@ class RocketChatDriver
sendMessageByRoomId: (content, roomid) =>
message = @prepareMessage content, roomid
@asteroid.call('sendMessage', message)
.then (result)->
.then (result)->
@logger.debug('[sendMessage] Success:', result)
.catch (error) ->
.catch (error) ->
@logger.error('[sendMessage] Error:', error)

customMessage: (message) =>
Expand Down Expand Up @@ -111,10 +115,10 @@ class RocketChatDriver
return @asteroid.loginWithPassword username, password

prepMeteorSubscriptions: (data) =>
# use data to cater for param differences - until we learn more
# data.uid
# data.roomid
# return promise
# use data to cater for param differences - until we learn more
# data.uid
# data.roomid
# return promise
@logger.info "Preparing Meteor Subscriptions.."
msgsub = @asteroid.subscribe _msgsubtopic, data.roomid, true
@logger.info "Subscribing to Room: #{data.roomid}"
Expand All @@ -126,12 +130,12 @@ class RocketChatDriver

rQ = @messages.reactiveQuery {}
rQ.on "change", (id) =>
# awkward syntax due to asteroid limitations
# - it ain't no minimongo cursor
# @logger.info "Change received on ID " + id
# awkward syntax due to asteroid limitations
# - it ain't no minimongo cursor
# @logger.info "Change received on ID " + id
changedMsgQuery = @messages.reactiveQuery {"_id": id}
if changedMsgQuery.result && changedMsgQuery.result.length > 0
# console.log('result:', JSON.stringify(changedMsgQuery.result, null, 2))
# console.log('result:', JSON.stringify(changedMsgQuery.result, null, 2))
changedMsg = changedMsgQuery.result[0]
# console.log('changed:', JSON.stringify(changedMsg, null, 2));
if changedMsg.args?
Expand Down

0 comments on commit f84f504

Please sign in to comment.