From b6d8311a5565a84986a3bd07d9539c55d9b7ca4e Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 13 Oct 2022 13:40:25 +0200 Subject: [PATCH] Fix iter_messages was stopping too early in some channels Closes #3949. --- telethon/client/messages.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/telethon/client/messages.py b/telethon/client/messages.py index 2bff203ee..805be4f6f 100644 --- a/telethon/client/messages.py +++ b/telethon/client/messages.py @@ -204,7 +204,20 @@ async def _load_next_chunk(self): message._finish_init(self.client, entities, self.entity) self.buffer.append(message) - if len(r.messages) < self.request.limit: + # Some channels are "buggy" and may return less messages than + # requested (apparently, the messages excluded are, for example, + # "not displayable due to local laws"). + # + # This means it's not safe to rely on `len(r.messages) < req.limit` as + # the stop condition. Unfortunately more requests must be made. + # + # However we can still check if the highest ID is equal to or lower + # than the limit, in which case there won't be any more messages + # because the lowest message ID is 1. + # + # We also assume the API will always return, at least, one message if + # there is more to fetch. + if not r.messages or r.messages[0].id <= self.request.limit: return True # Get the last message that's not empty (in some rare cases