2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 13:27:47 +00:00

Refactor ChatMember and ChatMembers

This commit is contained in:
Dan 2018-12-16 16:22:46 +01:00
parent 3ecbb35470
commit d62bc1d8ba
2 changed files with 104 additions and 43 deletions

View File

@ -16,10 +16,11 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from pyrogram.api.core import Object from pyrogram.api import types
from ..pyrogram_type import PyrogramType
class ChatMember(Object): class ChatMember(PyrogramType):
"""This object contains information about one member of a chat. """This object contains information about one member of a chat.
Args: Args:
@ -78,40 +79,77 @@ class ChatMember(Object):
Restricted only. True, if user may add web page previews to his messages, implies can_send_media_messages. Restricted only. True, if user may add web page previews to his messages, implies can_send_media_messages.
""" """
ID = 0xb0700016 def __init__(self, *, user, status: str, until_date: int = None, can_be_edited: bool = None,
can_change_info: bool = None, can_post_messages: bool = None, can_edit_messages: bool = None,
can_delete_messages: bool = None, can_invite_users: bool = None, can_restrict_members: bool = None,
can_pin_messages: bool = None, can_promote_members: bool = None, can_send_messages: bool = None,
can_send_media_messages: bool = None, can_send_other_messages: bool = None,
can_add_web_page_previews: bool = None, client=None):
self.user = user
self.status = status
self.until_date = until_date
self.can_be_edited = can_be_edited
self.can_change_info = can_change_info
self.can_post_messages = can_post_messages
self.can_edit_messages = can_edit_messages
self.can_delete_messages = can_delete_messages
self.can_invite_users = can_invite_users
self.can_restrict_members = can_restrict_members
self.can_pin_messages = can_pin_messages
self.can_promote_members = can_promote_members
self.can_send_messages = can_send_messages
self.can_send_media_messages = can_send_media_messages
self.can_send_other_messages = can_send_other_messages
self.can_add_web_page_previews = can_add_web_page_previews
def __init__( self.client = client
self,
user, @staticmethod
status: str, def parse(client, member, user) -> "ChatMember":
until_date: int = None, if isinstance(member, (types.ChannelParticipant, types.ChannelParticipantSelf, types.ChatParticipant)):
can_be_edited: bool = None, return ChatMember(user=user, status="member", client=client)
can_change_info: bool = None,
can_post_messages: bool = None, if isinstance(member, (types.ChannelParticipantCreator, types.ChatParticipantCreator)):
can_edit_messages: bool = None, return ChatMember(user=user, status="creator", client=client)
can_delete_messages: bool = None,
can_invite_users: bool = None, if isinstance(member, types.ChatParticipantAdmin):
can_restrict_members: bool = None, return ChatMember(user=user, status="administrator", client=client)
can_pin_messages: bool = None,
can_promote_members: bool = None, if isinstance(member, types.ChannelParticipantAdmin):
can_send_messages: bool = None, rights = member.admin_rights
can_send_media_messages: bool = None,
can_send_other_messages: bool = None, return ChatMember(
can_add_web_page_previews: bool = None user=user,
): status="administrator",
self.user = user # User can_be_edited=member.can_edit,
self.status = status # string can_change_info=rights.change_info,
self.until_date = until_date # flags.0?int can_post_messages=rights.post_messages,
self.can_be_edited = can_be_edited # flags.1?Bool can_edit_messages=rights.edit_messages,
self.can_change_info = can_change_info # flags.2?Bool can_delete_messages=rights.delete_messages,
self.can_post_messages = can_post_messages # flags.3?Bool can_invite_users=rights.invite_users or rights.invite_link,
self.can_edit_messages = can_edit_messages # flags.4?Bool can_restrict_members=rights.ban_users,
self.can_delete_messages = can_delete_messages # flags.5?Bool can_pin_messages=rights.pin_messages,
self.can_invite_users = can_invite_users # flags.6?Bool can_promote_members=rights.add_admins,
self.can_restrict_members = can_restrict_members # flags.7?Bool client=client
self.can_pin_messages = can_pin_messages # flags.8?Bool )
self.can_promote_members = can_promote_members # flags.9?Bool
self.can_send_messages = can_send_messages # flags.10?Bool if isinstance(member, types.ChannelParticipantBanned):
self.can_send_media_messages = can_send_media_messages # flags.11?Bool rights = member.banned_rights
self.can_send_other_messages = can_send_other_messages # flags.12?Bool
self.can_add_web_page_previews = can_add_web_page_previews # flags.13?Bool chat_member = ChatMember(
user=user,
status="kicked" if rights.view_messages else "restricted",
until_date=0 if rights.until_date == (1 << 31) - 1 else rights.until_date,
client=client
)
if chat_member.status == "restricted":
chat_member.can_send_messages = not rights.send_messages
chat_member.can_send_media_messages = not rights.send_media
chat_member.can_send_other_messages = (
not rights.send_stickers or not rights.send_gifs or
not rights.send_games or not rights.send_inline
)
chat_member.can_add_web_page_previews = not rights.embed_links
return chat_member

View File

@ -16,10 +16,12 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from pyrogram.api.core import Object from pyrogram.api import types
from ..pyrogram_type import PyrogramType
from ..user_and_chats import ChatMember, User
class ChatMembers(Object): class ChatMembers(PyrogramType):
"""This object contains information about the members list of a chat. """This object contains information about the members list of a chat.
Args: Args:
@ -30,8 +32,29 @@ class ChatMembers(Object):
Requested chat members. Requested chat members.
""" """
ID = 0xb0700030 def __init__(self, *, total_count: int, chat_members: list, client=None):
def __init__(self, total_count: int, chat_members: list):
self.total_count = total_count self.total_count = total_count
self.chat_members = chat_members self.chat_members = chat_members
self.client = client
@staticmethod
def parse(client, members, users: dict):
if isinstance(members, types.channels.ChannelParticipants):
total_count = members.count
members = members.participants
else:
members = members.full_chat.participants.participants
total_count = len(members)
chat_members = []
for member in members:
user = User.parse(client, users[member.user_id])
chat_members.append(ChatMember.parse(client, member, user))
return ChatMembers(
total_count=total_count,
chat_members=chat_members,
client=client
)