Avoid redundant code in ParticipantPermissions

This commit is contained in:
Lonami Exo 2020-10-06 11:13:52 +02:00
parent cf1645b598
commit 05af5d0d74

View File

@ -1,6 +1,23 @@
from .. import types from .. import types
def _admin_prop(field_name, doc):
"""
Helper method to build properties that return `True` if the user is an
administrator of a normal chat, or otherwise return `True` if the user
has a specific permission being an admin of a channel.
"""
def fget(self):
if not self.is_admin:
return False
if self.is_chat:
return True
return getattr(self.participant.admin_rights, field_name)
return {'fget': fget, 'doc': doc}
class ParticipantPermissions: class ParticipantPermissions:
""" """
Participant permissions information. Participant permissions information.
@ -62,103 +79,40 @@ class ParticipantPermissions:
""" """
return isinstance(self.participant, types.ChannelParticipantBanned) return isinstance(self.participant, types.ChannelParticipantBanned)
@property ban_users = property(**_admin_prop('ban_users', """
def ban_users(self):
"""
Whether the administrator can ban other users or not. Whether the administrator can ban other users or not.
""" """))
if not self.is_admin:
return False
if self.is_chat:
return True
return self.participant.admin_rights.ban_users
@property pin_messages = property(**_admin_prop('pin_messages', """
def pin_messages(self):
"""
Whether the administrator can pin messages or not. Whether the administrator can pin messages or not.
""" """))
if not self.is_admin:
return False
if self.is_chat:
return True
return self.participant.admin_rights.pin_messages
@property add_admins = property(**_admin_prop('add_admins', """
def add_admins(self):
"""
Whether the administrator can add new administrators with the same or Whether the administrator can add new administrators with the same or
less permissions than them. less permissions than them.
""" """))
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 invite_users = property(**_admin_prop('invite_users', """
def invite_users(self):
"""
Whether the administrator can add new users to the chat. Whether the administrator can add new users to the chat.
""" """))
if not self.is_admin:
return False
if self.is_chat:
return True
return self.participant.admin_rights.invite_users
@property delete_messages = property(**_admin_prop('delete_messages', """
def delete_messages(self):
"""
Whether the administrator can delete messages from other participants. Whether the administrator can delete messages from other participants.
""" """))
if not self.is_admin:
return False
if self.is_chat:
return True
return self.participant.admin_rights.delete_messages
@property edit_messages = property(**_admin_prop('edit_messages', """
def edit_messages(self):
"""
Whether the administrator can edit messages. Whether the administrator can edit messages.
""" """))
if not self.is_admin:
return False
if self.is_chat:
return True
return self.participant.admin_rights.edit_messages
@property post_messages = property(**_admin_prop('post_messages', """
def post_messages(self):
"""
Whether the administrator can post messages in the broadcast channel. Whether the administrator can post messages in the broadcast channel.
""" """))
if not self.is_admin:
return False
if self.is_chat:
return True
return self.participant.admin_rights.post_messages
@property change_info = property(**_admin_prop('change_info', """
def change_info(self):
"""
Whether the administrator can change the information about the chat, Whether the administrator can change the information about the chat,
such as title or description. such as title or description.
""" """))
if not self.is_admin:
return False
if self.is_chat:
return True
return self.participant.admin_rights.change_info
@property anonymous = property(**_admin_prop('anonymous', """
def anonymous(self):
"""
Whether the administrator will remain anonymous when sending messages. Whether the administrator will remain anonymous when sending messages.
""" """))
if not self.is_admin:
return False
if self.is_chat:
return True
return self.participant.admin_rights.anonymous