Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Add supports for slack_team_joined events #130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 12 additions & 1 deletion lib/lita/adapters/slack/message_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion spec/lita/adapters/slack/message_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -528,6 +527,24 @@

subject.handle
end

it "triggers an event" do
user_id = data["user"]["id"]
name = data["user"]["name"]
real_name = data["user"]["real_name"]

bobby = Lita::Adapters::Slack::SlackUser.new(user_id, name, 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, args|
expect(args[:slack_user].metadata).to eq(bobby.metadata)
end

subject.handle
end
end

context "with a bot added message" do
Expand Down