Fix transfer of my gifts from Resale.

This commit is contained in:
John Preston 2025-04-23 15:01:49 +04:00
parent ddfab824c3
commit 15146725e3
4 changed files with 52 additions and 11 deletions

View file

@ -2628,7 +2628,10 @@ void SendGiftBox(
button->setDescriptor(descriptor, GiftButton::Mode::Full);
button->setClickedCallback([=] {
const auto star = std::get_if<GiftTypeStars>(&descriptor);
if (star && star->info.unique && star->mine) {
if (star
&& star->info.unique
&& star->mine
&& !peer->isSelf()) {
const auto done = [=] {
window->session().credits().load(true);
window->showPeerHistory(peer);
@ -3066,6 +3069,28 @@ void GiftResaleBox(
});
}, content->lifetime());
peer->owner().giftUpdates(
) | rpl::start_with_next([=](const Data::GiftUpdate &update) {
using Action = Data::GiftUpdate::Action;
const auto action = update.action;
if (action != Action::Transfer && action != Action::ResaleChange) {
return;
}
const auto i = ranges::find(
state->data.list,
update.slug,
[](const Data::StarGift &gift) {
return gift.unique ? gift.unique->slug : QString();
});
if (i == end(state->data.list)) {
return;
} else if (action == Action::Transfer
|| !i->unique->starsForResale) {
state->data.list.erase(i);
}
state->updated.fire({});
}, box->lifetime());
content->add(MakeGiftsList(window, peer, rpl::single(
rpl::empty
) | rpl::then(
@ -4749,6 +4774,8 @@ void RequestStarsFormAndSubmit(
const auto type = error.type();
if (type == u"STARGIFT_EXPORT_IN_PROGRESS"_q) {
done(Payments::CheckoutResult::Cancelled, nullptr);
} else if (type == u"NO_PAYMENT_NEEDED"_q) {
done(Payments::CheckoutResult::Free, nullptr);
} else {
show->showToast(type);
done(Payments::CheckoutResult::Failed, nullptr);

View file

@ -422,7 +422,8 @@ void TransferGift(
not_null<PeerData*> to,
std::shared_ptr<Data::UniqueGift> gift,
Data::SavedStarGiftId savedId,
Fn<void(Payments::CheckoutResult)> done) {
Fn<void(Payments::CheckoutResult)> done,
bool skipPaymentForm = false) {
Expects(to->isUser());
const auto session = &window->session();
@ -430,6 +431,11 @@ void TransferGift(
auto formDone = [=](
Payments::CheckoutResult result,
const MTPUpdates *updates) {
if (result == Payments::CheckoutResult::Free) {
Assert(!skipPaymentForm);
TransferGift(window, to, gift, savedId, done, true);
return;
}
done(result);
if (result == Payments::CheckoutResult::Paid) {
session->data().notifyGiftUpdate({
@ -441,7 +447,11 @@ void TransferGift(
}
}
};
if (gift->starsForTransfer <= 0) {
if (skipPaymentForm) {
// We can't check (gift->starsForTransfer <= 0) here.
//
// Sometimes we don't know the price for transfer.
// Like when we transfer a gift from Resale tab.
session->api().request(MTPpayments_TransferStarGift(
Api::InputSavedStarGiftId(savedId, gift),
to->input
@ -454,8 +464,7 @@ void TransferGift(
strong->showToast(error.type());
}
}).send();
return;
}
} else {
Ui::RequestStarsFormAndSubmit(
window->uiShow(),
MTP_inputInvoiceStarGiftTransfer(
@ -463,6 +472,7 @@ void TransferGift(
to->input),
std::move(formDone));
}
}
void BuyResaleGift(
std::shared_ptr<ChatHelpers::Show> show,

View file

@ -54,6 +54,7 @@ enum class CheckoutResult {
Pending,
Cancelled,
Failed,
Free, // Gift transfer attempt that doesn't need any payment.
};
struct RealFormPresentedNotification {

View file

@ -2108,6 +2108,7 @@ void GlobalStarGiftBox(
const Data::StarGift &data,
PeerId resaleRecipientId,
CreditsEntryBoxStyleOverrides st) {
const auto selfId = show->session().userPeerId();
const auto ownerId = data.unique ? data.unique->ownerId.value : 0;
Settings::GenericCreditsEntryBox(
box,
@ -2116,7 +2117,9 @@ void GlobalStarGiftBox(
.credits = StarsAmount(data.stars),
.bareGiftStickerId = data.document->id,
.bareGiftOwnerId = ownerId,
.bareGiftResaleRecipientId = resaleRecipientId.value,
.bareGiftResaleRecipientId = ((resaleRecipientId != selfId)
? resaleRecipientId.value
: 0),
.stargiftId = data.id,
.uniqueGift = data.unique,
.peerType = Data::CreditsHistoryEntry::PeerType::Peer,