Skip to content

Commit 1472b82

Browse files
committed
Add ChatPermissions type
1 parent e99f86b commit 1472b82

5 files changed

Lines changed: 178 additions & 2 deletions

File tree

docs/source/pyrogram/Types.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Users & Chats
1616
ChatPhoto
1717
ChatMember
1818
ChatMembers
19+
ChatPermissions
1920
Dialog
2021
Dialogs
2122

pyrogram/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
Location, Message, MessageEntity, Dialog, Dialogs, Photo, PhotoSize, Sticker, User, UserStatus,
3333
UserProfilePhotos, Venue, Animation, Video, VideoNote, Voice, CallbackQuery, Messages, ForceReply,
3434
InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove,
35-
Poll, PollOption, ChatPreview, StopPropagation, Game, CallbackGame, GameHighScore, GameHighScores
35+
Poll, PollOption, ChatPreview, StopPropagation, Game, CallbackGame, GameHighScore, GameHighScores,
36+
ChatPermissions
3637
)
3738
from .client import (
3839
Client, ChatAction, ParseMode, Emoji,

pyrogram/client/types/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@
3333
from .update import StopPropagation
3434
from .user_and_chats import (
3535
Chat, ChatMember, ChatMembers, ChatPhoto,
36-
Dialog, Dialogs, User, UserStatus, ChatPreview
36+
Dialog, Dialogs, User, UserStatus, ChatPreview, ChatPermissions
3737
)

pyrogram/client/types/user_and_chats/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from .chat import Chat
2020
from .chat_member import ChatMember
2121
from .chat_members import ChatMembers
22+
from .chat_permissions import ChatPermissions
2223
from .chat_photo import ChatPhoto
2324
from .chat_preview import ChatPreview
2425
from .dialog import Dialog
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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 typing import Union
20+
21+
from pyrogram.api import types
22+
from ..pyrogram_type import PyrogramType
23+
24+
25+
class ChatPermissions(PyrogramType):
26+
"""This object represents both a chat default permissions and a single member permissions within a chat.
27+
28+
Some permissions make sense depending on the context: default chat permissions, restricted/kicked member or
29+
administrators in groups or channels.
30+
31+
Args:
32+
until_date (``int``, *optional*):
33+
Applicable to restricted and kicked members only.
34+
Date when user restrictions will be lifted, unix time.
35+
0 means the restrictions will never be lifted (user restricted forever).
36+
37+
can_be_edited (``bool``, *optional*):
38+
Applicable to administrators only.
39+
True, if you are allowed to edit administrator privileges of the user.
40+
41+
can_change_info (``bool``, *optional*):
42+
Applicable to default chat permissions in private groups and administrators in public groups only.
43+
True, if the chat title, photo and other settings can be changed.
44+
45+
can_post_messages (``bool``, *optional*):
46+
Applicable to channel administrators only.
47+
True, if the administrator can post messages in the channel, channels only.
48+
49+
can_edit_messages (``bool``, *optional*):
50+
Applicable to channel administrators only.
51+
True, if the administrator can edit messages of other users and can pin messages, channels only.
52+
53+
can_delete_messages (``bool``, *optional*):
54+
Applicable to administrators only.
55+
True, if the administrator can delete messages of other users.
56+
57+
can_restrict_members (``bool``, *optional*):
58+
Applicable to administrators only.
59+
True, if the administrator can restrict, ban or unban chat members.
60+
61+
can_invite_users (``bool``, *optional*):
62+
Applicable to default chat permissions and administrators only.
63+
True, if new users can be invited to the chat.
64+
65+
can_pin_messages (``bool``, *optional*):
66+
Applicable to default chat permissions in private groups and administrators in public groups only.
67+
True, if messages can be pinned, supergroups only.
68+
69+
can_promote_members (``bool``, *optional*):
70+
Applicable to administrators only.
71+
True, if the administrator can add new administrators with a subset of his own privileges or demote
72+
administrators that he has promoted, directly or indirectly (promoted by administrators that were appointed
73+
by the user).
74+
75+
can_send_messages (``bool``, *optional*):
76+
Applicable to default chat permissions and restricted members only.
77+
True, if text messages, contacts, locations and venues can be sent.
78+
79+
can_send_media_messages (``bool``, *optional*):
80+
Applicable to default chat permissions and restricted members only.
81+
True, if audios, documents, photos, videos, video notes and voice notes can be sent, implies
82+
can_send_messages.
83+
84+
can_send_other_messages (``bool``, *optional*):
85+
Applicable to default chat permissions and restricted members only.
86+
True, if animations, games, stickers and inline bot results can be sent, implies can_send_media_messages.
87+
88+
can_add_web_page_previews (``bool``, *optional*):
89+
Applicable to default chat permissions and restricted members only.
90+
True, if web page previews can be attached to text messages, implies can_send_media_messages.
91+
92+
can_send_polls (``bool``, *optional*):
93+
Applicable to default chat permissions and restricted members only.
94+
True, if polls can be sent, implies can_send_media_messages.
95+
"""
96+
97+
def __init__(
98+
self,
99+
*,
100+
until_date: int = None,
101+
102+
# Admin permissions
103+
can_be_edited: bool = None,
104+
can_change_info: bool = None,
105+
can_post_messages: bool = None, # Channels only
106+
can_edit_messages: bool = None, # Channels only
107+
can_delete_messages: bool = None,
108+
can_restrict_members: bool = None,
109+
can_invite_users: bool = None,
110+
can_pin_messages: bool = None, # Supergroups only
111+
can_promote_members: bool = None,
112+
113+
# Restricted user permissions
114+
can_send_messages: bool = None, # Text, contacts, locations and venues
115+
can_send_media_messages: bool = None, # Audios, documents, photos, videos, video notes and voice notes
116+
can_send_other_messages: bool = None, # Animations (GIFs), games, stickers, inline bot results
117+
can_add_web_page_previews: bool = None,
118+
can_send_polls: bool = None
119+
):
120+
super().__init__(None)
121+
122+
self.until_date = until_date
123+
self.can_be_edited = can_be_edited
124+
125+
self.can_change_info = can_change_info
126+
self.can_post_messages = can_post_messages
127+
self.can_edit_messages = can_edit_messages
128+
self.can_delete_messages = can_delete_messages
129+
self.can_restrict_members = can_restrict_members
130+
self.can_invite_users = can_invite_users
131+
self.can_pin_messages = can_pin_messages
132+
self.can_promote_members = can_promote_members
133+
134+
self.can_send_messages = can_send_messages
135+
self.can_send_media_messages = can_send_media_messages
136+
self.can_send_other_messages = can_send_other_messages
137+
self.can_add_web_page_previews = can_add_web_page_previews
138+
self.can_send_polls = can_send_polls
139+
140+
@staticmethod
141+
def _parse(member: Union[types.ChannelParticipantAdmin, types.ChannelParticipantBanned]) -> "ChatPermissions":
142+
if isinstance(member, types.ChannelParticipantAdmin):
143+
permissions = member.admin_rights
144+
145+
return ChatPermissions(
146+
can_be_edited=member.can_edit,
147+
can_change_info=permissions.change_info,
148+
can_post_messages=permissions.post_messages,
149+
can_edit_messages=permissions.edit_messages,
150+
can_delete_messages=permissions.delete_messages,
151+
can_restrict_members=permissions.ban_users,
152+
can_invite_users=permissions.invite_users,
153+
can_pin_messages=permissions.pin_messages,
154+
can_promote_members=permissions.add_admins
155+
)
156+
157+
if isinstance(member, types.ChannelParticipantBanned):
158+
denied_permissions = member.banned_rights # type: types.ChatBannedRights
159+
160+
return ChatPermissions(
161+
until_date=0 if denied_permissions.until_date == (1 << 31) - 1 else denied_permissions.until_date,
162+
can_send_messages=not denied_permissions.send_messages,
163+
can_send_media_messages=not denied_permissions.send_media,
164+
can_send_other_messages=(
165+
not denied_permissions.send_stickers or not denied_permissions.send_gifs or
166+
not denied_permissions.send_games or not denied_permissions.send_inline
167+
),
168+
can_add_web_page_previews=not denied_permissions.embed_links,
169+
can_send_polls=not denied_permissions.send_polls,
170+
can_change_info=not denied_permissions.change_info,
171+
can_invite_users=not denied_permissions.invite_users,
172+
can_pin_messages=not denied_permissions.pin_messages
173+
)

0 commit comments

Comments
 (0)