From 6d2a5dada5e5f03d67a5aae26727911f613a208c Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 22 Dec 2022 20:23:06 +0100 Subject: [PATCH] Fix PERSISTENT_TIMESTAMP_EMPTY for new entries with pts 1, count 0 Because Telegram can't actually use 0 for the pts, it uses 1, even if the count is 0. This forces the next update to use 2, or else it could not be fetched when using an offset of 1 (despite the count being 0 on the first update, which should not have bumped the second update to use 2). This caused Telethon to create an initial state of 0 for the new entry (and also "incorrectly" detected following updates as gaps, which would quickly trigger the call to get difference with a bad pts). Now Telethon is aware of this special-case and will not initialize state as 0, even if that's not the "correct" thing to do. --- telethon/_updates/messagebox.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/telethon/_updates/messagebox.py b/telethon/_updates/messagebox.py index 72081eb9..e5f67f93 100644 --- a/telethon/_updates/messagebox.py +++ b/telethon/_updates/messagebox.py @@ -572,8 +572,14 @@ class MessageBox: if pts.entry in self.map: self.map[pts.entry].pts = pts.pts else: + # When a chat is migrated to a megagroup, the first update can be a `ReadChannelInbox` + # with `pts = 1, pts_count = 0` followed by a `NewChannelMessage` with `pts = 2, pts_count=1`. + # Note how the `pts` for the message is 2 and not 1 unlike the case described before! + # This is likely because the `pts` cannot be 0 (or it would fail with PERSISTENT_TIMESTAMP_EMPTY), + # which forces the first update to be 1. But if we got difference with 1 and the second update + # also used 1, we would miss it, so Telegram probably uses 2 to work around that. self.map[pts.entry] = State( - pts=pts.pts - (0 if pts.pts_count else 1), + pts=(pts.pts - (0 if pts.pts_count else 1)) or 1, deadline=next_updates_deadline() )