diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2023-09-17 13:00:00 +0200 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 11:32:54 +0100 |
commit | f103563e404b3f4e3d24ed57834c2fe517c979d8 (patch) | |
tree | 0c4230d7204ccf88bb266ae9d79ff8d5ffe32f75 /slack | |
parent | c102376e7b3052126680465c3f99c1b3818ed697 (diff) | |
download | wee-slack-f103563e404b3f4e3d24ed57834c2fe517c979d8.tar.gz |
Fetch thread replies when receiving thread message
This is done so we can know if we should include a message hash when
completing thread hashes, and it also has the benefit of eliminating the
loading time when opening thread buffers.
Diffstat (limited to 'slack')
-rw-r--r-- | slack/slack_api.py | 10 | ||||
-rw-r--r-- | slack/slack_conversation.py | 41 | ||||
-rw-r--r-- | slack/slack_thread.py | 4 |
3 files changed, 35 insertions, 20 deletions
diff --git a/slack/slack_api.py b/slack/slack_api.py index 72c82e4..0e38bf4 100644 --- a/slack/slack_api.py +++ b/slack/slack_api.py @@ -7,7 +7,7 @@ from urllib.parse import urlencode from slack.error import SlackApiError from slack.http import http_request from slack.shared import shared -from slack.slack_message import SlackMessage +from slack.slack_message import SlackTs if TYPE_CHECKING: from slack_api.slack_bots_info import SlackBotInfoResponse, SlackBotsInfoResponse @@ -138,11 +138,13 @@ class SlackApi(SlackApiCommon): raise SlackApiError(self.workspace, method, response, params) return response - async def fetch_conversations_replies(self, parent_message: SlackMessage): + async def fetch_conversations_replies( + self, conversation: SlackConversation, parent_message_ts: SlackTs + ): method = "conversations.replies" params: Params = { - "channel": parent_message.conversation.id, - "ts": str(parent_message.ts), + "channel": conversation.id, + "ts": parent_message_ts, } response: SlackConversationsRepliesResponse = await self._fetch_list( method, "messages", params diff --git a/slack/slack_conversation.py b/slack/slack_conversation.py index 8d559b0..ef3583c 100644 --- a/slack/slack_conversation.py +++ b/slack/slack_conversation.py @@ -6,6 +6,7 @@ from typing import TYPE_CHECKING, Dict, List, Mapping, NoReturn, Optional, Tuple import weechat +from slack.error import SlackError from slack.python_compatibility import removeprefix from slack.shared import shared from slack.slack_buffer import SlackBuffer @@ -235,25 +236,33 @@ class SlackConversation(SlackBuffer): self.workspace.users.initialize_items(self._members) return self._members - async def fetch_replies( - self, message: SlackMessage - ) -> Optional[List[SlackMessage]]: - if not message.is_thread_parent: - return - - replies_response = await self._api.fetch_conversations_replies(message) - replies = [ - SlackMessage(self, reply) for reply in replies_response["messages"][1:] + async def fetch_replies(self, thread_ts: SlackTs) -> List[SlackMessage]: + replies_response = await self._api.fetch_conversations_replies(self, thread_ts) + messages = [ + SlackMessage(self, message) for message in replies_response["messages"] ] + if thread_ts != messages[0].ts: + raise SlackError( + self.workspace, + f"First message in conversations.replies response did not match thread_ts {thread_ts}", + replies_response, + ) + + if thread_ts not in self._messages: + self._messages[thread_ts] = messages[0] + + parent_message = self._messages[thread_ts] + + replies = messages[1:] for reply in replies: - message.replies[reply.ts] = reply + parent_message.replies[reply.ts] = reply self._messages[reply.ts] = reply - message.replies = OrderedDict(sorted(message.replies.items())) + parent_message.replies = OrderedDict(sorted(parent_message.replies.items())) self._messages = OrderedDict(sorted(self._messages.items())) - message.reply_history_filled = True + parent_message.reply_history_filled = True return replies async def fill_history(self): @@ -273,7 +282,11 @@ class SlackConversation(SlackBuffer): if self.display_thread_replies(): await gather( - *(self.fetch_replies(message) for message in conversation_messages) + *( + self.fetch_replies(message.ts) + for message in conversation_messages + if message.is_thread_parent + ) ) self._messages = OrderedDict(sorted(self._messages.items())) @@ -344,6 +357,8 @@ class SlackConversation(SlackBuffer): parent_message.replies[message.ts] = message if parent_message.thread_buffer: await parent_message.thread_buffer.print_message(message) + elif message.thread_ts is not None: + await self.fetch_replies(message.thread_ts) if message.sender_user_id: # TODO: thread buffers diff --git a/slack/slack_thread.py b/slack/slack_thread.py index f92dfc4..fbbeb21 100644 --- a/slack/slack_thread.py +++ b/slack/slack_thread.py @@ -71,9 +71,7 @@ class SlackThread(SlackBuffer): with self.loading(): self.history_pending = True - messages = await self.parent.conversation.fetch_replies(self.parent) - if messages is None: - return + messages = await self.parent.conversation.fetch_replies(self.parent.ts) sender_user_ids = [m.sender_user_id for m in messages if m.sender_user_id] self.workspace.users.initialize_items(sender_user_ids) |