diff --git a/pyrogram/methods/chats/get_dialogs.py b/pyrogram/methods/chats/get_dialogs.py index cfd8c145a3..3318dd2535 100644 --- a/pyrogram/methods/chats/get_dialogs.py +++ b/pyrogram/methods/chats/get_dialogs.py @@ -20,7 +20,7 @@ from typing import AsyncGenerator, Optional import pyrogram -from pyrogram import types, raw, utils +from pyrogram import raw, types, utils class GetDialogs: @@ -58,19 +58,21 @@ async def get_dialogs( """ current = 0 total = limit or (1 << 31) - 1 - limit = min(100, total) + request_limit = min(100, total) offset_date = 0 offset_id = 0 offset_peer = raw.types.InputPeerEmpty() + seen_dialog_ids = set() + while True: r = await self.invoke( raw.functions.messages.GetDialogs( offset_date=offset_date, offset_id=offset_id, offset_peer=offset_peer, - limit=limit, + limit=request_limit, hash=0, exclude_pinned=not pinned_only, folder_id=chat_list @@ -102,13 +104,27 @@ async def get_dialogs( if not isinstance(dialog, raw.types.Dialog): continue - dialogs.append(types.Dialog._parse(self, dialog, messages, users, chats)) + parsed = types.Dialog._parse(self, dialog, messages, users, chats) + if parsed is None: + continue + + if parsed.chat is None: + continue + + if parsed.chat.id in seen_dialog_ids: + continue + + seen_dialog_ids.add(parsed.chat.id) + dialogs.append(parsed) if not dialogs: return last = dialogs[-1] + if last.top_message is None: + return + offset_id = last.top_message.id offset_date = utils.datetime_to_timestamp(last.top_message.date) offset_peer = await self.resolve_peer(last.chat.id) @@ -116,8 +132,6 @@ async def get_dialogs( for dialog in dialogs: await sleep(0) yield dialog - current += 1 - if current >= total: return