eff3dd9f36
GitOrigin-RevId: 4e03cee13e38b682c6e063f16cd078345c0c00ff
59 lines
1.7 KiB
C++
59 lines
1.7 KiB
C++
//
|
|
// 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, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
|
|
}
|
|
|
|
void CountryInfoManager::tear_down() {
|
|
parent_.reset();
|
|
}
|
|
|
|
void CountryInfoManager::get_current_country_code(Promise<string> &&promise) {
|
|
td_->create_handler<GetNearestDcQuery>(std::move(promise))->send();
|
|
}
|
|
|
|
} // namespace td
|