From 8bba532fd59bdef07bc55e441de7c119829f0ce2 Mon Sep 17 00:00:00 2001 From: Ankur Oberoi Date: Mon, 16 Jul 2018 18:14:04 -0700 Subject: [PATCH 1/2] swaps out rtm message subtypes for discreet messages for enter and leave, fixes #296 --- src/bot.coffee | 24 +++++++++++++----------- src/client.coffee | 2 ++ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/bot.coffee b/src/bot.coffee index 1c2009f5..21cb31cb 100644 --- a/src/bot.coffee +++ b/src/bot.coffee @@ -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 @@ -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 @@ -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 diff --git a/src/client.coffee b/src/client.coffee index e4f1e050..590fcdeb 100644 --- a/src/client.coffee +++ b/src/client.coffee @@ -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 From 7cf2eb26af5159c9b393747f7f9adad59eb8e4c9 Mon Sep 17 00:00:00 2001 From: Ankur Oberoi Date: Mon, 16 Jul 2018 19:04:26 -0700 Subject: [PATCH 2/2] update tests for new enter and leave events --- test/bot.coffee | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/test/bot.coffee b/test/bot.coffee index 59c14da3..29a62176 100644 --- a/test/bot.coffee +++ b/test/bot.coffee @@ -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', -> @@ -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 @@ -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