2020-10-03 17:59:54 +03:00
|
|
|
from .. import types
|
|
|
|
|
|
|
|
|
2020-10-06 12:13:52 +03:00
|
|
|
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}
|
|
|
|
|
|
|
|
|
2020-10-03 17:59:54 +03:00
|
|
|
class ParticipantPermissions:
|
|
|
|
"""
|
2020-10-05 11:50:47 +03:00
|
|
|
Participant permissions information.
|
|
|
|
|
|
|
|
The properties in this objects are boolean values indicating whether the
|
|
|
|
user has the permission or not.
|
|
|
|
|
|
|
|
Example
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
permissions = ...
|
|
|
|
|
|
|
|
if permissions.is_banned:
|
|
|
|
"this user is banned"
|
|
|
|
elif permissions.is_admin:
|
|
|
|
"this user is an administrator"
|
2020-10-03 17:59:54 +03:00
|
|
|
"""
|
2020-10-05 11:50:47 +03:00
|
|
|
def __init__(self, participant, chat: bool):
|
2020-10-03 17:59:54 +03:00
|
|
|
self.participant = participant
|
2020-10-05 11:50:47 +03:00
|
|
|
self.is_chat = chat
|
2020-10-03 17:59:54 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_admin(self):
|
2020-10-06 12:06:37 +03:00
|
|
|
"""
|
|
|
|
Whether the user is an administrator of the chat or not. The creator
|
|
|
|
also counts as begin an administrator, since they have all permissions.
|
|
|
|
"""
|
2020-10-03 17:59:54 +03:00
|
|
|
return self.is_creator or isinstance(self.participant, (
|
|
|
|
types.ChannelParticipantAdmin,
|
|
|
|
types.ChatParticipantAdmin
|
|
|
|
))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_creator(self):
|
2020-10-06 12:06:37 +03:00
|
|
|
"""
|
|
|
|
Whether the user is the creator of the chat or not.
|
|
|
|
"""
|
2020-10-03 18:16:10 +03:00
|
|
|
return isinstance(self.participant, (
|
|
|
|
types.ChannelParticipantCreator,
|
|
|
|
types.ChatParticipantCreator
|
|
|
|
))
|
2020-10-03 17:59:54 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def has_default_permissions(self):
|
2020-10-06 12:06:37 +03:00
|
|
|
"""
|
|
|
|
Whether the user is a normal user of the chat (not administrator, but
|
|
|
|
not banned either, and has no restrictions applied).
|
|
|
|
"""
|
2020-10-03 17:59:54 +03:00
|
|
|
return isinstance(self.participant, (
|
|
|
|
types.ChannelParticipant,
|
|
|
|
types.ChatParticipant,
|
|
|
|
types.ChannelParticipantSelf
|
|
|
|
))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_banned(self):
|
2020-10-06 12:06:37 +03:00
|
|
|
"""
|
|
|
|
Whether the user is banned in the chat.
|
|
|
|
"""
|
2020-10-03 17:59:54 +03:00
|
|
|
return isinstance(self.participant, types.ChannelParticipantBanned)
|
|
|
|
|
2020-10-07 10:44:17 +03:00
|
|
|
@property
|
|
|
|
def add_admins(self):
|
|
|
|
"""
|
|
|
|
Whether the administrator can add new administrators with the same or
|
|
|
|
less permissions than them.
|
|
|
|
"""
|
|
|
|
if not self.is_admin or (self.is_chat and not self.is_creator):
|
|
|
|
return False
|
|
|
|
|
|
|
|
return self.participant.admin_rights.add_admins
|
|
|
|
|
2020-10-06 12:13:52 +03:00
|
|
|
ban_users = property(**_admin_prop('ban_users', """
|
2020-10-06 12:06:37 +03:00
|
|
|
Whether the administrator can ban other users or not.
|
2020-10-06 12:13:52 +03:00
|
|
|
"""))
|
2020-10-03 17:59:54 +03:00
|
|
|
|
2020-10-06 12:13:52 +03:00
|
|
|
pin_messages = property(**_admin_prop('pin_messages', """
|
2020-10-06 12:06:37 +03:00
|
|
|
Whether the administrator can pin messages or not.
|
2020-10-06 12:13:52 +03:00
|
|
|
"""))
|
2020-10-03 17:59:54 +03:00
|
|
|
|
2020-10-06 12:13:52 +03:00
|
|
|
invite_users = property(**_admin_prop('invite_users', """
|
2020-10-06 12:06:37 +03:00
|
|
|
Whether the administrator can add new users to the chat.
|
2020-10-06 12:13:52 +03:00
|
|
|
"""))
|
2020-10-03 17:59:54 +03:00
|
|
|
|
2020-10-06 12:13:52 +03:00
|
|
|
delete_messages = property(**_admin_prop('delete_messages', """
|
2020-10-06 12:06:37 +03:00
|
|
|
Whether the administrator can delete messages from other participants.
|
2020-10-06 12:13:52 +03:00
|
|
|
"""))
|
2020-10-03 17:59:54 +03:00
|
|
|
|
2020-10-06 12:13:52 +03:00
|
|
|
edit_messages = property(**_admin_prop('edit_messages', """
|
2020-10-06 12:06:37 +03:00
|
|
|
Whether the administrator can edit messages.
|
2020-10-06 12:13:52 +03:00
|
|
|
"""))
|
2020-10-03 17:59:54 +03:00
|
|
|
|
2020-10-06 12:13:52 +03:00
|
|
|
post_messages = property(**_admin_prop('post_messages', """
|
2020-10-06 12:06:37 +03:00
|
|
|
Whether the administrator can post messages in the broadcast channel.
|
2020-10-06 12:13:52 +03:00
|
|
|
"""))
|
2020-10-03 17:59:54 +03:00
|
|
|
|
2020-10-06 12:13:52 +03:00
|
|
|
change_info = property(**_admin_prop('change_info', """
|
2020-10-06 12:06:37 +03:00
|
|
|
Whether the administrator can change the information about the chat,
|
|
|
|
such as title or description.
|
2020-10-06 12:13:52 +03:00
|
|
|
"""))
|
2020-10-06 11:43:00 +03:00
|
|
|
|
2020-10-06 12:13:52 +03:00
|
|
|
anonymous = property(**_admin_prop('anonymous', """
|
2020-10-06 12:06:37 +03:00
|
|
|
Whether the administrator will remain anonymous when sending messages.
|
2020-10-06 12:13:52 +03:00
|
|
|
"""))
|