added support for chats in get_permissions and added more permissions in ParticipantPermissions

This commit is contained in:
kolay 2020-10-02 19:19:54 +03:00
parent 90b9d5fbc5
commit 3aacc71c93
2 changed files with 91 additions and 15 deletions

View File

@ -1147,20 +1147,28 @@ class ChatMethods:
async def get_permissions( async def get_permissions(
self: 'TelegramClient', self: 'TelegramClient',
channel: 'hints.EntityLike', entity: 'hints.EntityLike',
user: 'hints.EntityLike' user: 'hints.EntityLike'
) -> 'custom.ParticipantPermissions': ) -> 'custom.ParticipantPermissions':
channel = await self.get_input_entity(channel) entity = await self.get_input_entity(entity)
user = await self.get_input_entity(user) user = await self.get_input_entity(user)
if helpers._entity_type(user) != helpers._EntityType.USER: if helpers._entity_type(user) != helpers._EntityType.USER:
raise ValueError('You must pass a user entity') raise ValueError('You must pass a user entity')
if helpers._entity_type(channel) != helpers._EntityType.CHANNEL: if helpers._entity_type(entity) == helpers._EntityType.CHANNEL:
raise ValueError('You must pass a channel entity')
participant = await self(functions.channels.GetParticipantRequest( participant = await self(functions.channels.GetParticipantRequest(
channel, entity,
user user
)) ))
return custom.ParticipantPermissions(participant.participant) return custom.ParticipantPermissions(participant.participant, False)
elif helpers._entity_type(entity) == helpers._EntityType.CHAT:
chat = await self(functions.messages.GetFullChatRequest(
entity
))
for participant in chat.participants.participants:
if participant.user_id == user.id:
return custom.ParticipantPermissions(participant.participant, True)
raise ValueError('You must pass either a channel or a chat')
async def get_stats( async def get_stats(
self: 'TelegramClient', self: 'TelegramClient',

View File

@ -5,25 +5,93 @@ class ParticipantPermissions:
""" """
Participant permissions information Participant permissions information
""" """
def __init__(self, participant): def __init__(self, participant, chat):
self.participant = participant self.participant = participant
self.is_chat: bool = chat
@property @property
def is_admin(self): def is_admin(self):
return self.is_creator or isinstance(self.participant, types.ChannelParticipantAdmin) return self.is_creator or isinstance(self.participant, (
types.ChannelParticipantAdmin,
types.ChatParticipantAdmin
))
@property @property
def is_creator(self): def is_creator(self):
return isinstance(self.participant, types.ChannelParticipantCreator) return isinstance(self.participant, types.ChannelParticipantCreator)
@property @property
def is_default_permissions(self): def has_default_permissions(self):
return isinstance(self.participant, types.ChannelParticipant) return isinstance(self.participant, (
types.ChannelParticipant,
types.ChatParticipant,
types.ChannelParticipantSelf
))
@property @property
def is_banned(self): def is_banned(self):
return isinstance(self.participant, types.ChannelParticipantBanned) return isinstance(self.participant, types.ChannelParticipantBanned)
@property @property
def is_self(self): def ban_users(self):
return isinstance(self.participant, types.ChannelParticipantSelf) if not self.is_admin:
return False
if self.is_chat:
return True
return self.participant.admin_rights.ban_users
@property
def pin_messages(self):
if not self.is_admin:
return False
if self.is_chat:
return True
return self.participant.admin_rights.pin_messages
@property
def add_admins(self):
if not self.is_admin:
return False
if self.is_chat and not self.is_creator:
return False
return self.participant.admin_rights.add_admins
@property
def invite_users(self):
if not self.is_admin:
return False
if self.is_chat:
return True
return self.participant.admin_rights.invite_users
@property
def delete_messages(self):
if not self.is_admin:
return False
if self.is_chat:
return True
return self.participant.admin_rights.delete_messages
@property
def edit_messages(self):
if not self.is_admin:
return False
if self.is_chat:
return True
return self.participant.admin_rights.edit_messages
@property
def post_messages(self):
if not self.is_admin:
return False
if self.is_chat:
return True
return self.participant.admin_rights.post_messages
@property
def change_info(self):
if not self.is_admin:
return False
if self.is_chat:
return True
return self.participant.admin_rights.change_info