Skip to content

Commit

Permalink
Add method to check if method exists
Browse files Browse the repository at this point in the history
  • Loading branch information
geekgonecrazy committed Jul 25, 2017
1 parent a7bb24b commit aacb0e4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/rocketchat.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,16 @@ class RocketChatBotAdapter extends Adapter

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

@chatdriver.getRoomName(newmsg.rid).then((roomName)=>
user.room = roomName
@chatdriver.checkMethodExists("getRoomNameById").then(() =>
return @chatdriver.getRoomName(newmsg.rid).then((roomName) =>
@robot.logger.info("setting roomName: #{roomName}")
user.room = roomName
)
).catch((err) =>
return Q()
).then(() =>
user.roomID = newmsg.rid

if newmsg.t is 'uj'
@robot.receive new EnterMessage user, null, newmsg._id
else
Expand Down Expand Up @@ -193,9 +200,6 @@ 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(() =>
Expand Down
27 changes: 26 additions & 1 deletion src/rocketchat_driver.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ _msgsubtopic = 'stream-room-messages' # 'messages'
_msgsublimit = 10 # this is not actually used right now
_messageCollection = 'stream-room-messages'

_methodExists = {}

# room id cache
_roomCacheSize = parseInt(process.env.ROOM_ID_CACHE_SIZE) || 10
_directMessageRoomCacheSize = parseInt(process.env.DM_ROOM_ID_CACHE_SIZE) || 100
Expand Down Expand Up @@ -46,6 +48,28 @@ class RocketChatDriver
getDirectMessageRoomId: (username) =>
@tryCache _directMessageRoomIdCache, 'createDirectMessage', username, 'DM Room ID'

checkMethodExists: (method) =>
if !_methodExists[method]?
@logger.info "Checking to see if method: #{method} exists"
r = @asteroid.call(method, "")
r.result.then((res) =>
_methodExists[method] = true
return Q()
).catch((err) =>
if err.error == 404
_methodExists[method] = false
@logger.info "Method: #{method} does not exist"
return Q.reject("Method: #{method} does not exist")
else
_methodExists[method] = true
return Q()
)
else
if _methodExists[method]
return Q()
else
return Q.reject()

tryCache: (cacheArray, method, key, name) =>
name ?= method
cached = cacheArray.get key
Expand All @@ -55,9 +79,10 @@ class RocketChatDriver
else
@logger.info "Looking up #{name} for: #{key}"
r = @asteroid.call method, key
return r.result.then (res) =>
return r.result.then((res) =>
cacheArray.set key, res
return Q(res)
)

joinRoom: (userid, uname, roomid, cb) =>
@logger.info "Joining Room: #{roomid}"
Expand Down

0 comments on commit aacb0e4

Please sign in to comment.