Skip to content

Commit

Permalink
Update gifts to new layer
Browse files Browse the repository at this point in the history
  • Loading branch information
KurimuzonAkuma committed Jan 27, 2025
1 parent a7afa32 commit 469e13b
Show file tree
Hide file tree
Showing 16 changed files with 214 additions and 88 deletions.
4 changes: 2 additions & 2 deletions compiler/docs/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ def get_title_list(s: str) -> list:
get_payment_form
get_available_gifts
get_upgraded_gift
get_user_gifts_count
get_user_gifts
get_chat_gifts_count
get_chat_gifts
hide_gift
send_payment_form
send_gift
Expand Down
8 changes: 4 additions & 4 deletions pyrogram/methods/payments/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from .get_stars_balance import GetStarsBalance
from .get_upgraded_gift import GetUpgradedGift
from .get_available_gifts import GetAvailableGifts
from .get_user_gifts_count import GetUserGiftsCount
from .get_user_gifts import GetUserGifts
from .get_chat_gifts_count import GetChatGiftsCount
from .get_chat_gifts import GetChatGifts
from .hide_gift import HideGift
from .send_payment_form import SendPaymentForm
from .send_gift import SendGift
Expand All @@ -40,8 +40,8 @@ class Payments(
GetStarsBalance,
GetUpgradedGift,
GetAvailableGifts,
GetUserGiftsCount,
GetUserGifts,
GetChatGiftsCount,
GetChatGifts,
HideGift,
SendPaymentForm,
SendGift,
Expand Down
4 changes: 3 additions & 1 deletion pyrogram/methods/payments/convert_gift.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ async def convert_gift(
"""
r = await self.invoke(
raw.functions.payments.ConvertStarGift(
msg_id=message_id
stargift=raw.types.InputSavedStarGiftUser(
msg_id=message_id
)
)
)

Expand Down
2 changes: 1 addition & 1 deletion pyrogram/methods/payments/get_available_gifts.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ async def get_available_gifts(
raw.functions.payments.GetStarGifts(hash=0)
)

return types.List([await types.Gift._parse(self, gift) for gift in r.gifts])
return types.List([await types.Gift._parse_regular(self, gift) for gift in r.gifts])
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

from typing import Union
from typing import Optional, Union

import pyrogram
from pyrogram import raw, types


class GetUserGifts:
async def get_user_gifts(
class GetChatGifts:
async def get_chat_gifts(
self: "pyrogram.Client",
chat_id: Union[int, str],
exclude_unsaved: Optional[bool] = None,
exclude_saved: Optional[bool] = None,
exclude_unlimited: Optional[bool] = None,
exclude_limited: Optional[bool] = None,
exclude_upgraded: Optional[bool] = None,
sort_by_value: Optional[bool] = None,
limit: int = 0,
offset: str = ""
):
Expand All @@ -39,6 +45,24 @@ async def get_user_gifts(
For your personal cloud (Saved Messages) you can simply use "me" or "self".
For a contact that exists in your Telegram address book you can use his phone number (str).
exclude_unsaved (``bool``, *optional*):
Exclude unsaved star gifts.
exclude_saved (``bool``, *optional*):
Exclude saved star gifts.
exclude_unlimited (``bool``, *optional*):
Exclude unlimited star gifts.
exclude_limited (``bool``, *optional*):
Exclude limited star gifts.
exclude_upgraded (``bool``, *optional*):
Exclude upgraded star gifts.
sort_by_value (``bool``, *optional*):
Sort star gifts by value.
offset (``str``, *optional*):
Offset of the results to be returned.
Expand All @@ -51,32 +75,36 @@ async def get_user_gifts(
Example:
.. code-block:: python
async for gift in app.get_user_gifts(chat_id):
async for gift in app.get_chat_gifts(chat_id):
print(gift)
"""
peer = await self.resolve_peer(chat_id)

if not isinstance(peer, (raw.types.InputPeerUser, raw.types.InputPeerSelf)):
raise ValueError("chat_id must belong to a user.")

current = 0
total = abs(limit) or (1 << 31) - 1
limit = min(100, total)

while True:
r = await self.invoke(
raw.functions.payments.GetUserStarGifts(
user_id=peer,
raw.functions.payments.GetSavedStarGifts(
peer=peer,
offset=offset,
limit=limit
limit=limit,
exclude_unsaved=exclude_unsaved,
exclude_saved=exclude_saved,
exclude_unlimited=exclude_unlimited,
exclude_limited=exclude_limited,
exclude_unique=exclude_upgraded,
sort_by_value=sort_by_value
),
sleep_threshold=60
)

users = {u.id: u for u in r.users}
users = {i.id: i for i in r.users}
chats = {i.id: i for i in r.chats}

user_star_gifts = [
await types.Gift._parse_user(self, gift, users)
await types.Gift._parse_saved(self, gift, users, chats)
for gift in r.gifts
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

import logging
from typing import Union
from typing import Optional, Union

import pyrogram
from pyrogram import raw

log = logging.getLogger(__name__)


class GetUserGiftsCount:
async def get_user_gifts_count(
class GetChatGiftsCount:
async def get_chat_gifts_count(
self: "pyrogram.Client",
chat_id: Union[int, str]
chat_id: Union[int, str],
exclude_unsaved: Optional[bool] = None,
exclude_saved: Optional[bool] = None,
exclude_unlimited: Optional[bool] = None,
exclude_limited: Optional[bool] = None,
exclude_upgraded: Optional[bool] = None
) -> int:
"""Get the total count of star gifts of specified user.
Expand All @@ -40,23 +45,40 @@ async def get_user_gifts_count(
For your personal cloud (Saved Messages) you can simply use "me" or "self".
For a contact that exists in your Telegram address book you can use his phone number (str).
exclude_unsaved (``bool``, *optional*):
Exclude unsaved star gifts.
exclude_saved (``bool``, *optional*):
Exclude saved star gifts.
exclude_unlimited (``bool``, *optional*):
Exclude unlimited star gifts.
exclude_limited (``bool``, *optional*):
Exclude limited star gifts.
exclude_upgraded (``bool``, *optional*):
Exclude upgraded star gifts.
Returns:
``int``: On success, the star gifts count is returned.
Example:
.. code-block:: python
await app.get_user_gifts_count(chat_id)
await app.get_chat_gifts_count(chat_id)
"""
peer = await self.resolve_peer(chat_id)

if not isinstance(peer, (raw.types.InputPeerUser, raw.types.InputPeerSelf)):
raise ValueError("chat_id must belong to a user.")

r = await self.invoke(
raw.functions.payments.GetUserStarGifts(
user_id=peer,
raw.functions.payments.GetSavedStarGifts(
peer=peer,
offset="",
exclude_unsaved=exclude_unsaved,
exclude_saved=exclude_saved,
exclude_unlimited=exclude_unlimited,
exclude_limited=exclude_limited,
exclude_unique=exclude_upgraded,
limit=1
)
)
Expand Down
3 changes: 2 additions & 1 deletion pyrogram/methods/payments/get_upgraded_gift.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ async def get_upgraded_gift(
)

users = {i.id: i for i in r.users}
chats = {i.id: i for i in r.chats}

return await types.Gift._parse_unique(self, r.gift, users)
return await types.Gift._parse_unique(self, r.gift, users, chats)
4 changes: 3 additions & 1 deletion pyrogram/methods/payments/hide_gift.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ async def hide_gift(
"""
r = await self.invoke(
raw.functions.payments.SaveStarGift(
msg_id=message_id,
stargift=raw.types.InputSavedStarGiftUser(
msg_id=message_id
),
unsave=True
)
)
Expand Down
4 changes: 3 additions & 1 deletion pyrogram/methods/payments/show_gift.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ async def show_gift(
"""
r = await self.invoke(
raw.functions.payments.SaveStarGift(
msg_id=message_id,
stargift=raw.types.InputSavedStarGiftUser(
msg_id=message_id
),
unsave=False
)
)
Expand Down
10 changes: 7 additions & 3 deletions pyrogram/methods/payments/transfer_gift.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from typing import Union

import pyrogram
from pyrogram import raw, errors
from pyrogram import errors, raw


class TransferGift:
Expand Down Expand Up @@ -58,13 +58,17 @@ async def transfer_gift(
try:
await self.invoke(
raw.functions.payments.TransferStarGift(
msg_id=message_id,
stargift=raw.types.InputSavedStarGiftUser(
msg_id=message_id
),
to_id=peer
)
)
except errors.PaymentRequired:
invoice = raw.types.InputInvoiceStarGiftTransfer(
msg_id=message_id,
stargift=raw.types.InputSavedStarGiftUser(
msg_id=message_id
),
to_id=peer
)

Expand Down
10 changes: 7 additions & 3 deletions pyrogram/methods/payments/upgrade_gift.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from typing import Optional

import pyrogram
from pyrogram import raw, errors
from pyrogram import errors, raw


class UpgradeGift:
Expand Down Expand Up @@ -51,13 +51,17 @@ async def upgrade_gift(
try:
await self.invoke(
raw.functions.payments.UpgradeStarGift(
msg_id=message_id,
stargift=raw.types.InputSavedStarGiftUser(
msg_id=message_id
),
keep_original_details=keep_details
)
)
except errors.PaymentRequired:
invoice = raw.types.InputInvoiceStarGiftUpgrade(
msg_id=message_id,
stargift=raw.types.InputSavedStarGiftUser(
msg_id=message_id
),
keep_original_details=keep_details
)

Expand Down
Loading

0 comments on commit 469e13b

Please sign in to comment.