From 87c4d08d9ce6adacfe81148b1c93e782d27abea7 Mon Sep 17 00:00:00 2001 From: bakatrouble Date: Fri, 1 Mar 2019 20:28:46 +0300 Subject: [PATCH] client.join_chat() now returns pyrogram.Chat instead of MTProto Update (#206) * client.join_chat() now returns pyrogram.Chat instead of MTProto Update * Do not use Chat._parse_mtproto_chat() method * Update chat.py Rename _parse_mtproto_chat to a generic _parse_chat_chat Hint about its current usage (none). --- pyrogram/client/methods/chats/join_chat.py | 14 ++++++++++++-- pyrogram/client/types/user_and_chats/chat.py | 11 +++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/pyrogram/client/methods/chats/join_chat.py b/pyrogram/client/methods/chats/join_chat.py index f5b632fc..b50e50c6 100644 --- a/pyrogram/client/methods/chats/join_chat.py +++ b/pyrogram/client/methods/chats/join_chat.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram from pyrogram.api import functions, types from ...ext import BaseClient @@ -30,17 +31,24 @@ class JoinChat(BaseClient): Unique identifier for the target chat in form of a *t.me/joinchat/* link or username of the target channel/supergroup (in the format @username). + Returns: + On success, a :obj:`Chat ` object is returned. + Raises: :class:`Error ` in case of a Telegram RPC error. """ match = self.INVITE_LINK_RE.match(chat_id) if match: - return self.send( + chat = self.send( functions.messages.ImportChatInvite( hash=match.group(1) ) ) + if isinstance(chat.chats[0], types.Chat): + return pyrogram.Chat._parse_chat_chat(self, chat.chats[0]) + elif isinstance(chat.chats[0], types.Channel): + return pyrogram.Chat._parse_channel_chat(self, chat.chats[0]) else: resolved_peer = self.send( functions.contacts.ResolveUsername( @@ -53,8 +61,10 @@ class JoinChat(BaseClient): access_hash=resolved_peer.chats[0].access_hash ) - return self.send( + chat = self.send( functions.channels.JoinChannel( channel=channel ) ) + + return pyrogram.Chat._parse_channel_chat(self, chat.chats[0]) diff --git a/pyrogram/client/types/user_and_chats/chat.py b/pyrogram/client/types/user_and_chats/chat.py index ec30b866..1f2c0e6e 100644 --- a/pyrogram/client/types/user_and_chats/chat.py +++ b/pyrogram/client/types/user_and_chats/chat.py @@ -209,3 +209,14 @@ class Chat(PyrogramType): parsed_chat.invite_link = full_chat.exported_invite.link return parsed_chat + + @staticmethod + def _parse_chat(client, chat): + # A wrapper around each entity parser: User, Chat and Channel. + # Currently unused, might become useful in future. + if isinstance(chat, types.Chat): + return Chat._parse_chat_chat(client, chat) + elif isinstance(chat, types.User): + return Chat._parse_user_chat(client, chat) + else: + return Chat._parse_channel_chat(client, chat)