From 2db2ca3283ed433e4e8be5512eca1b0fc571a987 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 8 Jun 2019 13:49:01 +0200 Subject: [PATCH] Remove ChatMembers type --- docs/source/api/types.rst | 2 - .../client/methods/chats/get_chat_member.py | 9 ++- .../client/methods/chats/get_chat_members.py | 48 +++++++------ .../client/methods/chats/iter_chat_members.py | 2 +- pyrogram/client/types/__init__.py | 2 + .../client/types/user_and_chats/__init__.py | 4 +- .../types/user_and_chats/chat_members.py | 71 ------------------- 7 files changed, 38 insertions(+), 100 deletions(-) delete mode 100644 pyrogram/client/types/user_and_chats/chat_members.py diff --git a/docs/source/api/types.rst b/docs/source/api/types.rst index 4eef9638..0aab7b5e 100644 --- a/docs/source/api/types.rst +++ b/docs/source/api/types.rst @@ -30,7 +30,6 @@ Users & Chats - :class:`ChatPreview` - :class:`ChatPhoto` - :class:`ChatMember` - - :class:`ChatMembers` - :class:`ChatPermissions` - :class:`Dialog` - :class:`Dialogs` @@ -123,7 +122,6 @@ Details .. autoclass:: ChatPreview() .. autoclass:: ChatPhoto() .. autoclass:: ChatMember() -.. autoclass:: ChatMembers() .. autoclass:: ChatPermissions() .. autoclass:: Dialog() .. autoclass:: Dialogs() diff --git a/pyrogram/client/methods/chats/get_chat_member.py b/pyrogram/client/methods/chats/get_chat_member.py index b84836c6..b0d0641a 100644 --- a/pyrogram/client/methods/chats/get_chat_member.py +++ b/pyrogram/client/methods/chats/get_chat_member.py @@ -51,13 +51,18 @@ class GetChatMember(BaseClient): user = self.resolve_peer(user_id) if isinstance(chat, types.InputPeerChat): - full_chat = self.send( + r = self.send( functions.messages.GetFullChat( chat_id=chat.chat_id ) ) - for member in pyrogram.ChatMembers._parse(self, full_chat).chat_members: + members = r.full_chat.participants.participants + users = {i.id: i for i in r.users} + + for member in members: + member = pyrogram.ChatMember._parse(self, member, users) + if isinstance(user, types.InputPeerSelf): if member.user.is_self: return member diff --git a/pyrogram/client/methods/chats/get_chat_members.py b/pyrogram/client/methods/chats/get_chat_members.py index 1c966f36..0b4613d8 100644 --- a/pyrogram/client/methods/chats/get_chat_members.py +++ b/pyrogram/client/methods/chats/get_chat_members.py @@ -18,7 +18,7 @@ import logging import time -from typing import Union +from typing import Union, List import pyrogram from pyrogram.api import functions, types @@ -45,7 +45,7 @@ class GetChatMembers(BaseClient): limit: int = 200, query: str = "", filter: str = Filters.ALL - ) -> "pyrogram.ChatMembers": + ) -> List["pyrogram.ChatMember"]: """Get a chunk of the members list of a chat. You can get up to 200 chat members at once. @@ -59,15 +59,16 @@ class GetChatMembers(BaseClient): offset (``int``, *optional*): Sequential number of the first member to be returned. - Defaults to 0 [1]_. + Only applicable to supergroups and channels. Defaults to 0 [1]_. limit (``int``, *optional*): Limits the number of members to be retrieved. + Only applicable to supergroups and channels. Defaults to 200, which is also the maximum server limit allowed per method call. query (``str``, *optional*): Query string to filter members based on their display names and usernames. - Defaults to "" (empty string) [2]_. + Only applicable to supergroups and channels. Defaults to "" (empty string) [2]_. filter (``str``, *optional*): Filter used to select the kind of members you want to retrieve. Only applicable for supergroups @@ -78,6 +79,7 @@ class GetChatMembers(BaseClient): *"bots"* - bots only, *"recent"* - recent members only, *"administrators"* - chat administrators only. + Only applicable to supergroups and channels. Defaults to *"all"*. .. [1] Server limit: on supergroups, you can get up to 10,000 members for a single query and up to 200 members @@ -86,7 +88,7 @@ class GetChatMembers(BaseClient): .. [2] A query string is applicable only for *"all"*, *"kicked"* and *"restricted"* filters only. Returns: - :obj:`ChatMembers`: On success, an object containing a list of chat members is returned. + List of :obj:`ChatMember`: On success, a list of chat members is returned. Raises: RPCError: In case of a Telegram RPC error. @@ -95,14 +97,16 @@ class GetChatMembers(BaseClient): peer = self.resolve_peer(chat_id) if isinstance(peer, types.InputPeerChat): - return pyrogram.ChatMembers._parse( - self, - self.send( - functions.messages.GetFullChat( - chat_id=peer.chat_id - ) + r = self.send( + functions.messages.GetFullChat( + chat_id=peer.chat_id ) ) + + members = r.full_chat.participants.participants + users = {i.id: i for i in r.users} + + return pyrogram.List(pyrogram.ChatMember._parse(self, member, users) for member in members) elif isinstance(peer, types.InputPeerChannel): filter = filter.lower() @@ -123,18 +127,20 @@ class GetChatMembers(BaseClient): while True: try: - return pyrogram.ChatMembers._parse( - self, - self.send( - functions.channels.GetParticipants( - channel=peer, - filter=filter, - offset=offset, - limit=limit, - hash=0 - ) + r = self.send( + functions.channels.GetParticipants( + channel=peer, + filter=filter, + offset=offset, + limit=limit, + hash=0 ) ) + + members = r.participants + users = {i.id: i for i in r.users} + + return pyrogram.List(pyrogram.ChatMember._parse(self, member, users) for member in members) except FloodWait as e: log.warning("Sleeping for {}s".format(e.x)) time.sleep(e.x) diff --git a/pyrogram/client/methods/chats/iter_chat_members.py b/pyrogram/client/methods/chats/iter_chat_members.py index 961f6d98..fe117694 100644 --- a/pyrogram/client/methods/chats/iter_chat_members.py +++ b/pyrogram/client/methods/chats/iter_chat_members.py @@ -106,7 +106,7 @@ class IterChatMembers(BaseClient): limit=limit, query=q, filter=filter - ).chat_members + ) if not chat_members: break diff --git a/pyrogram/client/types/__init__.py b/pyrogram/client/types/__init__.py index 3d430c44..8a725a9e 100644 --- a/pyrogram/client/types/__init__.py +++ b/pyrogram/client/types/__init__.py @@ -20,6 +20,8 @@ from .keyboards import * from .inline_mode import * from .input_media import * from .input_message_content import * +from .list import List from .messages_and_media import * +from .object import Object from .update import * from .user_and_chats import * diff --git a/pyrogram/client/types/user_and_chats/__init__.py b/pyrogram/client/types/user_and_chats/__init__.py index 2059589a..cf032850 100644 --- a/pyrogram/client/types/user_and_chats/__init__.py +++ b/pyrogram/client/types/user_and_chats/__init__.py @@ -18,7 +18,6 @@ from .chat import Chat from .chat_member import ChatMember -from .chat_members import ChatMembers from .chat_permissions import ChatPermissions from .chat_photo import ChatPhoto from .chat_preview import ChatPreview @@ -28,6 +27,5 @@ from .user import User from .user_status import UserStatus __all__ = [ - "Chat", "ChatMember", "ChatMembers", "ChatPermissions", "ChatPhoto", "ChatPreview", "Dialog", "Dialogs", "User", - "UserStatus" + "Chat", "ChatMember", "ChatPermissions", "ChatPhoto", "ChatPreview", "Dialog", "Dialogs", "User", "UserStatus" ] diff --git a/pyrogram/client/types/user_and_chats/chat_members.py b/pyrogram/client/types/user_and_chats/chat_members.py deleted file mode 100644 index 6abdd719..00000000 --- a/pyrogram/client/types/user_and_chats/chat_members.py +++ /dev/null @@ -1,71 +0,0 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-2019 Dan Tès -# -# This file is part of Pyrogram. -# -# Pyrogram is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Pyrogram is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with Pyrogram. If not, see . - -from typing import List - -import pyrogram -from pyrogram.api import types -from .chat_member import ChatMember -from ..object import Object - - -class ChatMembers(Object): - """Contains information about the members list of a chat. - - Parameters: - total_count (``int``): - Total number of members the chat has. - - chat_members (List of :obj:`ChatMember `): - Requested chat members. - """ - - __slots__ = ["total_count", "chat_members"] - - def __init__( - self, - *, - client: "pyrogram.BaseClient" = None, - total_count: int, - chat_members: List[ChatMember] - ): - super().__init__(client) - - self.total_count = total_count - self.chat_members = chat_members - - @staticmethod - def _parse(client, members): - users = {i.id: i for i in members.users} - chat_members = [] - - if isinstance(members, types.channels.ChannelParticipants): - total_count = members.count - members = members.participants - else: - members = members.full_chat.participants.participants - total_count = len(members) - - for member in members: - chat_members.append(ChatMember._parse(client, member, users)) - - return ChatMembers( - total_count=total_count, - chat_members=chat_members, - client=client - )