diff --git a/pyrogram/methods/stories/get_peer_stories.py b/pyrogram/methods/stories/get_peer_stories.py index 356ab668..0b78652d 100644 --- a/pyrogram/methods/stories/get_peer_stories.py +++ b/pyrogram/methods/stories/get_peer_stories.py @@ -65,5 +65,5 @@ class GetPeerStories: story, {i.id: i for i in r.users}, {i.id: i for i in r.chats}, - peer + r.stories.peer ) diff --git a/pyrogram/methods/stories/get_stories.py b/pyrogram/methods/stories/get_stories.py index 99d0b59c..64321550 100644 --- a/pyrogram/methods/stories/get_stories.py +++ b/pyrogram/methods/stories/get_stories.py @@ -69,16 +69,15 @@ class GetStories: stories = [] - for i in r.updates: - if isinstance(i, raw.types.stories.PeerStories): - stories = [ - await types.Story._parse( - self, - story, - {i.id: i for i in r.users}, - {i.id: i for i in r.chats}, - peer - ) for story in r.stories - ] + for story in r.stories: + stories.append( + await types.Story._parse( + self, + story, + {i.id: i for i in r.users}, + {i.id: i for i in r.chats}, + peer + ) + ) return types.List(stories) if is_iterable else stories[0] diff --git a/pyrogram/types/messages_and_media/story.py b/pyrogram/types/messages_and_media/story.py index e19435d2..8ac2452a 100644 --- a/pyrogram/types/messages_and_media/story.py +++ b/pyrogram/types/messages_and_media/story.py @@ -163,29 +163,13 @@ class Story(Object, Update): peer: Union["raw.types.PeerChannel", "raw.types.PeerUser"] ) -> "Story": if isinstance(stories, raw.types.StoryItemSkipped): - return await types.StorySkipped._parse(client, stories, peer) + return await types.StorySkipped._parse(client, stories, users, chats, peer) if isinstance(stories, raw.types.StoryItemDeleted): - return await types.StoryDeleted._parse(client, stories, peer) + return await types.StoryDeleted._parse(client, stories, users, chats, peer) entities = [types.MessageEntity._parse(client, entity, {}) for entity in stories.entities] entities = types.List(filter(lambda x: x is not None, entities)) or None - peer_id = utils.get_raw_peer_id(peer) - - if isinstance(peer, raw.types.PeerUser) and peer_id not in users: - try: - r = await client.invoke( - raw.functions.users.GetUsers( - id=[ - await client.resolve_peer(peer_id) - ] - ) - ) - except PeerIdInvalid: - pass - else: - users.update({i.id: i for i in r}) - photo = None video = None from_user = None @@ -208,8 +192,40 @@ class Story(Object, Update): video = types.Video._parse(client, doc, video_attributes, None) media_type = enums.MessageMediaType.VIDEO + peer_id = None + if hasattr(peer, "user_id"): + peer_id = peer.user_id + elif hasattr(peer, "chat_id"): + peer_id = peer.chat_id + elif hasattr(peer, "channel_id"): + peer_id = peer.channel_id + + if isinstance(peer, (raw.types.PeerUser, raw.types.InputPeerUser)) and peer_id not in users: + try: + r = await client.invoke( + raw.functions.users.GetUsers( + id=[ + await client.resolve_peer(peer_id) + ] + ) + ) + except PeerIdInvalid: + pass + else: + users.update({i.id: i for i in r}) + if isinstance(peer, raw.types.InputPeerSelf): + 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}) + from_user = types.User._parse(client, users.get(peer_id, None)) - sender_chat = types.Chat._parse_channel_chat(client, chats.get(peer_id, None)) if not from_user else None + sender_chat = types.Chat._parse_channel_chat(client, chats[peer_id]) if not from_user else None for priv in stories.privacy: if isinstance(priv, raw.types.PrivacyValueAllowAll): diff --git a/pyrogram/types/messages_and_media/story_deleted.py b/pyrogram/types/messages_and_media/story_deleted.py index ff52bee1..2384ca05 100644 --- a/pyrogram/types/messages_and_media/story_deleted.py +++ b/pyrogram/types/messages_and_media/story_deleted.py @@ -19,6 +19,7 @@ import pyrogram from pyrogram import raw, types +from pyrogram.errors import PeerIdInvalid from typing import Union from ..object import Object from ..update import Update @@ -51,19 +52,51 @@ class StoryDeleted(Object, Update): self.from_user = from_user self.sender_chat = sender_chat + @staticmethod async def _parse( client: "pyrogram.Client", stories: raw.base.StoryItem, + users: dict, + chats: dict, peer: Union["raw.types.PeerChannel", "raw.types.PeerUser"] ) -> "StoryDeleted": from_user = None sender_chat = None - if isinstance(peer, raw.types.PeerChannel): - sender_chat = await client.get_chat(peer.channel_id) - elif isinstance(peer, raw.types.InputPeerSelf): - from_user = client.me - else: - from_user = await client.get_users(peer.user_id) + + peer_id = None + if hasattr(peer, "user_id"): + peer_id = peer.user_id + elif hasattr(peer, "chat_id"): + peer_id = peer.chat_id + elif hasattr(peer, "channel_id"): + peer_id = peer.channel_id + + if isinstance(peer, (raw.types.PeerUser, raw.types.InputPeerUser)) and peer_id not in users: + try: + r = await client.invoke( + raw.functions.users.GetUsers( + id=[ + await client.resolve_peer(peer_id) + ] + ) + ) + except PeerIdInvalid: + pass + else: + users.update({i.id: i for i in r}) + if isinstance(peer, raw.types.InputPeerSelf): + 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}) + + from_user = types.User._parse(client, users.get(peer_id, None)) + sender_chat = types.Chat._parse_channel_chat(client, chats[peer_id]) if not from_user else None return StoryDeleted( id=stories.id, diff --git a/pyrogram/types/messages_and_media/story_skipped.py b/pyrogram/types/messages_and_media/story_skipped.py index d109e0b5..a8b74fae 100644 --- a/pyrogram/types/messages_and_media/story_skipped.py +++ b/pyrogram/types/messages_and_media/story_skipped.py @@ -20,6 +20,7 @@ import pyrogram from datetime import datetime from pyrogram import raw, types, utils +from pyrogram.errors import PeerIdInvalid from typing import Union from ..object import Object from ..update import Update @@ -67,19 +68,51 @@ class StorySkipped(Object, Update): self.expire_date = expire_date self.close_friends = close_friends + @staticmethod async def _parse( client: "pyrogram.Client", stories: raw.base.StoryItem, + users: dict, + chats: dict, peer: Union["raw.types.PeerChannel", "raw.types.PeerUser"] ) -> "StorySkipped": from_user = None sender_chat = None - if isinstance(peer, raw.types.PeerChannel): - sender_chat = await client.get_chat(peer.channel_id) - elif isinstance(peer, raw.types.InputPeerSelf): - from_user = client.me - else: - from_user = await client.get_users(peer.user_id) + + peer_id = None + if hasattr(peer, "user_id"): + peer_id = peer.user_id + elif hasattr(peer, "chat_id"): + peer_id = peer.chat_id + elif hasattr(peer, "channel_id"): + peer_id = peer.channel_id + + if isinstance(peer, (raw.types.PeerUser, raw.types.InputPeerUser)) and peer_id not in users: + try: + r = await client.invoke( + raw.functions.users.GetUsers( + id=[ + await client.resolve_peer(peer_id) + ] + ) + ) + except PeerIdInvalid: + pass + else: + users.update({i.id: i for i in r}) + if isinstance(peer, raw.types.InputPeerSelf): + 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}) + + from_user = types.User._parse(client, users.get(peer_id, None)) + sender_chat = types.Chat._parse_channel_chat(client, chats[peer_id]) if not from_user else None return StorySkipped( id=stories.id,