From aacb0e4706ba722383eddb1dec08ccd8df83369c Mon Sep 17 00:00:00 2001 From: Aaron Ogle Date: Tue, 25 Jul 2017 15:17:01 -0500 Subject: [PATCH] Add method to check if method exists --- src/rocketchat.coffee | 14 +++++++++----- src/rocketchat_driver.coffee | 27 ++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/rocketchat.coffee b/src/rocketchat.coffee index 40dacc9f4..bab16e833 100644 --- a/src/rocketchat.coffee +++ b/src/rocketchat.coffee @@ -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 @@ -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(() => diff --git a/src/rocketchat_driver.coffee b/src/rocketchat_driver.coffee index 177bb0558..c24ff0d50 100644 --- a/src/rocketchat_driver.coffee +++ b/src/rocketchat_driver.coffee @@ -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 @@ -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 @@ -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}"