Skip to content

Commit 86d0fc1

Browse files
committed
(feat): Add offset_date and offset_message_id in get_dialogs and search_global
1 parent 261d7bc commit 86d0fc1

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

pyrogram/methods/chats/get_dialogs.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
from asyncio import sleep
20+
from datetime import datetime
2021
from typing import AsyncGenerator, Optional
2122

2223
import pyrogram
@@ -28,7 +29,9 @@ async def get_dialogs(
2829
self: "pyrogram.Client",
2930
limit: int = 0,
3031
pinned_only: bool = False,
31-
chat_list: int = 0
32+
chat_list: int = 0,
33+
offset_date: datetime = utils.zero_datetime(),
34+
offset_message_id: int = 0,
3235
) -> Optional[AsyncGenerator["types.Dialog", None]]:
3336
"""Get a user's dialogs sequentially.
3437
@@ -46,6 +49,12 @@ async def get_dialogs(
4649
chat_list (``int``, *optional*):
4750
Chat list from which to get the dialogs; Only Main (0) and Archive (1) chat lists are supported. Defaults to (0) Main chat list.
4851
52+
offset_date (:py:obj:`~datetime.datetime`, *optional*):
53+
The date starting from which the dialogs need to be fetched. Use 0 or any date in the future to get results from the last dialog.
54+
55+
offset_message_id (``int``, *optional*):
56+
The message identifier of the last message in the last found dialog, or 0 for the first request.
57+
4958
Returns:
5059
``Generator``: A generator yielding :obj:`~pyrogram.types.Dialog` objects.
5160
@@ -60,8 +69,7 @@ async def get_dialogs(
6069
total = limit or (1 << 31) - 1
6170
request_limit = min(100, total)
6271

63-
offset_date = 0
64-
offset_id = 0
72+
offset_date = utils.datetime_to_timestamp(offset_date)
6573
offset_peer = raw.types.InputPeerEmpty()
6674

6775
seen_dialog_ids = set()
@@ -70,7 +78,7 @@ async def get_dialogs(
7078
r = await self.invoke(
7179
raw.functions.messages.GetDialogs(
7280
offset_date=offset_date,
73-
offset_id=offset_id,
81+
offset_id=offset_message_id,
7482
offset_peer=offset_peer,
7583
limit=request_limit,
7684
hash=0,
@@ -125,7 +133,7 @@ async def get_dialogs(
125133
if last.top_message is None:
126134
return
127135

128-
offset_id = last.top_message.id
136+
offset_message_id = last.top_message.id
129137
offset_date = utils.datetime_to_timestamp(last.top_message.date)
130138
offset_peer = await self.resolve_peer(last.chat.id)
131139

pyrogram/methods/messages/search_global.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
from asyncio import sleep
20+
from datetime import datetime
2021
from typing import AsyncGenerator, Optional
2122

2223
import pyrogram
@@ -31,6 +32,8 @@ async def search_global(
3132
limit: int = 0,
3233
chat_list: int = 0,
3334
chat_type_filter: "enums.ChatType" = None,
35+
offset_date: datetime = utils.zero_datetime(),
36+
offset_message_id: int = 0,
3437
) -> Optional[AsyncGenerator["types.Message", None]]:
3538
"""Search messages globally from all of your chats.
3639
@@ -62,6 +65,12 @@ async def search_global(
6265
chat_type_filter (:obj:`~pyrogram.enums.ChatType`, *optional*):
6366
Additional filter for type of the chat (:obj:`~pyrogram.enums.ChatType.PRIVATE`, :obj:`~pyrogram.enums.ChatType.GROUP`, :obj:`~pyrogram.enums.ChatType.CHANNEL`) of the searched messages; pass None to search for messages in all chats.
6467
68+
offset_date (:py:obj:`~datetime.datetime`, *optional*):
69+
The date starting from which the dialogs need to be fetched. Use 0 or any date in the future to get results from the last dialog.
70+
71+
offset_message_id (``int``, *optional*):
72+
The message identifier of the last message in the last found dialog, or 0 for the first request.
73+
6574
Returns:
6675
``Generator``: A generator yielding :obj:`~pyrogram.types.Message` objects.
6776
@@ -83,9 +92,8 @@ async def search_global(
8392
total = abs(limit) or (1 << 31)
8493
limit = min(100, total)
8594

86-
offset_date = 0
95+
offset_date = utils.datetime_to_timestamp(offset_date)
8796
offset_peer = raw.types.InputPeerEmpty()
88-
offset_id = 0
8997

9098
while True:
9199
messages = await utils.parse_messages(
@@ -99,7 +107,7 @@ async def search_global(
99107
max_date=0,
100108
offset_rate=offset_date,
101109
offset_peer=offset_peer,
102-
offset_id=offset_id,
110+
offset_id=offset_message_id,
103111
limit=limit,
104112
folder_id=chat_list,
105113
broadcasts_only=(chat_type_filter == enums.ChatType.CHANNEL) if chat_type_filter else None,
@@ -118,7 +126,7 @@ async def search_global(
118126

119127
offset_date = utils.datetime_to_timestamp(last.date)
120128
offset_peer = await self.resolve_peer(last.chat.id)
121-
offset_id = last.id
129+
offset_message_id = last.id
122130

123131
for message in messages:
124132
await sleep(0)

0 commit comments

Comments
 (0)