aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Wang <gnawrice@gmail.com>2017-10-23 17:20:49 -0700
committerEric Wang <gnawrice@gmail.com>2017-11-29 20:17:25 -0800
commitcdf6f49984d83d094999efcf5355d12e15ee4e51 (patch)
tree297a52f39d0b5047db75c37fdd28b14a8ea17050
parentfc1e90b5f5309e06204ee3e8e93d1b621c53103b (diff)
downloadwee-slack-cdf6f49984d83d094999efcf5355d12e15ee4e51.tar.gz
Refactor command_talk and fix MPDMs not being created
- Fix incorrect parameter name for MPDMs: `users` instead of `user_ids` - Accept comma-separated nicks instead of space-separated to match weechat's `/query` syntax - Check user IDs instead of usernames to find channels - Use `SLACK_API_TRANSLATOR` instead of hard coding API methods - Update documentation
-rw-r--r--README.md4
-rw-r--r--wee_slack.py92
2 files changed, 51 insertions, 45 deletions
diff --git a/README.md b/README.md
index 9a67989..ace8980 100644
--- a/README.md
+++ b/README.md
@@ -123,8 +123,8 @@ Join a channel:
Start a direct chat with someone or multiple users:
```
-/query [username] ([username2] [username3]...)
-/slack talk [username] ([username2] [username3]...)
+/query <username>[,<username2>[,<username3>...]]
+/slack talk <username>[,<username2>[,<username3>...]]
```
List channels:
diff --git a/wee_slack.py b/wee_slack.py
index db92c34..ffd1c84 100644
--- a/wee_slack.py
+++ b/wee_slack.py
@@ -1048,6 +1048,16 @@ class SlackTeam(object):
def get_username_map(self):
return {v.slack_name: k for k, v in self.users.iteritems()}
+ def find_channel_by_name(self, name):
+ for channel in self.channels.itervalues():
+ if channel.slack_name == name:
+ return channel
+
+ def find_channel_by_members(self, channel_type, users):
+ for channel in self.channels.itervalues():
+ if channel.type == channel_type and channel.get_members() == users:
+ return channel
+
def get_team_hash(self):
return self.team_hash
@@ -1194,6 +1204,9 @@ class SlackChannel(object):
return True
return False
+ def get_members(self):
+ return self.members
+
def set_unread_count_display(self, count):
self.unread_count_display = count
self.new_messages = bool(self.unread_count_display)
@@ -1602,6 +1615,9 @@ class SlackDMChannel(SlackChannel):
def set_name(self, slack_name):
self.name = slack_name
+ def get_members(self):
+ return {self.user}
+
def create_buffer(self):
if not self.channel_buffer:
super(SlackDMChannel, self).create_buffer()
@@ -2982,59 +2998,49 @@ def command_users(data, current_buffer, args):
def command_talk(data, current_buffer, args):
"""
Open a chat with the specified user(s)
- /slack talk [user] ([user2] [user3]...)
+ /slack talk <user>[,<user2>[,<user3>...]]
"""
e = EVENTROUTER
team = e.weechat_controller.buffers[current_buffer].team
- users = args.split(' ')[1:]
- c = team.get_channel_map()
-
- if len(users) > 1:
- # Add the current user if not specified since all MPDM names have it
- if team.nick not in users:
- users.append(team.nick)
-
- # Use the user order given if no channels are found below
- channel_name = 'mpdm-{}-1'.format('--'.join(users))
-
- # Use the name of any existing channel with the same set of users
- user_set = set(users)
- mpdm_regex = re.compile(r'-+')
- for channel in c:
- if channel.startswith('mpdm-') and user_set == set(
- mpdm_regex.split(channel)[1:-1]):
- channel_name = channel
- break
- else:
- channel_name = users[0]
- if channel_name.startswith('@'):
- channel_name = channel_name[1:]
+ channel_name = args.split(' ')[1]
+
+ if channel_name.startswith('#'):
+ channel_name = channel_name[1:]
+
+ chan = team.find_channel_by_name(channel_name)
- if channel_name not in c:
+ # If the channel name doesn't exist, try finding a DM or MPDM instead
+ if not chan:
# Get the IDs of the users
u = team.get_username_map()
- user_ids = tuple(u[user] for user in users if user in u)
-
- # Open the DM or MPDM depending on the number of users
- if user_ids:
- if len(user_ids) > 1:
- method = 'mpim.open'
- params = {'user_ids': ','.join(user_ids)}
+ users = set()
+ for user in channel_name.split(','):
+ if user.startswith('@'):
+ user = user[1:]
+ if user in u:
+ users.add(u[user])
+
+ if users:
+ if len(users) > 1:
+ channel_type = 'mpim'
+ # Add the current user if not given since they'll be in MPDMs
+ if team.myidentifier not in users:
+ users.add(team.myidentifier)
else:
- method = 'im.open'
- params = {'user': user_ids[0]}
+ channel_type = 'im'
- s = SlackRequest(team.token, method, params, team_hash=team.team_hash)
- EVENTROUTER.receive(s)
- dbg("found user")
- # refresh channel map here
- c = team.get_channel_map()
+ chan = team.find_channel_by_members(channel_type, users)
- if channel_name.startswith('#'):
- channel_name = channel_name[1:]
- if channel_name in c:
- chan = team.channels[c[channel_name]]
+ # If the DM or MPDM doesn't exist, create it
+ if not chan:
+ s = SlackRequest(team.token, SLACK_API_TRANSLATOR[channel_type]['join'], {'users': ','.join(users)}, team_hash=team.team_hash)
+ EVENTROUTER.receive(s)
+ dbg("found user")
+ # Find the channel after creating it
+ chan = team.find_channel_by_members(channel_type, users)
+
+ if chan:
chan.open()
if config.switch_buffer_on_join:
w.buffer_set(chan.channel_buffer, "display", "1")