|
| 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 AsyncGenerator |
| 20 | + |
| 21 | +import pyrogram |
| 22 | +from pyrogram import enums, raw, types, utils |
| 23 | + |
| 24 | + |
| 25 | +class GetBlockedMessageSenders: |
| 26 | + async def get_blocked_message_senders( |
| 27 | + self: "pyrogram.Client", |
| 28 | + block_list: "enums.BlockList" = enums.BlockList.MAIN, |
| 29 | + offset: int = 0, |
| 30 | + limit: int = 0, |
| 31 | + ) -> AsyncGenerator["types.Chat", None]: |
| 32 | + """Returns users and chats that were blocked by the current user. |
| 33 | +
|
| 34 | + .. include:: /_includes/usable-by/users.rst |
| 35 | +
|
| 36 | + Parameters: |
| 37 | + block_list (``pyrogram.enums.BlockList``, *optional*): |
| 38 | + The block list from which to return users. |
| 39 | +
|
| 40 | + offset (``int``, *optional*): |
| 41 | + Number of users and chats to skip in the result, must be non-negative. |
| 42 | +
|
| 43 | + limit (``int``, *optional*): |
| 44 | + The maximum number of users and chats to return. |
| 45 | +
|
| 46 | + Returns: |
| 47 | + AsyncGenerator of :obj:`~pyrogram.types.Chat`: An async generator that yields Chat objects. |
| 48 | +
|
| 49 | + Example: |
| 50 | + .. code-block:: python |
| 51 | + async for chat in app.get_blocked_message_senders(): |
| 52 | + print(chat) |
| 53 | + """ |
| 54 | + |
| 55 | + current = 0 |
| 56 | + total = abs(limit) or (1 << 31) - 1 |
| 57 | + limit = min(100, total) |
| 58 | + |
| 59 | + while True: |
| 60 | + r = await self.invoke( |
| 61 | + raw.functions.contacts.GetBlocked( |
| 62 | + offset=offset, |
| 63 | + limit=limit, |
| 64 | + my_stories_from=block_list == enums.BlockList.STORIES, |
| 65 | + ) |
| 66 | + ) |
| 67 | + |
| 68 | + if not r.blocked: |
| 69 | + return |
| 70 | + |
| 71 | + users = {i.id: i for i in r.users} |
| 72 | + chats = {i.id: i for i in r.chats} |
| 73 | + |
| 74 | + offset += len(r.blocked) |
| 75 | + |
| 76 | + for blocked_user in r.blocked: |
| 77 | + peer_id = utils.get_raw_peer_id(blocked_user.peer_id) |
| 78 | + |
| 79 | + yield types.Chat._parse_chat(self, users.get(peer_id) or chats.get(peer_id)) |
| 80 | + |
| 81 | + current += 1 |
| 82 | + |
| 83 | + if current >= total: |
| 84 | + return |
0 commit comments