Skip to content

Commit

Permalink
Pyrofork: Refactor Story
Browse files Browse the repository at this point in the history
Signed-off-by: wulan17 <wulan17@nusantararom.org>
  • Loading branch information
wulan17 committed Dec 25, 2023
1 parent 2bd393f commit 4c0b3e4
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 55 deletions.
8 changes: 4 additions & 4 deletions pyrogram/methods/users/delete_stories.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DeleteStories:
async def delete_stories(
self: "pyrogram.Client",
story_ids: Union[int, Iterable[int]],
channel_id: int = None
chat_id: int = None
) -> bool:
"""Delete one or more story by using story identifiers.
Expand All @@ -40,7 +40,7 @@ async def delete_stories(
Pass a single story identifier or an iterable of story ids (as integers) to get the content of the
story themselves.
channel_id (``int``, *optional*):
chat_id (``int``, *optional*):
Unique identifier (int) of the target channel.
Returns:
Expand All @@ -59,8 +59,8 @@ async def delete_stories(
is_iterable = not isinstance(story_ids, int)
ids = list(story_ids) if is_iterable else [story_ids]

if channel_id:
peer = await self.resolve_peer(channel_id)
if chat_id:
peer = await self.resolve_peer(chat_id)
else:
peer = await self.resolve_peer("me")

Expand Down
8 changes: 4 additions & 4 deletions pyrogram/methods/users/edit_story.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _split(self, message, entities, *args, **kwargs):
async def edit_story(
self: "pyrogram.Client",
story_id: int,
channel_id: int = None,
chat_id: int = None,
privacy: "enums.StoriesPrivacyRules" = None,
allowed_users: List[int] = None,
denied_users: List[int] = None,
Expand All @@ -53,7 +53,7 @@ async def edit_story(
story_id (``int``):
Unique identifier (int) of the target story.
channel_id (``int``, *optional*):
chat_id (``int``, *optional*):
Unique identifier (int) of the target channel.
animation (``str`` | ``BinaryIO``, *optional*):
Expand Down Expand Up @@ -113,8 +113,8 @@ async def edit_story(
ValueError: In case of invalid arguments.
"""

if channel_id:
peer = await self.resolve_peer(channel_id)
if chat_id:
peer = await self.resolve_peer(chat_id)
else:
peer = await self.resolve_peer("me")

Expand Down
8 changes: 4 additions & 4 deletions pyrogram/methods/users/export_story_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
class ExportStoryLink:
async def export_story_link(
self: "pyrogram.Client",
from_id: Union[int, str],
chat_id: Union[int, str],
story_id: int,
) -> types.ExportedStoryLink:
"""Get one story link from an user by using story identifiers.
.. include:: /_includes/usable-by/users.rst
Parameters:
from_id (``int`` | ``str``):
chat_id (``int`` | ``str``):
Unique identifier (int) or username (str) of the target user/channel.
For your personal story you can simply use "me" or "self".
For a contact that exists in your Telegram address book you can use his phone number (str).
Expand All @@ -51,13 +51,13 @@ async def export_story_link(
.. code-block:: python
# Get story link
await app.export_story_link(from_id, 12345)
await app.export_story_link(chat_id, 12345)
Raises:
ValueError: In case of invalid arguments.
"""

peer = await self.resolve_peer(from_id)
peer = await self.resolve_peer(chat_id)

rpc = raw.functions.stories.ExportStoryLink(peer=peer, id=story_id)

