diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2023-10-01 21:31:08 +0200 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 11:32:54 +0100 |
commit | 6cf20324f97fa49bade19ecf1f46e2900c2eda08 (patch) | |
tree | a4322caa518f577d8d7154cf4a45509b726ecfb3 /slack | |
parent | 2733d6a0faf3ea9150ee8e16950ca3d54a6361e6 (diff) | |
download | wee-slack-6cf20324f97fa49bade19ecf1f46e2900c2eda08.tar.gz |
Correctly tag unread messages from history
Now notify tags are set based on last_read so both read and unread
messages gets the correct tags irregardless if they come from history or
websocket.
Diffstat (limited to 'slack')
-rw-r--r-- | slack/slack_buffer.py | 10 | ||||
-rw-r--r-- | slack/slack_conversation.py | 10 | ||||
-rw-r--r-- | slack/slack_message.py | 9 | ||||
-rw-r--r-- | slack/slack_thread.py | 6 |
4 files changed, 28 insertions, 7 deletions
diff --git a/slack/slack_buffer.py b/slack/slack_buffer.py index 0e16845..18a0f89 100644 --- a/slack/slack_buffer.py +++ b/slack/slack_buffer.py @@ -176,6 +176,11 @@ class SlackBuffer(ABC): def messages(self) -> Mapping[SlackTs, SlackMessage]: raise NotImplementedError() + @property + @abstractmethod + def last_read(self) -> SlackTs: + raise NotImplementedError() + @abstractmethod async def get_name_and_buffer_props(self) -> Tuple[str, Dict[str, str]]: raise NotImplementedError() @@ -246,9 +251,10 @@ class SlackBuffer(ABC): self._typing_self_last_sent = now self.workspace.send_typing(self) - async def print_message(self, message: SlackMessage, backlog: bool = False): + async def print_message(self, message: SlackMessage): rendered = await message.render(self.context) - tags = await message.tags(backlog=backlog) + backlog = message.ts <= self.last_read + tags = await message.tags(backlog) 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: diff --git a/slack/slack_conversation.py b/slack/slack_conversation.py index 97fd85b..cb5e53f 100644 --- a/slack/slack_conversation.py +++ b/slack/slack_conversation.py @@ -152,6 +152,10 @@ class SlackConversation(SlackBuffer): def buffer_type(self) -> Literal["private", "channel"]: return "private" if self.type in ("im", "mpim") else "channel" + @property + def last_read(self) -> SlackTs: + return SlackTs(self._info["last_read"]) + async def sort_key(self) -> str: type_sort_key = { "channel": 0, @@ -335,7 +339,7 @@ class SlackConversation(SlackBuffer): await gather(*(message.render(self.context) for message in messages)) for message in messages: - await self.print_message(message, backlog=True) + await self.print_message(message) self.history_filled = True self.history_pending = False @@ -512,8 +516,8 @@ class SlackConversation(SlackBuffer): 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) + async def print_message(self, message: SlackMessage): + await super().print_message(message) sender = await message.sender if message.subtype in ["channel_leave", "group_leave"]: self.nicklist_remove_user(sender) diff --git a/slack/slack_message.py b/slack/slack_message.py index 36faa5f..968c1f7 100644 --- a/slack/slack_message.py +++ b/slack/slack_message.py @@ -195,6 +195,13 @@ class SlackMessage: return self.conversation.messages.get(self.thread_ts) @property + def last_read(self) -> SlackTs: + if "last_read" in self._message_json: + return SlackTs(self._message_json["last_read"]) + else: + return SlackTs("0.0") + + @property def is_bot_message(self) -> bool: return ( "subtype" in self._message_json @@ -290,7 +297,7 @@ class SlackMessage: return False - async def tags(self, backlog: bool = False) -> str: + async def tags(self, backlog: bool) -> str: nick = await self.nick(colorize=False, only_nick=True) tags = [f"slack_ts_{self.ts}", f"nick_{nick}"] diff --git a/slack/slack_thread.py b/slack/slack_thread.py index 1c94c7d..44a6751 100644 --- a/slack/slack_thread.py +++ b/slack/slack_thread.py @@ -30,6 +30,10 @@ class SlackThread(SlackBuffer): def messages(self) -> Mapping[SlackTs, SlackMessage]: return self.parent.replies + @property + def last_read(self) -> SlackTs: + return self.parent.last_read + async def get_name_and_buffer_props(self) -> Tuple[str, Dict[str, str]]: conversation_name = await self.parent.conversation.name_with_prefix("full_name") name = f"{conversation_name}.${self.parent.hash}" @@ -58,7 +62,7 @@ class SlackThread(SlackBuffer): with self.loading(): messages = chain([self.parent], self.parent.replies.values()) for message in messages: - await self.print_message(message, backlog=True) + await self.print_message(message) async def fill_history(self): if self.history_pending: |