diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2023-10-13 00:24:01 +0200 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 11:32:54 +0100 |
commit | 0cd9168d99b673ee0adf6528c1ac5afc4de7eff0 (patch) | |
tree | 76b7fb6e6ad799eba88e07ee5c7574ca0cbd850e /slack | |
parent | 01c1a010f9d03f69aab27c40f2773147a0fb6bd5 (diff) | |
download | wee-slack-0cd9168d99b673ee0adf6528c1ac5afc4de7eff0.tar.gz |
Update message when subscribed/unsubscribed
Pyright doesn't allow me to set subscribed/last_read in _message_json
since some of the _message_json types don't have them, so had to use
separate attributes.
Diffstat (limited to 'slack')
-rw-r--r-- | slack/slack_message.py | 30 | ||||
-rw-r--r-- | slack/slack_workspace.py | 15 |
2 files changed, 32 insertions, 13 deletions
diff --git a/slack/slack_message.py b/slack/slack_message.py index 0335c67..134adfa 100644 --- a/slack/slack_message.py +++ b/slack/slack_message.py @@ -38,6 +38,7 @@ if TYPE_CHECKING: SlackMessageReaction, SlackMessageSubtypeHuddleThreadRoom, ) + from slack_rtm.slack_rtm_message import SlackThreadSubscription from typing_extensions import Literal, assert_never from slack.slack_conversation import SlackConversation @@ -153,6 +154,12 @@ class SlackMessage: self.replies: OrderedDict[SlackTs, SlackMessage] = OrderedDict() self.reply_history_filled = False self.thread_buffer: Optional[SlackThread] = None + self._subscribed: bool = message_json.get("subscribed", False) + self._last_read = ( + SlackTs(self._message_json["last_read"]) + if "last_read" in self._message_json + else None + ) self._deleted = False @property @@ -196,19 +203,13 @@ class SlackMessage: @property def last_read(self) -> Optional[SlackTs]: - if "last_read" in self._message_json: - return SlackTs(self._message_json["last_read"]) + return self._last_read @last_read.setter def last_read(self, value: SlackTs): - if "last_read" in self._message_json: - self._message_json["last_read"] = value - if self.thread_buffer: - self.thread_buffer.set_unread_and_hotlist() - else: - raise SlackError( - self.workspace, "Cannot set last_read on a message without last_read" - ) + self._last_read = value + if self.thread_buffer: + self.thread_buffer.set_unread_and_hotlist() @property def latest_reply(self) -> Optional[SlackTs]: @@ -292,6 +293,13 @@ class SlackMessage: self._message_json["room"] = room self._rendered_message = None + async def update_subscribed( + self, subscribed: bool, subscription: SlackThreadSubscription + ): + self._subscribed = subscribed + self.last_read = SlackTs(subscription["last_read"]) + await self.conversation.rerender_message(self) + def _get_reaction(self, reaction_name: str): for reaction in self._message_json.get("reactions", []): if reaction["name"] == reaction_name: @@ -720,7 +728,7 @@ class SlackMessage: if not reply_count: return "" - subscribed_text = " Subscribed" if self._message_json.get("subscribed") else "" + subscribed_text = " Subscribed" if self._subscribed else "" text = f"[ Thread: {self.hash} Replies: {reply_count}{subscribed_text} ]" return " " + with_color(nick_color(str(self.hash)), text) diff --git a/slack/slack_workspace.py b/slack/slack_workspace.py index 992e025..1b60013 100644 --- a/slack/slack_workspace.py +++ b/slack/slack_workspace.py @@ -336,8 +336,9 @@ class SlackWorkspace: channel_id = data["item"]["channel"] elif ( data["type"] == "thread_marked" - and data["subscription"]["type"] == "thread" - ): + or data["type"] == "thread_subscribed" + or data["type"] == "thread_unsubscribed" + ) and data["subscription"]["type"] == "thread": channel_id = data["subscription"]["channel"] elif data["type"] == "sh_room_join" or data["type"] == "sh_room_update": channel_id = data["huddle"]["channel_id"] @@ -391,6 +392,16 @@ class SlackWorkspace: ) if message: message.last_read = SlackTs(data["subscription"]["last_read"]) + elif ( + data["type"] == "thread_subscribed" + or data["type"] == "thread_unsubscribed" + ) and data["subscription"]["type"] == "thread": + message = channel.messages.get( + SlackTs(data["subscription"]["thread_ts"]) + ) + if message: + subscribed = data["type"] == "thread_subscribed" + await message.update_subscribed(subscribed, data["subscription"]) elif data["type"] == "sh_room_join" or data["type"] == "sh_room_update": await channel.update_message_room(data) elif data["type"] == "user_typing": |