Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add email to User class #241

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 12 additions & 5 deletions slackclient/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Server(object):


'''

def __init__(self, token, connect=True, proxies=None):
self.token = token
self.username = None
Expand Down Expand Up @@ -92,7 +93,7 @@ def parse_slack_login_data(self, login_data, use_rtm_start):
if use_rtm_start:
self.parse_channel_data(login_data["channels"])
self.parse_channel_data(login_data["groups"])
self.parse_user_data(login_data["users"])
self.parse_user_data(login_data["users"], login_data["team"])
self.parse_channel_data(login_data["ims"])

def connect_slack_websocket(self, ws_url):
Expand Down Expand Up @@ -124,13 +125,19 @@ def parse_channel_data(self, channel_data):
channel["id"],
channel["members"])

def parse_user_data(self, user_data):
def parse_user_data(self, user_data, team_data):
for user in user_data:
if "tz" not in user:
user["tz"] = "unknown"
if "real_name" not in user:
user["real_name"] = user["name"]
self.attach_user(user["name"], user["id"], user["real_name"], user["tz"])
if "profile" in user and "email" in user["profile"]:
user["email"] = user["profile"]["email"]
elif team_data and "email_domain" in team_data:
user["email"] = "{}@{}".format(user["name"], team_data["email_domain"])
else:
user["email"] = "" # default to empty.
self.attach_user(user["name"], user["id"], user["real_name"], user["email"], user["tz"])

def send_to_websocket(self, data):
"""
Expand Down Expand Up @@ -196,8 +203,8 @@ def websocket_safe_read(self):
raise
return data.rstrip()

def attach_user(self, name, user_id, real_name, tz):
self.users.update({user_id: User(self, name, user_id, real_name, tz)})
def attach_user(self, name, user_id, real_name, email, tz):
self.users.update({user_id: User(self, name, user_id, real_name, email, tz)})

def attach_channel(self, name, channel_id, members=None):
if members is None:
Expand Down
3 changes: 2 additions & 1 deletion slackclient/user.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class User(object):
def __init__(self, server, name, user_id, real_name, tz):
def __init__(self, server, name, user_id, real_name, email, tz):
self.tz = tz
self.name = name
self.real_name = real_name
self.email = email
self.server = server
self.id = user_id

Expand Down
4 changes: 3 additions & 1 deletion tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ def test_server_parse_channel_data(server, rtm_start_fixture):


def test_server_parse_user_data(server, rtm_start_fixture):
server.parse_user_data(rtm_start_fixture["users"])
server.parse_user_data(rtm_start_fixture["users"], rtm_start_fixture["team"])
# Find user by Name
userbyname = server.users.find('fakeuser')
assert type(userbyname) == User
assert userbyname == "fakeuser"
assert userbyname != "someotheruser"
assert userbyname.email == "fakeuser@example.com"

# Find user by ID
userbyid = server.users.find('U10CX1234')
assert type(userbyid) == User
Expand Down