Skip to content

Commit b6038c4

Browse files
committed
Update Chat and ChatPermissions to accommodate default chat permissions
1 parent 8c9e5e6 commit b6038c4

2 files changed

Lines changed: 21 additions & 18 deletions

File tree

pyrogram/client/types/user_and_chats/chat.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import pyrogram
2020
from pyrogram.api import types
21+
from .chat_permissions import ChatPermissions
2122
from .chat_photo import ChatPhoto
2223
from ..pyrogram_type import PyrogramType
2324

@@ -44,9 +45,6 @@ class Chat(PyrogramType):
4445
last_name (``str``, *optional*):
4546
Last name of the other party in a private chat.
4647
47-
all_members_are_administrators (``bool``, *optional*):
48-
True if a basic group has "All Members Are Admins" enabled.
49-
5048
photo (:obj:`ChatPhoto <pyrogram.ChatPhoto>`, *optional*):
5149
Chat photo. Suitable for downloads only.
5250
@@ -86,15 +84,15 @@ def __init__(self,
8684
username: str = None,
8785
first_name: str = None,
8886
last_name: str = None,
89-
all_members_are_administrators: bool = None,
9087
photo: ChatPhoto = None,
9188
description: str = None,
9289
invite_link: str = None,
9390
pinned_message=None,
9491
sticker_set_name: str = None,
9592
can_set_sticker_set: bool = None,
9693
members_count: int = None,
97-
restriction_reason: str = None):
94+
restriction_reason: str = None,
95+
default_permissions: "pyrogram.ChatPermissions" = None):
9896
super().__init__(client)
9997

10098
self.id = id
@@ -103,7 +101,6 @@ def __init__(self,
103101
self.username = username
104102
self.first_name = first_name
105103
self.last_name = last_name
106-
self.all_members_are_administrators = all_members_are_administrators
107104
self.photo = photo
108105
self.description = description
109106
self.invite_link = invite_link
@@ -112,6 +109,7 @@ def __init__(self,
112109
self.can_set_sticker_set = can_set_sticker_set
113110
self.members_count = members_count
114111
self.restriction_reason = restriction_reason
112+
self.default_permissions = default_permissions
115113

116114
@staticmethod
117115
def _parse_user_chat(client, user: types.User) -> "Chat":
@@ -128,17 +126,12 @@ def _parse_user_chat(client, user: types.User) -> "Chat":
128126

129127
@staticmethod
130128
def _parse_chat_chat(client, chat: types.Chat) -> "Chat":
131-
admins_enabled = getattr(chat, "admins_enabled", None)
132-
133-
if admins_enabled is not None:
134-
admins_enabled = not admins_enabled
135-
136129
return Chat(
137130
id=-chat.id,
138131
type="group",
139132
title=chat.title,
140-
all_members_are_administrators=admins_enabled,
141133
photo=ChatPhoto._parse(client, getattr(chat, "photo", None)),
134+
default_permissions=ChatPermissions._parse(chat.default_banned_rights),
142135
client=client
143136
)
144137

@@ -151,6 +144,7 @@ def _parse_channel_chat(client, channel: types.Channel) -> "Chat":
151144
username=getattr(channel, "username", None),
152145
photo=ChatPhoto._parse(client, getattr(channel, "photo", None)),
153146
restriction_reason=getattr(channel, "restriction_reason", None),
147+
default_permissions=ChatPermissions._parse(channel.default_banned_rights),
154148
client=client
155149
)
156150

pyrogram/client/types/user_and_chats/chat_permissions.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,18 @@ def __init__(
138138
self.can_send_polls = can_send_polls
139139

140140
@staticmethod
141-
def _parse(member: Union[types.ChannelParticipantAdmin, types.ChannelParticipantBanned]) -> "ChatPermissions":
142-
if isinstance(member, types.ChannelParticipantAdmin):
143-
permissions = member.admin_rights
141+
def _parse(
142+
entity: Union[
143+
types.ChannelParticipantAdmin,
144+
types.ChannelParticipantBanned,
145+
types.ChatBannedRights
146+
]
147+
) -> "ChatPermissions":
148+
if isinstance(entity, types.ChannelParticipantAdmin):
149+
permissions = entity.admin_rights
144150

145151
return ChatPermissions(
146-
can_be_edited=member.can_edit,
152+
can_be_edited=entity.can_edit,
147153
can_change_info=permissions.change_info,
148154
can_post_messages=permissions.post_messages,
149155
can_edit_messages=permissions.edit_messages,
@@ -154,8 +160,11 @@ def _parse(member: Union[types.ChannelParticipantAdmin, types.ChannelParticipant
154160
can_promote_members=permissions.add_admins
155161
)
156162

157-
if isinstance(member, types.ChannelParticipantBanned):
158-
denied_permissions = member.banned_rights # type: types.ChatBannedRights
163+
if isinstance(entity, (types.ChannelParticipantBanned, types.ChatBannedRights)):
164+
if isinstance(entity, types.ChannelParticipantBanned):
165+
denied_permissions = entity.banned_rights # type: types.ChatBannedRights
166+
else:
167+
denied_permissions = entity
159168

160169
return ChatPermissions(
161170
until_date=0 if denied_permissions.until_date == (1 << 31) - 1 else denied_permissions.until_date,

0 commit comments

Comments
 (0)