Skip to content

Commit 0b1fa1c

Browse files
authored
Merge pull request #229 from RocketChat/add-method-check
Add method to check if Rocket.Chat method exists
2 parents a7bb24b + aacb0e4 commit 0b1fa1c

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/rocketchat.coffee

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,16 @@ class RocketChatBotAdapter extends Adapter
163163

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

166-
@chatdriver.getRoomName(newmsg.rid).then((roomName)=>
167-
user.room = roomName
166+
@chatdriver.checkMethodExists("getRoomNameById").then(() =>
167+
return @chatdriver.getRoomName(newmsg.rid).then((roomName) =>
168+
@robot.logger.info("setting roomName: #{roomName}")
169+
user.room = roomName
170+
)
171+
).catch((err) =>
172+
return Q()
173+
).then(() =>
168174
user.roomID = newmsg.rid
175+
169176
if newmsg.t is 'uj'
170177
@robot.receive new EnterMessage user, null, newmsg._id
171178
else
@@ -193,9 +200,6 @@ class RocketChatBotAdapter extends Adapter
193200
message.text = "#{ @robot.name } #{ message.text }"
194201
@robot.receive message
195202
@robot.logger.info "Message sent to hubot brain."
196-
).catch((roomErr) =>
197-
@robot.logger.error "Unable to get room name: #{JSON.stringify(roomErr)} Reason: #{roomErr.reason}"
198-
throw roomErr
199203
)
200204
)
201205
.then(() =>

src/rocketchat_driver.coffee

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ _msgsubtopic = 'stream-room-messages' # 'messages'
1111
_msgsublimit = 10 # this is not actually used right now
1212
_messageCollection = 'stream-room-messages'
1313

14+
_methodExists = {}
15+
1416
# room id cache
1517
_roomCacheSize = parseInt(process.env.ROOM_ID_CACHE_SIZE) || 10
1618
_directMessageRoomCacheSize = parseInt(process.env.DM_ROOM_ID_CACHE_SIZE) || 100
@@ -46,6 +48,28 @@ class RocketChatDriver
4648
getDirectMessageRoomId: (username) =>
4749
@tryCache _directMessageRoomIdCache, 'createDirectMessage', username, 'DM Room ID'
4850

51+
checkMethodExists: (method) =>
52+
if !_methodExists[method]?
53+
@logger.info "Checking to see if method: #{method} exists"
54+
r = @asteroid.call(method, "")
55+
r.result.then((res) =>
56+
_methodExists[method] = true
57+
return Q()
58+
).catch((err) =>
59+
if err.error == 404
60+
_methodExists[method] = false
61+
@logger.info "Method: #{method} does not exist"
62+
return Q.reject("Method: #{method} does not exist")
63+
else
64+
_methodExists[method] = true
65+
return Q()
66+
)
67+
else
68+
if _methodExists[method]
69+
return Q()
70+
else
71+
return Q.reject()
72+
4973
tryCache: (cacheArray, method, key, name) =>
5074
name ?= method
5175
cached = cacheArray.get key
@@ -55,9 +79,10 @@ class RocketChatDriver
5579
else
5680
@logger.info "Looking up #{name} for: #{key}"
5781
r = @asteroid.call method, key
58-
return r.result.then (res) =>
82+
return r.result.then((res) =>
5983
cacheArray.set key, res
6084
return Q(res)
85+
)
6186

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

0 commit comments

Comments
 (0)