From 840a9d1cc7b8f7acd9044443bd9fe3ae3e3d8419 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 14 Sep 2019 19:30:07 +0200 Subject: [PATCH] Update ChatPhoto parser --- pyrogram/client/ext/utils.py | 2 +- pyrogram/client/types/user_and_chats/chat.py | 6 +++--- .../client/types/user_and_chats/chat_photo.py | 18 +++++------------- pyrogram/client/types/user_and_chats/user.py | 2 +- 4 files changed, 10 insertions(+), 18 deletions(-) diff --git a/pyrogram/client/ext/utils.py b/pyrogram/client/ext/utils.py index 39031adf..663d67ff 100644 --- a/pyrogram/client/ext/utils.py +++ b/pyrogram/client/ext/utils.py @@ -215,7 +215,7 @@ def get_peer_id(peer: Union[PeerUser, PeerChat, PeerChannel]) -> int: raise ValueError("Peer type invalid: {}".format(peer)) -def get_type(peer_id: int) -> str: +def get_peer_type(peer_id: int) -> str: if peer_id < 0: if MIN_CHAT_ID <= peer_id: return "chat" diff --git a/pyrogram/client/types/user_and_chats/chat.py b/pyrogram/client/types/user_and_chats/chat.py index 642c1ea2..80545446 100644 --- a/pyrogram/client/types/user_and_chats/chat.py +++ b/pyrogram/client/types/user_and_chats/chat.py @@ -162,7 +162,7 @@ class Chat(Object): username=user.username, first_name=user.first_name, last_name=user.last_name, - photo=ChatPhoto._parse(client, user.photo, peer_id), + photo=ChatPhoto._parse(client, user.photo, peer_id, user.access_hash), restrictions=pyrogram.List([Restriction._parse(r) for r in user.restriction_reason]) or None, client=client ) @@ -175,7 +175,7 @@ class Chat(Object): id=peer_id, type="group", title=chat.title, - photo=ChatPhoto._parse(client, getattr(chat, "photo", None), peer_id), + photo=ChatPhoto._parse(client, getattr(chat, "photo", None), peer_id, 0), permissions=ChatPermissions._parse(getattr(chat, "default_banned_rights", None)), members_count=getattr(chat, "participants_count", None), client=client @@ -194,7 +194,7 @@ class Chat(Object): is_scam=getattr(channel, "scam", None), title=channel.title, username=getattr(channel, "username", None), - photo=ChatPhoto._parse(client, getattr(channel, "photo", None), peer_id), + photo=ChatPhoto._parse(client, getattr(channel, "photo", None), peer_id, channel.access_hash), restrictions=pyrogram.List([Restriction._parse(r) for r in restriction_reason]) or None, permissions=ChatPermissions._parse(getattr(channel, "default_banned_rights", None)), members_count=getattr(channel, "participants_count", None), diff --git a/pyrogram/client/types/user_and_chats/chat_photo.py b/pyrogram/client/types/user_and_chats/chat_photo.py index b19ea771..498ac72c 100644 --- a/pyrogram/client/types/user_and_chats/chat_photo.py +++ b/pyrogram/client/types/user_and_chats/chat_photo.py @@ -20,7 +20,7 @@ from struct import pack import pyrogram from pyrogram.api import types -from pyrogram.errors import PeerIdInvalid +from pyrogram.client.ext import utils from ..object import Object from ...ext.utils import encode @@ -51,7 +51,7 @@ class ChatPhoto(Object): self.big_file_id = big_file_id @staticmethod - def _parse(client, chat_photo: types.UserProfilePhoto or types.ChatPhoto, peer_id: int): + def _parse(client, chat_photo: types.UserProfilePhoto or types.ChatPhoto, peer_id: int, peer_access_hash: int): if not isinstance(chat_photo, (types.UserProfilePhoto, types.ChatPhoto)): return None @@ -59,22 +59,14 @@ class ChatPhoto(Object): loc_small = chat_photo.photo_small loc_big = chat_photo.photo_big - try: - peer = client.resolve_peer(peer_id) - except PeerIdInvalid: - return None + peer_type = utils.get_peer_type(peer_id) - if isinstance(peer, types.InputPeerUser): - peer_id = peer.user_id - peer_access_hash = peer.access_hash + if peer_type == "user": x = 0 - elif isinstance(peer, types.InputPeerChat): - peer_id = -peer.chat_id - peer_access_hash = 0 + elif peer_type == "chat": x = -1 else: peer_id += 1000727379968 - peer_access_hash = peer.access_hash x = -234 return ChatPhoto( diff --git a/pyrogram/client/types/user_and_chats/user.py b/pyrogram/client/types/user_and_chats/user.py index 878a2084..3696a3fd 100644 --- a/pyrogram/client/types/user_and_chats/user.py +++ b/pyrogram/client/types/user_and_chats/user.py @@ -187,7 +187,7 @@ class User(Object, Update): language_code=user.lang_code, dc_id=getattr(user.photo, "dc_id", None), phone_number=user.phone, - photo=ChatPhoto._parse(client, user.photo, user.id), + photo=ChatPhoto._parse(client, user.photo, user.id, user.access_hash), restrictions=pyrogram.List([Restriction._parse(r) for r in user.restriction_reason]) or None, client=client )