[Bot API 4.4] Update chat permissions

- Move can_* permissions back to ChatMember objects
- Rename restrict_chat to set_chat_permissions
- Update restrict_chat_member to accept a single ChatPermissions arg.
- Update ChatPermissions to be the same as the one on the Bot API
This commit is contained in:
Dan 2019-08-03 19:09:42 +02:00
parent 091552e5d9
commit c6f346f83d
7 changed files with 231 additions and 248 deletions

View File

@ -189,6 +189,7 @@ def pyrogram_api():
delete_chat_photo
set_chat_title
set_chat_description
set_chat_permissions
pin_chat_message
unpin_chat_message
get_chat
@ -199,7 +200,6 @@ def pyrogram_api():
get_dialogs
iter_dialogs
get_dialogs_count
restrict_chat
update_chat_username
archive_chats
unarchive_chats

View File

@ -38,9 +38,9 @@ from .kick_chat_member import KickChatMember
from .leave_chat import LeaveChat
from .pin_chat_message import PinChatMessage
from .promote_chat_member import PromoteChatMember
from .restrict_chat import RestrictChat
from .restrict_chat_member import RestrictChatMember
from .set_chat_description import SetChatDescription
from .set_chat_permissions import SetChatPermissions
from .set_chat_photo import SetChatPhoto
from .set_chat_title import SetChatTitle
from .unarchive_chats import UnarchiveChats
@ -71,7 +71,7 @@ class Chats(
IterDialogs,
IterChatMembers,
UpdateChatUsername,
RestrictChat,
SetChatPermissions,
GetDialogsCount,
ArchiveChats,
UnarchiveChats,

View File

@ -20,7 +20,7 @@ from typing import Union
from pyrogram.api import functions, types
from ...ext import BaseClient
from ...types.user_and_chats import Chat
from ...types.user_and_chats import Chat, ChatPermissions
class RestrictChatMember(BaseClient):
@ -28,20 +28,13 @@ class RestrictChatMember(BaseClient):
self,
chat_id: Union[int, str],
user_id: Union[int, str],
until_date: int = 0,
can_send_messages: bool = False,
can_send_media_messages: bool = False,
can_send_other_messages: bool = False,
can_add_web_page_previews: bool = False,
can_send_polls: bool = False,
can_change_info: bool = False,
can_invite_users: bool = False,
can_pin_messages: bool = False
permissions: ChatPermissions,
until_date: int = 0
) -> Chat:
"""Restrict a user in a supergroup.
The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights.
Pass True for all boolean parameters to lift restrictions from a user.
You must be an administrator in the supergroup for this to work and must have the appropriate admin rights.
Pass True for all permissions to lift restrictions from a user.
Parameters:
chat_id (``int`` | ``str``):
@ -51,37 +44,14 @@ class RestrictChatMember(BaseClient):
Unique identifier (int) or username (str) of the target user.
For a contact that exists in your Telegram address book you can use his phone number (str).
permissions (:obj:`ChatPermissions`):
New user permissions.
until_date (``int``, *optional*):
Date when the user will be unbanned, unix time.
If user is banned for more than 366 days or less than 30 seconds from the current time they are
considered to be banned forever. Defaults to 0 (ban forever).
can_send_messages (``bool``, *optional*):
Pass True, if the user can send text messages, contacts, locations and venues.
can_send_media_messages (``bool``, *optional*):
Pass True, if the user can send audios, documents, photos, videos, video notes and voice notes,
implies can_send_messages.
can_send_other_messages (``bool``, *optional*):
Pass True, if the user can send animations, games, stickers and use inline bots,
implies can_send_messages.
can_add_web_page_previews (``bool``, *optional*):
Pass True, if the user may add web page previews to their messages, implies can_send_messages.
can_send_polls (``bool``, *optional*):
Pass True, if the user can send polls, implies can_send_messages.
can_change_info (``bool``, *optional*):
Pass True, if the user can change the chat title, photo and other settings.
can_invite_users (``bool``, *optional*):
Pass True, if the user can invite new users to the chat.
can_pin_messages (``bool``, *optional*):
Pass True, if the user can pin messages.
Returns:
:obj:`Chat`: On success, a chat object is returned.
@ -90,14 +60,16 @@ class RestrictChatMember(BaseClient):
from time import time
# Completely restrict chat member forever
app.restrict_chat_member(chat_id, user_id)
from pyrogram import ChatPermissions
# Chat member can't send messages for 24h
app.restrict_chat_member(chat_id, user_id, int(time.time() + 86400))
# Completely restrict chat member (mute) forever
app.restrict_chat_member(chat_id, user_id, ChatPermissions())
# Chat member muted for 24h
app.restrict_chat_member(chat_id, user_id, ChatPermissions(), int(time.time() + 86400))
# Chat member can only send text messages
app.restrict_chat_member(chat_id, user_id, can_send_messages=True)
app.restrict_chat_member(chat_id, user_id, ChatPermissions(can_send_messages=True))
"""
send_messages = True
send_media = True
@ -111,35 +83,35 @@ class RestrictChatMember(BaseClient):
invite_users = True
pin_messages = True
if can_send_messages:
if permissions.can_send_messages:
send_messages = None
if can_send_media_messages:
if permissions.can_send_media_messages:
send_messages = None
send_media = None
if can_send_other_messages:
if permissions.can_send_other_messages:
send_messages = None
send_stickers = None
send_gifs = None
send_games = None
send_inline = None
if can_add_web_page_previews:
if permissions.can_add_web_page_previews:
send_messages = None
embed_links = None
if can_send_polls:
if permissions.can_send_polls:
send_messages = None
send_polls = None
if can_change_info:
if permissions.can_change_info:
change_info = None
if can_invite_users:
if permissions.can_invite_users:
invite_users = None
if can_pin_messages:
if permissions.can_pin_messages:
pin_messages = None
r = self.send(

View File

@ -20,54 +20,26 @@ from typing import Union
from pyrogram.api import functions, types
from ...ext import BaseClient
from ...types.user_and_chats import Chat
from ...types.user_and_chats import Chat, ChatPermissions
class RestrictChat(BaseClient):
def restrict_chat(
class SetChatPermissions(BaseClient):
def set_chat_permissions(
self,
chat_id: Union[int, str],
can_send_messages: bool = False,
can_send_media_messages: bool = False,
can_send_other_messages: bool = False,
can_add_web_page_previews: bool = False,
can_send_polls: bool = False,
can_change_info: bool = False,
can_invite_users: bool = False,
can_pin_messages: bool = False
permissions: ChatPermissions,
) -> Chat:
"""Restrict a chat.
Pass True for all boolean parameters to lift restrictions from a chat.
"""Set default chat permissions for all members.
You must be an administrator in the group or a supergroup for this to work and must have the
*can_restrict_members* admin rights.
Parameters:
chat_id (``int`` | ``str``):
Unique identifier (int) or username (str) of the target chat.
can_send_messages (``bool``, *optional*):
Pass True, if the user can send text messages, contacts, locations and venues.
can_send_media_messages (``bool``, *optional*):
Pass True, if the user can send audios, documents, photos, videos, video notes and voice notes,
implies can_send_messages.
can_send_other_messages (``bool``, *optional*):
Pass True, if the user can send animations, games, stickers and use inline bots,
implies can_send_messages.
can_add_web_page_previews (``bool``, *optional*):
Pass True, if the user may add web page previews to their messages, implies can_send_messages.
can_send_polls (``bool``, *optional*):
Pass True, if the user can send polls, implies can_send_messages.
can_change_info (``bool``, *optional*):
Pass True, if the user can change the chat title, photo and other settings.
can_invite_users (``bool``, *optional*):
Pass True, if the user can invite new users to the chat.
can_pin_messages (``bool``, *optional*):
Pass True, if the user can pin messages.
permissions (:obj:`ChatPermissions`):
New default chat permissions.
Returns:
:obj:`Chat`: On success, a chat object is returned.
@ -75,11 +47,20 @@ class RestrictChat(BaseClient):
Example:
.. code-block:: python
# Completely restrict chat
app.restrict_chat(chat_id)
from pyrogram import ChatPermissions
# All chat members can only send text messages
app.restrict_chat(chat_id, can_send_messages=True)
# Completely restrict chat
app.set_chat_permissions(chat_id, ChatPermissions())
# Chat members can only send text messages, media, stickers and GIFs
app.set_chat_permissions(
chat_id,
ChatPermissions(
can_send_messages=True,
can_send_media_messages=True,
can_send_other_messages=True
)
)
"""
send_messages = True
send_media = True
@ -93,35 +74,35 @@ class RestrictChat(BaseClient):
invite_users = True
pin_messages = True
if can_send_messages:
if permissions.can_send_messages:
send_messages = None
if can_send_media_messages:
if permissions.can_send_media_messages:
send_messages = None
send_media = None
if can_send_other_messages:
if permissions.can_send_other_messages:
send_messages = None
send_stickers = None
send_gifs = None
send_games = None
send_inline = None
if can_add_web_page_previews:
if permissions.can_add_web_page_previews:
send_messages = None
embed_links = None
if can_send_polls:
if permissions.can_send_polls:
send_messages = None
send_polls = None
if can_change_info:
if permissions.can_change_info:
change_info = None
if can_invite_users:
if permissions.can_invite_users:
invite_users = None
if can_pin_messages:
if permissions.can_pin_messages:
pin_messages = None
r = self.send(

View File

@ -92,7 +92,7 @@ class Chat(Object):
This field is available only in case *is_restricted* is True.
permissions (:obj:`ChatPermissions` *optional*):
Information about the chat default permissions, for groups and supergroups.
Default chat member permissions, for groups and supergroups.
"""
def __init__(

View File

@ -33,11 +33,13 @@ class ChatMember(Object):
The member's status in the chat.
Can be "creator", "administrator", "member", "restricted", "left" or "kicked".
date (``int``, *optional*):
Date when the user joined, unix time. Not available for creator.
until_date (``int``, *optional*):
Restricted and kicked only.
Date when restrictions will be lifted for this user; unix time.
is_member (``bool``, *optional*):
Restricted only. True, if the user is a member of the chat at the moment of the request.
joined_date (``int``, *optional*):
Date when the user joined, unix time.
Not available for creator.
invited_by (:obj:`User`, *optional*):
Administrators and self member only. Information about the user who invited this member.
@ -49,9 +51,66 @@ class ChatMember(Object):
restricted_by (:obj:`User`, *optional*):
Restricted and kicked only. Information about the user who restricted or kicked this member.
permissions (:obj:`ChatPermissions` *optional*):
Administrators, restricted and kicked members only.
Information about the member permissions.
is_member (``bool``, *optional*):
Restricted only. True, if the user is a member of the chat at the moment of the request.
can_be_edited (``bool``, *optional*):
Administrators only.
True, if you are allowed to edit administrator privileges of the user.
can_post_messages (``bool``, *optional*):
Administrators only. Channels only.
True, if the administrator can post messages in the channel.
can_edit_messages (``bool``, *optional*):
Administrators only. Channels only.
True, if the administrator can edit messages of other users and can pin messages.
can_delete_messages (``bool``, *optional*):
Administrators only.
True, if the administrator can delete messages of other users.
can_restrict_members (``bool``, *optional*):
Administrators only.
True, if the administrator can restrict, ban or unban chat members.
can_promote_members (``bool``, *optional*):
Administrators only.
True, if the administrator can add new administrators with a subset of his own privileges or demote
administrators that he has promoted, directly or indirectly (promoted by administrators that were appointed
by the user).
can_change_info (``bool``, *optional*):
Administrators and restricted only.
True, if the user is allowed to change the chat title, photo and other settings.
can_invite_users (``bool``, *optional*):
Administrators and restricted only.
True, if the user is allowed to invite new users to the chat.
can_pin_messages (``bool``, *optional*):
Administrators and restricted only. Groups and supergroups only.
True, if the user is allowed to pin messages.
can_send_messages (``bool``, *optional*):
Restricted only.
True, if the user is allowed to send text messages, contacts, locations and venues.
can_send_media_messages (``bool``, *optional*):
Restricted only.
True, if the user is allowed to send audios, documents, photos, videos, video notes and voice notes.
can_send_other_messages (``bool``, *optional*):
Restricted only.
True, if the user is allowed to send animations, games, stickers and use inline bots.
can_add_web_page_previews (``bool``, *optional*):
Restricted only.
True, if the user is allowed to add web page previews to their messages.
can_send_polls (``bool``, *optional*):
Restricted only.
True, if the user is allowed to send polls.
"""
def __init__(
@ -60,23 +119,57 @@ class ChatMember(Object):
client: "pyrogram.BaseClient" = None,
user: "pyrogram.User",
status: str,
date: int = None,
is_member: bool = None,
until_date: int = None,
joined_date: int = None,
invited_by: "pyrogram.User" = None,
promoted_by: "pyrogram.User" = None,
restricted_by: "pyrogram.User" = None,
permissions: "pyrogram.ChatPermissions" = None
is_member: bool = None,
# Admin permissions
can_be_edited: bool = None,
can_post_messages: bool = None, # Channels only
can_edit_messages: bool = None, # Channels only
can_delete_messages: bool = None,
can_restrict_members: bool = None,
can_promote_members: bool = None,
can_change_info: bool = None,
can_invite_users: bool = None,
can_pin_messages: bool = None, # Groups and supergroups only
# Restricted user permissions
can_send_messages: bool = None, # Text, contacts, locations and venues
can_send_media_messages: bool = None, # Audios, documents, photos, videos, video notes and voice notes
can_send_other_messages: bool = None, # Animations (GIFs), games, stickers, inline bot results
can_add_web_page_previews: bool = None,
can_send_polls: bool = None
):
super().__init__(client)
self.user = user
self.status = status
self.date = date
self.is_member = is_member
self.until_date = until_date
self.joined_date = joined_date
self.invited_by = invited_by
self.promoted_by = promoted_by
self.restricted_by = restricted_by
self.permissions = permissions
self.is_member = is_member
self.can_be_edited = can_be_edited
self.can_post_messages = can_post_messages
self.can_edit_messages = can_edit_messages
self.can_delete_messages = can_delete_messages
self.can_restrict_members = can_restrict_members
self.can_promote_members = can_promote_members
self.can_change_info = can_change_info
self.can_invite_users = can_invite_users
self.can_pin_messages = can_pin_messages
self.can_send_messages = can_send_messages
self.can_send_media_messages = can_send_media_messages
self.can_send_other_messages = can_send_other_messages
self.can_add_web_page_previews = can_add_web_page_previews
self.can_send_polls = can_send_polls
@staticmethod
def _parse(client, member, users) -> "ChatMember":
@ -91,7 +184,7 @@ class ChatMember(Object):
return ChatMember(
user=user,
status="member",
date=member.date,
joined_date=member.date,
invited_by=invited_by,
client=client
)
@ -107,29 +200,52 @@ class ChatMember(Object):
return ChatMember(
user=user,
status="administrator",
date=member.date,
joined_date=member.date,
invited_by=invited_by,
client=client
)
if isinstance(member, types.ChannelParticipantAdmin):
permissions = member.admin_rights
return ChatMember(
user=user,
status="administrator",
date=member.date,
joined_date=member.date,
invited_by=invited_by,
promoted_by=pyrogram.User._parse(client, users[member.promoted_by]),
permissions=pyrogram.ChatPermissions._parse(member),
can_be_edited=member.can_edit,
can_change_info=permissions.change_info,
can_post_messages=permissions.post_messages,
can_edit_messages=permissions.edit_messages,
can_delete_messages=permissions.delete_messages,
can_restrict_members=permissions.ban_users,
can_invite_users=permissions.invite_users,
can_pin_messages=permissions.pin_messages,
can_promote_members=permissions.add_admins,
client=client
)
if isinstance(member, types.ChannelParticipantBanned):
denied_permissions = member.banned_rights
return ChatMember(
user=user,
status="kicked" if member.banned_rights.view_messages else "restricted",
date=member.date,
until_date=denied_permissions.until_date,
joined_date=member.date,
is_member=not member.left,
restricted_by=pyrogram.User._parse(client, users[member.kicked_by]),
permissions=pyrogram.ChatPermissions._parse(member),
can_send_messages=not denied_permissions.send_messages,
can_send_media_messages=not denied_permissions.send_media,
can_send_other_messages=(
not denied_permissions.send_stickers or not denied_permissions.send_gifs or
not denied_permissions.send_games or not denied_permissions.send_inline
),
can_add_web_page_previews=not denied_permissions.embed_links,
can_send_polls=not denied_permissions.send_polls,
can_change_info=not denied_permissions.change_info,
can_invite_users=not denied_permissions.invite_users,
can_pin_messages=not denied_permissions.pin_messages,
client=client
)

View File

@ -16,8 +16,6 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Union
from pyrogram.api import types
from ..object import Object
@ -29,154 +27,70 @@ class ChatPermissions(Object):
administrators in groups or channels.
Parameters:
until_date (``int``, *optional*):
Applicable to restricted and kicked members only.
Date when user restrictions will be lifted, unix time.
0 means the restrictions will never be lifted (user restricted forever).
can_be_edited (``bool``, *optional*):
Applicable to administrators only.
True, if you are allowed to edit administrator privileges of the user.
can_change_info (``bool``, *optional*):
Applicable to default chat permissions in private groups and administrators in public groups only.
True, if the chat title, photo and other settings can be changed.
can_post_messages (``bool``, *optional*):
Applicable to channel administrators only.
True, if the administrator can post messages in the channel, channels only.
can_edit_messages (``bool``, *optional*):
Applicable to channel administrators only.
True, if the administrator can edit messages of other users and can pin messages, channels only.
can_delete_messages (``bool``, *optional*):
Applicable to administrators only.
True, if the administrator can delete messages of other users.
can_restrict_members (``bool``, *optional*):
Applicable to administrators only.
True, if the administrator can restrict, ban or unban chat members.
can_invite_users (``bool``, *optional*):
Applicable to default chat permissions and administrators only.
True, if new users can be invited to the chat.
can_pin_messages (``bool``, *optional*):
Applicable to default chat permissions in private groups and administrators in public groups only.
True, if messages can be pinned, supergroups only.
can_promote_members (``bool``, *optional*):
Applicable to administrators only.
True, if the administrator can add new administrators with a subset of his own privileges or demote
administrators that he has promoted, directly or indirectly (promoted by administrators that were appointed
by the user).
can_send_messages (``bool``, *optional*):
Applicable to default chat permissions and restricted members only.
True, if text messages, contacts, locations and venues can be sent.
True, if the user is allowed to send text messages, contacts, locations and venues.
can_send_media_messages (``bool``, *optional*):
Applicable to default chat permissions and restricted members only.
True, if audios, documents, photos, videos, video notes and voice notes can be sent, implies
True, if the user is allowed to send audios, documents, photos, videos, video notes and voice notes, implies
can_send_messages.
can_send_other_messages (``bool``, *optional*):
Applicable to default chat permissions and restricted members only.
True, if animations, games, stickers and inline bot results can be sent, implies can_send_media_messages.
True, if the user is allowed to send animations, games, stickers and use inline bots, implies
can_send_media_messages
can_add_web_page_previews (``bool``, *optional*):
Applicable to default chat permissions and restricted members only.
True, if web page previews can be attached to text messages, implies can_send_media_messages.
True, if the user is allowed to add web page previews to their messages, implies can_send_media_messages.
can_send_polls (``bool``, *optional*):
Applicable to default chat permissions and restricted members only.
True, if polls can be sent, implies can_send_media_messages.
True, if the user is allowed to send polls, implies can_send_messages.
can_change_info (``bool``, *optional*):
True, if the user is allowed to change the chat title, photo and other settings.
Ignored in public supergroups.
can_invite_users (``bool``, *optional*):
True, if the user is allowed to invite new users to the chat.
can_pin_messages (``bool``, *optional*):
True, if the user is allowed to pin messages.
Ignored in public supergroups.
"""
def __init__(
self,
*,
until_date: int = None,
# Admin permissions
can_be_edited: bool = None,
can_change_info: bool = None,
can_post_messages: bool = None, # Channels only
can_edit_messages: bool = None, # Channels only
can_delete_messages: bool = None,
can_restrict_members: bool = None,
can_invite_users: bool = None,
can_pin_messages: bool = None, # Supergroups only
can_promote_members: bool = None,
# Restricted user permissions
can_send_messages: bool = None, # Text, contacts, locations and venues
can_send_media_messages: bool = None, # Audios, documents, photos, videos, video notes and voice notes
can_send_other_messages: bool = None, # Animations (GIFs), games, stickers, inline bot results
can_add_web_page_previews: bool = None,
can_send_polls: bool = None
can_send_polls: bool = None,
can_change_info: bool = None,
can_invite_users: bool = None,
can_pin_messages: bool = None
):
super().__init__(None)
self.until_date = until_date
self.can_be_edited = can_be_edited
self.can_change_info = can_change_info
self.can_post_messages = can_post_messages
self.can_edit_messages = can_edit_messages
self.can_delete_messages = can_delete_messages
self.can_restrict_members = can_restrict_members
self.can_invite_users = can_invite_users
self.can_pin_messages = can_pin_messages
self.can_promote_members = can_promote_members
self.can_send_messages = can_send_messages
self.can_send_media_messages = can_send_media_messages
self.can_send_other_messages = can_send_other_messages
self.can_add_web_page_previews = can_add_web_page_previews
self.can_send_polls = can_send_polls
self.can_change_info = can_change_info
self.can_invite_users = can_invite_users
self.can_pin_messages = can_pin_messages
@staticmethod
def _parse(
entity: Union[
types.ChannelParticipantAdmin,
types.ChannelParticipantBanned,
types.ChatBannedRights
]
) -> "ChatPermissions":
if isinstance(entity, types.ChannelParticipantAdmin):
permissions = entity.admin_rights
return ChatPermissions(
can_be_edited=entity.can_edit,
can_change_info=permissions.change_info,
can_post_messages=permissions.post_messages,
can_edit_messages=permissions.edit_messages,
can_delete_messages=permissions.delete_messages,
can_restrict_members=permissions.ban_users,
can_invite_users=permissions.invite_users,
can_pin_messages=permissions.pin_messages,
can_promote_members=permissions.add_admins
)
if isinstance(entity, (types.ChannelParticipantBanned, types.ChatBannedRights)):
if isinstance(entity, types.ChannelParticipantBanned):
denied_permissions = entity.banned_rights # type: types.ChatBannedRights
else:
denied_permissions = entity
return ChatPermissions(
until_date=0 if denied_permissions.until_date == (1 << 31) - 1 else denied_permissions.until_date,
can_send_messages=not denied_permissions.send_messages,
can_send_media_messages=not denied_permissions.send_media,
can_send_other_messages=(
not denied_permissions.send_stickers or not denied_permissions.send_gifs or
not denied_permissions.send_games or not denied_permissions.send_inline
),
can_add_web_page_previews=not denied_permissions.embed_links,
can_send_polls=not denied_permissions.send_polls,
can_change_info=not denied_permissions.change_info,
can_invite_users=not denied_permissions.invite_users,
can_pin_messages=not denied_permissions.pin_messages
)
def _parse(denied_permissions: types.ChatBannedRights) -> "ChatPermissions":
return ChatPermissions(
can_send_messages=not denied_permissions.send_messages,
can_send_media_messages=not denied_permissions.send_media,
can_send_other_messages=(
not denied_permissions.send_stickers or not denied_permissions.send_gifs or
not denied_permissions.send_games or not denied_permissions.send_inline
),
can_add_web_page_previews=not denied_permissions.embed_links,
can_send_polls=not denied_permissions.send_polls,
can_change_info=not denied_permissions.change_info,
can_invite_users=not denied_permissions.invite_users,
can_pin_messages=not denied_permissions.pin_messages
)