From 37dd648686ef6774845165a60924509f155181d2 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 20 Feb 2025 18:12:35 +0400 Subject: [PATCH] Implement paid location sending. --- .../inline_bots/bot_attach_web_view.cpp | 31 +++++++++++++++- .../SourceFiles/window/window_peer_menu.cpp | 36 ++++++++++--------- 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp index 2e7ef597f..09a643580 100644 --- a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp +++ b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp @@ -41,6 +41,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/stickers/data_stickers.h" #include "history/history.h" #include "history/history_item.h" +#include "history/history_item_helpers.h" #include "info/bot/starref/info_bot_starref_common.h" // MakePeerBubbleButton #include "info/profile/info_profile_values.h" #include "inline_bots/inline_bot_result.h" @@ -2482,18 +2483,46 @@ void ChooseAndSendLocation( not_null controller, const Ui::LocationPickerConfig &config, Api::SendAction action) { + const auto weak = base::make_weak(controller); const auto session = &controller->session(); if (const auto picker = session->locationPickers().lookup(action)) { picker->activate(); return; } - const auto callback = [=](Data::InputVenue venue) { + struct State { + SendPaymentHelper sendPayment; + Fn send; + }; + const auto state = std::make_shared(); + state->send = [=](Data::InputVenue venue, Api::SendAction action) { + if (const auto strong = weak.get()) { + const auto withPaymentApproved = [=](int stars) { + if (const auto onstack = state->send) { + auto copy = action; + copy.options.starsApproved = stars; + onstack(venue, copy); + } + }; + const auto checked = state->sendPayment.check( + strong, + action.history->peer, + 1, + action.options.starsApproved, + withPaymentApproved); + if (!checked) { + return; + } + } + state->send = nullptr; if (venue.justLocation()) { Api::SendLocation(action, venue.lat, venue.lon); } else { Api::SendVenue(action, venue); } }; + const auto callback = [=](Data::InputVenue venue) { + state->send(venue, action); + }; const auto picker = Ui::LocationPicker::Show({ .parent = controller->widget(), .config = config, diff --git a/Telegram/SourceFiles/window/window_peer_menu.cpp b/Telegram/SourceFiles/window/window_peer_menu.cpp index 760c27537..8854d89a0 100644 --- a/Telegram/SourceFiles/window/window_peer_menu.cpp +++ b/Telegram/SourceFiles/window/window_peer_menu.cpp @@ -1684,20 +1684,23 @@ void PeerMenuShareContactBox( : ('\xAB' + title + '\xBB'); struct State { base::weak_ptr weak; - FnMut share; + Fn share; SendPaymentHelper sendPayment; }; - auto state = std::make_shared(); + const auto state = std::make_shared(); state->weak = thread; - state->share = [=](Api::SendOptions options) mutable { + state->share = [=](Api::SendOptions options) { const auto strong = state->weak.get(); if (!strong) { + state->share = nullptr; return; } const auto withPaymentApproved = [=](int stars) { - auto copy = options; - copy.starsApproved = stars; - state->share(copy); + if (const auto onstack = state->share) { + auto copy = options; + copy.starsApproved = stars; + onstack(copy); + } }; const auto checked = state->sendPayment.check( navigation, @@ -1715,7 +1718,7 @@ void PeerMenuShareContactBox( auto action = Api::SendAction(strong, options); action.clearDraft = false; strong->session().api().shareContact(user, action); - state = nullptr; + state->share = nullptr; }; navigation->parentController()->show( @@ -1765,19 +1768,20 @@ void PeerMenuCreatePoll( sendType, sendMenuDetails); struct State { - QPointer weak; Fn create; SendPaymentHelper sendPayment; bool lock = false; }; - const auto state = std::make_shared(); - state->weak = box; + const auto weak = QPointer(box); + const auto state = box->lifetime().make_state(); state->create = [=](const CreatePollBox::Result &result) { - const auto withPaymentApproved = [=](int stars) { - auto copy = result; - copy.options.starsApproved = stars; - state->create(copy); - }; + const auto withPaymentApproved = crl::guard(weak, [=](int stars) { + if (const auto onstack = state->create) { + auto copy = result; + copy.options.starsApproved = stars; + onstack(copy); + } + }); const auto checked = state->sendPayment.check( controller, peer, @@ -1798,8 +1802,8 @@ void PeerMenuCreatePoll( action.clearDraft = false; } const auto api = &peer->session().api(); - const auto weak = state->weak; api->polls().create(result.poll, action, crl::guard(weak, [=] { + state->create = nullptr; weak->closeBox(); }), crl::guard(weak, [=] { state->lock = false;