aboutsummaryrefslogtreecommitdiffstats
path: root/slack/slack_conversation.py
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 /slack/slack_conversation.py
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.
Diffstat (limited to 'slack/slack_conversation.py')
-rw-r--r--slack/slack_conversation.py50
1 files changed, 43 insertions, 7 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)