Skip to content

Commit 8ac84cd

Browse files
(FIX): get_dialogs.py (#195)
- Fix infinite loop in get_dialogs() by preventing repeated dialogs - Added tracking of seen dialog IDs to avoid yielding duplicates - Added check for None top_message to safely stop pagination - Preserved support for limit, pinned_only, and chat_list parameters - Ensured correct offset updates for reliable pagination - fix: skip dialogs with no parsed chat to avoid errors fixes #194
1 parent fc838cf commit 8ac84cd

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

pyrogram/methods/chats/get_dialogs.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from typing import AsyncGenerator, Optional
2121

2222
import pyrogram
23-
from pyrogram import types, raw, utils
23+
from pyrogram import raw, types, utils
2424

2525

2626
class GetDialogs:
@@ -58,19 +58,21 @@ async def get_dialogs(
5858
"""
5959
current = 0
6060
total = limit or (1 << 31) - 1
61-
limit = min(100, total)
61+
request_limit = min(100, total)
6262

6363
offset_date = 0
6464
offset_id = 0
6565
offset_peer = raw.types.InputPeerEmpty()
6666

67+
seen_dialog_ids = set()
68+
6769
while True:
6870
r = await self.invoke(
6971
raw.functions.messages.GetDialogs(
7072
offset_date=offset_date,
7173
offset_id=offset_id,
7274
offset_peer=offset_peer,
73-
limit=limit,
75+
limit=request_limit,
7476
hash=0,
7577
exclude_pinned=not pinned_only,
7678
folder_id=chat_list
@@ -102,22 +104,34 @@ async def get_dialogs(
102104
if not isinstance(dialog, raw.types.Dialog):
103105
continue
104106

105-
dialogs.append(types.Dialog._parse(self, dialog, messages, users, chats))
107+
parsed = types.Dialog._parse(self, dialog, messages, users, chats)
108+
if parsed is None:
109+
continue
110+
111+
if parsed.chat is None:
112+
continue
113+
114+
if parsed.chat.id in seen_dialog_ids:
115+
continue
116+
117+
seen_dialog_ids.add(parsed.chat.id)
118+
dialogs.append(parsed)
106119

107120
if not dialogs:
108121
return
109122

110123
last = dialogs[-1]
111124

125+
if last.top_message is None:
126+
return
127+
112128
offset_id = last.top_message.id
113129
offset_date = utils.datetime_to_timestamp(last.top_message.date)
114130
offset_peer = await self.resolve_peer(last.chat.id)
115131

116132
for dialog in dialogs:
117133
await sleep(0)
118134
yield dialog
119-
120135
current += 1
121-
122136
if current >= total:
123137
return

0 commit comments

Comments
 (0)