From f8c013b871f5829d58bfc879108eb3d0d0f22e5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20M=C3=B8llegaard=20Pedersen?= Date: Sun, 7 Oct 2018 22:57:48 +0200 Subject: [PATCH] Add supports for slack_team_joined events --- README.md | 1 + lib/lita/adapters/slack/message_handler.rb | 13 ++++++++++++- spec/lita/adapters/slack/message_handler_spec.rb | 15 ++++++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cc94cd9..fe0ce78 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ Lita will join your default channel after initial setup. To have it join additio * `:slack_reaction_added` - When a reaction has been added to a previous message. The payload includes `:user` (a `Lita::User` for the sender of the message in question), `:name` (the string name of the reaction added), `:item_user` (a `Lita::User` for the user that created the original item that has been reacted to), `:item` (a hash of raw data from Slack about the message), and `:event_ts` (a string timestamp used to identify the message). * `:slack_reaction_removed` - When a reaction has been removed from a previous message. The payload is the same as the `:slack_reaction_added` message. * `:slack_user_created` - When the robot creates/updates a user's info - name, mention name, etc., as directed by Slack. The payload has a single object, a `Lita::Slack::Adapters::SlackUser` object, under the `:slack_user` key. +* `:slack_team_joined` - When a new user joins slack. Is triggered in addition to `:slack_user_created` with the same payload ## Chat service API diff --git a/lib/lita/adapters/slack/message_handler.rb b/lib/lita/adapters/slack/message_handler.rb index b1572b2..f1661b1 100644 --- a/lib/lita/adapters/slack/message_handler.rb +++ b/lib/lita/adapters/slack/message_handler.rb @@ -18,8 +18,10 @@ def handle handle_message when "reaction_added", "reaction_removed" handle_reaction - when "user_change", "team_join" + when "user_change" handle_user_change + when "team_join" + handle_team_join when "bot_added", "bot_changed" handle_bot_change when "channel_created", "channel_rename", "group_rename" @@ -190,6 +192,15 @@ def handle_user_change UserCreator.create_user(SlackUser.from_data(data["user"]), robot, robot_id) end + def handle_team_join + log.debug("Updating user data. New user joined slack") + + slack_user = SlackUser.from_data(data["user"]) + + UserCreator.create_user(slack_user, robot, robot_id) + robot.trigger(:slack_team_joined, slack_user: slack_user) + end + def log Lita.logger end diff --git a/spec/lita/adapters/slack/message_handler_spec.rb b/spec/lita/adapters/slack/message_handler_spec.rb index c449b0c..6803323 100644 --- a/spec/lita/adapters/slack/message_handler_spec.rb +++ b/spec/lita/adapters/slack/message_handler_spec.rb @@ -507,7 +507,6 @@ end context "with a team join message" do - # let(:bobby) { Lita::Adapters::Slack::SlackUser.new('U023BECGF', 'bobby', real_name) } let(:data) do { "type" => "team_join", @@ -528,6 +527,20 @@ subject.handle end + + it "triggers an event" do + bobby = Lita::Adapters::Slack::SlackUser.new(data.dig("user", "id"), data.dig("user", "name"), data.dig("user", "real_name"), data["user"]) + + expect( + Lita::Adapters::Slack::UserCreator + ).to receive(:create_user).and_return(bobby) + + expect(robot).to receive(:trigger).with(:slack_team_joined, slack_user: Lita::Adapters::Slack::SlackUser) do |event, slack_user:| + expect(slack_user.metadata).to eq(bobby.metadata) + end + + subject.handle + end end context "with a bot added message" do