diff options
Diffstat (limited to 'slack')
-rw-r--r-- | slack/slack_conversation.py | 17 | ||||
-rw-r--r-- | slack/slack_workspace.py | 20 |
2 files changed, 31 insertions, 6 deletions
diff --git a/slack/slack_conversation.py b/slack/slack_conversation.py index d1cb337..fe8cd79 100644 --- a/slack/slack_conversation.py +++ b/slack/slack_conversation.py @@ -152,6 +152,16 @@ class SlackConversation(SlackBuffer): def buffer_type(self) -> Literal["private", "channel"]: return "private" if self.type in ("im", "mpim") else "channel" + async def sort_key(self) -> str: + type_sort_key = { + "channel": 0, + "private": 1, + "mpim": 2, + "im": 3, + }[self.type] + name = await self.name() + return f"{type_sort_key}{name}".lower() + async def name(self) -> str: if self._info["is_im"] is True: im_user = await self.workspace.users[self._info["user"]] @@ -192,12 +202,13 @@ class SlackConversation(SlackBuffer): ) -> str: return f"{self.name_prefix(name_type)}{await self.name()}" - async def open_if_open(self): + def should_open(self): if "is_open" in self._info: if self._info["is_open"]: - await self.open_buffer() + return True elif self._info.get("is_member"): - await self.open_buffer() + return True + return False async def get_name_and_buffer_props(self) -> Tuple[str, Dict[str, str]]: name_without_prefix = await self.name() diff --git a/slack/slack_workspace.py b/slack/slack_workspace.py index 4ad5d73..dcf0279 100644 --- a/slack/slack_workspace.py +++ b/slack/slack_workspace.py @@ -47,6 +47,7 @@ if TYPE_CHECKING: from slack_api.slack_bots_info import SlackBotInfo from slack_api.slack_conversations_info import SlackConversationsInfo from slack_api.slack_usergroups_info import SlackUsergroupInfo + from slack_api.slack_users_conversations import SlackUsersConversations from slack_api.slack_users_info import SlackUserInfo from slack_rtm.slack_rtm_message import SlackRtmMessage else: @@ -242,12 +243,25 @@ class SlackWorkspace: ) channels = users_conversations_response["channels"] self.conversations.initialize_items(channel["id"] for channel in channels) - for channel in channels: - conversation = await self.conversations[channel["id"]] - run_async(conversation.open_if_open()) + + conversations_if_should_open = await gather( + *(self._conversation_if_should_open(channel) for channel in channels) + ) + conversations_to_open = [ + c for c in conversations_if_should_open if c is not None + ] + + for _, conversation in sorted(conversations_to_open): + await conversation.open_buffer() self.is_connected = True + async def _conversation_if_should_open(self, info: SlackUsersConversations): + conversation = await self.conversations[info["id"]] + if conversation.should_open(): + sort_key = await conversation.sort_key() + return sort_key, conversation + async def _connect_ws(self, url: str): proxy = Proxy() # TODO: Handle errors |