aboutsummaryrefslogtreecommitdiffstats
path: root/slack
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2023-08-20 13:24:51 +0200
committerTrygve Aaberge <trygveaa@gmail.com>2024-02-18 11:32:53 +0100
commit2d72de0c88a3889182d40e306ebb1e5c9a38ef47 (patch)
treebe4266ea6388ca1bee257329477916f1e50fa80f /slack
parent60f560ef4d70350524514dfb5a516ec4e7c558f9 (diff)
downloadwee-slack-2d72de0c88a3889182d40e306ebb1e5c9a38ef47.tar.gz
Add tags to messages
Diffstat (limited to 'slack')
-rw-r--r--slack/slack_conversation.py11
-rw-r--r--slack/slack_message.py56
-rw-r--r--slack/slack_user.py19
3 files changed, 68 insertions, 18 deletions
diff --git a/slack/slack_conversation.py b/slack/slack_conversation.py
index 7c0e90f..8de3a26 100644
--- a/slack/slack_conversation.py
+++ b/slack/slack_conversation.py
@@ -217,7 +217,7 @@ class SlackConversation:
await gather(*(message.render() for message in messages))
for message in reversed(messages):
- self.print_message(message, await message.render())
+ await self.print_message(message, backlog=True)
print(f"history w/o fetch took: {time.time() - start}")
self.history_filled = True
@@ -245,8 +245,7 @@ class SlackConversation:
async def add_message(self, message: SlackMessage):
self._messages[message.ts] = message
if self.history_filled:
- rendered = await message.render()
- self.print_message(message, rendered)
+ await self.print_message(message)
else:
weechat.buffer_set(
self.buffer_pointer, "hotlist", str(message.priority.value)
@@ -278,8 +277,10 @@ class SlackConversation:
self._typing_self_last_sent = now
self.workspace.send_typing(self.id)
- def print_message(self, message: SlackMessage, rendered: str):
- weechat.prnt_date_tags(self.buffer_pointer, message.ts.major, "", rendered)
+ async def print_message(self, message: SlackMessage, backlog: bool = False):
+ tags = await message.tags(backlog=backlog)
+ rendered = await message.render()
+ weechat.prnt_date_tags(self.buffer_pointer, message.ts.major, tags, rendered)
def _buffer_input_cb(self, data: str, buffer: str, input_data: str) -> int:
weechat.prnt(buffer, "Text: %s" % input_data)
diff --git a/slack/slack_message.py b/slack/slack_message.py
index bc667d7..376695c 100644
--- a/slack/slack_message.py
+++ b/slack/slack_message.py
@@ -25,7 +25,7 @@ class MessagePriority(Enum):
HIGHLIGHT = 3
-class SlackTs:
+class SlackTs(str):
def __init__(self, ts: str):
self.major, self.minor = [int(x) for x in ts.split(".", 1)]
@@ -37,6 +37,9 @@ class SlackTs:
def __hash__(self) -> int:
return hash((self.major, self.minor))
+ def __repr__(self) -> str:
+ return f"SlackTs('{self}')"
+
class SlackMessage:
def __init__(self, conversation: SlackConversation, message_json: SlackMessageDict):
@@ -50,13 +53,54 @@ class SlackMessage:
return self.conversation.workspace
@property
+ def is_bot_message(self) -> bool:
+ return (
+ "subtype" in self._message_json
+ and self._message_json["subtype"] == "bot_message"
+ )
+
+ @property
def sender_user_id(self) -> Optional[str]:
- return self._message_json.get("user")
+ if not self.is_bot_message:
+ return self._message_json.get("user")
+
+ @property
+ def sender_bot_id(self) -> Optional[str]:
+ if self.is_bot_message:
+ return self._message_json.get("bot_id")
@property
def priority(self) -> MessagePriority:
return MessagePriority.MESSAGE
+ async def tags(self, backlog: bool = False) -> str:
+ nick = await self._prefix(colorize=False, only_nick=True)
+ tags = ["slack_privmsg", f"slack_ts_{self.ts}", f"nick_{nick}"]
+
+ if self.sender_user_id:
+ user_or_bot = await self.workspace.users[self.sender_user_id]
+ elif self.sender_bot_id:
+ user_or_bot = await self.workspace.bots[self.sender_bot_id]
+ else:
+ user_or_bot = None
+
+ if user_or_bot and shared.weechat_version >= 0x04000000:
+ tags.append(f"prefix_nick_{user_or_bot.nick_color()}")
+
+ if self.is_bot_message:
+ tags.append("bot_message")
+ if self.sender_user_id:
+ tags.append(f"slack_user_id_{self.sender_user_id}")
+ if self.sender_bot_id:
+ tags.append(f"slack_bot_id_{self.sender_bot_id}")
+
+ if backlog:
+ tags += ["no_highlight", "notify_none", "logger_backlog", "no_log"]
+ else:
+ tags += ["notify_message", "log1"]
+
+ return ",".join(tags)
+
async def render(self) -> str:
if self._rendered is not None:
return self._rendered
@@ -67,20 +111,20 @@ class SlackMessage:
self._rendered = f"{prefix}\t{message}"
return self._rendered
- async def _prefix(self) -> str:
+ async def _prefix(self, colorize: bool = True, only_nick: bool = False) -> str:
if (
"subtype" in self._message_json
and self._message_json["subtype"] == "bot_message"
):
username = self._message_json.get("username")
if username:
- return format_bot_nick(username, colorize=True)
+ return format_bot_nick(username, colorize=colorize, only_nick=only_nick)
else:
bot = await self.workspace.bots[self._message_json["bot_id"]]
- return bot.nick(colorize=True)
+ return bot.nick(colorize=colorize, only_nick=only_nick)
else:
user = await self.workspace.users[self._message_json["user"]]
- return user.nick(colorize=True)
+ return user.nick(colorize=colorize, only_nick=only_nick)
def _item_prefix(self, item_id: str):
if item_id.startswith("#") or item_id.startswith("@"):
diff --git a/slack/slack_user.py b/slack/slack_user.py
index 9a5989d..e888b41 100644
--- a/slack/slack_user.py
+++ b/slack/slack_user.py
@@ -35,13 +35,16 @@ def name_from_user_info_without_spaces(
return _name_from_user_info(workspace, info).replace(" ", "")
-def format_bot_nick(nick: str, colorize: bool = False) -> str:
+def format_bot_nick(nick: str, colorize: bool = False, only_nick: bool = False) -> str:
nick = nick.replace(" ", "")
if colorize:
nick = with_color(nick_color(nick), nick)
- return nick + shared.config.look.bot_user_suffix.value
+ if not only_nick:
+ nick = nick + shared.config.look.bot_user_suffix.value
+
+ return nick
class SlackUser:
@@ -54,13 +57,13 @@ class SlackUser:
info_response = await workspace.api.fetch_user_info(id)
return cls(workspace, info_response["user"])
- def nick(self, colorize: bool = False) -> str:
+ def nick(self, colorize: bool = False, only_nick: bool = False) -> str:
nick = self._name_without_spaces()
if colorize:
nick = with_color(self.nick_color(), nick)
- if self._info["profile"]["team"] != self.workspace.id:
+ if not only_nick and self._info["profile"]["team"] != self.workspace.id:
nick += shared.config.look.external_user_suffix.value
return nick
@@ -87,11 +90,13 @@ class SlackBot:
info_response = await workspace.api.fetch_bot_info(id)
return cls(workspace, info_response["bot"])
- def nick(self, colorize: bool = False) -> str:
- return format_bot_nick(self._info["name"], colorize)
+ def nick(self, colorize: bool = False, only_nick: bool = False) -> str:
+ return format_bot_nick(
+ self._info["name"], colorize=colorize, only_nick=only_nick
+ )
def nick_color(self):
- return nick_color(self._info["name"].replace(" ", ""))
+ return nick_color(self.nick(colorize=False, only_nick=True))
class SlackUsergroup: