Skip to content

Commit

Permalink
Merge pull request #513 from aoberoi/feat-reliable-enter-and-leave
Browse files Browse the repository at this point in the history
swaps out rtm message subtypes for discreet messages for enter and leave
  • Loading branch information
aoberoi authored Jul 17, 2018
2 parents 407a209 + 23ed052 commit 55d41be
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
24 changes: 13 additions & 11 deletions src/bot.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class SlackBot extends Adapter
@client.loadUsers @usersLoaded
else
@isLoaded = true

# Brain will emit 'loaded' the first time it connects to its storage and then again each time a key is set
@robot.brain.on "loaded", () =>
if not @brainIsLoaded
Expand Down Expand Up @@ -239,16 +239,6 @@ class SlackBot extends Adapter
@receive message
)

# NOTE: channel_join should be replaced with a member_joined_channel event
when "channel_join", "group_join"
@robot.logger.debug "Received enter message for user: #{user.id}, joining: #{channel}"
@receive new EnterMessage user

# NOTE: channel_leave should be replaced with a member_left_channel event
when "channel_leave", "group_leave"
@robot.logger.debug "Received leave message for user: #{user.id}, leaving: #{channel}"
@receive new LeaveMessage user

when "channel_topic", "group_topic"
@robot.logger.debug "Received topic change message in conversation: #{channel}, new topic: #{event.topic}, set by: #{user.id}"
@receive new TopicMessage user, event.topic, event.ts
Expand All @@ -260,6 +250,18 @@ class SlackBot extends Adapter
@receive message
)

else if event.type is "member_joined_channel"
# this event type always has a channel
user.room = channel
@robot.logger.debug "Received enter message for user: #{user.id}, joining: #{channel}"
@receive new EnterMessage user

else if event.type is "member_left_channel"
# this event type always has a channel
user.room = channel
@robot.logger.debug "Received leave message for user: #{user.id}, joining: #{channel}"
@receive new LeaveMessage user

else if event.type is "reaction_added" or event.type is "reaction_removed"

# Once again Hubot expects all user objects to have a room property that is used in the envelope for the message
Expand Down
2 changes: 2 additions & 0 deletions src/client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class SlackClient
@rtm.on "reaction_added", @eventWrapper, this
@rtm.on "reaction_removed", @eventWrapper, this
@rtm.on "presence_change", @eventWrapper, this
@rtm.on "member_joined_channel", @eventWrapper, this
@rtm.on "member_left_channel", @eventWrapper, this
@rtm.on "user_change", @updateUserInBrain, this
@eventHandler = undefined

Expand Down
22 changes: 6 additions & 16 deletions test/bot.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ describe 'Disable Sync', ->
@slackbot.options.disableUserSync = true
@slackbot.run()
@slackbot.robot.brain.data.users.should.be.empty()

# Test moved to fetchUsers() in client.coffee because of change in code logic
#it 'Should still sync interacting users when disabled'

describe 'Send Messages', ->

it 'Should send a message', ->
Expand Down Expand Up @@ -207,13 +207,13 @@ describe 'Handling incoming messages', ->
@slackbot.eventHandler messageData
return

it 'Should handle channel_join events as envisioned', ->
@slackbot.eventHandler {type: 'message', subtype: 'channel_join', user: @stubs.user, channel: @stubs.channel.id}
it 'Should handle member_joined_channel events as envisioned', ->
@slackbot.eventHandler {type: 'member_joined_channel', user: @stubs.user, channel: @stubs.channel.id}
should.equal (@stubs._received instanceof EnterMessage), true
@stubs._received.user.id.should.equal @stubs.user.id

it 'Should handle channel_leave events as envisioned', ->
@slackbot.eventHandler {type: 'message', subtype: 'channel_leave', user: @stubs.user, channel: @stubs.channel.id}
it 'Should handle member_left_channel events as envisioned', ->
@slackbot.eventHandler {type: 'member_left_channel', user: @stubs.user, channel: @stubs.channel.id}
should.equal (@stubs._received instanceof LeaveMessage), true
@stubs._received.user.id.should.equal @stubs.user.id

Expand All @@ -222,16 +222,6 @@ describe 'Handling incoming messages', ->
should.equal (@stubs._received instanceof TopicMessage), true
@stubs._received.user.id.should.equal @stubs.user.id

it 'Should handle group_join events as envisioned', ->
@slackbot.eventHandler {type: 'message', subtype: 'group_join', user: @stubs.user, channel: @stubs.channel.id}
should.equal (@stubs._received instanceof EnterMessage), true
@stubs._received.user.id.should.equal @stubs.user.id

it 'Should handle group_leave events as envisioned', ->
@slackbot.eventHandler {type: 'message', subtype: 'group_leave', user: @stubs.user, channel: @stubs.channel.id}
should.equal (@stubs._received instanceof LeaveMessage), true
@stubs._received.user.id.should.equal @stubs.user.id

it 'Should handle group_topic events as envisioned', ->
@slackbot.eventHandler {type: 'message', subtype: 'group_topic', user: @stubs.user, channel: @stubs.channel.id}
should.equal (@stubs._received instanceof TopicMessage), true
Expand Down

0 comments on commit 55d41be

Please sign in to comment.