Add CountryInfoManager.

GitOrigin-RevId: cdd8cab21bac58e6162cae7b806aef8dfe400b81
This commit is contained in:
levlam 2020-08-24 00:43:31 +03:00
parent 7bf004c8c3
commit 59265a41f5
5 changed files with 93 additions and 37 deletions

View File

@ -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

View File

@ -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<string> promise_;
public:
explicit GetNearestDcQuery(Promise<string> &&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<telegram_api::help_getNearestDc>(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<string> &&promise) {
td_->create_handler<GetNearestDcQuery>(std::move(promise))->send();
}
} // namespace td

View File

@ -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<string> &&promise);
private:
Td *td_;
};
} // namespace td

View File

@ -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<string> promise_;
public:
explicit GetNearestDcQuery(Promise<string> &&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<telegram_api::help_getNearestDc>(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<telegram_api::object_ptr<telegram_api::help_PromoData>> 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<string>());
country_info_manager_->get_current_country_code(Promise<string>());
} 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<AudiosManager>(this);
callback_queries_manager_ = make_unique<CallbackQueriesManager>(this);
country_info_manager_ = make_unique<CountryInfoManager>(this);
documents_manager_ = make_unique<DocumentsManager>(this);
video_notes_manager_ = make_unique<VideoNotesManager>(this);
videos_manager_ = make_unique<VideosManager>(this);
@ -4475,10 +4452,6 @@ void Td::init_managers() {
"VerifyPhoneNumberManager", PhoneNumberManager::Type::VerifyPhone, create_reference());
}
void Td::send_get_nearest_dc_query(Promise<string> promise) {
create_handler<GetNearestDcQuery>(std::move(promise))->send();
}
void Td::send_update(tl_object_ptr<td_api::Update> &&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<td_api::text>(result.move_as_ok()));
}
});
create_handler<GetNearestDcQuery>(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) {

View File

@ -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<AudiosManager> audios_manager_;
unique_ptr<CallbackQueriesManager> callback_queries_manager_;
unique_ptr<CountryInfoManager> country_info_manager_;
unique_ptr<DocumentsManager> documents_manager_;
unique_ptr<VideoNotesManager> video_notes_manager_;
unique_ptr<VideosManager> videos_manager_;
@ -1169,8 +1171,6 @@ class Td final : public NetQueryCallback {
static Status fix_parameters(TdParameters &parameters) TD_WARN_UNUSED_RESULT;
Status set_parameters(td_api::object_ptr<td_api::tdlibParameters> parameters) TD_WARN_UNUSED_RESULT;
void send_get_nearest_dc_query(Promise<string> promise);
static td_api::object_ptr<td_api::error> make_error(int32 code, CSlice error) {
return td_api::make_object<td_api::error>(code, error.str());
}