|
| 1 | +# Pyrogram - Telegram MTProto API Client Library for Python |
| 2 | +# Copyright (C) 2017-present <https://github.com/KurimuzonAkuma> |
| 3 | +# |
| 4 | +# This file is part of Pyrogram. |
| 5 | +# |
| 6 | +# Pyrogram is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Lesser General Public License as published |
| 8 | +# by the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# Pyrogram is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public License |
| 17 | +# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +from asyncio import sleep |
| 20 | +from typing import AsyncGenerator, Optional, Union |
| 21 | + |
| 22 | +import pyrogram |
| 23 | +from pyrogram import raw, types, utils |
| 24 | + |
| 25 | + |
| 26 | +class GetDirectMessagesTopics: |
| 27 | + async def get_direct_messages_topics( |
| 28 | + self: "pyrogram.Client", |
| 29 | + chat_id: Union[int, str], |
| 30 | + limit: int = 0 |
| 31 | + ) -> AsyncGenerator["types.DirectMessagesTopic", None]: |
| 32 | + """Get one or more topic from a direct messages channel chat. |
| 33 | +
|
| 34 | + .. include:: /_includes/usable-by/users.rst |
| 35 | +
|
| 36 | + Parameters: |
| 37 | + chat_id (``int`` | ``str``): |
| 38 | + Unique identifier (int) or username (str) of the target chat. |
| 39 | +
|
| 40 | + limit (``int``, *optional*): |
| 41 | + Limits the number of topics to be retrieved. |
| 42 | + By default, no limit is applied and all topics are returned. |
| 43 | +
|
| 44 | + Returns: |
| 45 | + ``Generator``: A generator yielding :obj:`~pyrogram.types.DirectMessagesTopic` objects. |
| 46 | +
|
| 47 | + Example: |
| 48 | + .. code-block:: python |
| 49 | +
|
| 50 | + # Iterate through all topics |
| 51 | + async for topic in app.get_direct_messages_topics(chat_id): |
| 52 | + print(topic) |
| 53 | +
|
| 54 | + """ |
| 55 | + current = 0 |
| 56 | + total = limit or (1 << 31) - 1 |
| 57 | + limit = min(100, total) |
| 58 | + |
| 59 | + offset_date = 0 |
| 60 | + offset_id = 0 |
| 61 | + offset_peer = raw.types.InputPeerEmpty() |
| 62 | + |
| 63 | + while True: |
| 64 | + r = await self.invoke( |
| 65 | + raw.functions.messages.GetSavedDialogs( |
| 66 | + offset_date=offset_date, |
| 67 | + offset_id=offset_id, |
| 68 | + offset_peer=offset_peer, |
| 69 | + limit=limit, |
| 70 | + hash=0, |
| 71 | + exclude_pinned=None, |
| 72 | + parent_peer=await self.resolve_peer(chat_id) |
| 73 | + ) |
| 74 | + ) |
| 75 | + |
| 76 | + users = {i.id: i for i in r.users} |
| 77 | + chats = {i.id: i for i in r.chats} |
| 78 | + |
| 79 | + messages = {} |
| 80 | + |
| 81 | + for message in r.messages: |
| 82 | + if isinstance(message, raw.types.MessageEmpty): |
| 83 | + continue |
| 84 | + |
| 85 | + messages[message.id] = await types.Message._parse( |
| 86 | + self, |
| 87 | + message, |
| 88 | + users, |
| 89 | + chats, |
| 90 | + replies=self.fetch_replies |
| 91 | + ) |
| 92 | + |
| 93 | + topics = [] |
| 94 | + |
| 95 | + for topic in r.dialogs: |
| 96 | + topics.append( |
| 97 | + types.DirectMessagesTopic._parse_dialog( |
| 98 | + client=self, |
| 99 | + topic=topic, |
| 100 | + messages=messages, |
| 101 | + users=users, |
| 102 | + chats=chats |
| 103 | + ) |
| 104 | + ) |
| 105 | + |
| 106 | + if not topics: |
| 107 | + return |
| 108 | + |
| 109 | + last = topics[-1] |
| 110 | + |
| 111 | + offset_id = last.last_message.id |
| 112 | + offset_date = utils.datetime_to_timestamp(last.last_message.date) |
| 113 | + offset_peer = await self.resolve_peer(last.topic_id) |
| 114 | + |
| 115 | + for topic in topics: |
| 116 | + await sleep(0) |
| 117 | + |
| 118 | + yield topic |
| 119 | + |
| 120 | + current += 1 |
| 121 | + |
| 122 | + if current >= total: |
| 123 | + return |
0 commit comments