Pyrofork: Refactor Story

Signed-off-by: wulan17 <wulan17@nusantararom.org>
This commit is contained in:
wulan17 2023-12-25 17:49:34 +07:00
parent 2bd393fe0d
commit 4c0b3e4090
No known key found for this signature in database
GPG key ID: 318CD6CD3A6AC0A5
11 changed files with 80 additions and 55 deletions

View file

@ -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.
@ -40,7 +40,7 @@ class DeleteStories:
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:
@ -59,8 +59,8 @@ class DeleteStories:
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")

View file

@ -31,7 +31,7 @@ class EditStory:
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,
@ -53,7 +53,7 @@ class EditStory:
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*):
@ -113,8 +113,8 @@ class EditStory:
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")

View file

@ -28,7 +28,7 @@ log = logging.getLogger(__name__)
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.
@ -36,7 +36,7 @@ class ExportStoryLink:
.. 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).
@ -51,13 +51,13 @@ class ExportStoryLink:
.. 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)

View file

@ -29,7 +29,7 @@ class ForwardStory:
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,
@ -55,7 +55,7 @@ class ForwardStory:
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.
@ -105,7 +105,7 @@ class ForwardStory:
"""
return await self.send_story(
channel_id=channel_id,
chat_id=chat_id,
privacy=privacy,
allowed_users=allowed_users,
denied_users=denied_users,

View file

@ -28,14 +28,14 @@ log = logging.getLogger(__name__)
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).
@ -47,14 +47,14 @@ class GetPeerStories:
.. 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)

View file

@ -28,7 +28,7 @@ log = logging.getLogger(__name__)
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.
@ -36,7 +36,7 @@ class GetStories:
.. 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).
@ -53,16 +53,16 @@ class GetStories:
.. 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]

View file

@ -28,7 +28,7 @@ log = logging.getLogger(__name__)
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]]:
@ -37,7 +37,7 @@ class GetUserStoriesHistory:
.. 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*):
@ -61,8 +61,8 @@ class GetUserStoriesHistory:
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")

View file

@ -30,7 +30,7 @@ class SendStory:
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,
@ -56,7 +56,7 @@ class SendStory:
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*):
@ -137,8 +137,8 @@ class SendStory:
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")

View file

@ -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*):
@ -411,7 +411,7 @@ class Message(Object, Update):
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,
@ -886,7 +886,7 @@ class Message(Object, Update):
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

View file

@ -16,7 +16,9 @@
# 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
@ -24,8 +26,11 @@ 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.
@ -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
)

View file

@ -246,6 +246,7 @@ class Story(Object, Update):
if stories.fwd_from is not None:
forward_from = await types.StoryForwardHeader._parse(client, stories.fwd_from)
media_areas = None
if stories.media_areas is not None and len(stories.media_areas) > 0:
media_areas = [
await types.MediaArea._parse(client, media_area)
@ -1399,7 +1400,7 @@ class Story(Object, Update):
RPCError: In case of a Telegram RPC error.
"""
return await self._client.delete_stories(
channel_id=self.sender_chat.id if self.sender_chat else None,
chat_id=self.sender_chat.id if self.sender_chat else None,
story_ids=self.id
)
@ -1434,7 +1435,7 @@ class Story(Object, Update):
RPCError: In case of a Telegram RPC error.
"""
return await self._client.edit_story(
channel_id=self.sender_chat.id if self.sender_chat else None,
chat_id=self.sender_chat.id if self.sender_chat else None,
story_id=self.id,
animation=animation
)
@ -1524,7 +1525,7 @@ class Story(Object, Update):
RPCError: In case of a Telegram RPC error.
"""
return await self._client.edit_story(
channel_id=self.sender_chat.id if self.sender_chat else None,
chat_id=self.sender_chat.id if self.sender_chat else None,
story_id=self.id,
privacy=privacy,
#allowed_chats=allowed_chats,
@ -1580,7 +1581,7 @@ class Story(Object, Update):
RPCError: In case of a Telegram RPC error.
"""
return await self._client.edit_story(
channel_id=self.sender_chat.id if self.sender_chat else None,
chat_id=self.sender_chat.id if self.sender_chat else None,
story_id=self.id,
caption=caption,
parse_mode=parse_mode,
@ -1618,7 +1619,7 @@ class Story(Object, Update):
RPCError: In case of a Telegram RPC error.
"""
return await self._client.edit_story(
channel_id=self.sender_chat.id if self.sender_chat else None,
chat_id=self.sender_chat.id if self.sender_chat else None,
story_id=self.id,
photo=photo
)
@ -1664,7 +1665,7 @@ class Story(Object, Update):
RPCError: In case of a Telegram RPC error.
"""
return await self._client.edit_story(
channel_id=self.sender_chat.id if self.sender_chat else None,
chat_id=self.sender_chat.id if self.sender_chat else None,
story_id=self.id,
privacy=privacy,
#allowed_chats=allowed_chats,
@ -1704,7 +1705,7 @@ class Story(Object, Update):
RPCError: In case of a Telegram RPC error.
"""
return await self._client.edit_story(
channel_id=self.sender_chat.id if self.sender_chat else None,
chat_id=self.sender_chat.id if self.sender_chat else None,
story_id=self.id,
video=video
)
@ -1732,11 +1733,11 @@ class Story(Object, Update):
Raises:
RPCError: In case of a Telegram RPC error.
"""
return await self._client.export_story_link(from_id=self.from_user.id if self.from_user else self.sender_chat.id, story_id=self.id)
return await self._client.export_story_link(chat_id=self.from_user.id if self.from_user else self.sender_chat.id, story_id=self.id)
async def forward(
self,
channel_id: int = None,
chat_id: int = None,
privacy: "enums.StoriesPrivacyRules" = None,
allowed_users: List[int] = None,
denied_users: List[int] = None,
@ -1762,7 +1763,7 @@ class Story(Object, Update):
)
Parameters:
channel_id (``int``, *optional*):
chat_id (``int``, *optional*):
Unique identifier (int) of the target channel.
If you want to forward story to a channel.
@ -1805,7 +1806,7 @@ class Story(Object, Update):
ValueError: In case of invalid arguments.
"""
return await self._client.send_story(
channel_id=channel_id,
chat_id=chat_id,
privacy=privacy,
allowed_users=allowed_users,
denied_users=denied_users,