diff options
Diffstat (limited to 'slack')
-rw-r--r-- | slack/commands.py | 2 | ||||
-rw-r--r-- | slack/config.py | 13 | ||||
-rw-r--r-- | slack/slack_conversation.py | 2 | ||||
-rw-r--r-- | slack/slack_message.py | 4 | ||||
-rw-r--r-- | slack/slack_user.py | 41 |
5 files changed, 41 insertions, 21 deletions
diff --git a/slack/commands.py b/slack/commands.py index ffd833f..c33372a 100644 --- a/slack/commands.py +++ b/slack/commands.py @@ -85,7 +85,7 @@ def display_workspace(workspace: SlackWorkspace, detailed_list: bool): f"{with_color('chat_delimiters', '[')}" f"connected" f"{with_color('chat_delimiters', ']')}" - f", nick: {workspace.my_user.nick}" + f", nick: {workspace.my_user.nick()}" f", 0 channel(s), 0 pv", ) else: diff --git a/slack/config.py b/slack/config.py index d4a36bd..56b828f 100644 --- a/slack/config.py +++ b/slack/config.py @@ -44,6 +44,18 @@ class SlackConfigSectionColor: ) +class SlackConfigSectionLook: + def __init__(self, weechat_config: WeeChatConfig): + self._section = WeeChatSection(weechat_config, "look") + + self.external_user_suffix = WeeChatOption( + self._section, + "external_user_suffix", + "the suffix appended to nicks to indicate external users", + "*", + ) + + class SlackConfigSectionWorkspace: def __init__( self, @@ -178,6 +190,7 @@ class SlackConfig: def __init__(self): self.weechat_config = WeeChatConfig("slack") self.color = SlackConfigSectionColor(self.weechat_config) + self.look = SlackConfigSectionLook(self.weechat_config) self._section_workspace_default = WeeChatSection( self.weechat_config, "workspace_default" ) diff --git a/slack/slack_conversation.py b/slack/slack_conversation.py index 5b35a99..fb5554d 100644 --- a/slack/slack_conversation.py +++ b/slack/slack_conversation.py @@ -70,7 +70,7 @@ class SlackConversation: self.name, get_callback_name(self.buffer_input_cb), "", "", "" ) weechat.buffer_set( - self.buffer_pointer, "localvar_set_nick", self.workspace.my_user.nick + self.buffer_pointer, "localvar_set_nick", self.workspace.my_user.nick() ) async def fill_history(self): diff --git a/slack/slack_message.py b/slack/slack_message.py index 2ad8987..d6431d7 100644 --- a/slack/slack_message.py +++ b/slack/slack_message.py @@ -26,7 +26,7 @@ class SlackMessage: message = await self._unfurl_refs(self._message_json["text"]) if "user" in self._message_json: user = await self.workspace.users[self._message_json["user"]] - prefix = user.formatted_name() + prefix = user.nick(colorize=True) else: prefix = "bot" @@ -41,6 +41,6 @@ class SlackMessage: users = dict(zip(user_ids, users_list)) def unfurl_user(user_id: str): - return "@" + users[user_id].nick + return "@" + users[user_id].nick() return re_user.sub(lambda match: unfurl_user(match.group(1)), message) diff --git a/slack/slack_user.py b/slack/slack_user.py index 5baaf02..7b04605 100644 --- a/slack/slack_user.py +++ b/slack/slack_user.py @@ -4,6 +4,7 @@ from typing import TYPE_CHECKING import weechat +from slack.shared import shared from slack.util import with_color if TYPE_CHECKING: @@ -19,11 +20,6 @@ class SlackUser: def _api(self) -> SlackApi: return self.workspace.api - @property - def nick(self) -> str: - nick = self._nick_from_profile() - return nick.replace(" ", "") - async def init(self): info = await self._api.fetch_users_info(self) if info["ok"] is False: @@ -31,24 +27,35 @@ class SlackUser: raise Exception("Failed fetching user info") self._info = info["user"] - def formatted_name(self, prepend: str = "", enable_color: bool = True): - name = prepend + self.nick - if enable_color: - return with_color(self._nick_color(name), name) - else: - return name + def nick(self, colorize: bool = False) -> str: + nick = self._name_without_spaces() + + if colorize: + nick = with_color(self._nick_color(), nick) - def _nick_from_profile(self) -> str: - if self.workspace.config.use_real_names.value: - return self._info["real_name"] + if self._info["profile"]["team"] != self.workspace.id: + nick += shared.config.look.external_user_suffix.value + return nick + + def _name_from_profile(self) -> str: display_name = self._info["profile"].get("display_name") - return display_name or self._info["real_name"] + if display_name and not self.workspace.config.use_real_names.value: + return display_name + + return ( + self._info["profile"].get("display_name") + or self._info.get("real_name") + or self._info["name"] + ) + + def _name_without_spaces(self) -> str: + return self._name_from_profile().replace(" ", "") - def _nick_color(self, nick: str) -> str: + def _nick_color(self) -> str: if self.id == self.workspace.my_user.id: return weechat.config_string( weechat.config_get("weechat.color.chat_nick_self") ) - return weechat.info_get("nick_color_name", nick) + return weechat.info_get("nick_color_name", self._name_without_spaces()) |