Skip to content

Commit 5eeba19

Browse files
authored
Add BlockList enumeration and get_blocked_message_senders method (#282)
1 parent 0646030 commit 5eeba19

5 files changed

Lines changed: 122 additions & 1 deletion

File tree

compiler/docs/compiler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ def get_title_list(s: str) -> list:
432432
add_contact
433433
delete_contacts
434434
import_contacts
435+
get_blocked_message_senders
435436
get_contacts
436437
get_contacts_count
437438
search_contacts
@@ -1200,6 +1201,7 @@ def get_title_list(s: str) -> list:
12001201
categories = dict(
12011202
enums="""
12021203
Enumerations
1204+
BlockList
12031205
BusinessSchedule
12041206
ButtonStyle
12051207
ChatAction

pyrogram/enums/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
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+
from .block_list import BlockList
1920
from .business_schedule import BusinessSchedule
2021
from .button_style import ButtonStyle
2122
from .chat_action import ChatAction
@@ -57,6 +58,7 @@
5758
from .user_status import UserStatus
5859

5960
__all__ = [
61+
'BlockList',
6062
'BusinessSchedule',
6163
'ButtonStyle',
6264
'ChatAction',

pyrogram/enums/block_list.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 enum import auto
20+
21+
from .auto_name import AutoName
22+
23+
24+
class BlockList(AutoName):
25+
"""Block list enumeration"""
26+
27+
MAIN = auto()
28+
"The main block list that disallows writing messages to the current user, receiving their status and photo, viewing of stories, and some other actions"
29+
30+
STORIES = auto()
31+
"The block list that disallows viewing of stories of the current user"

pyrogram/methods/contacts/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from .add_contact import AddContact
2020
from .delete_contacts import DeleteContacts
21+
from .get_blocked_message_senders import GetBlockedMessageSenders
2122
from .get_contacts import GetContacts
2223
from .get_contacts_count import GetContactsCount
2324
from .import_contacts import ImportContacts
@@ -26,12 +27,13 @@
2627

2728

2829
class Contacts(
30+
GetBlockedMessageSenders,
2931
GetContacts,
3032
DeleteContacts,
3133
ImportContacts,
3234
GetContactsCount,
3335
AddContact,
3436
SearchContacts,
35-
SetContactNote
37+
SetContactNote,
3638
):
3739
pass
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)