Skip to content

Commit 4632879

Browse files
committed
Changes for L119
1 parent 5ee932b commit 4632879

File tree

7 files changed

+69
-25
lines changed

7 files changed

+69
-25
lines changed

pyrogram/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ async def handle_updates(self, updates):
539539
getattr(
540540
getattr(
541541
update, "message", None
542-
), "to_id", None
542+
), "peer_id", None
543543
), "channel_id", None
544544
) or getattr(update, "channel_id", None)
545545

pyrogram/methods/chats/get_dialogs.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ async def get_dialogs(
8989
messages = {}
9090

9191
for message in r.messages:
92-
to_id = message.to_id
92+
peer_id = message.peer_id
9393

94-
if isinstance(to_id, raw.types.PeerUser):
94+
if isinstance(peer_id, raw.types.PeerUser):
9595
if message.out:
96-
chat_id = to_id.user_id
96+
chat_id = peer_id.user_id
9797
else:
98-
chat_id = message.from_id
98+
chat_id = utils.get_raw_peer_id(message.from_id)
9999
else:
100-
chat_id = utils.get_peer_id(to_id)
100+
chat_id = utils.get_peer_id(peer_id)
101101

102102
messages[chat_id] = await types.Message._parse(self, message, users, chats)
103103

pyrogram/types/bots_and_keyboards/game_high_score.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import pyrogram
20-
from pyrogram import raw
20+
from pyrogram import raw, utils
2121
from pyrogram import types
2222
from ..object import Object
2323

@@ -64,7 +64,7 @@ def _parse(client, game_high_score: raw.types.HighScore, users: dict) -> "GameHi
6464
@staticmethod
6565
def _parse_action(client, service: raw.types.MessageService, users: dict):
6666
return GameHighScore(
67-
user=types.User._parse(client, users[service.from_id]),
67+
user=types.User._parse(client, users[utils.get_raw_peer_id(service.from_id)]),
6868
score=service.action.score,
6969
client=client
7070
)

pyrogram/types/messages_and_media/message.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ async def _parse(
422422
if isinstance(action, raw.types.MessageActionChatAddUser):
423423
new_chat_members = [types.User._parse(client, users[i]) for i in action.users]
424424
elif isinstance(action, raw.types.MessageActionChatJoinedByLink):
425-
new_chat_members = [types.User._parse(client, users[message.from_id])]
425+
new_chat_members = [types.User._parse(client, users[utils.get_raw_peer_id(message.from_id)])]
426426
elif isinstance(action, raw.types.MessageActionChatDeleteUser):
427427
left_chat_member = types.User._parse(client, users[action.user_id])
428428
elif isinstance(action, raw.types.MessageActionChatEditTitle):
@@ -444,7 +444,7 @@ async def _parse(
444444
message_id=message.id,
445445
date=message.date,
446446
chat=types.Chat._parse(client, message, users, chats),
447-
from_user=types.User._parse(client, users.get(message.from_id, None)),
447+
from_user=types.User._parse(client, users.get(utils.get_raw_peer_id(message.from_id), None)),
448448
service=True,
449449
new_chat_members=new_chat_members,
450450
left_chat_member=left_chat_member,
@@ -472,7 +472,7 @@ async def _parse(
472472
if isinstance(action, raw.types.MessageActionGameScore):
473473
parsed_message.game_high_score = types.GameHighScore._parse_action(client, message, users)
474474

475-
if message.reply_to_msg_id and replies:
475+
if message.reply_to and replies:
476476
try:
477477
parsed_message.reply_to_message = await client.get_messages(
478478
parsed_message.chat.id,
@@ -501,13 +501,17 @@ async def _parse(
501501
forward_date = forward_header.date
502502

503503
if forward_header.from_id:
504-
forward_from = types.User._parse(client, users[forward_header.from_id])
504+
raw_peer_id = utils.get_raw_peer_id(forward_header.from_id)
505+
peer_id = utils.get_peer_id(forward_header.from_id)
506+
507+
if peer_id > 0:
508+
forward_from = types.User._parse(client, users[raw_peer_id])
509+
else:
510+
forward_from_chat = types.Chat._parse_channel_chat(client, chats[raw_peer_id])
511+
forward_from_message_id = forward_header.channel_post
512+
forward_signature = forward_header.post_author
505513
elif forward_header.from_name:
506514
forward_sender_name = forward_header.from_name
507-
else:
508-
forward_from_chat = types.Chat._parse_channel_chat(client, chats[forward_header.channel_id])
509-
forward_from_message_id = forward_header.channel_post
510-
forward_signature = forward_header.post_author
511515

512516
photo = None
513517
location = None
@@ -608,7 +612,7 @@ async def _parse(
608612
message_id=message.id,
609613
date=message.date,
610614
chat=types.Chat._parse(client, message, users, chats),
611-
from_user=types.User._parse(client, users.get(message.from_id, None)),
615+
from_user=types.User._parse(client, users.get(utils.get_raw_peer_id(message.from_id), None)),
612616
text=(
613617
Str(message.message).init(entities) or None
614618
if media is None or web_page is not None
@@ -664,7 +668,7 @@ async def _parse(
664668
client=client
665669
)
666670

667-
if message.reply_to_msg_id and replies:
671+
if message.reply_to and replies:
668672
try:
669673
parsed_message.reply_to_message = await client.get_messages(
670674
parsed_message.chat.id,

pyrogram/types/user_and_chats/chat.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,20 @@ def _parse_channel_chat(client, channel: raw.types.Channel) -> "Chat":
224224

225225
@staticmethod
226226
def _parse(client, message: raw.types.Message or raw.types.MessageService, users: dict, chats: dict) -> "Chat":
227-
if isinstance(message.to_id, raw.types.PeerUser):
228-
return Chat._parse_user_chat(client, users[message.to_id.user_id if message.out else message.from_id])
227+
if isinstance(message.peer_id, raw.types.PeerUser):
228+
return Chat._parse_user_chat(
229+
client,
230+
users[
231+
message.peer_id.user_id
232+
if message.out
233+
else utils.get_raw_peer_id(message.from_id)
234+
]
235+
)
229236

230-
if isinstance(message.to_id, raw.types.PeerChat):
231-
return Chat._parse_chat_chat(client, chats[message.to_id.chat_id])
237+
if isinstance(message.peer_id, raw.types.PeerChat):
238+
return Chat._parse_chat_chat(client, chats[message.peer_id.chat_id])
232239

233-
return Chat._parse_channel_chat(client, chats[message.to_id.channel_id])
240+
return Chat._parse_channel_chat(client, chats[message.peer_id.channel_id])
234241

235242
@staticmethod
236243
def _parse_dialog(client, peer, users: dict, chats: dict):

pyrogram/types/user_and_chats/chat_member.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,10 @@ def _parse(client, member, users) -> "ChatMember":
211211
client=client
212212
)
213213

214-
if isinstance(member, (raw.types.ChannelParticipantCreator, raw.types.ChatParticipantCreator)):
214+
if isinstance(member, raw.types.ChatParticipantCreator):
215215
return ChatMember(
216216
user=user,
217217
status="creator",
218-
title=getattr(member, "rank", None),
219218
client=client
220219
)
221220

@@ -228,6 +227,25 @@ def _parse(client, member, users) -> "ChatMember":
228227
client=client
229228
)
230229

230+
if isinstance(member, raw.types.ChannelParticipantCreator):
231+
permissions = member.admin_rights
232+
233+
return ChatMember(
234+
user=user,
235+
status="creator",
236+
title=member.rank,
237+
invited_by=invited_by,
238+
can_change_info=permissions.change_info,
239+
can_post_messages=permissions.post_messages,
240+
can_edit_messages=permissions.edit_messages,
241+
can_delete_messages=permissions.delete_messages,
242+
can_restrict_members=permissions.ban_users,
243+
can_invite_users=permissions.invite_users,
244+
can_pin_messages=permissions.pin_messages,
245+
can_promote_members=permissions.add_admins,
246+
client=client
247+
)
248+
231249
if isinstance(member, raw.types.ChannelParticipantAdmin):
232250
permissions = member.admin_rights
233251

pyrogram/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,22 @@ def unpack_inline_message_id(inline_message_id: str) -> "raw.types.InputBotInlin
231231
MAX_USER_ID = 2147483647
232232

233233

234+
def get_raw_peer_id(peer: raw.base.Peer) -> Union[int, None]:
235+
"""Get the raw peer id from a Peer object"""
236+
if isinstance(peer, raw.types.PeerUser):
237+
return peer.user_id
238+
239+
if isinstance(peer, raw.types.PeerChat):
240+
return peer.chat_id
241+
242+
if isinstance(peer, raw.types.PeerChannel):
243+
return peer.channel_id
244+
245+
return None
246+
247+
234248
def get_peer_id(peer: raw.base.Peer) -> int:
249+
"""Get the non-raw peer id from a Peer object"""
235250
if isinstance(peer, raw.types.PeerUser):
236251
return peer.user_id
237252

0 commit comments

Comments
 (0)