diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2023-01-28 21:44:44 +0100 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 11:32:53 +0100 |
commit | 584a580243c4bb246b15e07928a6561047de9e9b (patch) | |
tree | 464c785c72452d8194f49be544382f751b346921 | |
parent | b7a3eb918171ac2049dda68c1c1dda19a1319162 (diff) | |
download | wee-slack-584a580243c4bb246b15e07928a6561047de9e9b.tar.gz |
Initial work for matching usergroups
Doesn't get info/name yet. Apparently there's no API method for getting
info for one usergroup...
-rw-r--r-- | slack/config.py | 7 | ||||
-rw-r--r-- | slack/slack_message.py | 27 |
2 files changed, 30 insertions, 4 deletions
diff --git a/slack/config.py b/slack/config.py index 7efe69b..6470816 100644 --- a/slack/config.py +++ b/slack/config.py @@ -50,6 +50,13 @@ class SlackConfigSectionColor: WeeChatColor("blue"), ) + self.usergroup_mention_color = WeeChatOption( + self._section, + "usergroup_mention_color", + "text color for mentioned user group names in the chat", + WeeChatColor("blue"), + ) + class SlackConfigSectionLook: def __init__(self, weechat_config: WeeChatConfig): diff --git a/slack/slack_message.py b/slack/slack_message.py index 1a0d482..66b9a70 100644 --- a/slack/slack_message.py +++ b/slack/slack_message.py @@ -1,7 +1,7 @@ from __future__ import annotations import re -from typing import TYPE_CHECKING, List, Optional +from typing import TYPE_CHECKING, List, Match, Optional from slack.shared import shared from slack.slack_user import SlackUser, format_bot_nick @@ -51,14 +51,30 @@ class SlackMessage: return user.nick(colorize=True) async def _unfurl_refs(self, message: str) -> str: - re_user = re.compile("<@([^>]+)>") - user_ids: List[str] = re_user.findall(message) + re_mention = re.compile(r"<@(?P<user>[^>]+)>|<!subteam\^(?P<usergroup>[^>]+)>") + mention_matches = list(re_mention.finditer(message)) + + user_ids: List[str] = [ + match["user"] for match in mention_matches if match["user"] + ] + # usergroup_ids: List[str] = [ + # match["usergroup"] for match in mention_matches if match["usergroup"] + # ] + users_list = await gather( *(self.workspace.users[user_id] for user_id in user_ids), return_exceptions=True, ) users = dict(zip(user_ids, users_list)) + def unfurl_ref(match: Match[str]): + if match["user"]: + return unfurl_user(match["user"]) + elif match["usergroup"]: + return unfurl_usergroup(match["usergroup"]) + else: + return match[0] + def unfurl_user(user_id: str): user = users[user_id] return ( @@ -69,4 +85,7 @@ class SlackMessage: else f"@{user_id}" ) - return re_user.sub(lambda match: unfurl_user(match.group(1)), message) + def unfurl_usergroup(usergroup_id: str): + return f"@{usergroup_id}" + + return re_mention.sub(unfurl_ref, message) |