From fc765f6014252425c90fb4fd42d32a0cc2ed730f Mon Sep 17 00:00:00 2001 From: kolay Date: Sat, 3 Oct 2020 17:59:54 +0300 Subject: [PATCH] Add new get_permissions method (#1575) Closes #1574. --- telethon/client/chats.py | 43 +++++++++ telethon/tl/custom/__init__.py | 1 + telethon/tl/custom/participantpermissions.py | 97 ++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 telethon/tl/custom/participantpermissions.py diff --git a/telethon/client/chats.py b/telethon/client/chats.py index 9dbdadb2..1cabd544 100644 --- a/telethon/client/chats.py +++ b/telethon/client/chats.py @@ -1145,6 +1145,49 @@ class ChatMethods: else: raise ValueError('You must pass either a channel or a chat') + async def get_permissions( + self: 'TelegramClient', + entity: 'hints.EntityLike', + user: 'hints.EntityLike' + ) -> 'typing.Optional[custom.ParticipantPermissions]': + """ + Fetches the permissions of a user in a specific chat or channel. + + Arguments + entity (`entity`): + The channel or chat the user is participant of. + + user (`entity`): + Target user. + + Example + .. code-block:: python + + permissions = await client.get_permissions(chat, user) + if permissions.is_admin: + # do something + """ + entity = await self.get_input_entity(entity) + user = await self.get_input_entity(user) + if helpers._entity_type(user) != helpers._EntityType.USER: + raise ValueError('You must pass a user entity') + if helpers._entity_type(entity) == helpers._EntityType.CHANNEL: + participant = await self(functions.channels.GetParticipantRequest( + entity, + user + )) + 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 errors.UserNotParticipantError(None) + + raise ValueError('You must pass either a channel or a chat') + async def get_stats( self: 'TelegramClient', entity: 'hints.EntityLike', diff --git a/telethon/tl/custom/__init__.py b/telethon/tl/custom/__init__.py index b5599c53..9804969e 100644 --- a/telethon/tl/custom/__init__.py +++ b/telethon/tl/custom/__init__.py @@ -11,3 +11,4 @@ from .inlineresult import InlineResult from .inlineresults import InlineResults from .conversation import Conversation from .qrlogin import QRLogin +from .participantpermissions import ParticipantPermissions diff --git a/telethon/tl/custom/participantpermissions.py b/telethon/tl/custom/participantpermissions.py new file mode 100644 index 00000000..654f32b6 --- /dev/null +++ b/telethon/tl/custom/participantpermissions.py @@ -0,0 +1,97 @@ +from .. import types + + +class ParticipantPermissions: + """ + Participant permissions information + """ + def __init__(self, participant, chat): + self.participant = participant + self.is_chat: bool = chat + + @property + def is_admin(self): + return self.is_creator or isinstance(self.participant, ( + types.ChannelParticipantAdmin, + types.ChatParticipantAdmin + )) + + @property + def is_creator(self): + return isinstance(self.participant, types.ChannelParticipantCreator) + + @property + def has_default_permissions(self): + return isinstance(self.participant, ( + types.ChannelParticipant, + types.ChatParticipant, + types.ChannelParticipantSelf + )) + + @property + def is_banned(self): + return isinstance(self.participant, types.ChannelParticipantBanned) + + @property + def ban_users(self): + 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