Skip to content

Commit 5ce62bd

Browse files
committed
Add new Restriction object and make User and Chat objects use it
1 parent 8db3d90 commit 5ce62bd

4 files changed

Lines changed: 67 additions & 12 deletions

File tree

compiler/docs/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ def get_title_list(s: str) -> list:
309309
ChatMember
310310
ChatPermissions
311311
Dialog
312+
Restriction
312313
""",
313314
messages_media="""
314315
Messages & Media

pyrogram/client/types/user_and_chats/chat.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
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 typing import Union
19+
from typing import Union, List
2020

2121
import pyrogram
2222
from pyrogram.api import types
2323
from .chat_permissions import ChatPermissions
2424
from .chat_photo import ChatPhoto
25+
from .restriction import Restriction
2526
from ..object import Object
2627
from ...ext import utils
2728

@@ -87,8 +88,8 @@ class Chat(Object):
8788
members_count (``int``, *optional*):
8889
Chat members count, for groups, supergroups and channels only.
8990
90-
restriction_reason (``str``, *optional*):
91-
The reason why this chat might be unavailable to some users.
91+
restrictions (List of :obj:`Restriction`, *optional*):
92+
The list of reasons why this chat might be unavailable to some users.
9293
This field is available only in case *is_restricted* is True.
9394
9495
permissions (:obj:`ChatPermissions` *optional*):
@@ -120,7 +121,7 @@ def __init__(
120121
sticker_set_name: str = None,
121122
can_set_sticker_set: bool = None,
122123
members_count: int = None,
123-
restriction_reason: str = None,
124+
restrictions: List[Restriction] = None,
124125
permissions: "pyrogram.ChatPermissions" = None,
125126
distance: int = None
126127
):
@@ -143,7 +144,7 @@ def __init__(
143144
self.sticker_set_name = sticker_set_name
144145
self.can_set_sticker_set = can_set_sticker_set
145146
self.members_count = members_count
146-
self.restriction_reason = restriction_reason
147+
self.restrictions = restrictions
147148
self.permissions = permissions
148149
self.distance = distance
149150

@@ -162,7 +163,7 @@ def _parse_user_chat(client, user: types.User) -> "Chat":
162163
first_name=user.first_name,
163164
last_name=user.last_name,
164165
photo=ChatPhoto._parse(client, user.photo, peer_id),
165-
restriction_reason=user.restriction_reason,
166+
restrictions=pyrogram.List([Restriction._parse(r) for r in user.restriction_reason]) or None,
166167
client=client
167168
)
168169

@@ -183,6 +184,7 @@ def _parse_chat_chat(client, chat: types.Chat) -> "Chat":
183184
@staticmethod
184185
def _parse_channel_chat(client, channel: types.Channel) -> "Chat":
185186
peer_id = utils.get_channel_id(channel.id)
187+
restriction_reason = getattr(channel, "restriction_reason", [])
186188

187189
return Chat(
188190
id=peer_id,
@@ -193,7 +195,7 @@ def _parse_channel_chat(client, channel: types.Channel) -> "Chat":
193195
title=channel.title,
194196
username=getattr(channel, "username", None),
195197
photo=ChatPhoto._parse(client, getattr(channel, "photo", None), peer_id),
196-
restriction_reason=getattr(channel, "restriction_reason", None),
198+
restrictions=pyrogram.List([Restriction._parse(r) for r in restriction_reason]) or None,
197199
permissions=ChatPermissions._parse(getattr(channel, "default_banned_rights", None)),
198200
members_count=getattr(channel, "participants_count", None),
199201
client=client
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2019 Dan Tès <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 pyrogram.api import types
20+
from ..object import Object
21+
22+
23+
class Restriction(Object):
24+
"""A restriction applied to bots or chats.
25+
26+
Parameters:
27+
platform (``str``):
28+
The platform the restriction is applied to, e.g. "ios", "android"
29+
30+
reason (``str``):
31+
The restriction reason, e.g. "porn", "copyright".
32+
33+
text (``str``):
34+
The restriction text.
35+
"""
36+
37+
def __init__(self, *, platform: str, reason: str, text: str):
38+
super().__init__(None)
39+
40+
self.platform = platform
41+
self.reason = reason
42+
self.text = text
43+
44+
@staticmethod
45+
def _parse(restriction: types.RestrictionReason) -> "Restriction":
46+
return Restriction(
47+
platform=restriction.platform,
48+
reason=restriction.reason,
49+
text=restriction.text
50+
)

pyrogram/client/types/user_and_chats/user.py

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

1919
import html
20+
from typing import List
2021

2122
import pyrogram
2223
from pyrogram.api import types
2324
from .chat_photo import ChatPhoto
25+
from .restriction import Restriction
2426
from ..object import Object
2527
from ..update import Update
2628

@@ -101,8 +103,8 @@ class User(Object, Update):
101103
photo (:obj:`ChatPhoto <pyrogram.ChatPhoto>`, *optional*):
102104
User's or bot's current profile photo. Suitable for downloads only.
103105
104-
restriction_reason (``str``, *optional*):
105-
The reason why this bot might be unavailable to some users.
106+
restrictions (List of :obj:`Restriction`, *optional*):
107+
The list of reasons why this bot might be unavailable to some users.
106108
This field is available only in case *is_restricted* is True.
107109
"""
108110

@@ -130,7 +132,7 @@ def __init__(
130132
dc_id: int = None,
131133
phone_number: str = None,
132134
photo: ChatPhoto = None,
133-
restriction_reason: str = None
135+
restrictions: List[Restriction] = None
134136
):
135137
super().__init__(client)
136138

@@ -154,7 +156,7 @@ def __init__(
154156
self.dc_id = dc_id
155157
self.phone_number = phone_number
156158
self.photo = photo
157-
self.restriction_reason = restriction_reason
159+
self.restrictions = restrictions
158160

159161
def __format__(self, format_spec):
160162
if format_spec == "mention":
@@ -186,7 +188,7 @@ def _parse(client, user: types.User) -> "User" or None:
186188
dc_id=getattr(user.photo, "dc_id", None),
187189
phone_number=user.phone,
188190
photo=ChatPhoto._parse(client, user.photo, user.id),
189-
restriction_reason=user.restriction_reason,
191+
restrictions=pyrogram.List([Restriction._parse(r) for r in user.restriction_reason]) or None,
190192
client=client
191193
)
192194

0 commit comments

Comments
 (0)