pyrofork: Refactor Reactions

Signed-off-by: wulan17 <wulan17@nusantararom.org>
This commit is contained in:
wulan17 2024-08-18 20:15:39 +07:00
parent b6d9e2c782
commit cbd632c600
No known key found for this signature in database
GPG key ID: 318CD6CD3A6AC0A5
9 changed files with 33 additions and 156 deletions

View file

@ -527,8 +527,6 @@ def pyrogram_api():
GeneralTopicHidden
GeneralTopicUnhidden
Reaction
ReactionCount
ReactionType
MessageReactionUpdated
MessageReactionCountUpdated
""",

View file

@ -27,3 +27,6 @@ class ReactionType(AutoName):
CUSTOM_EMOJI = auto()
"""Custom emoji reaction type."""
PAID = auto()
"""Paid reaction type."""

View file

@ -39,8 +39,6 @@ from .photo import Photo
from .poll import Poll
from .poll_option import PollOption
from .reaction import Reaction
from .reaction_type import ReactionType
from .reaction_count import ReactionCount
from .sticker import Sticker
from .stickerset import StickerSet
from .stories_privacy_rules import StoriesPrivacyRules
@ -68,5 +66,5 @@ from .exported_story_link import ExportedStoryLink
__all__ = [
"Animation", "Audio", "AvailableEffect", "Contact", "Document", "Game", "GiftedPremium", "Giveaway", "GiveawayLaunched", "GiveawayResult", "LabeledPrice", "Location", "MediaArea", "MediaAreaChannelPost", "MediaAreaCoordinates", "Message", "MessageEntity", "Photo", "Thumbnail",
"StrippedThumbnail", "Poll", "PollOption", "Sticker", "StickerSet", "Venue", "Video", "VideoNote", "Voice", "WebPage", "WebPageEmpty", "WebPagePreview", "Dice",
"Reaction", "WebAppData", "MessageReactions", "ReactionCount", "ReactionType", "MessageReactionUpdated", "MessageReactionCountUpdated", "MessageStory", "Story", "StoryDeleted", "StorySkipped", "StoryViews", "StoryForwardHeader", "StoriesPrivacyRules", "ExportedStoryLink"
"Reaction", "WebAppData", "MessageReactions", "MessageReactionUpdated", "MessageReactionCountUpdated", "MessageStory", "Story", "StoryDeleted", "StorySkipped", "StoryViews", "StoryForwardHeader", "StoriesPrivacyRules", "ExportedStoryLink"
]

View file

@ -40,7 +40,7 @@ class MessageReactionCountUpdated(Object, Update):
date (:py:obj:`~datetime.datetime`):
Date of change of the reaction
reactions (:obj:`~pyrogram.types.ReactionCount`):
reactions (:obj:`~pyrogram.types.Reaction`):
List of reactions that are present on the message
"""
@ -51,7 +51,7 @@ class MessageReactionCountUpdated(Object, Update):
chat: "types.Chat",
message_id: int,
date: datetime,
reactions: List["types.ReactionCount"]
reactions: List["types.Reaction"]
):
super().__init__(client)
@ -66,7 +66,7 @@ class MessageReactionCountUpdated(Object, Update):
update: "raw.types.UpdateBotMessageReactions",
users: Dict[int, "raw.types.User"],
chats: Dict[int, "raw.types.Chat"]
) -> "MessageReactionUpdated":
) -> "MessageReactionCountUpdated":
chat = None
peer_id = utils.get_peer_id(update.peer)
raw_peer_id = utils.get_raw_peer_id(update.peer)
@ -81,7 +81,8 @@ class MessageReactionCountUpdated(Object, Update):
message_id=update.msg_id,
date=utils.timestamp_to_datetime(update.date),
reactions=[
types.ReactionCount._parse(
types.Reaction._parse_count(
client,
rt
) for rt in update.reactions
]

View file

@ -48,10 +48,10 @@ class MessageReactionUpdated(Object, Update):
date (:py:obj:`~datetime.datetime`):
Date of change of the reaction
old_reaction (:obj:`~pyrogram.types.ReactionType`):
old_reaction (:obj:`~pyrogram.types.Reaction`):
Previous list of reaction types that were set by the user
new_reaction (:obj:`~pyrogram.types.ReactionType`):
new_reaction (:obj:`~pyrogram.types.Reaction`):
New list of reaction types that have been set by the user
"""
@ -65,8 +65,8 @@ class MessageReactionUpdated(Object, Update):
actor_chat: "types.Chat",
date: datetime,
chat: "types.Chat",
old_reaction: List["types.ReactionType"],
new_reaction: List["types.ReactionType"]
old_reaction: List["types.Reaction"],
new_reaction: List["types.Reaction"]
):
super().__init__(client)
@ -112,12 +112,14 @@ class MessageReactionUpdated(Object, Update):
chat=chat,
actor_chat=actor_chat,
old_reaction=[
types.ReactionType._parse(
types.Reaction._parse(
client,
rt
) for rt in update.old_reactions
],
new_reaction=[
types.ReactionType._parse(
types.Reaction._parse(
client,
rt
) for rt in update.new_reactions
]

View file

@ -20,7 +20,7 @@
from typing import Optional
import pyrogram
from pyrogram import raw
from pyrogram import enums, raw
from ..object import Object
@ -28,6 +28,9 @@ class Reaction(Object):
"""Contains information about a reaction.
Parameters:
type (``enums.ReactionType``):
Reaction type.
emoji (``str``, *optional*):
Reaction emoji.
@ -46,6 +49,7 @@ class Reaction(Object):
self,
*,
client: "pyrogram.Client" = None,
type: "enums.ReactionType",
emoji: Optional[str] = None,
custom_emoji_id: Optional[int] = None,
count: Optional[int] = None,
@ -53,6 +57,7 @@ class Reaction(Object):
):
super().__init__(client)
self.type = type
self.emoji = emoji
self.custom_emoji_id = custom_emoji_id
self.count = count
@ -66,14 +71,21 @@ class Reaction(Object):
if isinstance(reaction, raw.types.ReactionEmoji):
return Reaction(
client=client,
type=enums.ReactionType.EMOJI,
emoji=reaction.emoticon
)
if isinstance(reaction, raw.types.ReactionCustomEmoji):
return Reaction(
client=client,
type=enums.ReactionType.CUSTOM_EMOJI,
custom_emoji_id=reaction.document_id
)
if isinstance(reaction, raw.types.ReactionPaid):
return Reaction(
client=client,
type=enums.ReactionType.PAID
)
@staticmethod
def _parse_count(

View file

@ -1,63 +0,0 @@
# PyroFork - Telegram MTProto API Client Library for Python
# Copyright (C) 2022-present Mayuri-Chan <https://github.com/Mayuri-Chan>
#
# This file is part of PyroFork.
#
# PyroFork is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyroFork is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# 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 typing import Optional
from pyrogram import raw
from .reaction_type import ReactionType
from ..object import Object
class ReactionCount(Object):
"""Represents a reaction added to a message along with the number of times it was added.
Parameters:
type (:obj:`~pyrogram.types.ReactionType`):
Reaction type.
total_count (``int``):
Total reaction count.
chosen_order (``int``):
Chosen reaction order.
Available for chosen reactions.
"""
def __init__(
self,
*,
type: ReactionType,
total_count: int,
chosen_order: int
):
super().__init__()
self.type = type
self.total_count = total_count
self.chosen_order = chosen_order
@staticmethod
def _parse(
update: "raw.types.ReactionCount",
) -> Optional["ReactionCount"]:
return ReactionCount(
type=ReactionType._parse(
update.reaction
),
total_count=update.count,
chosen_order=update.chosen_order
)

View file

@ -1,74 +0,0 @@
# PyroFork - Telegram MTProto API Client Library for Python
# Copyright (C) 2022-present Mayuri-Chan <https://github.com/Mayuri-Chan>
#
# This file is part of PyroFork.
#
# PyroFork is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyroFork is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# 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 typing import Optional
from pyrogram import enums, raw
from ..object import Object
class ReactionType(Object):
"""Contains information about a reaction.
Parameters:
type (``enums.ReactionType``, *optional*):
Reaction type.
emoji (``str``, *optional*):
Reaction emoji.
custom_emoji_id (``int``, *optional*):
Custom emoji id.
"""
def __init__(
self,
*,
type: str = "enums.ReactionType",
emoji: str = None,
custom_emoji_id: str = None
):
super().__init__()
self.type = type
self.emoji = emoji
self.custom_emoji_id = custom_emoji_id
@staticmethod
def _parse(
update: "raw.types.Reaction",
) -> Optional["ReactionType"]:
if isinstance(update, raw.types.ReactionEmpty):
return None
elif isinstance(update, raw.types.ReactionEmoji):
return ReactionType(
type=enums.ReactionType.EMOJI,
emoji=update.emoticon
)
elif isinstance(update, raw.types.ReactionCustomEmoji):
return ReactionType(
type=enums.ReactionType.CUSTOM_EMOJI,
custom_emoji_id=update.document_id
)
def write(self):
if self.type == enums.ReactionType.EMOJI:
return raw.types.ReactionEmoji(
emoticon=self.emoji
)
if self.type == enums.ReactionType.CUSTOM_EMOJI:
return raw.types.ReactionCustomEmoji(
document_id=self.custom_emoji_id
)

View file

@ -64,7 +64,7 @@ class ChatReactions(Object):
return ChatReactions(
client=client,
reactions=[
types.ReactionType._parse(reaction)
types.Reaction._parse(client, reaction)
for reaction in chat_reactions.reactions
]
)