diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2017-10-09 23:09:11 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-09 23:09:11 +0200 |
commit | 31c7e9acde467b8df8efc319fad1971d387b1361 (patch) | |
tree | 1f6788c1009cea8638b67e0b02d21bac96516f16 /wee_slack.py | |
parent | 9f9e8384f418f4557c81e080205538372c35e018 (diff) | |
parent | d1a2abb989f3e769be84e16ee782c6927119ba58 (diff) | |
download | wee-slack-31c7e9acde467b8df8efc319fad1971d387b1361.tar.gz |
Merge pull request #443 from trygveaa/fix/topic-command-refactor
fix: Make /topic behave the same way as on IRC
Diffstat (limited to 'wee_slack.py')
-rw-r--r-- | wee_slack.py | 93 |
1 files changed, 51 insertions, 42 deletions
diff --git a/wee_slack.py b/wee_slack.py index 6f756b4..146f359 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -1132,8 +1132,8 @@ class SlackChannel(object): self.members = set(kwargs.get('members', set())) self.eventrouter = eventrouter self.slack_name = kwargs["name"] - self.slack_topic = kwargs.get("topic", {"value": ""}) self.slack_purpose = kwargs.get("purpose", {"value": ""}) + self.topic = kwargs.get("topic", {}).get("value", "") self.identifier = kwargs["id"] self.last_read = SlackTS(kwargs.get("last_read", SlackTS())) self.channel_buffer = None @@ -1199,15 +1199,18 @@ class SlackChannel(object): } return select[style] - def render_topic(self, topic=None): + def render_topic(self): if self.channel_buffer: - if not topic: - if self.slack_topic['value'] != "": - topic = self.slack_topic['value'] - else: - topic = self.slack_purpose['value'] + if self.topic != "": + topic = self.topic + else: + topic = self.slack_purpose['value'] w.buffer_set(self.channel_buffer, "title", topic) + def set_topic(self, value): + self.topic = value + self.render_topic() + def update_from_message_json(self, message_json): for key, value in message_json.items(): setattr(self, key, value) @@ -2360,7 +2363,7 @@ def subprocess_message_deleted(message_json, eventrouter, channel, team): def subprocess_channel_topic(message_json, eventrouter, channel, team): text = unhtmlescape(unfurl_refs(message_json["text"], ignore_alt_text=False)) channel.buffer_prnt(w.prefix("network").rstrip(), text, message_json["ts"], tagset="muted") - channel.render_topic(unhtmlescape(message_json["topic"])) + channel.set_topic(unhtmlescape(message_json["topic"])) def process_reply(message_json, eventrouter, **kwargs): @@ -2850,49 +2853,55 @@ def part_command_cb(data, current_buffer, args): return w.WEECHAT_RC_OK_EAT -@slack_buffer_or_ignore -def topic_command_cb(data, current_buffer, args): - n = len(args.split()) - if n < 2: - channel = channels.find(current_buffer) - if channel: - w.prnt(current_buffer, 'Topic for {} is "{}"'.format(channel.name, channel.topic)) - return w.WEECHAT_RC_OK_EAT - elif command_topic(data, current_buffer, args.split(None, 1)[1]): - return w.WEECHAT_RC_OK_EAT - else: - return w.WEECHAT_RC_ERROR +def parse_topic_command(command): + args = command.split()[1:] + channel_name = None + topic = None + if args: + if args[0].startswith('#'): + channel_name = args[0][1:] + topic = args[1:] + else: + topic = args -@slack_buffer_required -def command_topic(data, current_buffer, args): + if topic == []: + topic = None + if topic: + topic = ' '.join(topic) + if topic == '-delete': + topic = '' + + return channel_name, topic + + +@slack_buffer_or_ignore +def topic_command_cb(data, current_buffer, command): """ Change the topic of a channel - /slack topic [<channel>] [<topic>|-delete] + /topic [<channel>] [<topic>|-delete] """ data = decode_from_utf8(data) - args = decode_from_utf8(args) - e = EVENTROUTER - team = e.weechat_controller.buffers[current_buffer].team - # server = servers.find(current_domain_name()) - args = args.split(' ') - if len(args) > 2 and args[1].startswith('#'): - cmap = team.get_channel_map() - channel_name = args[1][1:] - channel = team.channels[cmap[channel_name]] - topic = " ".join(args[2:]) + command = decode_from_utf8(command) + + channel_name, topic = parse_topic_command(command) + + team = EVENTROUTER.weechat_controller.buffers[current_buffer].team + if channel_name: + channel = team.channels.get(team.get_channel_map().get(channel_name)) else: - channel = e.weechat_controller.buffers[current_buffer] - topic = " ".join(args[1:]) + channel = EVENTROUTER.weechat_controller.buffers[current_buffer] - if channel: - if topic == "-delete": - topic = '' - s = SlackRequest(team.token, "channels.setTopic", {"channel": channel.identifier, "topic": topic}, team_hash=team.team_hash) - EVENTROUTER.receive(s) + if not channel: + w.prnt(team.channel_buffer, "#{}: No such channel".format(channel_name)) return w.WEECHAT_RC_OK_EAT + + if topic is None: + w.prnt(channel.channel_buffer, 'Topic for {} is "{}"'.format(channel.name, channel.topic)) else: - return w.WEECHAT_RC_ERROR_EAT + s = SlackRequest(team.token, "channels.setTopic", {"channel": channel.identifier, "topic": topic}, team_hash=team.team_hash) + EVENTROUTER.receive(s) + return w.WEECHAT_RC_OK_EAT @slack_buffer_or_ignore @@ -3311,7 +3320,7 @@ def setup_hooks(): w.hook_command_run('/join', 'command_talk', '') w.hook_command_run('/part', 'part_command_cb', '') w.hook_command_run('/leave', 'part_command_cb', '') - w.hook_command_run('/topic', 'command_topic', '') + w.hook_command_run('/topic', 'topic_command_cb', '') w.hook_command_run('/thread', 'thread_command_callback', '') w.hook_command_run('/reply', 'thread_command_callback', '') w.hook_command_run('/rehistory', 'rehistory_command_callback', '') |