Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions pyrogram/methods/chats/get_dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -102,22 +104,34 @@ 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)

for dialog in dialogs:
await sleep(0)
yield dialog

current += 1

if current >= total:
return