diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-09 19:14:18 +0100 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 13:01:40 +0100 |
commit | 65c9b622572038a634bee3897b2e97989a050780 (patch) | |
tree | 265749c09f58141c6957c8ad160d921fef5453f0 | |
parent | 32a4739bcd9fd3ef289b0f9c8bb0b4248d9444b6 (diff) | |
download | wee-slack-65c9b622572038a634bee3897b2e97989a050780.tar.gz |
Add command `/slack part`
-rw-r--r-- | slack/commands.py | 43 | ||||
-rw-r--r-- | slack/config.py | 8 | ||||
-rw-r--r-- | slack/slack_conversation.py | 25 | ||||
-rw-r--r-- | slack/slack_workspace.py | 2 |
4 files changed, 66 insertions, 12 deletions
diff --git a/slack/commands.py b/slack/commands.py index 6bc1fdb..8f68045 100644 --- a/slack/commands.py +++ b/slack/commands.py @@ -331,8 +331,7 @@ async def conversation_join( await conversation.open_buffer(switch=switch) -@weechat_command("", min_args=1) -def command_slack_join(buffer: str, args: List[str], options: Options): +def get_conversation_from_args(buffer: str, args: List[str], options: Options): slack_buffer = shared.buffers.get(buffer) workspace_name = options.get("workspace") @@ -357,7 +356,18 @@ def command_slack_join(buffer: str, args: List[str], options: Options): ) return - conversation_name = args[0] + if len(args) == 0 or not args[0]: + if workspace_name is not None: + print_error( + "Must specify conversaton name when workspace name is specified" + ) + return + if isinstance(slack_buffer, SlackConversation): + return slack_buffer + else: + return + + conversation_name = args[0].strip() all_conversations = get_resolved_futures(workspace.conversations.values()) for conversation in all_conversations: if ( @@ -365,16 +375,31 @@ def command_slack_join(buffer: str, args: List[str], options: Options): == conversation_name or conversation.name() == conversation_name ): - run_async( - conversation_join( - workspace, conversation.id, switch=not options.get("noswitch") - ) - ) - return + return conversation print_error(f'Conversation "{conversation_name}" not found') +@weechat_command("", min_args=1) +def command_slack_join(buffer: str, args: List[str], options: Options): + conversation = get_conversation_from_args(buffer, args, options) + if conversation is not None: + run_async( + conversation_join( + conversation.workspace, + conversation.id, + switch=not options.get("noswitch"), + ) + ) + + +@weechat_command("") +def command_slack_part(buffer: str, args: List[str], options: Options): + conversation = get_conversation_from_args(buffer, args, options) + if conversation is not None: + run_async(conversation.part()) + + @weechat_command("%(threads)", min_args=1) def command_slack_thread(buffer: str, args: List[str], options: Options): slack_buffer = shared.buffers.get(buffer) diff --git a/slack/config.py b/slack/config.py index bf785ea..bc6f89a 100644 --- a/slack/config.py +++ b/slack/config.py @@ -207,6 +207,14 @@ class SlackConfigSectionLook: string_values=["none", "personal_highlights", "all_highlights", "all"], ) + self.part_closes_buffer = WeeChatOption( + self._section, + "part_closes_buffer", + "close buffer when /slack part is issued on a channel", + False, + parent_option="irc.look.part_closes_buffer", + ) + self.render_emoji_as: WeeChatOption[ Literal["emoji", "name", "both"] ] = WeeChatOption( diff --git a/slack/slack_conversation.py b/slack/slack_conversation.py index 1da093d..ffea7c6 100644 --- a/slack/slack_conversation.py +++ b/slack/slack_conversation.py @@ -150,6 +150,7 @@ class SlackConversation(SlackBuffer): super().__init__() self._workspace = workspace self._info = info + self._is_joined: bool = False self._members: Optional[List[str]] = None self._im_user: Optional[SlackUser] = None self._mpim_users: Optional[List[SlackUser]] = None @@ -218,6 +219,10 @@ class SlackConversation(SlackBuffer): return "conversation" @property + def is_joined(self) -> bool: + return self._is_joined + + @property def members(self) -> Generator[Nick, None, None]: for nick in self._nicklist: if nick.type == "user": @@ -353,6 +358,7 @@ class SlackConversation(SlackBuffer): async def open_buffer(self, switch: bool = False): await super().open_buffer(switch) + self._is_joined = True self.workspace.open_conversations[self.id] = self async def rerender_message(self, message: SlackMessage): @@ -446,11 +452,15 @@ class SlackConversation(SlackBuffer): weechat.buffer_set(self.buffer_pointer, "hotlist", priority.value) self.hotlist_tss.add(message.latest_reply) - async def fill_history(self): + async def fill_history(self, update: bool = False): if self.history_pending: return - if self.last_printed_ts is not None and not self.history_needs_refresh: + if ( + self.last_printed_ts is not None + and not self.history_needs_refresh + and not update + ): return with self.loading(): @@ -722,10 +732,21 @@ class SlackConversation(SlackBuffer): self.nicklist_add_nick(nick) async def mark_read(self): + if not self._is_joined: + return last_read_line_ts = self.last_read_line_ts() if last_read_line_ts and last_read_line_ts != self.last_read: await self._api.conversations_mark(self, last_read_line_ts) + async def part(self): + self._is_joined = False + await self._api.conversations_leave(self) + if shared.config.look.part_closes_buffer.value: + await self.close_buffer() + else: + # Update history to get the part message + await self.fill_history(update=True) + async def _buffer_close( self, call_buffer_close: bool = False, update_server: bool = False ): diff --git a/slack/slack_workspace.py b/slack/slack_workspace.py index 45d453b..41e4a21 100644 --- a/slack/slack_workspace.py +++ b/slack/slack_workspace.py @@ -549,7 +549,7 @@ class SlackWorkspace: or data["type"] == "channel_left" or data["type"] == "group_left" ): - if channel.buffer_pointer is not None: + if channel.buffer_pointer is not None and channel.is_joined: await channel.close_buffer() elif data["type"] == "reaction_added" and data["item"]["type"] == "message": await channel.reaction_add( |