|
| 1 | +# Pyrogram - Telegram MTProto API Client Library for Python |
| 2 | +# Copyright (C) 2017-present Dan <https://github.com/delivrance> |
| 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 typing import Union, Optional, AsyncGenerator |
| 20 | + |
| 21 | +import pyrogram |
| 22 | +from pyrogram import raw |
| 23 | +from pyrogram import types |
| 24 | + |
| 25 | + |
| 26 | +class GetChatJoinRequests: |
| 27 | + async def get_chat_join_requests( |
| 28 | + self: "pyrogram.Client", |
| 29 | + chat_id: Union[int, str], |
| 30 | + limit: int = 0, |
| 31 | + query: str = "" |
| 32 | + ) -> Optional[AsyncGenerator["types.ChatJoiner", None]]: |
| 33 | + """Get the pending join requests of a chat. |
| 34 | +
|
| 35 | + Parameters: |
| 36 | + chat_id (``int`` | ``str``): |
| 37 | + Unique identifier for the target chat or username of the target channel/supergroup |
| 38 | + (in the format @username). |
| 39 | +
|
| 40 | + limit (``int``, *optional*): |
| 41 | + Limits the number of invite links to be retrieved. |
| 42 | + By default, no limit is applied and all invite links are returned. |
| 43 | +
|
| 44 | + query (``str``, *optional*): |
| 45 | + Query to search for a user. |
| 46 | +
|
| 47 | + Returns: |
| 48 | + ``Generator``: A generator yielding :obj:`~pyrogram.types.ChatJoiner` objects. |
| 49 | +
|
| 50 | + Yields: |
| 51 | + :obj:`~pyrogram.types.ChatJoiner` objects. |
| 52 | + """ |
| 53 | + current = 0 |
| 54 | + total = abs(limit) or (1 << 31) - 1 |
| 55 | + limit = min(100, total) |
| 56 | + |
| 57 | + offset_date = 0 |
| 58 | + offset_user = raw.types.InputUserEmpty() |
| 59 | + |
| 60 | + while True: |
| 61 | + r = await self.invoke( |
| 62 | + raw.functions.messages.GetChatInviteImporters( |
| 63 | + peer=await self.resolve_peer(chat_id), |
| 64 | + limit=limit, |
| 65 | + offset_date=offset_date, |
| 66 | + offset_user=offset_user, |
| 67 | + requested=True, |
| 68 | + q=query |
| 69 | + ) |
| 70 | + ) |
| 71 | + |
| 72 | + if not r.importers: |
| 73 | + break |
| 74 | + |
| 75 | + users = {i.id: i for i in r.users} |
| 76 | + |
| 77 | + offset_date = r.importers[-1].date |
| 78 | + offset_user = await self.resolve_peer(r.importers[-1].user_id) |
| 79 | + |
| 80 | + for i in r.importers: |
| 81 | + yield types.ChatJoiner._parse(self, i, users) |
| 82 | + |
| 83 | + current += 1 |
| 84 | + |
| 85 | + if current >= total: |
| 86 | + return |
0 commit comments