Skip to content

Commit

Permalink
pyrofork: Refactor UserGift
Browse files Browse the repository at this point in the history
Signed-off-by: Yasir Aris <[email protected]>
  • Loading branch information
yasirarism committed Jan 4, 2025
1 parent 4344f3f commit 20c55f3
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 32 deletions.
3 changes: 0 additions & 3 deletions pyrogram/enums/message_service_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,5 @@ class MessageServiceType(AutoName):
USER_GIFT = auto()
"Star gift"

STAR_GIFT = auto()
"Star gift"

SCREENSHOT_TAKEN = auto()
"Screenshot taken"
5 changes: 1 addition & 4 deletions pyrogram/types/messages_and_media/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,12 +906,9 @@ async def _parse(
elif isinstance(action, raw.types.MessageActionGiftCode):
gift_code = types.GiftCode._parse(client, action, chats)
service_type = enums.MessageServiceType.GIFT_CODE
elif isinstance(action, raw.types.MessageActionStarGift):
user_gift = await types.UserGift._parse_action(client, message, users)
service_type = enums.MessageServiceType.USER_GIFT
elif isinstance(action, (raw.types.MessageActionStarGift, raw.types.MessageActionStarGiftUnique)):
star_gift = await types.StarGift._parse_action(client, message, users)
service_type = enums.MessageServiceType.STAR_GIFT
service_type = enums.MessageServiceType.UserGift
elif isinstance(action, raw.types.MessageActionScreenshotTaken):
screenshot_taken = types.ScreenshotTaken()
service_type = enums.MessageServiceType.SCREENSHOT_TAKEN
Expand Down
114 changes: 90 additions & 24 deletions pyrogram/types/payments/user_gift.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

from datetime import datetime
from typing import Optional, List
from typing import Optional

import pyrogram
from pyrogram import raw, types, utils
from ..messages_and_media.message import Str
from ..messages_and_media import Str
from ..object import Object


Expand All @@ -44,7 +44,7 @@ class UserGift(Object):
is_saved (``bool``, *optional*):
True, if the gift is displayed on the user's profile page; may be False only for the receiver of the gift.
date (``datetime``):
date (:py:obj:`~datetime.datetime`, *optional*):
Date when the gift was sent.
gift (:obj:`~pyrogram.types.Gift`, *optional*):
Expand All @@ -56,6 +56,27 @@ class UserGift(Object):
sell_star_count (``int``, *optional*):
Number of Telegram Stars that can be claimed by the receiver instead of the gift; only for the gift receiver.
was_converted (``bool``, *optional*):
True, if the gift was converted to Telegram Stars; only for the receiver of the gift.
can_be_upgraded (``bool``, *optional*):
True, if the gift is a regular gift that can be upgraded to a unique gift; only for the receiver of the gift.
was_refunded (``bool``, *optional*):
True, if the gift was refunded and isn't available anymore.
prepaid_upgrade_star_count (``int``, *optional*):
Number of Telegram Stars that were paid by the sender for the ability to upgrade the gift.
can_be_transferred (``bool``, *optional*):
True, if the gift is an upgraded gift that can be transferred to another user; only for the receiver of the gift.
transfer_star_count (``int``, *optional*):
Number of Telegram Stars that must be paid to transfer the upgraded gift; only for the receiver of the gift.
export_date (:py:obj:`~datetime.datetime`, *optional*):
Point in time (Unix timestamp) when the upgraded gift can be transferred to TON blockchain as an NFT; None if NFT export isn't possible; only for the receiver of the gift.
"""

def __init__(
Expand All @@ -64,13 +85,20 @@ def __init__(
client: "pyrogram.Client" = None,
sender_user: Optional["types.User"] = None,
text: Optional[str] = None,
entities: List["types.MessageEntity"] = None,
entities: list["types.MessageEntity"] = None,
date: datetime,
is_private: Optional[bool] = None,
is_saved: Optional[bool] = None,
gift: Optional["types.Gift"] = None,
message_id: Optional[int] = None,
sell_star_count: Optional[int] = None
sell_star_count: Optional[int] = None,
was_converted: Optional[bool] = None,
can_be_upgraded: Optional[bool] = None,
was_refunded: Optional[bool] = None,
prepaid_upgrade_star_count: Optional[int] = None,
can_be_transferred: Optional[bool] = None,
transfer_star_count: Optional[int] = None,
export_date: datetime = None,
):
super().__init__(client)

Expand All @@ -83,6 +111,14 @@ def __init__(
self.entities = entities
self.message_id = message_id
self.sell_star_count = sell_star_count
self.was_converted = was_converted
self.can_be_upgraded = can_be_upgraded
self.was_refunded = was_refunded
self.prepaid_upgrade_star_count = prepaid_upgrade_star_count
self.can_be_transferred = can_be_transferred
self.transfer_star_count = transfer_star_count
self.export_date = export_date


@staticmethod
async def _parse(
Expand Down Expand Up @@ -126,25 +162,55 @@ async def _parse_action(
entities = [types.MessageEntity._parse(client, entity, users) for entity in action.message.entities]
entities = types.List(filter(lambda x: x is not None, entities))

return UserGift(
gift=types.Gift(
id=action.gift.id,
sticker=await types.Sticker._parse(client, doc, attributes),
star_count=action.gift.stars,
default_sell_star_count=action.gift.convert_stars,
remaining_count=getattr(action.gift, "availability_remains", None),
total_count=getattr(action.gift, "availability_total", None),
is_limited=getattr(action.gift, "limited", None),
),
date=utils.timestamp_to_datetime(message.date),
is_private=getattr(action, "name_hidden", None),
is_saved=getattr(action, "saved", None),
sender_user=types.User._parse(client, users.get(utils.get_raw_peer_id(message.peer_id))),
message_id=message.id,
text=Str(text).init(entities) if text else None,
entities=entities,
client=client
)
if isinstance(action, raw.types.MessageActionStarGift):
return UserGift(
gift=types.Gift(
id=action.gift.id,
sticker=await types.Sticker._parse(client, doc, attributes),
star_count=action.gift.stars,
total_count=getattr(action.gift, "availability_total", None),
remaining_count=getattr(action.gift, "availability_remains", None),
default_sell_star_count=action.gift.convert_stars,
is_limited=getattr(action.gift, "limited", None),
),
date=utils.timestamp_to_datetime(message.date),
is_private=getattr(action, "name_hidden", None),
is_saved=getattr(action, "saved", None),
sender_user=types.User._parse(client, users.get(utils.get_raw_peer_id(message.peer_id))),
message_id=message.id,
text=Str(text).init(entities) if text else None,
entities=entities,
sell_star_count=getattr(action, "convert_stars", None),
was_converted=getattr(action, "converted", None),
can_be_upgraded=getattr(action, "can_upgrade", None),
was_refunded=getattr(action, "refunded", None),
prepaid_upgrade_star_count=getattr(action, "upgrade_stars", None),
client=client
)

if isinstance(action, raw.types.MessageActionStarGiftUnique):
return UserGift(
gift=types.Gift(
id=action.gift.id,
sticker=await types.Sticker._parse(client, doc, attributes),
star_count=action.gift.stars,
total_count=getattr(action.gift, "availability_total", None),
remaining_count=getattr(action.gift, "availability_remains", None),
default_sell_star_count=action.gift.convert_stars,
is_limited=getattr(action.gift, "limited", None),
),
date=utils.timestamp_to_datetime(message.date),
sender_user=types.User._parse(client, users.get(utils.get_raw_peer_id(message.peer_id))),
message_id=message.id,
text=Str(text).init(entities) if text else None,
entities=entities,
can_be_transferred=getattr(action, "transferred", None),
was_refunded=getattr(action, "refunded", None),
prepaid_upgrade_star_count=getattr(action, "upgrade_stars", None),
export_date=utils.timestamp_to_datetime(action.can_export_at) if action.can_export_at else None,
transfer_star_count=getattr(action, "transfer_stars", None),
client=client
)

async def toggle(self, is_saved: bool) -> bool:
"""Bound method *toggle* of :obj:`~pyrogram.types.UserGift`.
Expand Down
2 changes: 1 addition & 1 deletion pyrogram/types/payments/user_star_gift.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class UserStarGift(Object):
"""A user star gift.
Parameters:
date (``datetime``):
date (:py:obj:`~datetime.datetime`, *optional*):
Date when the star gift was received.
star_gift (:obj:`~pyrogram.types.StarGift`, *optional*):
Expand Down

0 comments on commit 20c55f3

Please sign in to comment.