Skip to content

Commit 4cb9dec

Browse files
committed
Remove remaining iter_* methods
1 parent 6eadb75 commit 4cb9dec

File tree

8 files changed

+110
-295
lines changed

8 files changed

+110
-295
lines changed

compiler/docs/compiler.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ def get_title_list(s: str) -> list:
213213
get_chat_members
214214
get_chat_members_count
215215
get_dialogs
216-
iter_dialogs
217216
get_dialogs_count
218217
set_chat_username
219218
get_nearby_chats
@@ -238,9 +237,8 @@ def get_title_list(s: str) -> list:
238237
Users
239238
get_me
240239
get_users
241-
get_profile_photos
242-
get_profile_photos_count
243-
iter_profile_photos
240+
get_chat_photos
241+
get_chat_photos_count
244242
set_profile_photo
245243
delete_profile_photos
246244
set_username

pyrogram/methods/chats/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
from .get_dialogs_count import GetDialogsCount
3737
from .get_nearby_chats import GetNearbyChats
3838
from .get_send_as_chats import GetSendAsChats
39-
from .iter_dialogs import IterDialogs
4039
from .join_chat import JoinChat
4140
from .leave_chat import LeaveChat
4241
from .mark_chat_unread import MarkChatUnread
@@ -76,7 +75,6 @@ class Chats(
7675
UnpinChatMessage,
7776
GetDialogs,
7877
GetChatMembersCount,
79-
IterDialogs,
8078
SetChatUsername,
8179
SetChatPermissions,
8280
GetDialogsCount,

pyrogram/methods/chats/get_dialogs.py

Lines changed: 52 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,92 +16,87 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19-
import logging
20-
from datetime import datetime
21-
from typing import List
19+
from typing import AsyncGenerator, Optional
2220

2321
import pyrogram
24-
from pyrogram import raw
25-
from pyrogram import types
26-
from pyrogram import utils
27-
28-
log = logging.getLogger(__name__)
22+
from pyrogram import types, raw, utils
2923

3024

3125
class GetDialogs:
3226
async def get_dialogs(
3327
self: "pyrogram.Client",
34-
offset_date: datetime = datetime.fromtimestamp(0),
35-
limit: int = 100,
36-
pinned_only: bool = False
37-
) -> List["types.Dialog"]:
38-
"""Get a chunk of the user's dialogs.
39-
40-
You can get up to 100 dialogs at once.
41-
For a more convenient way of getting a user's dialogs see :meth:`~pyrogram.Client.iter_dialogs`.
28+
limit: int = 0
29+
) -> Optional[AsyncGenerator["types.Dialog", None]]:
30+
"""Get a user's dialogs sequentially.
4231
4332
Parameters:
44-
offset_date (:py:obj:`~datetime.datetime`):
45-
The offset date taken from the top message of a :obj:`~pyrogram.types.Dialog`.
46-
Defaults to epoch. Valid for non-pinned dialogs only.
47-
48-
limit (``str``, *optional*):
33+
limit (``int``, *optional*):
4934
Limits the number of dialogs to be retrieved.
50-
Defaults to 100. Valid for non-pinned dialogs only.
51-
52-
pinned_only (``bool``, *optional*):
53-
Pass True if you want to get only pinned dialogs.
54-
Defaults to False.
35+
By default, no limit is applied and all dialogs are returned.
5536
5637
Returns:
57-
List of :obj:`~pyrogram.types.Dialog`: On success, a list of dialogs is returned.
38+
``Generator``: A generator yielding :obj:`~pyrogram.types.Dialog` objects.
5839
5940
Example:
6041
.. code-block:: python
6142
62-
# Get first 100 dialogs
63-
await app.get_dialogs()
64-
65-
# Get pinned dialogs
66-
await app.get_dialogs(pinned_only=True)
43+
# Iterate through all dialogs
44+
async for dialog in app.get_dialogs():
45+
print(dialog.chat.first_name or dialog.chat.title)
6746
"""
47+
current = 0
48+
total = limit or (1 << 31) - 1
49+
limit = min(100, total)
6850

69-
if pinned_only:
70-
r = await self.invoke(
71-
raw.functions.messages.GetPinnedDialogs(folder_id=0),
72-
sleep_threshold=60
73-
)
74-
else:
51+
offset_date = 0
52+
offset_id = 0
53+
offset_peer = raw.types.InputPeerEmpty()
54+
55+
while True:
7556
r = await self.invoke(
7657
raw.functions.messages.GetDialogs(
77-
offset_date=utils.datetime_to_timestamp(offset_date),
78-
offset_id=0,
79-
offset_peer=raw.types.InputPeerEmpty(),
58+
offset_date=offset_date,
59+
offset_id=offset_id,
60+
offset_peer=offset_peer,
8061
limit=limit,
81-
hash=0,
82-
exclude_pinned=True
62+
hash=0
8363
),
8464
sleep_threshold=60
8565
)
8666

87-
users = {i.id: i for i in r.users}
88-
chats = {i.id: i for i in r.chats}
67+
users = {i.id: i for i in r.users}
68+
chats = {i.id: i for i in r.chats}
69+
70+
messages = {}
71+
72+
for message in r.messages:
73+
if isinstance(message, raw.types.MessageEmpty):
74+
continue
75+
76+
chat_id = utils.get_peer_id(message.peer_id)
77+
messages[chat_id] = await types.Message._parse(self, message, users, chats)
78+
79+
dialogs = []
80+
81+
for dialog in r.dialogs:
82+
if not isinstance(dialog, raw.types.Dialog):
83+
continue
8984

90-
messages = {}
85+
dialogs.append(types.Dialog._parse(self, dialog, messages, users, chats))
9186

92-
for message in r.messages:
93-
if isinstance(message, raw.types.MessageEmpty):
94-
continue
87+
if not dialogs:
88+
return
9589

96-
chat_id = utils.get_peer_id(message.peer_id)
97-
messages[chat_id] = await types.Message._parse(self, message, users, chats)
90+
last = dialogs[-1]
9891

99-
parsed_dialogs = []
92+
offset_id = last.top_message.id
93+
offset_date = utils.datetime_to_timestamp(last.top_message.date)
94+
offset_peer = await self.resolve_peer(last.chat.id)
10095

101-
for dialog in r.dialogs:
102-
if not isinstance(dialog, raw.types.Dialog):
103-
continue
96+
for dialog in dialogs:
97+
yield dialog
10498

105-
parsed_dialogs.append(types.Dialog._parse(self, dialog, messages, users, chats))
99+
current += 1
106100

107-
return types.List(parsed_dialogs)
101+
if current >= total:
102+
return

pyrogram/methods/chats/iter_dialogs.py

Lines changed: 0 additions & 106 deletions
This file was deleted.

pyrogram/methods/users/__init__.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818

1919
from .block_user import BlockUser
2020
from .delete_profile_photos import DeleteProfilePhotos
21+
from .get_chat_photos import GetChatPhotos
22+
from .get_chat_photos_count import GetChatPhotosCount
2123
from .get_common_chats import GetCommonChats
2224
from .get_me import GetMe
23-
from .get_profile_photos import GetProfilePhotos
24-
from .get_profile_photos_count import GetProfilePhotosCount
2525
from .get_users import GetUsers
26-
from .iter_profile_photos import IterProfilePhotos
2726
from .set_profile_photo import SetProfilePhoto
2827
from .set_username import SetUsername
2928
from .unblock_user import UnblockUser
@@ -33,14 +32,13 @@
3332
class Users(
3433
BlockUser,
3534
GetCommonChats,
36-
GetProfilePhotos,
35+
GetChatPhotos,
3736
SetProfilePhoto,
3837
DeleteProfilePhotos,
3938
GetUsers,
4039
GetMe,
4140
SetUsername,
42-
GetProfilePhotosCount,
43-
IterProfilePhotos,
41+
GetChatPhotosCount,
4442
UnblockUser,
4543
UpdateProfile,
4644
):

0 commit comments

Comments
 (0)