Implement paid location sending.

This commit is contained in:
John Preston 2025-02-20 18:12:35 +04:00
parent 93a590774e
commit 37dd648686
2 changed files with 50 additions and 17 deletions

View file

@ -41,6 +41,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/stickers/data_stickers.h" #include "data/stickers/data_stickers.h"
#include "history/history.h" #include "history/history.h"
#include "history/history_item.h" #include "history/history_item.h"
#include "history/history_item_helpers.h"
#include "info/bot/starref/info_bot_starref_common.h" // MakePeerBubbleButton #include "info/bot/starref/info_bot_starref_common.h" // MakePeerBubbleButton
#include "info/profile/info_profile_values.h" #include "info/profile/info_profile_values.h"
#include "inline_bots/inline_bot_result.h" #include "inline_bots/inline_bot_result.h"
@ -2482,18 +2483,46 @@ void ChooseAndSendLocation(
not_null<Window::SessionController*> controller, not_null<Window::SessionController*> controller,
const Ui::LocationPickerConfig &config, const Ui::LocationPickerConfig &config,
Api::SendAction action) { Api::SendAction action) {
const auto weak = base::make_weak(controller);
const auto session = &controller->session(); const auto session = &controller->session();
if (const auto picker = session->locationPickers().lookup(action)) { if (const auto picker = session->locationPickers().lookup(action)) {
picker->activate(); picker->activate();
return; return;
} }
const auto callback = [=](Data::InputVenue venue) { struct State {
SendPaymentHelper sendPayment;
Fn<void(Data::InputVenue, Api::SendAction)> send;
};
const auto state = std::make_shared<State>();
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()) { if (venue.justLocation()) {
Api::SendLocation(action, venue.lat, venue.lon); Api::SendLocation(action, venue.lat, venue.lon);
} else { } else {
Api::SendVenue(action, venue); Api::SendVenue(action, venue);
} }
}; };
const auto callback = [=](Data::InputVenue venue) {
state->send(venue, action);
};
const auto picker = Ui::LocationPicker::Show({ const auto picker = Ui::LocationPicker::Show({
.parent = controller->widget(), .parent = controller->widget(),
.config = config, .config = config,

View file

@ -1684,20 +1684,23 @@ void PeerMenuShareContactBox(
: ('\xAB' + title + '\xBB'); : ('\xAB' + title + '\xBB');
struct State { struct State {
base::weak_ptr<Data::Thread> weak; base::weak_ptr<Data::Thread> weak;
FnMut<void(Api::SendOptions)> share; Fn<void(Api::SendOptions)> share;
SendPaymentHelper sendPayment; SendPaymentHelper sendPayment;
}; };
auto state = std::make_shared<State>(); const auto state = std::make_shared<State>();
state->weak = thread; state->weak = thread;
state->share = [=](Api::SendOptions options) mutable { state->share = [=](Api::SendOptions options) {
const auto strong = state->weak.get(); const auto strong = state->weak.get();
if (!strong) { if (!strong) {
state->share = nullptr;
return; return;
} }
const auto withPaymentApproved = [=](int stars) { const auto withPaymentApproved = [=](int stars) {
auto copy = options; if (const auto onstack = state->share) {
copy.starsApproved = stars; auto copy = options;
state->share(copy); copy.starsApproved = stars;
onstack(copy);
}
}; };
const auto checked = state->sendPayment.check( const auto checked = state->sendPayment.check(
navigation, navigation,
@ -1715,7 +1718,7 @@ void PeerMenuShareContactBox(
auto action = Api::SendAction(strong, options); auto action = Api::SendAction(strong, options);
action.clearDraft = false; action.clearDraft = false;
strong->session().api().shareContact(user, action); strong->session().api().shareContact(user, action);
state = nullptr; state->share = nullptr;
}; };
navigation->parentController()->show( navigation->parentController()->show(
@ -1765,19 +1768,20 @@ void PeerMenuCreatePoll(
sendType, sendType,
sendMenuDetails); sendMenuDetails);
struct State { struct State {
QPointer<CreatePollBox> weak;
Fn<void(const CreatePollBox::Result &)> create; Fn<void(const CreatePollBox::Result &)> create;
SendPaymentHelper sendPayment; SendPaymentHelper sendPayment;
bool lock = false; bool lock = false;
}; };
const auto state = std::make_shared<State>(); const auto weak = QPointer<CreatePollBox>(box);
state->weak = box; const auto state = box->lifetime().make_state<State>();
state->create = [=](const CreatePollBox::Result &result) { state->create = [=](const CreatePollBox::Result &result) {
const auto withPaymentApproved = [=](int stars) { const auto withPaymentApproved = crl::guard(weak, [=](int stars) {
auto copy = result; if (const auto onstack = state->create) {
copy.options.starsApproved = stars; auto copy = result;
state->create(copy); copy.options.starsApproved = stars;
}; onstack(copy);
}
});
const auto checked = state->sendPayment.check( const auto checked = state->sendPayment.check(
controller, controller,
peer, peer,
@ -1798,8 +1802,8 @@ void PeerMenuCreatePoll(
action.clearDraft = false; action.clearDraft = false;
} }
const auto api = &peer->session().api(); const auto api = &peer->session().api();
const auto weak = state->weak;
api->polls().create(result.poll, action, crl::guard(weak, [=] { api->polls().create(result.poll, action, crl::guard(weak, [=] {
state->create = nullptr;
weak->closeBox(); weak->closeBox();
}), crl::guard(weak, [=] { }), crl::guard(weak, [=] {
state->lock = false; state->lock = false;