Skip to content

Commit

Permalink
Merge pull request slackapi#113 from paulhammond/user-changes
Browse files Browse the repository at this point in the history
User changes (fixes slackapi#56, slackapi#94, slackapi#118)
  • Loading branch information
paulhammond committed Dec 16, 2014
2 parents aadb994 + db20579 commit ff7685e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@
"test": "mocha --compilers coffee:coffee-script/register --reporter spec"
},
"dependencies": {
"slack-client": "~1.1.0"
"slack-client": "~1.2.0"
}
}
25 changes: 20 additions & 5 deletions src/slack.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class SlackBot extends Adapter
@client.on 'open', @.open
@client.on 'close', @.close
@client.on 'message', @.message
@client.on 'userChange', @.userChange

# Start logging in
@client.login()
Expand All @@ -44,9 +45,24 @@ class SlackBot extends Adapter
loggedIn: (self, team) =>
@robot.logger.info "Logged in as #{self.name} of #{team.name}, but not yet connected"

# store a copy of our own user data
@self = self

# Provide our name to Hubot
@robot.name = self.name

for id, user of @client.users
@userChange user

userChange: (user) =>
newUser = {name: user.name, email_address: user.profile.email}
if user.id of @robot.brain.data.users
for key, value of @robot.brain.data.users[user.id]
unless key of newUser
newUser[key] = value
delete @robot.brain.data.users[user.id]
@robot.brain.userForId user.id, newUser

open: =>
@robot.logger.info 'Slack client now connected'

Expand All @@ -71,14 +87,13 @@ class SlackBot extends Adapter
# Ignore message subtypes that don't have a top level user property
return if not msg.user

channel = @client.getChannelGroupOrDMByID msg.channel
slackUser = @client.getUserByID msg.user

# Ignore our own messages
return if slackUser.name == @robot.name
return if msg.user == @self.id

channel = @client.getChannelGroupOrDMByID msg.channel

# Process the user into a full hubot user
user = @robot.brain.userForId slackUser.id, {name: slackUser.name, email_address: slackUser.profile.email}
user = @robot.brain.userForId msg.user
user.room = channel.name

# Test for enter/leave messages
Expand Down

0 comments on commit ff7685e

Please sign in to comment.