From f48a69a6b6baaba96cdf29a204e8751600c909b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 11 Sep 2019 13:26:59 +0200 Subject: [PATCH] buffer: Check for already printed events while fetching the backlog. --- matrix/buffer.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/matrix/buffer.py b/matrix/buffer.py index 921b6b2..dab8f08 100644 --- a/matrix/buffer.py +++ b/matrix/buffer.py @@ -869,6 +869,7 @@ class RoomBuffer(object): self.leave_event_id = None # type: Optional[str] self.members_fetched = False self.first_view = True + self.first_backlog_request = True self.unhandled_users = [] # type: List[str] self.inactive_users = [] @@ -979,6 +980,14 @@ class RoomBuffer(object): return "" + @property + def printed_event_ids(self): + for line in self.weechat_buffer.lines: + for tag in line.tags: + if tag.startswith("matrix_id"): + event_id = tag[10:] + yield event_id + @property def read_markers_enabled(self): # type: () -> bool @@ -1688,10 +1697,22 @@ class RoomBuffer(object): self.prev_batch = response.end for event in response.chunk: + # The first backlog request seems to have a race condition going on + # where we receive a message in a sync response, get a prev_batch, + # yet when we request older messages with the prev_batch the same + # message might appear in the room messages response. This only + # seems to happen if the message is relatively recently sent. + # Because of this we check if our first backlog request contains + # some already printed events, if so; skip printing them. + if (self.first_backlog_request + and event.event_id in self.printed_event_ids): + continue + self.old_message(event) self.sort_messages() + self.first_backlog_request = False self.backlog_pending = False def handle_joined_room(self, info):