Skip to content

Commit

Permalink
pyrofork: Add InputPeer support for utils.get_raw_peer_id and utils.g…
Browse files Browse the repository at this point in the history
…et_peer_id

Signed-off-by: wulan17 <[email protected]>
  • Loading branch information
wulan17 committed Sep 13, 2024
1 parent 0f3313a commit 217cc04
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions pyrogram/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,31 +284,53 @@ def unpack_inline_message_id(inline_message_id: str) -> "raw.base.InputBotInline
def get_raw_peer_id(
peer: Union[
raw.base.Peer,
raw.base.RequestedPeer
raw.base.RequestedPeer,
raw.base.InputPeer
]
) -> Optional[int]:
"""Get the raw peer id from a Peer object"""
if isinstance(peer, raw.types.PeerUser) or isinstance(peer, raw.types.RequestedPeerUser):
if (
isinstance(peer, raw.types.PeerUser)
or isinstance(peer, raw.types.RequestedPeerUser)
or isinstance(peer, raw.types.InputPeerUser)
):
return peer.user_id

if isinstance(peer, raw.types.PeerChat) or isinstance(peer, raw.types.RequestedPeerChat):
if (
isinstance(peer, raw.types.PeerChat)
or isinstance(peer, raw.types.RequestedPeerChat)
or isinstance(peer, raw.types.InputPeerChat)
):
return peer.chat_id

if isinstance(peer, raw.types.PeerChannel) or isinstance(peer, raw.types.RequestedPeerChannel):
if (
isinstance(peer, raw.types.PeerChannel)
or isinstance(peer, raw.types.RequestedPeerChannel)
or isinstance(peer, raw.types.InputPeerChannel)
):
return peer.channel_id

return None


def get_peer_id(peer: raw.base.Peer) -> int:
def get_peer_id(peer: Union[raw.base.Peer, raw.base.InputPeer]) -> int:
"""Get the non-raw peer id from a Peer object"""
if isinstance(peer, raw.types.PeerUser):
if (
isinstance(peer, raw.types.PeerUser)
or isinstance(peer, raw.types.InputPeerUser)
):
return peer.user_id

if isinstance(peer, raw.types.PeerChat):
if (
isinstance(peer, raw.types.PeerChat)
or isinstance(peer, raw.types.InputPeerChat)
):
return -peer.chat_id

if isinstance(peer, raw.types.PeerChannel):
if (
isinstance(peer, raw.types.PeerChannel)
or isinstance(peer, raw.types.InputPeerChannel)
):
return MAX_CHANNEL_ID - peer.channel_id

raise ValueError(f"Peer type invalid: {peer}")
Expand Down

0 comments on commit 217cc04

Please sign in to comment.