From 84c0c20bd5bf04b89384691d90c9ef7f2a41ca0c Mon Sep 17 00:00:00 2001 From: Trygve Aaberge Date: Thu, 19 Oct 2023 22:43:01 +0200 Subject: Close IM/MPIM and remove from open_conversations on buffer close --- slack/slack_api.py | 8 ++++++++ slack/slack_conversation.py | 11 +++++++++++ 2 files changed, 19 insertions(+) (limited to 'slack') diff --git a/slack/slack_api.py b/slack/slack_api.py index 2264e36..c82fc5b 100644 --- a/slack/slack_api.py +++ b/slack/slack_api.py @@ -276,6 +276,14 @@ class SlackApi(SlackApiCommon): raise SlackApiError(self.workspace, method, response) return response + async def conversations_close(self, conversation: SlackConversation): + method = "conversations.close" + params: Params = {"channel": conversation.id} + response: SlackGenericResponse = await self._fetch(method, params) + if response["ok"] is False: + raise SlackApiError(self.workspace, method, response, params) + return response + async def conversations_mark(self, conversation: SlackConversation, ts: SlackTs): method = "conversations.mark" params: Params = {"channel": conversation.id, "ts": ts} diff --git a/slack/slack_conversation.py b/slack/slack_conversation.py index 351d24d..e415dd4 100644 --- a/slack/slack_conversation.py +++ b/slack/slack_conversation.py @@ -687,3 +687,14 @@ class SlackConversation(SlackBuffer): async def post_message(self, text: str) -> None: await self._api.chat_post_message(self.conversation, text) + + async def _buffer_closed(self): + if self.id in self.workspace.open_conversations: + del self.workspace.open_conversations[self.id] + if self.type in ["im", "mpim"]: + await self._api.conversations_close(self) + + def _buffer_close_cb(self, data: str, buffer: str) -> int: + super()._buffer_close_cb(data, buffer) + run_async(self._buffer_closed()) + return weechat.WEECHAT_RC_OK -- cgit