Move requests to TermsOfServiceManager.

This commit is contained in:
levlam 2024-08-01 11:19:52 +03:00
parent 01755c8fea
commit 307e934d5e
5 changed files with 101 additions and 100 deletions

View File

@ -2257,8 +2257,8 @@ void Td::on_alarm_timeout(int64 alarm_id) {
} }
if (alarm_id == TERMS_OF_SERVICE_ALARM_ID) { if (alarm_id == TERMS_OF_SERVICE_ALARM_ID) {
if (!close_flag_ && !auth_manager_->is_bot()) { if (!close_flag_ && !auth_manager_->is_bot()) {
get_terms_of_service( terms_of_service_manager_->get_terms_of_service(
this, PromiseCreator::lambda([actor_id = actor_id(this)](Result<std::pair<int32, TermsOfService>> result) { PromiseCreator::lambda([actor_id = actor_id(this)](Result<std::pair<int32, TermsOfService>> result) {
send_closure(actor_id, &Td::on_get_terms_of_service, std::move(result), false); send_closure(actor_id, &Td::on_get_terms_of_service, std::move(result), false);
})); }));
} }
@ -9422,7 +9422,7 @@ void Td::on_request(uint64 id, td_api::acceptTermsOfService &request) {
send_closure(actor_id, &Td::schedule_get_terms_of_service, 0); send_closure(actor_id, &Td::schedule_get_terms_of_service, 0);
} }
}); });
accept_terms_of_service(this, std::move(request.terms_of_service_id_), std::move(promise)); terms_of_service_manager_->accept_terms_of_service(std::move(request.terms_of_service_id_), std::move(promise));
} }
void Td::on_request(uint64 id, const td_api::getCountries &request) { void Td::on_request(uint64 id, const td_api::getCountries &request) {

View File

@ -6,88 +6,8 @@
// //
#include "td/telegram/TermsOfService.h" #include "td/telegram/TermsOfService.h"
#include "td/telegram/Global.h"
#include "td/telegram/net/NetQueryCreator.h"
#include "td/telegram/Td.h"
#include "td/telegram/telegram_api.h"
#include "td/utils/buffer.h"
#include "td/utils/logging.h"
#include "td/utils/Status.h"
namespace td { namespace td {
class GetTermsOfServiceUpdateQuery final : public Td::ResultHandler {
Promise<std::pair<int32, TermsOfService>> promise_;
public:
explicit GetTermsOfServiceUpdateQuery(Promise<std::pair<int32, TermsOfService>> &&promise)
: promise_(std::move(promise)) {
}
void send() {
// we don't poll terms of service before authorization
send_query(G()->net_query_creator().create(telegram_api::help_getTermsOfServiceUpdate()));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::help_getTermsOfServiceUpdate>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto result = result_ptr.move_as_ok();
switch (result->get_id()) {
case telegram_api::help_termsOfServiceUpdateEmpty::ID: {
auto update = move_tl_object_as<telegram_api::help_termsOfServiceUpdateEmpty>(result);
promise_.set_value(std::make_pair(update->expires_, TermsOfService()));
break;
}
case telegram_api::help_termsOfServiceUpdate::ID: {
auto update = move_tl_object_as<telegram_api::help_termsOfServiceUpdate>(result);
promise_.set_value(std::make_pair(update->expires_, TermsOfService(std::move(update->terms_of_service_))));
break;
}
default:
UNREACHABLE();
}
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
class AcceptTermsOfServiceQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
public:
explicit AcceptTermsOfServiceQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(const string &terms_of_service_id) {
send_query(G()->net_query_creator().create(telegram_api::help_acceptTermsOfService(
telegram_api::make_object<telegram_api::dataJSON>(terms_of_service_id))));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::help_acceptTermsOfService>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto result = result_ptr.ok();
if (!result) {
LOG(ERROR) << "Failed to accept terms of service";
}
promise_.set_value(Unit());
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
TermsOfService::TermsOfService(telegram_api::object_ptr<telegram_api::help_termsOfService> terms) { TermsOfService::TermsOfService(telegram_api::object_ptr<telegram_api::help_termsOfService> terms) {
if (terms == nullptr) { if (terms == nullptr) {
return; return;
@ -103,12 +23,4 @@ TermsOfService::TermsOfService(telegram_api::object_ptr<telegram_api::help_terms
show_popup_ = terms->popup_; show_popup_ = terms->popup_;
} }
void get_terms_of_service(Td *td, Promise<std::pair<int32, TermsOfService>> promise) {
td->create_handler<GetTermsOfServiceUpdateQuery>(std::move(promise))->send();
}
void accept_terms_of_service(Td *td, string &&terms_of_service_id, Promise<Unit> &&promise) {
td->create_handler<AcceptTermsOfServiceQuery>(std::move(promise))->send(terms_of_service_id);
}
} // namespace td } // namespace td

View File

@ -12,16 +12,11 @@
#include "td/telegram/telegram_api.h" #include "td/telegram/telegram_api.h"
#include "td/utils/common.h" #include "td/utils/common.h"
#include "td/utils/Promise.h"
#include "td/utils/Slice.h" #include "td/utils/Slice.h"
#include "td/utils/tl_helpers.h" #include "td/utils/tl_helpers.h"
#include <utility>
namespace td { namespace td {
class Td;
class TermsOfService { class TermsOfService {
string id_; string id_;
FormattedText text_; FormattedText text_;
@ -67,8 +62,4 @@ class TermsOfService {
} }
}; };
void get_terms_of_service(Td *td, Promise<std::pair<int32, TermsOfService>> promise);
void accept_terms_of_service(Td *td, string &&terms_of_service_id, Promise<Unit> &&promise);
} // namespace td } // namespace td

View File

@ -6,8 +6,89 @@
// //
#include "td/telegram/TermsOfServiceManager.h" #include "td/telegram/TermsOfServiceManager.h"
#include "td/telegram/Global.h"
#include "td/telegram/net/NetQueryCreator.h"
#include "td/telegram/Td.h"
#include "td/telegram/telegram_api.h"
#include "td/utils/buffer.h"
#include "td/utils/logging.h"
#include "td/utils/Status.h"
namespace td { namespace td {
class GetTermsOfServiceUpdateQuery final : public Td::ResultHandler {
Promise<std::pair<int32, TermsOfService>> promise_;
public:
explicit GetTermsOfServiceUpdateQuery(Promise<std::pair<int32, TermsOfService>> &&promise)
: promise_(std::move(promise)) {
}
void send() {
// we don't poll terms of service before authorization
send_query(G()->net_query_creator().create(telegram_api::help_getTermsOfServiceUpdate()));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::help_getTermsOfServiceUpdate>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto result = result_ptr.move_as_ok();
switch (result->get_id()) {
case telegram_api::help_termsOfServiceUpdateEmpty::ID: {
auto update = move_tl_object_as<telegram_api::help_termsOfServiceUpdateEmpty>(result);
promise_.set_value(std::make_pair(update->expires_, TermsOfService()));
break;
}
case telegram_api::help_termsOfServiceUpdate::ID: {
auto update = move_tl_object_as<telegram_api::help_termsOfServiceUpdate>(result);
promise_.set_value(std::make_pair(update->expires_, TermsOfService(std::move(update->terms_of_service_))));
break;
}
default:
UNREACHABLE();
}
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
class AcceptTermsOfServiceQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
public:
explicit AcceptTermsOfServiceQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(const string &terms_of_service_id) {
send_query(G()->net_query_creator().create(telegram_api::help_acceptTermsOfService(
telegram_api::make_object<telegram_api::dataJSON>(terms_of_service_id))));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::help_acceptTermsOfService>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto result = result_ptr.ok();
if (!result) {
LOG(ERROR) << "Failed to accept terms of service";
}
promise_.set_value(Unit());
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
TermsOfServiceManager::TermsOfServiceManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { TermsOfServiceManager::TermsOfServiceManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
} }
@ -15,4 +96,12 @@ void TermsOfServiceManager::tear_down() {
parent_.reset(); parent_.reset();
} }
void TermsOfServiceManager::get_terms_of_service(Promise<std::pair<int32, TermsOfService>> promise) {
td_->create_handler<GetTermsOfServiceUpdateQuery>(std::move(promise))->send();
}
void TermsOfServiceManager::accept_terms_of_service(string &&terms_of_service_id, Promise<Unit> &&promise) {
td_->create_handler<AcceptTermsOfServiceQuery>(std::move(promise))->send(terms_of_service_id);
}
} // namespace td } // namespace td

View File

@ -6,9 +6,14 @@
// //
#pragma once #pragma once
#include "td/telegram/TermsOfService.h"
#include "td/actor/actor.h" #include "td/actor/actor.h"
#include "td/utils/common.h" #include "td/utils/common.h"
#include "td/utils/Promise.h"
#include <utility>
namespace td { namespace td {
@ -18,6 +23,10 @@ class TermsOfServiceManager final : public Actor {
public: public:
TermsOfServiceManager(Td *td, ActorShared<> parent); TermsOfServiceManager(Td *td, ActorShared<> parent);
void get_terms_of_service(Promise<std::pair<int32, TermsOfService>> promise);
void accept_terms_of_service(string &&terms_of_service_id, Promise<Unit> &&promise);
private: private:
void tear_down() final; void tear_down() final;