1818
1919from typing import AsyncGenerator , Optional
2020
21- from pyrogram import types
21+ from pyrogram import types , raw , utils
2222from pyrogram .scaffold import Scaffold
2323
2424
2525class IterDialogs (Scaffold ):
2626 async def iter_dialogs (
2727 self ,
28- limit : int = 0 ,
29- offset_date : int = 0
28+ limit : int = 0
3029 ) -> Optional [AsyncGenerator ["types.Dialog" , None ]]:
3130 """Iterate through a user's dialogs sequentially.
3231
@@ -39,10 +38,6 @@ async def iter_dialogs(
3938 Limits the number of dialogs to be retrieved.
4039 By default, no limit is applied and all dialogs are returned.
4140
42- offset_date (``int``):
43- The offset date in Unix time taken from the top message of a :obj:`~pyrogram.types.Dialog`.
44- Defaults to 0 (most recent dialog).
45-
4641 Returns:
4742 ``Generator``: A generator yielding :obj:`~pyrogram.types.Dialog` objects.
4843
@@ -57,28 +52,50 @@ async def iter_dialogs(
5752 total = limit or (1 << 31 ) - 1
5853 limit = min (100 , total )
5954
60- pinned_dialogs = await self . get_dialogs (
61- pinned_only = True
62- )
55+ offset_date = 0
56+ offset_id = 0
57+ offset_peer = raw . types . InputPeerEmpty ( )
6358
64- for dialog in pinned_dialogs :
65- yield dialog
59+ while True :
60+ r = (await self .send (
61+ raw .functions .messages .GetDialogs (
62+ offset_date = offset_date ,
63+ offset_id = offset_id ,
64+ offset_peer = offset_peer ,
65+ limit = limit ,
66+ hash = 0
67+ ),
68+ sleep_threshold = 60
69+ ))
6670
67- current += 1
71+ users = {i .id : i for i in r .users }
72+ chats = {i .id : i for i in r .chats }
6873
69- if current >= total :
70- return
74+ messages = {}
7175
72- while True :
73- dialogs = await self .get_dialogs (
74- offset_date = offset_date ,
75- limit = limit
76- )
76+ for message in r .messages :
77+ if isinstance (message , raw .types .MessageEmpty ):
78+ continue
79+
80+ chat_id = utils .get_peer_id (message .peer_id )
81+ messages [chat_id ] = await types .Message ._parse (self , message , users , chats )
82+
83+ dialogs = []
84+
85+ for dialog in r .dialogs :
86+ if not isinstance (dialog , raw .types .Dialog ):
87+ continue
88+
89+ dialogs .append (types .Dialog ._parse (self , dialog , messages , users , chats ))
7790
7891 if not dialogs :
7992 return
8093
81- offset_date = dialogs [- 1 ].top_message .date
94+ last = dialogs [- 1 ]
95+
96+ offset_id = last .top_message .message_id
97+ offset_date = last .top_message .date
98+ offset_peer = await self .resolve_peer (last .chat .id )
8299
83100 for dialog in dialogs :
84101 yield dialog
0 commit comments