diff options
-rw-r--r-- | slack/slack_conversation.py | 56 | ||||
-rw-r--r-- | slack/slack_message.py | 59 |
2 files changed, 67 insertions, 48 deletions
diff --git a/slack/slack_conversation.py b/slack/slack_conversation.py index fbf42bf..c50e2ce 100644 --- a/slack/slack_conversation.py +++ b/slack/slack_conversation.py @@ -37,10 +37,9 @@ class SlackConversation: ): self.workspace = workspace self._info = info - self._members: List[str] + self._members: Optional[List[str]] = None # TODO: buffer_pointer may be accessed by buffer_switch before it's initialized self.buffer_pointer: str = "" - self.name: str self.is_loading = False self.history_filled = False self.history_pending = False @@ -67,6 +66,38 @@ class SlackConversation: def id(self) -> str: return self._info["id"] + async def name(self) -> str: + if "is_im" in self._info and self._info["is_im"] is True: + im_user = await self.workspace.users[self._info["user"]] + return im_user.nick() + elif self._info["is_mpim"] is True: + if self._members is None: + members_response = await self._api.fetch_conversations_members(self) + self._members = members_response["members"] + await self.workspace.users.initialize_items(self._members) + member_users = await gather( + *(self.workspace.users[user_id] for user_id in self._members) + ) + return ",".join([user.nick() for user in member_users]) + else: + return self._info["name"] + + def name_prefix( + self, name_type: Union[Literal["full_name"], Literal["short_name"]] + ) -> str: + if "is_im" in self._info and self._info["is_im"] is True: + if name_type == "short_name": + return " " + else: + return "" + elif self._info["is_mpim"]: + if name_type == "short_name": + return "@" + else: + return "" + else: + return "#" + @contextmanager def loading(self): self.is_loading = True @@ -93,27 +124,14 @@ class SlackConversation: await self.open_buffer() async def open_buffer(self): - with self.loading(): - if "is_im" in self._info and self._info["is_im"] is True: - im_user = await self.workspace.users[self._info["user"]] - self.name = im_user.nick() - elif self._info["is_mpim"] is True: - members_response = await self._api.fetch_conversations_members(self) - self._members = members_response["members"] - await self.workspace.users.initialize_items(self._members) - member_users = await gather( - *(self.workspace.users[user_id] for user_id in self._members) - ) - self.name = ",".join([user.nick() for user in member_users]) - else: - self.name = f"#{self._info['name']}" - - full_name = f"{shared.SCRIPT_NAME}.{self.workspace.name}.{self.name}" + name = await self.name() + full_name = f"{shared.SCRIPT_NAME}.{self.workspace.name}.{self.name_prefix('full_name')}{name}" + short_name = self.name_prefix("short_name") + name self.buffer_pointer = weechat.buffer_new( full_name, get_callback_name(self.buffer_input_cb), "", "", "" ) - weechat.buffer_set(self.buffer_pointer, "short_name", self.name) + weechat.buffer_set(self.buffer_pointer, "short_name", short_name) weechat.buffer_set( self.buffer_pointer, "localvar_set_nick", self.workspace.my_user.nick() ) diff --git a/slack/slack_message.py b/slack/slack_message.py index 07b03f4..dc7d79d 100644 --- a/slack/slack_message.py +++ b/slack/slack_message.py @@ -3,11 +3,10 @@ from __future__ import annotations import re from typing import TYPE_CHECKING, List, Match, Optional -import slack.slack_conversation from slack.log import print_exception_once from slack.python_compatibility import removeprefix from slack.shared import shared -from slack.slack_user import SlackUser, SlackUsergroup, format_bot_nick +from slack.slack_user import format_bot_nick from slack.task import gather from slack.util import with_color @@ -53,22 +52,40 @@ class SlackMessage: user = await self.workspace.users[self._message_json["user"]] return user.nick(colorize=True) - async def _lookup_item_id(self, item_id: str): - if item_id.startswith("#"): - return await self.workspace.conversations[removeprefix(item_id, "#")] - elif item_id.startswith("@"): - return await self.workspace.users[removeprefix(item_id, "@")] - elif item_id.startswith("!subteam^"): - return await self.workspace.usergroups[removeprefix(item_id, "!subteam^")] - def _item_prefix(self, item_id: str): if item_id.startswith("#") or item_id.startswith("@"): return item_id[0] - elif item_id.startswith("!subteam^"): + elif item_id.startswith("!subteam^") or item_id in [ + "!here", + "!channel", + "!everyone", + ]: return "@" else: return "" + async def _lookup_item_id(self, item_id: str): + if item_id.startswith("#"): + conversation = await self.workspace.conversations[ + removeprefix(item_id, "#") + ] + color = shared.config.color.channel_mention_color.value + name = conversation.name_prefix("short_name") + await conversation.name() + return (color, name) + elif item_id.startswith("@"): + user = await self.workspace.users[removeprefix(item_id, "@")] + color = shared.config.color.user_mention_color.value + return (color, self._item_prefix(item_id) + user.nick()) + elif item_id.startswith("!subteam^"): + usergroup = await self.workspace.usergroups[ + removeprefix(item_id, "!subteam^") + ] + color = shared.config.color.usergroup_mention_color.value + return (color, self._item_prefix(item_id) + usergroup.handle()) + elif item_id in ["!here", "!channel", "!everyone"]: + color = shared.config.color.usergroup_mention_color.value + return (color, self._item_prefix(item_id) + removeprefix(item_id, "!")) + async def _unfurl_refs(self, message: str) -> str: re_mention = re.compile(r"<(?P<id>[^|>]+)(?:\|(?P<fallback_name>[^>]*))?>") mention_matches = list(re_mention.finditer(message)) @@ -81,24 +98,8 @@ class SlackMessage: def unfurl_ref(match: Match[str]): item = items[match["id"]] - if match["id"] in ["!here", "!channel", "!everyone"]: - return with_color( - shared.config.color.usergroup_mention_color.value, - "@" + removeprefix(match["id"], "!"), - ) - if isinstance(item, slack.slack_conversation.SlackConversation): - return with_color( - shared.config.color.channel_mention_color.value, "#" + item.name - ) - elif isinstance(item, SlackUser): - return with_color( - shared.config.color.user_mention_color.value, "@" + item.nick() - ) - elif isinstance(item, SlackUsergroup): - return with_color( - shared.config.color.usergroup_mention_color.value, - "@" + item.handle(), - ) + if item and not isinstance(item, BaseException): + return with_color(item[0], item[1]) elif match["fallback_name"]: prefix = self._item_prefix(match["id"]) if match["fallback_name"].startswith(prefix): |