diff --git a/CMakeLists.txt b/CMakeLists.txt index f30a6c95b..ff647a9f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -395,6 +395,7 @@ set(TDLIB_SOURCE td/telegram/ConfigShared.cpp td/telegram/Contact.cpp td/telegram/ContactsManager.cpp + td/telegram/CountryInfoManager.cpp td/telegram/DelayDispatcher.cpp td/telegram/Dependencies.cpp td/telegram/DeviceTokenManager.cpp @@ -550,6 +551,7 @@ set(TDLIB_SOURCE td/telegram/ConfigShared.h td/telegram/Contact.h td/telegram/ContactsManager.h + td/telegram/CountryInfoManager.h td/telegram/DelayDispatcher.h td/telegram/Dependencies.h td/telegram/DeviceTokenManager.h diff --git a/td/telegram/CountryInfoManager.cpp b/td/telegram/CountryInfoManager.cpp new file mode 100644 index 000000000..8eaa5724b --- /dev/null +++ b/td/telegram/CountryInfoManager.cpp @@ -0,0 +1,54 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#include "td/telegram/CountryInfoManager.h" + +#include "td/telegram/Global.h" +#include "td/telegram/Td.h" +#include "td/telegram/telegram_api.h" + +#include "td/utils/buffer.h" +#include "td/utils/Status.h" + +namespace td { + +class GetNearestDcQuery : public Td::ResultHandler { + Promise promise_; + + public: + explicit GetNearestDcQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send() { + send_query(G()->net_query_creator().create_unauth(telegram_api::help_getNearestDc())); + } + + void on_result(uint64 id, BufferSlice packet) override { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(id, result_ptr.move_as_error()); + } + + auto result = result_ptr.move_as_ok(); + promise_.set_value(std::move(result->country_)); + } + + void on_error(uint64 id, Status status) override { + if (!G()->is_expected_error(status) && status.message() != "BOT_METHOD_INVALID") { + LOG(ERROR) << "GetNearestDc returned " << status; + } + promise_.set_error(std::move(status)); + } +}; + +CountryInfoManager::CountryInfoManager(Td *td) : td_(td) { +} + +void CountryInfoManager::get_current_country_code(Promise &&promise) { + td_->create_handler(std::move(promise))->send(); +} + +} // namespace td diff --git a/td/telegram/CountryInfoManager.h b/td/telegram/CountryInfoManager.h new file mode 100644 index 000000000..5b9b4c560 --- /dev/null +++ b/td/telegram/CountryInfoManager.h @@ -0,0 +1,27 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#pragma once + +#include "td/actor/PromiseFuture.h" + +#include "td/utils/common.h" + +namespace td { + +class Td; + +class CountryInfoManager { + public: + explicit CountryInfoManager(Td *td); + + void get_current_country_code(Promise &&promise); + + private: + Td *td_; +}; + +} // namespace td diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 97e6962ec..d583f696d 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -22,6 +22,7 @@ #include "td/telegram/ConfigManager.h" #include "td/telegram/ConfigShared.h" #include "td/telegram/ContactsManager.h" +#include "td/telegram/CountryInfoManager.h" #include "td/telegram/DeviceTokenManager.h" #include "td/telegram/DialogAdministrator.h" #include "td/telegram/DialogFilter.h" @@ -156,35 +157,6 @@ void Td::ResultHandler::send_query(NetQueryPtr query) { td->send(std::move(query)); } -class GetNearestDcQuery : public Td::ResultHandler { - Promise promise_; - - public: - explicit GetNearestDcQuery(Promise &&promise) : promise_(std::move(promise)) { - } - - void send() { - send_query(G()->net_query_creator().create_unauth(telegram_api::help_getNearestDc())); - } - - void on_result(uint64 id, BufferSlice packet) override { - auto result_ptr = fetch_result(packet); - if (result_ptr.is_error()) { - return on_error(id, result_ptr.move_as_error()); - } - - auto result = result_ptr.move_as_ok(); - promise_.set_value(std::move(result->country_)); - } - - void on_error(uint64 id, Status status) override { - if (!G()->is_expected_error(status) && status.message() != "BOT_METHOD_INVALID") { - LOG(ERROR) << "GetNearestDc returned " << status; - } - promise_.set_error(std::move(status)); - } -}; - class GetPromoDataQuery : public Td::ResultHandler { Promise> promise_; @@ -3813,8 +3785,12 @@ void Td::dec_actor_refcnt() { LOG(DEBUG) << "AuthManager was cleared" << timer; background_manager_.reset(); LOG(DEBUG) << "BackgroundManager was cleared" << timer; + callback_queries_manager_.reset(); + LOG(DEBUG) << "CallbackQueriesManager was cleared" << timer; contacts_manager_.reset(); LOG(DEBUG) << "ContactsManager was cleared" << timer; + country_info_manager_.reset(); + LOG(DEBUG) << "CountryInfoManager was cleared" << timer; documents_manager_.reset(); LOG(DEBUG) << "DocumentsManager was cleared" << timer; file_manager_.reset(); @@ -4230,7 +4206,7 @@ Status Td::init(DbKey key) { VLOG(td_init) << "Ping datacenter"; if (!auth_manager_->is_authorized()) { - send_get_nearest_dc_query(Promise()); + country_info_manager_->get_current_country_code(Promise()); } else { updates_manager_->get_difference("init"); schedule_get_terms_of_service(0); @@ -4419,6 +4395,7 @@ void Td::init_managers() { VLOG(td_init) << "Create Managers"; audios_manager_ = make_unique(this); callback_queries_manager_ = make_unique(this); + country_info_manager_ = make_unique(this); documents_manager_ = make_unique(this); video_notes_manager_ = make_unique(this); videos_manager_ = make_unique(this); @@ -4475,10 +4452,6 @@ void Td::init_managers() { "VerifyPhoneNumberManager", PhoneNumberManager::Type::VerifyPhone, create_reference()); } -void Td::send_get_nearest_dc_query(Promise promise) { - create_handler(std::move(promise))->send(); -} - void Td::send_update(tl_object_ptr &&object) { CHECK(object != nullptr); auto object_id = object->get_id(); @@ -7684,7 +7657,7 @@ void Td::on_request(uint64 id, const td_api::getCountryCode &request) { promise.set_value(make_tl_object(result.move_as_ok())); } }); - create_handler(std::move(query_promise))->send(); + country_info_manager_->get_current_country_code(std::move(query_promise)); } void Td::on_request(uint64 id, const td_api::getInviteText &request) { diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 208c02933..ba0db30ab 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -46,6 +46,7 @@ class CallManager; class CallbackQueriesManager; class ConfigManager; class ContactsManager; +class CountryInfoManager; class DeviceTokenManager; class DocumentsManager; class FileManager; @@ -136,6 +137,7 @@ class Td final : public NetQueryCallback { unique_ptr audios_manager_; unique_ptr callback_queries_manager_; + unique_ptr country_info_manager_; unique_ptr documents_manager_; unique_ptr video_notes_manager_; unique_ptr videos_manager_; @@ -1169,8 +1171,6 @@ class Td final : public NetQueryCallback { static Status fix_parameters(TdParameters ¶meters) TD_WARN_UNUSED_RESULT; Status set_parameters(td_api::object_ptr parameters) TD_WARN_UNUSED_RESULT; - void send_get_nearest_dc_query(Promise promise); - static td_api::object_ptr make_error(int32 code, CSlice error) { return td_api::make_object(code, error.str()); }