From d424a8b0393d66906ab26eab6e49ac01ec28eb3b Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Thu, 29 Dec 2022 02:33:57 +0300 Subject: [PATCH] Added handler of urls for login. --- .../SourceFiles/api/api_confirm_phone.cpp | 15 +++++++++++- .../SourceFiles/boxes/change_phone_box.cpp | 22 ++++++++++++----- .../SourceFiles/core/local_url_handlers.cpp | 24 +++++++++++++++++++ Telegram/SourceFiles/intro/intro_code.cpp | 6 +++++ Telegram/SourceFiles/main/main_account.cpp | 10 ++++++++ Telegram/SourceFiles/main/main_account.h | 5 ++++ .../passport/passport_panel_controller.cpp | 17 ++++++++++--- 7 files changed, 89 insertions(+), 10 deletions(-) diff --git a/Telegram/SourceFiles/api/api_confirm_phone.cpp b/Telegram/SourceFiles/api/api_confirm_phone.cpp index 677d6d13c0..d25e0d2924 100644 --- a/Telegram/SourceFiles/api/api_confirm_phone.cpp +++ b/Telegram/SourceFiles/api/api_confirm_phone.cpp @@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "apiwrap.h" #include "lang/lang_keys.h" +#include "main/main_account.h" #include "main/main_session.h" #include "ui/boxes/confirm_box.h" #include "ui/boxes/confirm_phone_box.h" @@ -77,6 +78,12 @@ void ConfirmPhone::resolve( fragmentUrl, timeout); const auto boxWeak = Ui::MakeWeak(box.data()); + using LoginCode = rpl::event_stream; + const auto codeHandles = box->lifetime().make_state(); + controller->session().account().setHandleLoginCode([=]( + const QString &code) { + codeHandles->fire_copy(code); + }); box->resendRequests( ) | rpl::start_with_next([=] { _api.request(MTPauth_ResendCode( @@ -88,7 +95,9 @@ void ConfirmPhone::resolve( } }).send(); }, box->lifetime()); - box->checkRequests( + rpl::merge( + codeHandles->events(), + box->checkRequests() ) | rpl::start_with_next([=](const QString &code) { if (_checkRequestId) { return; @@ -120,6 +129,10 @@ void ConfirmPhone::resolve( boxWeak->showServerError(errorText); }).handleFloodErrors().send(); }, box->lifetime()); + box->boxClosing( + ) | rpl::start_with_next([=] { + controller->session().account().setHandleLoginCode(nullptr); + }, box->lifetime()); controller->show(std::move(box), Ui::LayerOption::CloseOther); }); diff --git a/Telegram/SourceFiles/boxes/change_phone_box.cpp b/Telegram/SourceFiles/boxes/change_phone_box.cpp index 1a329b306f..a5fc990fd7 100644 --- a/Telegram/SourceFiles/boxes/change_phone_box.cpp +++ b/Telegram/SourceFiles/boxes/change_phone_box.cpp @@ -20,6 +20,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/boxes/confirm_box.h" #include "boxes/phone_banned_box.h" #include "countries/countries_instance.h" // Countries::ExtractPhoneCode. +#include "main/main_account.h" #include "main/main_session.h" #include "data/data_session.h" #include "data/data_user.h" @@ -126,7 +127,7 @@ protected: void prepare() override; private: - void submit(); + void submit(const QString &code); void sendCall(); void updateCall(); void sendCodeFail(const MTP::Error &error); @@ -344,18 +345,20 @@ void ChangePhone::EnterCode::prepare() { st::changePhoneLabel); description->moveToLeft(st::boxPadding.left(), 0); + const auto submitInput = [=] { submit(_code->getDigitsOnly()); }; + const auto phoneValue = QString(); _code.create( this, st::defaultInputField, tr::lng_change_phone_code_title(), phoneValue); - _code->setAutoSubmit(_codeLength, [=] { submit(); }); + _code->setAutoSubmit(_codeLength, submitInput); _code->setChangedCallback([=] { hideError(); }); _code->resize(width - 2 * st::boxPadding.left(), _code->height()); _code->moveToLeft(st::boxPadding.left(), description->bottomNoMargins()); - connect(_code, &Ui::InputField::submitted, [=] { submit(); }); + connect(_code, &Ui::InputField::submitted, submitInput); if (!_openUrl.isEmpty()) { _fragment.create( @@ -372,6 +375,14 @@ void ChangePhone::EnterCode::prepare() { codeBottom + ErrorSkip() + st::boxLittleSkip); } + _controller->session().account().setHandleLoginCode([=](QString code) { + submit(code); + }); + boxClosing( + ) | rpl::start_with_next([controller = _controller] { + controller->session().account().setHandleLoginCode(nullptr); + }, lifetime()); + setDimensions(width, countHeight()); if (_callTimeout > 0) { @@ -379,7 +390,7 @@ void ChangePhone::EnterCode::prepare() { updateCall(); } - addButton(tr::lng_change_phone_new_submit(), [=] { submit(); }); + addButton(tr::lng_change_phone_new_submit(), submitInput); addButton(tr::lng_cancel(), [=] { closeBox(); }); } @@ -390,14 +401,13 @@ int ChangePhone::EnterCode::countHeight() const { + (_fragment ? _fragment->height() : 0); } -void ChangePhone::EnterCode::submit() { +void ChangePhone::EnterCode::submit(const QString &code) { if (_requestId) { return; } hideError(); const auto session = &_controller->session(); - const auto code = _code->getDigitsOnly(); const auto weak = Ui::MakeWeak(this); _requestId = session->api().request(MTPaccount_ChangePhone( MTP_string(_phone), diff --git a/Telegram/SourceFiles/core/local_url_handlers.cpp b/Telegram/SourceFiles/core/local_url_handlers.cpp index 0b3aa18726..f3f9782b32 100644 --- a/Telegram/SourceFiles/core/local_url_handlers.cpp +++ b/Telegram/SourceFiles/core/local_url_handlers.cpp @@ -49,6 +49,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "settings/settings_chat.h" #include "settings/settings_premium.h" #include "mainwidget.h" +#include "main/main_account.h" #include "main/main_session.h" #include "main/main_session_settings.h" #include "inline_bots/bot_attach_web_view.h" @@ -792,6 +793,25 @@ bool ResolvePremiumOffer( return true; } +bool ResolveLoginCode( + Window::SessionController *controller, + const Match &match, + const QVariant &context) { + const auto loginCode = match->captured(2); + if (loginCode.isEmpty()) { + return false; + }; + (controller + ? controller->session().account() + : Core::App().activeAccount()).handleLoginCode(loginCode); + if (controller) { + controller->window().activate(); + } else if (const auto window = Core::App().activeWindow()) { + window->activate(); + } + return true; +} + } // namespace const std::vector &LocalUrlHandlers() { @@ -864,6 +884,10 @@ const std::vector &LocalUrlHandlers() { u"premium_offer/?(\\?.+)?(#|$)"_q, ResolvePremiumOffer, }, + { + u"^login/?(\\?code=([0-9]+))(&|$)"_q, + ResolveLoginCode + }, { u"^([^\\?]+)(\\?|#|$)"_q, HandleUnknown diff --git a/Telegram/SourceFiles/intro/intro_code.cpp b/Telegram/SourceFiles/intro/intro_code.cpp index 3d4eab1acd..360fbc9772 100644 --- a/Telegram/SourceFiles/intro/intro_code.cpp +++ b/Telegram/SourceFiles/intro/intro_code.cpp @@ -105,6 +105,11 @@ CodeWidget::CodeWidget( ? rpl::single(Ui::FormatPhone(getData()->phone)) : tr::lng_intro_fragment_title()); updateDescText(); + + account->setHandleLoginCode([=](const QString &code) { + _code->setText(code); + submitCode(); + }); } void CodeWidget::refreshLang() { @@ -217,6 +222,7 @@ void CodeWidget::activate() { void CodeWidget::finished() { Step::finished(); + account().setHandleLoginCode(nullptr); _checkRequestTimer.cancel(); _callTimer.cancel(); apiClear(); diff --git a/Telegram/SourceFiles/main/main_account.cpp b/Telegram/SourceFiles/main/main_account.cpp index 6de1954c4d..adef5d1c33 100644 --- a/Telegram/SourceFiles/main/main_account.cpp +++ b/Telegram/SourceFiles/main/main_account.cpp @@ -602,6 +602,16 @@ void Account::destroyStaleAuthorizationKeys() { } } +void Account::setHandleLoginCode(Fn callback) { + _handleLoginCode = std::move(callback); +} + +void Account::handleLoginCode(const QString &code) const { + if (_handleLoginCode) { + _handleLoginCode(code); + } +} + void Account::resetAuthorizationKeys() { Expects(_mtp != nullptr); diff --git a/Telegram/SourceFiles/main/main_account.h b/Telegram/SourceFiles/main/main_account.h index f4978655d2..ba09159457 100644 --- a/Telegram/SourceFiles/main/main_account.h +++ b/Telegram/SourceFiles/main/main_account.h @@ -110,6 +110,9 @@ public: void suggestMainDcId(MTP::DcId mainDcId); void destroyStaleAuthorizationKeys(); + void setHandleLoginCode(Fn callback); + void handleLoginCode(const QString &code) const; + [[nodiscard]] rpl::lifetime &lifetime() { return _lifetime; } @@ -152,6 +155,8 @@ private: std::unique_ptr _session; rpl::variable _sessionValue; + Fn _handleLoginCode = nullptr; + UserId _sessionUserId = 0; QByteArray _sessionUserSerialized; int32 _sessionUserStreamVersion = 0; diff --git a/Telegram/SourceFiles/passport/passport_panel_controller.cpp b/Telegram/SourceFiles/passport/passport_panel_controller.cpp index d3a39468c1..e180834f3b 100644 --- a/Telegram/SourceFiles/passport/passport_panel_controller.cpp +++ b/Telegram/SourceFiles/passport/passport_panel_controller.cpp @@ -7,6 +7,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "passport/passport_panel_controller.h" +#include "main/main_account.h" +#include "main/main_session.h" #include "lang/lang_keys.h" #include "passport/passport_panel_edit_document.h" #include "passport/passport_panel_edit_contact.h" @@ -1319,12 +1321,16 @@ void PanelController::processVerificationNeeded( }); const auto box = [&] { if (type == Value::Type::Phone) { - return show(VerifyPhoneBox( + const auto submit = [=](const QString &code) { + _form->verify(value, code); + }; + const auto account = &_form->window()->session().account(); + account->setHandleLoginCode(submit); + const auto box = show(VerifyPhoneBox( text, value->verification.codeLength, value->verification.fragmentUrl, - [=](const QString &code) { _form->verify(value, code); }, - + submit, value->verification.call ? rpl::single( value->verification.call->getText() ) | rpl::then(rpl::duplicate( @@ -1340,6 +1346,11 @@ void PanelController::processVerificationNeeded( ) | rpl::map([=](not_null field) { return field->verification.error; }) | rpl::distinct_until_changed())); + box->boxClosing( + ) | rpl::start_with_next([=] { + account->setHandleLoginCode(nullptr); + }, box->lifetime()); + return box; } else if (type == Value::Type::Email) { return show(VerifyEmailBox( text,