aboutsummaryrefslogtreecommitdiffstats
path: root/slack
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2024-01-10 22:37:29 +0100
committerTrygve Aaberge <trygveaa@gmail.com>2024-02-18 11:32:55 +0100
commitfa0001661c400327c4fdebeb226a08bcdb1a5fd1 (patch)
tree7915745a11f3c32494f202b736bec512177f7452 /slack
parent5dff7099aee39f1a2881387c00c55a3ea1641997 (diff)
downloadwee-slack-fa0001661c400327c4fdebeb226a08bcdb1a5fd1.tar.gz
Fix bots with multiple names only being added once to nicklist
If a bot uses multiple nicks, we have to add all of them to the nicklist, so none of the nicks are shown as offline.
Diffstat (limited to 'slack')
-rw-r--r--slack/commands.py2
-rw-r--r--slack/slack_buffer.py4
-rw-r--r--slack/slack_conversation.py68
-rw-r--r--slack/slack_message.py24
-rw-r--r--slack/slack_thread.py21
-rw-r--r--slack/slack_user.py7
6 files changed, 42 insertions, 84 deletions
diff --git a/slack/commands.py b/slack/commands.py
index cc2cbcb..ed57953 100644
--- a/slack/commands.py
+++ b/slack/commands.py
@@ -489,7 +489,7 @@ def completion_nicks_cb(
if slack_buffer is None:
return weechat.WEECHAT_RC_OK
- buffer_nicks = [user.nick.raw_nick for user in slack_buffer.members]
+ buffer_nicks = [nick.raw_nick for nick in slack_buffer.members]
for nick in buffer_nicks:
weechat.completion_list_add(
completion,
diff --git a/slack/slack_buffer.py b/slack/slack_buffer.py
index d627c5c..fad5689 100644
--- a/slack/slack_buffer.py
+++ b/slack/slack_buffer.py
@@ -21,7 +21,7 @@ import weechat
from slack.log import print_error
from slack.shared import MESSAGE_ID_REGEX_STRING, REACTION_CHANGE_REGEX_STRING, shared
from slack.slack_message import SlackMessage, SlackTs
-from slack.slack_user import SlackUser
+from slack.slack_user import Nick
from slack.task import gather, run_async
from slack.util import get_callback_name, htmlescape
@@ -207,7 +207,7 @@ class SlackBuffer(ABC):
@property
@abstractmethod
- def members(self) -> Generator[SlackUser, None, None]:
+ def members(self) -> Generator[Nick, None, None]:
raise NotImplementedError()
@property
diff --git a/slack/slack_conversation.py b/slack/slack_conversation.py
index d86259d..8b2bb67 100644
--- a/slack/slack_conversation.py
+++ b/slack/slack_conversation.py
@@ -19,7 +19,6 @@ from typing import (
import weechat
from slack.error import SlackApiError, SlackError
-from slack.log import print_exception_once
from slack.python_compatibility import removeprefix
from slack.shared import shared
from slack.slack_buffer import SlackBuffer
@@ -30,7 +29,7 @@ from slack.slack_message import (
SlackTs,
)
from slack.slack_thread import SlackThread
-from slack.slack_user import SlackBot, SlackUser, nick_color
+from slack.slack_user import Nick, SlackUser
from slack.task import Task, gather, run_async
from slack.util import unhtmlescape, with_color
@@ -155,7 +154,7 @@ class SlackConversation(SlackBuffer):
self._im_user: Optional[SlackUser] = None
self._mpim_users: Optional[List[SlackUser]] = None
self._messages: OrderedDict[SlackTs, SlackMessage] = OrderedDict()
- self._nicklist: Dict[Union[SlackUser, SlackBot], str] = {}
+ self._nicklist: Dict[Nick, str] = {}
self.nicklist_needs_refresh = True
self.message_hashes = SlackConversationMessageHashes(self)
@@ -219,10 +218,10 @@ class SlackConversation(SlackBuffer):
return "conversation"
@property
- def members(self) -> Generator[SlackUser, None, None]:
- for user_or_bot in self._nicklist:
- if isinstance(user_or_bot, SlackUser):
- yield user_or_bot
+ def members(self) -> Generator[Nick, None, None]:
+ for nick in self._nicklist:
+ if nick.type == "user":
+ yield nick
@property
def messages(self) -> Mapping[SlackTs, SlackMessage]:
@@ -553,44 +552,25 @@ class SlackConversation(SlackBuffer):
*(self.workspace.users[user_id] for user_id in members)
)
for user in users:
- self.nicklist_add_user(user)
+ self.nicklist_add_nick(user.nick)
- def nicklist_add_user(
- self, user: Optional[Union[SlackUser, SlackBot]], nick: Optional[str] = None
- ):
- if user in self._nicklist or self.type == "im":
- return
-
- nicklist_nick = nick if nick else user.nick.raw_nick if user else None
- if nicklist_nick is None:
+ def nicklist_add_nick(self, nick: Nick):
+ if nick in self._nicklist or self.type == "im":
return
# TODO: weechat.color.nicklist_away
- color = (
- ""
- if not shared.config.look.color_nicks_in_nicklist
- else nick_color(nicklist_nick)
- if user is None
- or 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
+ color = nick.color if shared.config.look.color_nicks_in_nicklist else ""
+ visible = 1 if nick.type == "user" else 0
+
nick_pointer = weechat.nicklist_add_nick(
- self.buffer_pointer, "", nicklist_nick, color, prefix, "", visible
+ self.buffer_pointer, "", nick.raw_nick, color, nick.suffix, "", visible
)
- if user is not None:
- self._nicklist[user] = nick_pointer
+ self._nicklist[nick] = nick_pointer
- def nicklist_remove_user(self, user: Union[SlackUser, SlackBot]):
+ def nicklist_remove_nick(self, nick: Nick):
if self.type == "im":
return
- nick_pointer = self._nicklist.pop(user)
+ nick_pointer = self._nicklist.pop(nick)
weechat.nicklist_remove_nick(self.buffer_pointer, nick_pointer)
def display_thread_replies(self) -> bool:
@@ -732,18 +712,12 @@ class SlackConversation(SlackBuffer):
async def print_message(self, message: SlackMessage):
await super().print_message(message)
+
nick = await message.nick()
- try:
- sender = await message.sender
- if sender is not None:
- if message.subtype in ["channel_leave", "group_leave"]:
- self.nicklist_remove_user(sender)
- else:
- self.nicklist_add_user(sender, nick.raw_nick)
- except Exception as e:
- self.nicklist_add_user(None, nick.raw_nick)
- if isinstance(e, SlackApiError) and e.response["error"] != "bots_not_found":
- print_exception_once(e)
+ if message.subtype in ["channel_leave", "group_leave"]:
+ self.nicklist_remove_nick(nick)
+ else:
+ self.nicklist_add_nick(nick)
async def mark_read(self):
last_read_line_ts = self.last_read_line_ts()
diff --git a/slack/slack_message.py b/slack/slack_message.py
index c188359..3e1ea20 100644
--- a/slack/slack_message.py
+++ b/slack/slack_message.py
@@ -21,8 +21,6 @@ from slack.shared import shared
from slack.slack_emoji import get_emoji
from slack.slack_user import (
Nick,
- SlackBot,
- SlackUser,
get_bot_nick,
get_user_nick,
name_from_user_profile,
@@ -398,13 +396,6 @@ class SlackMessage:
return self._message_json.get("bot_id")
@property
- async def sender(self) -> Union[SlackUser, SlackBot, None]:
- if "user" in self._message_json:
- return await self.workspace.users[self._message_json["user"]]
- elif "bot_id" in self._message_json:
- return await self.workspace.bots[self._message_json["bot_id"]]
-
- @property
def reactions(self) -> List[SlackMessageReaction]:
return self._message_json.get("reactions", [])
@@ -539,12 +530,6 @@ class SlackMessage:
if self.sender_bot_id:
tags.append(f"slack_bot_id_{self.sender_bot_id}")
- user = (
- await self.workspace.users[self.sender_user_id]
- if self.sender_user_id
- else None
- )
-
if self._message_json.get("subtype") in ["channel_join", "group_join"]:
tags.append("slack_join")
log_tags = ["log4"]
@@ -560,12 +545,9 @@ class SlackMessage:
tags.append("slack_action")
else:
if shared.weechat_version >= 0x04000000:
- if user:
- tags.append(f"prefix_nick_{user.nick.color}")
- else:
- tags.append(f"prefix_nick_{nick.color}")
+ tags.append(f"prefix_nick_{nick.color}")
- if user and user.is_self:
+ if self.sender_user_id == self.workspace.my_user.id:
tags.append("self_msg")
log_tags = ["notify_none", "no_highlight", "log1"]
else:
@@ -611,7 +593,7 @@ class SlackMessage:
bot = await self.workspace.bots[self._message_json["bot_id"]]
return bot.nick
else:
- return Nick("", "Unknown", "")
+ return Nick("", "Unknown", "", "unknown")
async def _render_prefix(self) -> str:
if self._message_json.get("subtype") in ["channel_join", "group_join"]:
diff --git a/slack/slack_thread.py b/slack/slack_thread.py
index cf1fda6..284bdab 100644
--- a/slack/slack_thread.py
+++ b/slack/slack_thread.py
@@ -3,10 +3,9 @@ from __future__ import annotations
from itertools import chain
from typing import TYPE_CHECKING, Dict, Generator, Mapping, Optional, Set, Tuple
-from slack.log import print_exception_once
from slack.slack_buffer import SlackBuffer
from slack.slack_message import SlackMessage, SlackTs
-from slack.slack_user import SlackUser
+from slack.slack_user import Nick
from slack.task import gather
if TYPE_CHECKING:
@@ -20,7 +19,7 @@ class SlackThread(SlackBuffer):
def __init__(self, parent: SlackMessage) -> None:
super().__init__()
self.parent = parent
- self._reply_users: Set[SlackUser] = set()
+ self._reply_nicks: Set[Nick] = set()
@property
def workspace(self) -> SlackWorkspace:
@@ -35,9 +34,10 @@ class SlackThread(SlackBuffer):
return "thread"
@property
- def members(self) -> Generator[SlackUser, None, None]:
- for user in self._reply_users:
- yield user
+ def members(self) -> Generator[Nick, None, None]:
+ for nick in self._reply_nicks:
+ if nick.type == "user":
+ yield nick
@property
def messages(self) -> Mapping[SlackTs, SlackMessage]:
@@ -118,13 +118,8 @@ class SlackThread(SlackBuffer):
async def print_message(self, message: SlackMessage):
await super().print_message(message)
- sender_user_id = message.sender_user_id
- if sender_user_id is not None:
- try:
- sender_user = await self.workspace.users[sender_user_id]
- self._reply_users.add(sender_user)
- except Exception as e:
- print_exception_once(e)
+ nick = await message.nick()
+ self._reply_nicks.add(nick)
async def mark_read(self):
# subscriptions.thread.mark is only available for session tokens
diff --git a/slack/slack_user.py b/slack/slack_user.py
index 2d3c6b6..07a87d2 100644
--- a/slack/slack_user.py
+++ b/slack/slack_user.py
@@ -15,6 +15,7 @@ if TYPE_CHECKING:
from slack_api.slack_conversations_history import SlackMessageUserProfile
from slack_api.slack_usergroups_info import SlackUsergroupInfo
from slack_api.slack_users_info import SlackProfile, SlackUserInfo
+ from typing_extensions import Literal
from slack.slack_workspace import SlackWorkspace
@@ -24,6 +25,10 @@ class Nick:
color: str
raw_nick: str
suffix: str
+ type: Literal["user", "bot", "unknown"]
+
+ def __hash__(self) -> int:
+ return hash(self.raw_nick)
def format(self, colorize: bool = False) -> str:
color = self.color if colorize else ""
@@ -67,6 +72,7 @@ def get_user_nick(
nick_color(nick, is_self),
nick,
suffix,
+ "user",
)
@@ -76,6 +82,7 @@ def get_bot_nick(nick: str) -> Nick:
nick_color(nick),
nick,
shared.config.look.bot_user_suffix.value,
+ "bot",
)