Skip to content

Commit 840a9d1

Browse files
committed
Update ChatPhoto parser
1 parent c5498c3 commit 840a9d1

4 files changed

Lines changed: 10 additions & 18 deletions

File tree

pyrogram/client/ext/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def get_peer_id(peer: Union[PeerUser, PeerChat, PeerChannel]) -> int:
215215
raise ValueError("Peer type invalid: {}".format(peer))
216216

217217

218-
def get_type(peer_id: int) -> str:
218+
def get_peer_type(peer_id: int) -> str:
219219
if peer_id < 0:
220220
if MIN_CHAT_ID <= peer_id:
221221
return "chat"

pyrogram/client/types/user_and_chats/chat.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def _parse_user_chat(client, user: types.User) -> "Chat":
162162
username=user.username,
163163
first_name=user.first_name,
164164
last_name=user.last_name,
165-
photo=ChatPhoto._parse(client, user.photo, peer_id),
165+
photo=ChatPhoto._parse(client, user.photo, peer_id, user.access_hash),
166166
restrictions=pyrogram.List([Restriction._parse(r) for r in user.restriction_reason]) or None,
167167
client=client
168168
)
@@ -175,7 +175,7 @@ def _parse_chat_chat(client, chat: types.Chat) -> "Chat":
175175
id=peer_id,
176176
type="group",
177177
title=chat.title,
178-
photo=ChatPhoto._parse(client, getattr(chat, "photo", None), peer_id),
178+
photo=ChatPhoto._parse(client, getattr(chat, "photo", None), peer_id, 0),
179179
permissions=ChatPermissions._parse(getattr(chat, "default_banned_rights", None)),
180180
members_count=getattr(chat, "participants_count", None),
181181
client=client
@@ -194,7 +194,7 @@ def _parse_channel_chat(client, channel: types.Channel) -> "Chat":
194194
is_scam=getattr(channel, "scam", None),
195195
title=channel.title,
196196
username=getattr(channel, "username", None),
197-
photo=ChatPhoto._parse(client, getattr(channel, "photo", None), peer_id),
197+
photo=ChatPhoto._parse(client, getattr(channel, "photo", None), peer_id, channel.access_hash),
198198
restrictions=pyrogram.List([Restriction._parse(r) for r in restriction_reason]) or None,
199199
permissions=ChatPermissions._parse(getattr(channel, "default_banned_rights", None)),
200200
members_count=getattr(channel, "participants_count", None),

pyrogram/client/types/user_and_chats/chat_photo.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import pyrogram
2222
from pyrogram.api import types
23-
from pyrogram.errors import PeerIdInvalid
23+
from pyrogram.client.ext import utils
2424
from ..object import Object
2525
from ...ext.utils import encode
2626

@@ -51,30 +51,22 @@ def __init__(
5151
self.big_file_id = big_file_id
5252

5353
@staticmethod
54-
def _parse(client, chat_photo: types.UserProfilePhoto or types.ChatPhoto, peer_id: int):
54+
def _parse(client, chat_photo: types.UserProfilePhoto or types.ChatPhoto, peer_id: int, peer_access_hash: int):
5555
if not isinstance(chat_photo, (types.UserProfilePhoto, types.ChatPhoto)):
5656
return None
5757

5858
photo_id = getattr(chat_photo, "photo_id", 0)
5959
loc_small = chat_photo.photo_small
6060
loc_big = chat_photo.photo_big
6161

62-
try:
63-
peer = client.resolve_peer(peer_id)
64-
except PeerIdInvalid:
65-
return None
62+
peer_type = utils.get_peer_type(peer_id)
6663

67-
if isinstance(peer, types.InputPeerUser):
68-
peer_id = peer.user_id
69-
peer_access_hash = peer.access_hash
64+
if peer_type == "user":
7065
x = 0
71-
elif isinstance(peer, types.InputPeerChat):
72-
peer_id = -peer.chat_id
73-
peer_access_hash = 0
66+
elif peer_type == "chat":
7467
x = -1
7568
else:
7669
peer_id += 1000727379968
77-
peer_access_hash = peer.access_hash
7870
x = -234
7971

8072
return ChatPhoto(

pyrogram/client/types/user_and_chats/user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def _parse(client, user: types.User) -> "User" or None:
187187
language_code=user.lang_code,
188188
dc_id=getattr(user.photo, "dc_id", None),
189189
phone_number=user.phone,
190-
photo=ChatPhoto._parse(client, user.photo, user.id),
190+
photo=ChatPhoto._parse(client, user.photo, user.id, user.access_hash),
191191
restrictions=pyrogram.List([Restriction._parse(r) for r in user.restriction_reason]) or None,
192192
client=client
193193
)

0 commit comments

Comments
 (0)