aboutsummaryrefslogtreecommitdiffstats
path: root/slack
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2023-10-21 22:18:08 +0200
committerTrygve Aaberge <trygveaa@gmail.com>2024-02-18 11:32:54 +0100
commitd50349d15dc0e74ebf9661048d09cf1e3f48e102 (patch)
tree55e4c47c0f88cf843c2e1e7d83530b3c023fde69 /slack
parentf2b37aaee3ac5815e9398dca530cf3c832b78a28 (diff)
downloadwee-slack-d50349d15dc0e74ebf9661048d09cf1e3f48e102.tar.gz
Support mentioning users
This currently has the limitation that the user has to be known to wee-slack for the mention to work. That is, there needs to be some interaction with it (message or reaction from or mention) or it has to be in the loaded members in any of the loaded conversations.
Diffstat (limited to 'slack')
-rw-r--r--slack/slack_buffer.py27
1 files changed, 24 insertions, 3 deletions
diff --git a/slack/slack_buffer.py b/slack/slack_buffer.py
index e6ec532..20dc86c 100644
--- a/slack/slack_buffer.py
+++ b/slack/slack_buffer.py
@@ -4,14 +4,14 @@ import re
import time
from abc import ABC, abstractmethod
from contextlib import contextmanager
-from typing import TYPE_CHECKING, Dict, List, Mapping, Optional, Set, Tuple
+from typing import TYPE_CHECKING, Dict, List, Mapping, Match, Optional, Set, Tuple
import weechat
from slack.log import print_error
from slack.shared import shared
from slack.slack_message import SlackMessage, SlackTs
-from slack.task import run_async
+from slack.task import gather, run_async
from slack.util import get_callback_name, htmlescape
if TYPE_CHECKING:
@@ -404,6 +404,26 @@ class SlackBuffer(ABC):
else:
print_error("The regex didn't match any part of the message")
+ async def linkify_text(self, text: str) -> str:
+ escaped_text = htmlescape(text)
+
+ users = await gather(*self.workspace.users.values(), return_exceptions=True)
+ nick_to_user_id = {
+ user.nick(only_nick=True): user.id
+ for user in users
+ if not isinstance(user, BaseException)
+ }
+
+ def linkify_word(match: Match[str]) -> str:
+ word = match.group(0)
+ nick = match.group(1)
+ if nick in nick_to_user_id:
+ return "<@{}>".format(nick_to_user_id[nick])
+ return word
+
+ linkify_regex = r"(?:^|(?<=\s))@([\w\(\)\'.-]+)"
+ return re.sub(linkify_regex, linkify_word, escaped_text, flags=re.UNICODE)
+
async def process_input(self, input_data: str):
special = re.match(
rf"{MESSAGE_ID_REGEX_STRING}?(?:{REACTION_CHANGE_REGEX_STRING}{EMOJI_CHAR_OR_NAME_REGEX_STRING}\s*|s/)",
@@ -438,7 +458,8 @@ class SlackBuffer(ABC):
else:
if input_data.startswith(("//", " ")):
input_data = input_data[1:]
- await self.post_message(htmlescape(input_data))
+ text = await self.linkify_text(input_data)
+ await self.post_message(text)
def _buffer_input_cb(self, data: str, buffer: str, input_data: str) -> int:
run_async(self.process_input(input_data))