From fd08d5325393e1f650e682ef3cb9427507ea0d2b Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sat, 3 Feb 2018 15:42:43 +0100 Subject: [PATCH] Trust the server will not send duplicates This change was also suggested by the test on the previous commit. --- telethon/update_state.py | 33 +++++---------------------------- 1 file changed, 5 insertions(+), 28 deletions(-) diff --git a/telethon/update_state.py b/telethon/update_state.py index 9f308d89..f98c0c04 100644 --- a/telethon/update_state.py +++ b/telethon/update_state.py @@ -30,7 +30,6 @@ class UpdateState: self.handlers = [] self._updates_lock = RLock() self._updates = Queue() - self._latest_updates = deque(maxlen=10) # https://core.telegram.org/api/updates self._state = tl.updates.State(0, 0, datetime.now(), 0, 0) @@ -130,34 +129,12 @@ class UpdateState: self._state = update return # Nothing else to be done - pts = getattr(update, 'pts', self._state.pts) - if hasattr(update, 'pts') and pts <= self._state.pts: - __log__.info('Ignoring %s, already have it', update) - return # We already handled this update - - self._state.pts = pts - - # TODO There must be a better way to handle updates rather than - # keeping a queue with the latest updates only, and handling - # the 'pts' correctly should be enough. However some updates - # like UpdateUserStatus (even inside UpdateShort) will be called - # repeatedly very often if invoking anything inside an update - # handler. TODO Figure out why. - """ - client = TelegramClient('anon', api_id, api_hash, update_workers=1) - client.connect() - def handle(u): - client.get_me() - client.add_update_handler(handle) - input('Enter to exit.') - """ - data = pickle.dumps(update.to_dict()) - if data in self._latest_updates: - __log__.info('Ignoring %s, already have it', update) - return # Duplicated too - - self._latest_updates.append(data) + if hasattr(update, 'pts'): + self._state.pts = update.pts + # After running the script for over an hour and receiving over + # 1000 updates, the only duplicates received were users going + # online or offline. We can trust the server until new reports. if isinstance(update, tl.UpdateShort): self._updates.put(update.update) # Expand "Updates" into "Update", and pass these to callbacks.