diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-09 23:35:09 +0100 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 13:01:40 +0100 |
commit | 312553ce2f33a120bee07b356d92ce848ab4ce86 (patch) | |
tree | cff37dbaab34b55b03ce29389123164e241bf6b2 | |
parent | 65c9b622572038a634bee3897b2e97989a050780 (diff) | |
download | wee-slack-312553ce2f33a120bee07b356d92ce848ab4ce86.tar.gz |
Don't print the same message multiple times
When joining a channel you seem to get the last messages for it over the
WebSocket. When joining a channel you've parted and you still have the
buffer for open, those messages will already be in the buffer, so we
have to make sure their not printed multiple times.
I've also been told that messages can appear multiple times in the
reconnect brach, which this should fix too.
-rw-r--r-- | slack/slack_buffer.py | 21 | ||||
-rw-r--r-- | slack/slack_conversation.py | 15 | ||||
-rw-r--r-- | slack/slack_thread.py | 10 |
3 files changed, 32 insertions, 14 deletions
diff --git a/slack/slack_buffer.py b/slack/slack_buffer.py index 761b9a3..65f2295 100644 --- a/slack/slack_buffer.py +++ b/slack/slack_buffer.py @@ -63,7 +63,7 @@ def tags_set_notify_none(tags: List[str]) -> List[str]: def modify_buffer_line(buffer_pointer: str, ts: SlackTs, new_text: str): if not buffer_pointer: - return + return False own_lines = weechat.hdata_pointer( weechat.hdata_get("buffer"), buffer_pointer, "own_lines" @@ -79,14 +79,14 @@ def modify_buffer_line(buffer_pointer: str, ts: SlackTs, new_text: str): line_pointer = weechat.hdata_move(weechat.hdata_get("line"), line_pointer, -1) if not line_pointer: - return + return False if shared.weechat_version >= 0x04000000: data = weechat.hdata_pointer(weechat.hdata_get("line"), line_pointer, "data") weechat.hdata_update( weechat.hdata_get("line_data"), data, {"message": new_text} ) - return + return True # Find all lines for the message pointers: List[str] = [] @@ -96,7 +96,7 @@ def modify_buffer_line(buffer_pointer: str, ts: SlackTs, new_text: str): pointers.reverse() if not pointers: - return + return False if is_last_line: lines = new_text.split("\n") @@ -145,6 +145,7 @@ def modify_buffer_line(buffer_pointer: str, ts: SlackTs, new_text: str): for pointer, line in zip(pointers, lines): data = weechat.hdata_pointer(weechat.hdata_get("line"), pointer, "data") weechat.hdata_update(weechat.hdata_get("line_data"), data, {"message": line}) + return True class SlackBuffer(ABC): @@ -335,7 +336,16 @@ class SlackBuffer(ABC): async def print_message(self, message: SlackMessage): if not self.buffer_pointer: - return + return False + + if self.last_printed_ts is not None and message.ts <= self.last_printed_ts: + new_text = await message.render_message(context=self.context, rerender=True) + did_update = modify_buffer_line(self.buffer_pointer, message.ts, new_text) + if not did_update: + print_error( + f"Didn't find message with ts {message.ts} when last_printed_ts is {self.last_printed_ts}, message: {message}" + ) + return False rendered = await message.render(self.context) backlog = self.last_read is not None and message.ts <= self.last_read @@ -346,6 +356,7 @@ class SlackBuffer(ABC): else: self.hotlist_tss.add(message.ts) self.last_printed_ts = message.ts + return True def last_read_line_ts(self) -> Optional[SlackTs]: if self.buffer_pointer: diff --git a/slack/slack_conversation.py b/slack/slack_conversation.py index ffea7c6..9085397 100644 --- a/slack/slack_conversation.py +++ b/slack/slack_conversation.py @@ -723,13 +723,16 @@ class SlackConversation(SlackBuffer): await thread_message.thread_buffer.open_buffer(switch) async def print_message(self, message: SlackMessage): - await super().print_message(message) + did_print = await super().print_message(message) - nick = await message.nick() - if message.subtype in ["channel_leave", "group_leave"]: - self.nicklist_remove_nick(nick) - else: - self.nicklist_add_nick(nick) + if did_print: + nick = await message.nick() + if message.subtype in ["channel_leave", "group_leave"]: + self.nicklist_remove_nick(nick) + else: + self.nicklist_add_nick(nick) + + return did_print async def mark_read(self): if not self._is_joined: diff --git a/slack/slack_thread.py b/slack/slack_thread.py index 284bdab..5e6a8bb 100644 --- a/slack/slack_thread.py +++ b/slack/slack_thread.py @@ -117,9 +117,13 @@ class SlackThread(SlackBuffer): self.history_pending = False async def print_message(self, message: SlackMessage): - await super().print_message(message) - nick = await message.nick() - self._reply_nicks.add(nick) + did_print = await super().print_message(message) + + if did_print: + nick = await message.nick() + self._reply_nicks.add(nick) + + return did_print async def mark_read(self): # subscriptions.thread.mark is only available for session tokens |