Expand Down
6 changes: 3 additions & 3 deletions pyrogram/methods/users/forward_story.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async def forward_story(
self: "pyrogram.Client",
from_chat_id: Union[int, str],
from_story_id: int,
channel_id: int = None,
chat_id: int = None,
privacy: "enums.StoriesPrivacyRules" = None,
allowed_users: List[int] = None,
denied_users: List[int] = None,
Expand All @@ -55,7 +55,7 @@ async def forward_story(
from_story_id (``int``):
Unique identifier of original story.
channel_id (``int``, *optional*):
chat_id (``int``, *optional*):
Unique identifier (int) of the target channel.
If you want to forward story to a channel.
Expand Down Expand Up @@ -105,7 +105,7 @@ async def forward_story(
"""

return await self.send_story(
channel_id=channel_id,
chat_id=chat_id,
privacy=privacy,
allowed_users=allowed_users,
denied_users=denied_users,
Expand Down
8 changes: 4 additions & 4 deletions pyrogram/methods/users/get_peer_stories.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
class GetPeerStories:
async def get_peer_stories(
self: "pyrogram.Client",
from_id: Union[int, str]
chat_id: Union[int, str]
) -> Optional[AsyncGenerator["types.Story", None]]:
"""Get all active stories from an user/channel by using user identifiers.
.. include:: /_includes/usable-by/users.rst
Parameters:
from_id (``int`` | ``str``):
chat_id (``int`` | ``str``):
Unique identifier (int) or username (str) of the target user/channel.
For your personal story you can simply use "me" or "self".
For a contact that exists in your Telegram address book you can use his phone number (str).
Expand All @@ -47,14 +47,14 @@ async def get_peer_stories(
.. code-block:: python
# Get all active story from spesific user/channel
async for story in app.get_peer_stories(from_id):
async for story in app.get_peer_stories(chat_id):
print(story)
Raises:
ValueError: In case of invalid arguments.
"""

peer = await self.resolve_peer(from_id)
peer = await self.resolve_peer(chat_id)


rpc = raw.functions.stories.GetPeerStories(peer=peer)
Expand Down
10 changes: 5 additions & 5 deletions pyrogram/methods/users/get_stories.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
class GetStories:
async def get_stories(
self: "pyrogram.Client",
from_id: Union[int, str],
chat_id: Union[int, str],
story_ids: Union[int, Iterable[int]],
) -> Union["types.Story", List["types.Story"]]:
"""Get one or more story from an user by using story identifiers.
.. include:: /_includes/usable-by/users.rst
Parameters:
from_id (``int`` | ``str``):
chat_id (``int`` | ``str``):
Unique identifier (int) or username (str) of the target user/channel.
For your personal story you can simply use "me" or "self".
For a contact that exists in your Telegram address book you can use his phone number (str).
Expand All @@ -53,16 +53,16 @@ async def get_stories(
.. code-block:: python
# Get one story
await app.get_stories(from_id, 12345)
await app.get_stories(chat_id, 12345)
# Get more than one story (list of stories)
await app.get_stories(from_id, [12345, 12346])
await app.get_stories(chat_id, [12345, 12346])
Raises:
ValueError: In case of invalid arguments.
"""

peer = await self.resolve_peer(from_id)
peer = await self.resolve_peer(chat_id)

is_iterable = not isinstance(story_ids, int)
ids = list(story_ids) if is_iterable else [story_ids]
Expand Down
8 changes: 4 additions & 4 deletions pyrogram/methods/users/get_stories_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
class GetUserStoriesHistory:
async def get_stories_history(
self: "pyrogram.Client",
channel_id: int = None,
chat_id: int = None,
limit: int = 0,
offset_id: int = 0
) -> Optional[AsyncGenerator["types.Story", None]]:
Expand All @@ -37,7 +37,7 @@ async def get_stories_history(
.. include:: /_includes/usable-by/users.rst
Parameters:
channel_id (``int``, *optional*):
chat_id (``int``, *optional*):
Unique identifier (int) of the target channel.
limit (``int``, *optional*):
Expand All @@ -61,8 +61,8 @@ async def get_stories_history(
ValueError: In case of invalid arguments.
"""

if channel_id:
peer = await self.resolve_peer(channel_id)
if chat_id:
peer = await self.resolve_peer(chat_id)
else:
peer = await self.resolve_peer("me")

Expand Down
8 changes: 4 additions & 4 deletions pyrogram/methods/users/send_story.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _split(self, message, entities, *args, **kwargs):

async def send_story(
self: "pyrogram.Client",
channel_id: int = None,
chat_id: int = None,
privacy: "enums.StoriesPrivacyRules" = None,
allowed_users: List[int] = None,
denied_users: List[int] = None,
Expand All @@ -56,7 +56,7 @@ async def send_story(
Note: You must pass one of following paramater *animation*, *photo*, *video*
Parameters:
channel_id (``int``, *optional*):
chat_id (``int``, *optional*):
Unique identifier (int) of the target channel.
animation (``str`` | ``BinaryIO``, *optional*):
Expand Down Expand Up @@ -137,8 +137,8 @@ async def send_story(
ValueError: In case of invalid arguments.
"""

if channel_id:
peer = await self.resolve_peer(channel_id)
if chat_id:
peer = await self.resolve_peer(chat_id)
else:
peer = await self.resolve_peer("me")

Expand Down
6 changes: 3 additions & 3 deletions pyrogram/types/messages_and_media/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class Message(Object, Update):
giveaway (:obj:`~pyrogram.types.Giveaway`, *optional*):
Message is a giveaway, information about the giveaway.
story (:obj:`~pyrogram.types.MessageStory`, *optional*):
story (:obj:`~pyrogram.types.MessageStory` | :obj:`~pyrogram.types.Story`, *optional*):
Message is a forwarded story, information about the forwarded story.
video (:obj:`~pyrogram.types.Video`, *optional*):
Expand Down Expand Up @@ -411,7 +411,7 @@ def __init__(
animation: "types.Animation" = None,
game: "types.Game" = None,
giveaway: "types.Giveaway" = None,
story: "types.MessageStory" = None,
story: Union["types.MessageStory", "types.Story"] = None,
video: "types.Video" = None,
voice: "types.Voice" = None,
video_note: "types.VideoNote" = None,
Expand Down Expand Up @@ -886,7 +886,7 @@ async def _parse(
giveaway = await types.Giveaway._parse(client, message)
media_type = enums.MessageMediaType.GIVEAWAY
elif isinstance(media, raw.types.MessageMediaStory):
story = types.MessageStory._parse(media)
story = await types.MessageStory._parse(client, media)
media_type = enums.MessageMediaType.STORY
elif isinstance(media, raw.types.MessageMediaDocument):
doc = media.document
Expand Down
42 changes: 33 additions & 9 deletions pyrogram/types/messages_and_media/message_story.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrofork. If not, see <http://www.gnu.org/licenses/>.

from pyrogram import raw
import pyrogram

from pyrogram import raw, types, utils
from ..object import Object


class MessageStory(Object):
"""Contains information about a forwarded story.
Parameters:
from_id (``int``):
Unique user/channel identifier of story sender.
from_user (:obj:`~pyrogram.types.User`, *optional*):
Sender of the story.
sender_chat (:obj:`~pyrogram.types.Chat`, *optional*):
Sender of the story. If the story is from channel.
story_id (``int``):
Unique story identifier.
Expand All @@ -34,21 +39,40 @@ class MessageStory(Object):
def __init__(
self,
*,
from_id: int,
from_user: "types.User" = None,
sender_chat: "types.Chat" = None,
story_id: int
):
super().__init__()

self.from_id = from_id
self.from_user = from_user
self.sender_chat = sender_chat
self.story_id = story_id

@staticmethod
def _parse(message_story: "raw.types.MessageMediaStory") -> "MessageStory":
async def _parse(
client: "pyrogram.Client",
message_story: "raw.types.MessageMediaStory"
) -> "MessageStory":
from_user = None
sender_chat = None
user_id = None
chat_id = None
if isinstance(message_story.peer, raw.types.PeerChannel):
from_id = message_story.peer.channel_id
chat_id = utils.get_channel_id(message_story.peer.channel_id)
chat = await client.invoke(
raw.functions.channels.GetChannels(
id=[await client.resolve_peer(chat_id)]
)
)
sender_chat = types.Chat._parse_chat(client, chat.chats[0])
else:
from_id = message_story.peer.user_id
user_id = message_story.peer.user_id
from_user = await client.get_users(user_id)
if not client.me.is_bot:
return await client.get_stories(user_id or chat_id, message_story.id)
return MessageStory(
from_id=from_id,
from_user=from_user,
sender_chat=sender_chat,
story_id=message_story.id
)
Loading

0 comments on commit 4c0b3e4

Please sign in to comment.