aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2023-09-17 21:54:51 +0200
committerTrygve Aaberge <trygveaa@gmail.com>2024-02-18 11:32:54 +0100
commit1672b9038226ac2001cd89bfcc8d096b995d4bbe (patch)
tree68166c0f5b225296ed30609bf9bf97f539e004e7
parent0f4d849576812a249ee3497ddac7ffdc42f7f7b3 (diff)
downloadwee-slack-1672b9038226ac2001cd89bfcc8d096b995d4bbe.tar.gz
Add/remove nicks from nicklist when messages are printed
This ensures that all nicks that have typed in the channel and still is in it are in the nicklist, so they are not colored as offline. It also removes nicks from the nicklist when they leave a channel.
-rw-r--r--slack/slack_conversation.py50
-rw-r--r--slack/slack_message.py27
-rw-r--r--slack/slack_user.py6
3 files changed, 69 insertions, 14 deletions
diff --git a/slack/slack_conversation.py b/slack/slack_conversation.py
index d6b0e88..fefcdfa 100644
--- a/slack/slack_conversation.py
+++ b/slack/slack_conversation.py
@@ -12,6 +12,7 @@ from slack.shared import shared
from slack.slack_buffer import SlackBuffer
from slack.slack_message import SlackMessage, SlackTs
from slack.slack_thread import SlackThread
+from slack.slack_user import SlackBot, SlackUser, nick_color
from slack.task import gather, run_async
if TYPE_CHECKING:
@@ -111,6 +112,7 @@ class SlackConversation(SlackBuffer):
self._info = info
self._members: Optional[List[str]] = None
self._messages: OrderedDict[SlackTs, SlackMessage] = OrderedDict()
+ self._nicklist: Dict[Union[SlackUser, SlackBot], str] = {}
self.nicklist_needs_refresh = True
self.message_hashes = SlackConversationMessageHashes(self)
@@ -315,16 +317,41 @@ class SlackConversation(SlackBuffer):
if self.nicklist_needs_refresh:
self.nicklist_needs_refresh = False
members = await self.load_members()
- weechat.nicklist_remove_all(self.buffer_pointer)
- await gather(*(self.nicklist_add_user(user_id) for user_id in members))
+ users = await gather(
+ *(self.workspace.users[user_id] for user_id in members)
+ )
+ for user in users:
+ self.nicklist_add_user(user)
- async def nicklist_add_user(self, user_id: str):
- user = await self.workspace.users[user_id]
+ def nicklist_add_user(
+ self, user: Union[SlackUser, SlackBot], nick: Optional[str] = None
+ ):
+ if user in self._nicklist:
+ return
+
+ nicklist_nick = nick if nick else user.nick(only_nick=True)
# TODO: weechat.color.nicklist_away
- color = user.nick_color() if shared.config.look.color_nicks_in_nicklist else ""
- weechat.nicklist_add_nick(
- self.buffer_pointer, "", user.nick(), color, "", "", 1
+ color = (
+ ""
+ if not shared.config.look.color_nicks_in_nicklist
+ else nick_color(nick)
+ if nick and (not isinstance(user, SlackUser) or not user.is_self)
+ else user.nick_color()
+ )
+ prefix = (
+ shared.config.look.external_user_suffix.value
+ if isinstance(user, SlackUser) and user.is_external
+ else ""
)
+ visible = 1 if isinstance(user, SlackUser) else 0
+ nick_pointer = weechat.nicklist_add_nick(
+ self.buffer_pointer, "", nicklist_nick, color, prefix, "", visible
+ )
+ self._nicklist[user] = nick_pointer
+
+ def nicklist_remove_user(self, user: Union[SlackUser, SlackBot]):
+ nick_pointer = self._nicklist.pop(user)
+ weechat.nicklist_remove_nick(self.buffer_pointer, nick_pointer)
def display_thread_replies(self) -> bool:
buffer_value = weechat.buffer_get_string(
@@ -447,3 +474,12 @@ class SlackConversation(SlackBuffer):
if thread_message.thread_buffer is None:
thread_message.thread_buffer = SlackThread(thread_message)
await thread_message.thread_buffer.open_buffer(switch)
+
+ async def print_message(self, message: SlackMessage, backlog: bool = False):
+ await super().print_message(message, backlog)
+ sender = await message.sender
+ if message.subtype in ["channel_leave", "group_leave"]:
+ self.nicklist_remove_user(sender)
+ else:
+ nick = await message.nick(colorize=False, only_nick=True)
+ self.nicklist_add_user(sender, nick)
diff --git a/slack/slack_message.py b/slack/slack_message.py
index bb4d141..6c97e24 100644
--- a/slack/slack_message.py
+++ b/slack/slack_message.py
@@ -17,7 +17,7 @@ from slack.error import (
from slack.log import print_error, print_exception_once
from slack.python_compatibility import removeprefix, removesuffix
from slack.shared import shared
-from slack.slack_user import format_bot_nick, nick_color
+from slack.slack_user import SlackBot, SlackUser, format_bot_nick, nick_color
from slack.task import gather
from slack.util import with_color
from slack.weechat_config import WeeChatColor
@@ -152,6 +152,11 @@ class SlackMessage:
return self.conversation.message_hashes[self.ts]
@property
+ def subtype(self):
+ if "subtype" in self._message_json:
+ return self._message_json["subtype"]
+
+ @property
def thread_ts(self) -> Optional[SlackTs]:
return (
SlackTs(self._message_json["thread_ts"])
@@ -195,6 +200,16 @@ class SlackMessage:
return self._message_json.get("bot_id")
@property
+ async def sender(self) -> Union[SlackUser, SlackBot]:
+ if (
+ "subtype" in self._message_json
+ and self._message_json["subtype"] == "bot_message"
+ ):
+ return await self.workspace.bots[self._message_json["bot_id"]]
+ else:
+ return await self.workspace.users[self._message_json["user"]]
+
+ @property
def priority(self) -> MessagePriority:
return MessagePriority.MESSAGE
@@ -245,7 +260,7 @@ class SlackMessage:
async def tags(self, backlog: bool = False) -> str:
# TODO: Add tags for highlight
- nick = await self._nick(colorize=False, only_nick=True)
+ nick = await self.nick(colorize=False, only_nick=True)
tags = [f"slack_ts_{self.ts}", f"nick_{nick}"]
if self.sender_user_id:
@@ -302,7 +317,7 @@ class SlackMessage:
self._rendered = f"{prefix}\t{message}"
return self._rendered
- async def _nick(self, colorize: bool = True, only_nick: bool = False) -> str:
+ async def nick(self, colorize: bool = True, only_nick: bool = False) -> str:
if (
"subtype" in self._message_json
and self._message_json["subtype"] == "bot_message"
@@ -327,7 +342,7 @@ class SlackMessage:
elif self._message_json.get("subtype") == "me_message":
return removesuffix(weechat.prefix("action"), "\t")
else:
- return await self._nick(colorize=colorize, only_nick=only_nick)
+ return await self.nick(colorize=colorize, only_nick=only_nick)
async def render_prefix(self) -> str:
if self._rendered_prefix is not None:
@@ -363,7 +378,7 @@ class SlackMessage:
else:
inviter_text = ""
- return f"{await self._nick()} {text_action} {text_conversation_name}{inviter_text}"
+ return f"{await self.nick()} {text_action} {text_conversation_name}{inviter_text}"
elif (
"subtype" in self._message_json
@@ -396,7 +411,7 @@ class SlackMessage:
full_text = "\n".join([text_with_files] + attachment_texts)
if self._message_json.get("subtype") == "me_message":
- return f"{await self._nick()} {full_text}"
+ return f"{await self.nick()} {full_text}"
else:
return full_text
diff --git a/slack/slack_user.py b/slack/slack_user.py
index c41f3aa..98d6170 100644
--- a/slack/slack_user.py
+++ b/slack/slack_user.py
@@ -65,13 +65,17 @@ class SlackUser:
def is_self(self) -> bool:
return self.id == self.workspace.my_user.id
+ @property
+ def is_external(self) -> bool:
+ return self._info["profile"]["team"] != self.workspace.id
+
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 not only_nick and self._info["profile"]["team"] != self.workspace.id:
+ if not only_nick and self.is_external:
nick += shared.config.look.external_user_suffix.value
return nick