Update Chat and ChatPermissions to accommodate default chat permissions

This commit is contained in:
Dan 2019-03-13 13:02:40 +01:00
parent 8c9e5e6753
commit b6038c4f2e
2 changed files with 21 additions and 18 deletions

View File

@ -18,6 +18,7 @@
import pyrogram
from pyrogram.api import types
from .chat_permissions import ChatPermissions
from .chat_photo import ChatPhoto
from ..pyrogram_type import PyrogramType
@ -44,9 +45,6 @@ class Chat(PyrogramType):
last_name (``str``, *optional*):
Last name of the other party in a private chat.
all_members_are_administrators (``bool``, *optional*):
True if a basic group has "All Members Are Admins" enabled.
photo (:obj:`ChatPhoto <pyrogram.ChatPhoto>`, *optional*):
Chat photo. Suitable for downloads only.
@ -86,7 +84,6 @@ class Chat(PyrogramType):
username: str = None,
first_name: str = None,
last_name: str = None,
all_members_are_administrators: bool = None,
photo: ChatPhoto = None,
description: str = None,
invite_link: str = None,
@ -94,7 +91,8 @@ class Chat(PyrogramType):
sticker_set_name: str = None,
can_set_sticker_set: bool = None,
members_count: int = None,
restriction_reason: str = None):
restriction_reason: str = None,
default_permissions: "pyrogram.ChatPermissions" = None):
super().__init__(client)
self.id = id
@ -103,7 +101,6 @@ class Chat(PyrogramType):
self.username = username
self.first_name = first_name
self.last_name = last_name
self.all_members_are_administrators = all_members_are_administrators
self.photo = photo
self.description = description
self.invite_link = invite_link
@ -112,6 +109,7 @@ class Chat(PyrogramType):
self.can_set_sticker_set = can_set_sticker_set
self.members_count = members_count
self.restriction_reason = restriction_reason
self.default_permissions = default_permissions
@staticmethod
def _parse_user_chat(client, user: types.User) -> "Chat":
@ -128,17 +126,12 @@ class Chat(PyrogramType):
@staticmethod
def _parse_chat_chat(client, chat: types.Chat) -> "Chat":
admins_enabled = getattr(chat, "admins_enabled", None)
if admins_enabled is not None:
admins_enabled = not admins_enabled
return Chat(
id=-chat.id,
type="group",
title=chat.title,
all_members_are_administrators=admins_enabled,
photo=ChatPhoto._parse(client, getattr(chat, "photo", None)),
default_permissions=ChatPermissions._parse(chat.default_banned_rights),
client=client
)
@ -151,6 +144,7 @@ class Chat(PyrogramType):
username=getattr(channel, "username", None),
photo=ChatPhoto._parse(client, getattr(channel, "photo", None)),
restriction_reason=getattr(channel, "restriction_reason", None),
default_permissions=ChatPermissions._parse(channel.default_banned_rights),
client=client
)

View File

@ -138,12 +138,18 @@ class ChatPermissions(PyrogramType):
self.can_send_polls = can_send_polls
@staticmethod
def _parse(member: Union[types.ChannelParticipantAdmin, types.ChannelParticipantBanned]) -> "ChatPermissions":
if isinstance(member, types.ChannelParticipantAdmin):
permissions = member.admin_rights
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=member.can_edit,
can_be_edited=entity.can_edit,
can_change_info=permissions.change_info,
can_post_messages=permissions.post_messages,
can_edit_messages=permissions.edit_messages,
@ -154,8 +160,11 @@ class ChatPermissions(PyrogramType):
can_promote_members=permissions.add_admins
)
if isinstance(member, types.ChannelParticipantBanned):
denied_permissions = member.banned_rights # type: types.ChatBannedRights
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,