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 /slack/slack_buffer.py | |
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.
Diffstat (limited to 'slack/slack_buffer.py')
-rw-r--r-- | slack/slack_buffer.py | 21 |
1 files changed, 16 insertions, 5 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: |