From 4632879ee4d1742600005141b6b0b5aaa38de2af Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 30 Sep 2020 20:26:57 +0200 Subject: [PATCH] Changes for L119 --- pyrogram/client.py | 2 +- pyrogram/methods/chats/get_dialogs.py | 10 ++++---- .../bots_and_keyboards/game_high_score.py | 4 ++-- pyrogram/types/messages_and_media/message.py | 24 +++++++++++-------- pyrogram/types/user_and_chats/chat.py | 17 +++++++++---- pyrogram/types/user_and_chats/chat_member.py | 22 +++++++++++++++-- pyrogram/utils.py | 15 ++++++++++++ 7 files changed, 69 insertions(+), 25 deletions(-) diff --git a/pyrogram/client.py b/pyrogram/client.py index 2577745a..28c65c16 100644 --- a/pyrogram/client.py +++ b/pyrogram/client.py @@ -539,7 +539,7 @@ class Client(Methods, Scaffold): getattr( getattr( update, "message", None - ), "to_id", None + ), "peer_id", None ), "channel_id", None ) or getattr(update, "channel_id", None) diff --git a/pyrogram/methods/chats/get_dialogs.py b/pyrogram/methods/chats/get_dialogs.py index 01bc0fd0..b437012b 100644 --- a/pyrogram/methods/chats/get_dialogs.py +++ b/pyrogram/methods/chats/get_dialogs.py @@ -89,15 +89,15 @@ class GetDialogs(Scaffold): messages = {} for message in r.messages: - to_id = message.to_id + peer_id = message.peer_id - if isinstance(to_id, raw.types.PeerUser): + if isinstance(peer_id, raw.types.PeerUser): if message.out: - chat_id = to_id.user_id + chat_id = peer_id.user_id else: - chat_id = message.from_id + chat_id = utils.get_raw_peer_id(message.from_id) else: - chat_id = utils.get_peer_id(to_id) + chat_id = utils.get_peer_id(peer_id) messages[chat_id] = await types.Message._parse(self, message, users, chats) diff --git a/pyrogram/types/bots_and_keyboards/game_high_score.py b/pyrogram/types/bots_and_keyboards/game_high_score.py index a9af42ee..a7448862 100644 --- a/pyrogram/types/bots_and_keyboards/game_high_score.py +++ b/pyrogram/types/bots_and_keyboards/game_high_score.py @@ -17,7 +17,7 @@ # along with Pyrogram. If not, see . import pyrogram -from pyrogram import raw +from pyrogram import raw, utils from pyrogram import types from ..object import Object @@ -64,7 +64,7 @@ class GameHighScore(Object): @staticmethod def _parse_action(client, service: raw.types.MessageService, users: dict): return GameHighScore( - user=types.User._parse(client, users[service.from_id]), + user=types.User._parse(client, users[utils.get_raw_peer_id(service.from_id)]), score=service.action.score, client=client ) diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index 42d85e9a..df6a5a95 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -422,7 +422,7 @@ class Message(Object, Update): if isinstance(action, raw.types.MessageActionChatAddUser): new_chat_members = [types.User._parse(client, users[i]) for i in action.users] elif isinstance(action, raw.types.MessageActionChatJoinedByLink): - new_chat_members = [types.User._parse(client, users[message.from_id])] + new_chat_members = [types.User._parse(client, users[utils.get_raw_peer_id(message.from_id)])] elif isinstance(action, raw.types.MessageActionChatDeleteUser): left_chat_member = types.User._parse(client, users[action.user_id]) elif isinstance(action, raw.types.MessageActionChatEditTitle): @@ -444,7 +444,7 @@ class Message(Object, Update): message_id=message.id, date=message.date, chat=types.Chat._parse(client, message, users, chats), - from_user=types.User._parse(client, users.get(message.from_id, None)), + from_user=types.User._parse(client, users.get(utils.get_raw_peer_id(message.from_id), None)), service=True, new_chat_members=new_chat_members, left_chat_member=left_chat_member, @@ -472,7 +472,7 @@ class Message(Object, Update): if isinstance(action, raw.types.MessageActionGameScore): parsed_message.game_high_score = types.GameHighScore._parse_action(client, message, users) - if message.reply_to_msg_id and replies: + if message.reply_to and replies: try: parsed_message.reply_to_message = await client.get_messages( parsed_message.chat.id, @@ -501,13 +501,17 @@ class Message(Object, Update): forward_date = forward_header.date if forward_header.from_id: - forward_from = types.User._parse(client, users[forward_header.from_id]) + raw_peer_id = utils.get_raw_peer_id(forward_header.from_id) + peer_id = utils.get_peer_id(forward_header.from_id) + + if peer_id > 0: + forward_from = types.User._parse(client, users[raw_peer_id]) + else: + forward_from_chat = types.Chat._parse_channel_chat(client, chats[raw_peer_id]) + forward_from_message_id = forward_header.channel_post + forward_signature = forward_header.post_author elif forward_header.from_name: forward_sender_name = forward_header.from_name - else: - forward_from_chat = types.Chat._parse_channel_chat(client, chats[forward_header.channel_id]) - forward_from_message_id = forward_header.channel_post - forward_signature = forward_header.post_author photo = None location = None @@ -608,7 +612,7 @@ class Message(Object, Update): message_id=message.id, date=message.date, chat=types.Chat._parse(client, message, users, chats), - from_user=types.User._parse(client, users.get(message.from_id, None)), + from_user=types.User._parse(client, users.get(utils.get_raw_peer_id(message.from_id), None)), text=( Str(message.message).init(entities) or None if media is None or web_page is not None @@ -664,7 +668,7 @@ class Message(Object, Update): client=client ) - if message.reply_to_msg_id and replies: + if message.reply_to and replies: try: parsed_message.reply_to_message = await client.get_messages( parsed_message.chat.id, diff --git a/pyrogram/types/user_and_chats/chat.py b/pyrogram/types/user_and_chats/chat.py index 44d6e156..5576e6ad 100644 --- a/pyrogram/types/user_and_chats/chat.py +++ b/pyrogram/types/user_and_chats/chat.py @@ -224,13 +224,20 @@ class Chat(Object): @staticmethod def _parse(client, message: raw.types.Message or raw.types.MessageService, users: dict, chats: dict) -> "Chat": - if isinstance(message.to_id, raw.types.PeerUser): - return Chat._parse_user_chat(client, users[message.to_id.user_id if message.out else message.from_id]) + if isinstance(message.peer_id, raw.types.PeerUser): + return Chat._parse_user_chat( + client, + users[ + message.peer_id.user_id + if message.out + else utils.get_raw_peer_id(message.from_id) + ] + ) - if isinstance(message.to_id, raw.types.PeerChat): - return Chat._parse_chat_chat(client, chats[message.to_id.chat_id]) + if isinstance(message.peer_id, raw.types.PeerChat): + return Chat._parse_chat_chat(client, chats[message.peer_id.chat_id]) - return Chat._parse_channel_chat(client, chats[message.to_id.channel_id]) + return Chat._parse_channel_chat(client, chats[message.peer_id.channel_id]) @staticmethod def _parse_dialog(client, peer, users: dict, chats: dict): diff --git a/pyrogram/types/user_and_chats/chat_member.py b/pyrogram/types/user_and_chats/chat_member.py index 31a27402..69ab4955 100644 --- a/pyrogram/types/user_and_chats/chat_member.py +++ b/pyrogram/types/user_and_chats/chat_member.py @@ -211,11 +211,10 @@ class ChatMember(Object): client=client ) - if isinstance(member, (raw.types.ChannelParticipantCreator, raw.types.ChatParticipantCreator)): + if isinstance(member, raw.types.ChatParticipantCreator): return ChatMember( user=user, status="creator", - title=getattr(member, "rank", None), client=client ) @@ -228,6 +227,25 @@ class ChatMember(Object): client=client ) + if isinstance(member, raw.types.ChannelParticipantCreator): + permissions = member.admin_rights + + return ChatMember( + user=user, + status="creator", + title=member.rank, + invited_by=invited_by, + can_change_info=permissions.change_info, + can_post_messages=permissions.post_messages, + can_edit_messages=permissions.edit_messages, + can_delete_messages=permissions.delete_messages, + can_restrict_members=permissions.ban_users, + can_invite_users=permissions.invite_users, + can_pin_messages=permissions.pin_messages, + can_promote_members=permissions.add_admins, + client=client + ) + if isinstance(member, raw.types.ChannelParticipantAdmin): permissions = member.admin_rights diff --git a/pyrogram/utils.py b/pyrogram/utils.py index 807d98a0..296a7c58 100644 --- a/pyrogram/utils.py +++ b/pyrogram/utils.py @@ -231,7 +231,22 @@ MIN_CHAT_ID = -2147483647 MAX_USER_ID = 2147483647 +def get_raw_peer_id(peer: raw.base.Peer) -> Union[int, None]: + """Get the raw peer id from a Peer object""" + if isinstance(peer, raw.types.PeerUser): + return peer.user_id + + if isinstance(peer, raw.types.PeerChat): + return peer.chat_id + + if isinstance(peer, raw.types.PeerChannel): + return peer.channel_id + + return None + + def get_peer_id(peer: raw.base.Peer) -> int: + """Get the non-raw peer id from a Peer object""" if isinstance(peer, raw.types.PeerUser): return peer.user_id