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->setDescriptor(descriptor, GiftButton::Mode::Full);
button->setClickedCallback([=] { button->setClickedCallback([=] {
const auto star = std::get_if<GiftTypeStars>(&descriptor); 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 = [=] { const auto done = [=] {
window->session().credits().load(true); window->session().credits().load(true);
window->showPeerHistory(peer); window->showPeerHistory(peer);
@ -3066,6 +3069,28 @@ void GiftResaleBox(
}); });
}, content->lifetime()); }, 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( content->add(MakeGiftsList(window, peer, rpl::single(
rpl::empty rpl::empty
) | rpl::then( ) | rpl::then(
@ -4749,6 +4774,8 @@ void RequestStarsFormAndSubmit(
const auto type = error.type(); const auto type = error.type();
if (type == u"STARGIFT_EXPORT_IN_PROGRESS"_q) { if (type == u"STARGIFT_EXPORT_IN_PROGRESS"_q) {
done(Payments::CheckoutResult::Cancelled, nullptr); done(Payments::CheckoutResult::Cancelled, nullptr);
} else if (type == u"NO_PAYMENT_NEEDED"_q) {
done(Payments::CheckoutResult::Free, nullptr);
} else { } else {
show->showToast(type); show->showToast(type);
done(Payments::CheckoutResult::Failed, nullptr); done(Payments::CheckoutResult::Failed, nullptr);

View file

@ -422,7 +422,8 @@ void TransferGift(
not_null<PeerData*> to, not_null<PeerData*> to,
std::shared_ptr<Data::UniqueGift> gift, std::shared_ptr<Data::UniqueGift> gift,
Data::SavedStarGiftId savedId, Data::SavedStarGiftId savedId,
Fn<void(Payments::CheckoutResult)> done) { Fn<void(Payments::CheckoutResult)> done,
bool skipPaymentForm = false) {
Expects(to->isUser()); Expects(to->isUser());
const auto session = &window->session(); const auto session = &window->session();
@ -430,6 +431,11 @@ void TransferGift(
auto formDone = [=]( auto formDone = [=](
Payments::CheckoutResult result, Payments::CheckoutResult result,
const MTPUpdates *updates) { const MTPUpdates *updates) {
if (result == Payments::CheckoutResult::Free) {
Assert(!skipPaymentForm);
TransferGift(window, to, gift, savedId, done, true);
return;
}
done(result); done(result);
if (result == Payments::CheckoutResult::Paid) { if (result == Payments::CheckoutResult::Paid) {
session->data().notifyGiftUpdate({ 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( session->api().request(MTPpayments_TransferStarGift(
Api::InputSavedStarGiftId(savedId, gift), Api::InputSavedStarGiftId(savedId, gift),
to->input to->input
@ -454,14 +464,14 @@ void TransferGift(
strong->showToast(error.type()); strong->showToast(error.type());
} }
}).send(); }).send();
return; } else {
}
Ui::RequestStarsFormAndSubmit( Ui::RequestStarsFormAndSubmit(
window->uiShow(), window->uiShow(),
MTP_inputInvoiceStarGiftTransfer( MTP_inputInvoiceStarGiftTransfer(
Api::InputSavedStarGiftId(savedId, gift), Api::InputSavedStarGiftId(savedId, gift),
to->input), to->input),
std::move(formDone)); std::move(formDone));
}
} }
void BuyResaleGift( void BuyResaleGift(

View file

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

View file

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