Skip to content

Commit

Permalink
Update get_raw_peer_id and get_peer_id
Browse files Browse the repository at this point in the history
  • Loading branch information
KurimuzonAkuma committed Dec 1, 2023
1 parent 97aeee2 commit a4b92aa
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pyrogram/methods/stories/get_pinned_stories.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async def get_pinned_stories(
chats = {i.id: i for i in r.chats}

if isinstance(peer, raw.types.InputPeerChannel):
peer_id = utils.get_input_peer_id(peer)
peer_id = utils.get_raw_peer_id(peer)
if peer_id not in r.chats:
channel = await self.invoke(raw.functions.channels.GetChannels(id=[peer]))
chats.update({peer_id: channel.chats[0]})
Expand Down
4 changes: 2 additions & 2 deletions pyrogram/types/messages_and_media/story.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ async def _parse(
peer_id = r[0].id
users.update({i.id: i for i in r})
elif isinstance(peer, raw.types.InputPeerUser):
peer_id = utils.get_input_peer_id(peer)
peer_id = utils.get_raw_peer_id(peer)
elif isinstance(peer, raw.types.InputPeerChannel):
peer_id = utils.get_input_peer_id(peer)
peer_id = utils.get_raw_peer_id(peer)
if peer_id not in chats:
r = await client.invoke(raw.functions.channels.GetChannels(id=[peer]))
chats.update({peer_id: r.chats[0]})
Expand Down
2 changes: 0 additions & 2 deletions pyrogram/types/messages_and_media/story_deleted.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ async def _parse(
r = await client.invoke(raw.functions.users.GetUsers(id=[raw.types.InputPeerSelf()]))
peer_id = r[0].id
users.update({i.id: i for i in r})
elif isinstance(peer, (raw.types.InputPeerUser, raw.types.InputPeerChannel)):
peer_id = utils.get_input_peer_id(peer)
else:
peer_id = utils.get_raw_peer_id(peer)

Expand Down
2 changes: 0 additions & 2 deletions pyrogram/types/messages_and_media/story_skipped.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ async def _parse(
r = await client.invoke(raw.functions.users.GetUsers(id=[raw.types.InputPeerSelf()]))
peer_id = r[0].id
users.update({i.id: i for i in r})
elif isinstance(peer, (raw.types.InputPeerUser, raw.types.InputPeerChannel)):
peer_id = utils.get_input_peer_id(peer)
else:
peer_id = utils.get_raw_peer_id(peer)

Expand Down
31 changes: 10 additions & 21 deletions pyrogram/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,42 +268,29 @@ def unpack_inline_message_id(inline_message_id: str) -> "raw.base.InputBotInline
MAX_USER_ID = 999999999999


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

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

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

return None

def get_input_peer_id(peer: raw.base.InputPeer) -> Optional[int]:
"""Get the raw peer id from a InputPeer object"""
if isinstance(peer, raw.types.InputPeerUser):
return peer.user_id

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

if 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, raw.types.InputPeerUser)):
return peer.user_id

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

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

raise ValueError(f"Peer type invalid: {peer}")
Expand All @@ -321,6 +308,7 @@ def get_peer_type(peer_id: int) -> str:

raise ValueError(f"Peer id invalid: {peer_id}")


def get_reply_to(
reply_to_message_id: Optional[int] = None,
message_thread_id: Optional[int] = None,
Expand All @@ -346,6 +334,7 @@ def get_reply_to(

return None


def get_channel_id(peer_id: int) -> int:
return MAX_CHANNEL_ID - peer_id

Expand Down

0 comments on commit a4b92aa

Please sign in to comment.