Fix requested_chats

This commit is contained in:
KurimuzonAkuma 2024-05-09 23:19:20 +03:00
parent 53ab40fef9
commit d2b710da9a
3 changed files with 24 additions and 14 deletions

View File

@ -16,7 +16,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import List
from typing import List, Union
import pyrogram
from pyrogram import enums
@ -48,12 +48,18 @@ class RequestedChats(Object):
self.chats = chats
@staticmethod
def _parse(client, action: "raw.types.MessageActionRequestedPeer") -> "RequestedChats":
def _parse(
client,
action: Union[
"raw.types.MessageActionRequestedPeer",
"raw.types.MessageActionRequestedPeerSentMe"
]
) -> "RequestedChats":
_requested_chats = []
for requested_peer in action.peers:
chat_id = utils.get_peer_id(requested_peer)
peer_type = utils.get_peer_type(chat_id)
peer_id = utils.get_peer_id(requested_peer)
peer_type = utils.get_peer_type(peer_id)
if peer_type == "user":
chat_type = enums.ChatType.PRIVATE
@ -64,8 +70,12 @@ class RequestedChats(Object):
_requested_chats.append(
types.Chat(
id=chat_id,
id=peer_id,
type=chat_type,
first_name=getattr(requested_peer, "first_name", None),
last_name=getattr(requested_peer, "last_name", None),
username=getattr(requested_peer, "username", None),
photo=types.ChatPhoto._parse(client, getattr(requested_peer, "photo", None), peer_id, 0),
client=client
)
)

View File

@ -753,7 +753,7 @@ class Message(Object, Update):
elif isinstance(action, raw.types.MessageActionGiftCode):
gift_code = types.GiftCode._parse(client, action, chats)
service_type = enums.MessageServiceType.GIFT_CODE
elif isinstance(action, raw.types.MessageActionRequestedPeer):
elif isinstance(action, (raw.types.MessageActionRequestedPeer, raw.types.MessageActionRequestedPeerSentMe)):
requested_chats = types.RequestedChats._parse(client, action)
service_type = enums.MessageServiceType.REQUESTED_CHAT
elif isinstance(action, raw.types.MessageActionSetMessagesTTL):

View File

@ -304,29 +304,29 @@ MAX_USER_ID_OLD = 2147483647
MAX_USER_ID = 999999999999
def get_raw_peer_id(peer: Union[raw.base.Peer, raw.base.InputPeer]) -> Optional[int]:
def get_raw_peer_id(peer: Union[raw.base.Peer, raw.base.InputPeer, raw.base.RequestedPeer]) -> Optional[int]:
"""Get the raw peer id from a Peer object"""
if isinstance(peer, (raw.types.PeerUser, raw.types.InputPeerUser)):
if isinstance(peer, (raw.types.PeerUser, raw.types.InputPeerUser, raw.types.RequestedPeerUser)):
return peer.user_id
if isinstance(peer, (raw.types.PeerChat, raw.types.InputPeerChat)):
if isinstance(peer, (raw.types.PeerChat, raw.types.InputPeerChat, raw.types.RequestedPeerChat)):
return peer.chat_id
if isinstance(peer, (raw.types.PeerChannel, raw.types.InputPeerChannel)):
if isinstance(peer, (raw.types.PeerChannel, raw.types.InputPeerChannel, raw.types.RequestedPeerChannel)):
return peer.channel_id
return None
def get_peer_id(peer: Union[raw.base.Peer, raw.base.InputPeer]) -> int:
def get_peer_id(peer: Union[raw.base.Peer, raw.base.InputPeer, raw.base.RequestedPeer]) -> int:
"""Get the non-raw peer id from a Peer object"""
if isinstance(peer, (raw.types.PeerUser, raw.types.InputPeerUser)):
if isinstance(peer, (raw.types.PeerUser, raw.types.InputPeerUser, raw.types.RequestedPeerUser)):
return peer.user_id
if isinstance(peer, (raw.types.PeerChat, raw.types.InputPeerChat)):
if isinstance(peer, (raw.types.PeerChat, raw.types.InputPeerChat, raw.types.RequestedPeerChat)):
return -peer.chat_id
if isinstance(peer, (raw.types.PeerChannel, raw.types.InputPeerChannel)):
if isinstance(peer, (raw.types.PeerChannel, raw.types.InputPeerChannel, raw.types.RequestedPeerChannel)):
return MAX_CHANNEL_ID - peer.channel_id
raise ValueError(f"Peer type invalid: {peer}")