From 90f7f482ee8ea3441cfd31fc34cc03bf481c7ca6 Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 29 Jun 2020 22:14:16 +0400 Subject: [PATCH] Take main DC and phone prefix from active account. --- Telegram/SourceFiles/intro/intro_widget.cpp | 22 ++++++++++++++++--- Telegram/SourceFiles/main/main_account.cpp | 2 +- Telegram/SourceFiles/main/main_account.h | 3 ++- Telegram/SourceFiles/main/main_domain.cpp | 5 ++++- .../SourceFiles/storage/storage_account.cpp | 2 +- 5 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Telegram/SourceFiles/intro/intro_widget.cpp b/Telegram/SourceFiles/intro/intro_widget.cpp index 2750cffb7..54181a9dd 100644 --- a/Telegram/SourceFiles/intro/intro_widget.cpp +++ b/Telegram/SourceFiles/intro/intro_widget.cpp @@ -18,7 +18,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "storage/localstorage.h" #include "main/main_account.h" #include "main/main_domain.h" +#include "main/main_session.h" #include "mainwindow.h" +#include "data/data_user.h" +#include "data/data_countries.h" #include "boxes/confirm_box.h" #include "ui/text/text_utilities.h" #include "ui/widgets/buttons.h" @@ -41,6 +44,20 @@ namespace { using namespace ::Intro::details; +[[nodiscard]] QString ComputeNewAccountCountry() { + if (const auto parent + = Core::App().domain().maybeLastOrSomeAuthedAccount()) { + if (const auto session = parent->maybeSession()) { + const auto iso = ::Data::CountryISO2ByPhone( + session->user()->phone()); + if (!iso.isEmpty()) { + return iso; + } + } + } + return Platform::SystemCountry(); +} + } // namespace Widget::Widget( @@ -65,10 +82,11 @@ Widget::Widget( rpl::single(true))) { Core::App().setDefaultFloatPlayerDelegate(floatPlayerDelegate()); - getNearestDC(); + getData()->country = ComputeNewAccountCountry(); switch (point) { case EnterPoint::Start: + getNearestDC(); appendStep(new StartWidget(this, _account, getData())); break; case EnterPoint::Phone: @@ -82,8 +100,6 @@ Widget::Widget( fixOrder(); - getData()->country = Platform::SystemCountry(); - _account->mtpValue( ) | rpl::start_with_next([=](not_null instance) { _api.emplace(instance); diff --git a/Telegram/SourceFiles/main/main_account.cpp b/Telegram/SourceFiles/main/main_account.cpp index 192dc8ac9..c2e9958d4 100644 --- a/Telegram/SourceFiles/main/main_account.cpp +++ b/Telegram/SourceFiles/main/main_account.cpp @@ -236,7 +236,7 @@ rpl::producer<> Account::mtpNewSessionCreated() const { return _mtpNewSessionCreated.events(); } -void Account::setLegacyMtpMainDcId(MTP::DcId mainDcId) { +void Account::setMtpMainDcId(MTP::DcId mainDcId) { Expects(!_mtp); _mtpFields.mainDcId = mainDcId; diff --git a/Telegram/SourceFiles/main/main_account.h b/Telegram/SourceFiles/main/main_account.h index d0e126675..d63eab11a 100644 --- a/Telegram/SourceFiles/main/main_account.h +++ b/Telegram/SourceFiles/main/main_account.h @@ -82,8 +82,9 @@ public: [[nodiscard]] rpl::producer> mtpValue() const; // Set from legacy storage. - void setLegacyMtpMainDcId(MTP::DcId mainDcId); void setLegacyMtpKey(std::shared_ptr key); + + void setMtpMainDcId(MTP::DcId mainDcId); void setSessionUserId(UserId userId); void setSessionFromStorage( std::unique_ptr data, diff --git a/Telegram/SourceFiles/main/main_domain.cpp b/Telegram/SourceFiles/main/main_domain.cpp index c61aa3533..6b8fd61cc 100644 --- a/Telegram/SourceFiles/main/main_domain.cpp +++ b/Telegram/SourceFiles/main/main_domain.cpp @@ -219,7 +219,9 @@ not_null Domain::add(MTP::Environment environment) { static const auto cloneConfig = [](const MTP::Config &config) { return std::make_unique(config); }; - static const auto accountConfig = [](not_null account) { + auto mainDcId = MTP::Instance::Fields::kNotSetMainDc; + const auto accountConfig = [&](not_null account) { + mainDcId = account->mtp().mainDcId(); return cloneConfig(account->mtp().config()); }; auto config = [&] { @@ -244,6 +246,7 @@ not_null Domain::add(MTP::Environment environment) { .account = std::make_unique(this, _dataName, index) }); const auto account = _accounts.back().account.get(); + account->setMtpMainDcId(mainDcId); _local->startAdded(account, std::move(config)); watchSession(account); _accountsChanges.fire({}); diff --git a/Telegram/SourceFiles/storage/storage_account.cpp b/Telegram/SourceFiles/storage/storage_account.cpp index 99d4d0672..1b0249032 100644 --- a/Telegram/SourceFiles/storage/storage_account.cpp +++ b/Telegram/SourceFiles/storage/storage_account.cpp @@ -853,7 +853,7 @@ std::unique_ptr Account::applyReadContext( _owner->setLegacyMtpKey(std::move(key)); } if (context.mtpLegacyMainDcId) { - _owner->setLegacyMtpMainDcId(context.mtpLegacyMainDcId); + _owner->setMtpMainDcId(context.mtpLegacyMainDcId); _owner->setSessionUserId(context.mtpLegacyUserId); } }