Move terms of service polling to TermsOfServiceManager.

This commit is contained in:
levlam 2024-08-01 13:11:59 +03:00
parent 307e934d5e
commit 34a27ad27a
5 changed files with 112 additions and 75 deletions

View File

@ -28,6 +28,7 @@
#include "td/telegram/Td.h"
#include "td/telegram/TdDb.h"
#include "td/telegram/telegram_api.h"
#include "td/telegram/TermsOfServiceManager.h"
#include "td/telegram/ThemeManager.h"
#include "td/telegram/TopDialogManager.h"
#include "td/telegram/UpdatesManager.h"
@ -1267,12 +1268,12 @@ void AuthManager::on_get_authorization(tl_object_ptr<telegram_api::auth_Authoriz
td_->notification_manager_->init();
td_->reaction_manager_->init();
td_->stickers_manager_->init();
td_->terms_of_service_manager_->init();
td_->theme_manager_->init();
td_->top_dialog_manager_->init();
td_->updates_manager_->get_difference("on_get_authorization");
if (!is_bot()) {
td_->on_online_updated(false, true);
td_->schedule_get_terms_of_service(0);
td_->reload_promo_data();
G()->td_db()->get_binlog_pmc()->set("fetched_marks_as_unread", "1");
} else {

View File

@ -2255,15 +2255,6 @@ void Td::on_alarm_timeout(int64 alarm_id) {
}
return;
}
if (alarm_id == TERMS_OF_SERVICE_ALARM_ID) {
if (!close_flag_ && !auth_manager_->is_bot()) {
terms_of_service_manager_->get_terms_of_service(
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);
}));
}
return;
}
if (alarm_id == PROMO_DATA_ALARM_ID) {
if (!close_flag_ && !auth_manager_->is_bot()) {
reloading_promo_data_ = true;
@ -2316,44 +2307,6 @@ void Td::on_update_status_success(bool is_online) {
}
}
td_api::object_ptr<td_api::updateTermsOfService> Td::get_update_terms_of_service_object() const {
auto terms_of_service = pending_terms_of_service_.get_terms_of_service_object();
if (terms_of_service == nullptr) {
return nullptr;
}
return td_api::make_object<td_api::updateTermsOfService>(pending_terms_of_service_.get_id().str(),
std::move(terms_of_service));
}
void Td::on_get_terms_of_service(Result<std::pair<int32, TermsOfService>> result, bool dummy) {
int32 expires_in = 0;
if (result.is_error()) {
expires_in = Random::fast(10, 60);
} else {
auto terms = result.move_as_ok();
pending_terms_of_service_ = std::move(terms.second);
auto update = get_update_terms_of_service_object();
if (update == nullptr) {
expires_in = min(max(terms.first, G()->unix_time() + 3600) - G()->unix_time(), 86400);
} else {
send_update(std::move(update));
}
}
if (expires_in > 0) {
schedule_get_terms_of_service(expires_in);
}
}
void Td::schedule_get_terms_of_service(int32 expires_in) {
if (expires_in == 0) {
// drop pending Terms of Service after successful accept
pending_terms_of_service_ = TermsOfService();
}
if (!close_flag_ && !auth_manager_->is_bot()) {
alarm_timeout_.set_timeout_in(TERMS_OF_SERVICE_ALARM_ID, expires_in);
}
}
void Td::on_get_promo_data(Result<telegram_api::object_ptr<telegram_api::help_PromoData>> r_promo_data, bool dummy) {
if (G()->close_flag()) {
return;
@ -2969,7 +2922,6 @@ void Td::clear() {
alarm_timeout_.cancel_timeout(ONLINE_ALARM_ID);
}
alarm_timeout_.cancel_timeout(PING_SERVER_ALARM_ID);
alarm_timeout_.cancel_timeout(TERMS_OF_SERVICE_ALARM_ID);
alarm_timeout_.cancel_timeout(PROMO_DATA_ALARM_ID);
auto reset_actor = [&timer](ActorOwn<Actor> actor) {
@ -3277,7 +3229,6 @@ void Td::init(Parameters parameters, Result<TdDb::OpenedDatabase> r_opened_datab
country_info_manager_->get_current_country_code(Promise<string>());
} else {
updates_manager_->get_difference("init");
schedule_get_terms_of_service(0);
reload_promo_data();
}
@ -4004,16 +3955,13 @@ void Td::on_request(uint64 id, const td_api::getCurrentState &request) {
business_connection_manager_->get_current_state(updates);
terms_of_service_manager_->get_current_state(updates);
// TODO updateFileGenerationStart generation_id:int64 original_path:string destination_path:string conversion:string = Update;
// TODO updateCall call:call = Update;
// TODO updateGroupCall call:groupCall = Update;
}
auto update_terms_of_service = get_update_terms_of_service_object();
if (update_terms_of_service != nullptr) {
updates.push_back(std::move(update_terms_of_service));
}
// send response synchronously to prevent "Request aborted" or other changes of the current state
send_result(id, td_api::make_object<td_api::updates>(std::move(updates)));
}
@ -9414,14 +9362,7 @@ void Td::on_request(uint64 id, const td_api::getBusinessFeatures &request) {
void Td::on_request(uint64 id, td_api::acceptTermsOfService &request) {
CHECK_IS_USER();
CLEAN_INPUT_STRING(request.terms_of_service_id_);
auto promise = PromiseCreator::lambda([actor_id = actor_id(this), id](Result<> result) {
if (result.is_error()) {
send_closure(actor_id, &Td::send_error, id, result.move_as_error());
} else {
send_closure(actor_id, &Td::send_result, id, td_api::make_object<td_api::ok>());
send_closure(actor_id, &Td::schedule_get_terms_of_service, 0);
}
});
CREATE_OK_REQUEST_PROMISE();
terms_of_service_manager_->accept_terms_of_service(std::move(request.terms_of_service_id_), std::move(promise));
}

View File

@ -15,7 +15,6 @@
#include "td/telegram/TdCallback.h"
#include "td/telegram/TdDb.h"
#include "td/telegram/telegram_api.h"
#include "td/telegram/TermsOfService.h"
#include "td/actor/actor.h"
#include "td/actor/MultiTimeout.h"
@ -137,8 +136,6 @@ class Td final : public Actor {
void destroy();
void schedule_get_terms_of_service(int32 expires_in);
void reload_promo_data();
void on_update(telegram_api::object_ptr<telegram_api::Updates> updates, uint64 auth_key_id);
@ -339,7 +336,6 @@ class Td final : public Actor {
static constexpr int64 ONLINE_ALARM_ID = 0;
static constexpr int64 PING_SERVER_ALARM_ID = -1;
static constexpr int32 PING_SERVER_TIMEOUT = 300;
static constexpr int64 TERMS_OF_SERVICE_ALARM_ID = -2;
static constexpr int64 PROMO_DATA_ALARM_ID = -3;
void on_connection_state_changed(ConnectionState new_state);
@ -401,8 +397,6 @@ class Td final : public Actor {
FlatHashMap<int64, uint64> pending_alarms_;
MultiTimeout alarm_timeout_{"AlarmTimeout"};
TermsOfService pending_terms_of_service_;
struct DownloadInfo {
int64 offset = -1;
int64 limit = -1;
@ -424,12 +418,9 @@ class Td final : public Actor {
vector<td_api::object_ptr<td_api::Update>> get_fake_current_state() const;
static void on_alarm_timeout_callback(void *td_ptr, int64 alarm_id);
void on_alarm_timeout(int64 alarm_id);
td_api::object_ptr<td_api::updateTermsOfService> get_update_terms_of_service_object() const;
void on_get_terms_of_service(Result<std::pair<int32, TermsOfService>> result, bool dummy);
void on_get_promo_data(Result<telegram_api::object_ptr<telegram_api::help_PromoData>> r_promo_data, bool dummy);
template <class T>

View File

@ -6,6 +6,7 @@
//
#include "td/telegram/TermsOfServiceManager.h"
#include "td/telegram/AuthManager.h"
#include "td/telegram/Global.h"
#include "td/telegram/net/NetQueryCreator.h"
#include "td/telegram/Td.h"
@ -13,11 +14,11 @@
#include "td/utils/buffer.h"
#include "td/utils/logging.h"
#include "td/utils/Random.h"
#include "td/utils/Status.h"
namespace td {
class GetTermsOfServiceUpdateQuery final : public Td::ResultHandler {
Promise<std::pair<int32, TermsOfService>> promise_;
@ -81,6 +82,7 @@ class AcceptTermsOfServiceQuery final : public Td::ResultHandler {
if (!result) {
LOG(ERROR) << "Failed to accept terms of service";
}
td_->terms_of_service_manager_->schedule_get_terms_of_service(0);
promise_.set_value(Unit());
}
@ -96,6 +98,78 @@ void TermsOfServiceManager::tear_down() {
parent_.reset();
}
void TermsOfServiceManager::start_up() {
init();
}
void TermsOfServiceManager::init() {
if (G()->close_flag()) {
return;
}
if (is_inited_ || !td_->auth_manager_->is_authorized() || td_->auth_manager_->is_bot()) {
return;
}
is_inited_ = true;
schedule_get_terms_of_service(0);
}
void TermsOfServiceManager::schedule_get_terms_of_service(int32 expires_in) {
if (G()->close_flag() || !is_inited_) {
return;
}
if (expires_in == 0) {
// drop pending Terms of Service after successful accept
pending_terms_of_service_ = TermsOfService();
}
set_timeout_in(expires_in);
}
void TermsOfServiceManager::timeout_expired() {
if (G()->close_flag() || !is_inited_) {
return;
}
get_terms_of_service(
PromiseCreator::lambda([actor_id = actor_id(this)](Result<std::pair<int32, TermsOfService>> result) {
send_closure(actor_id, &TermsOfServiceManager::on_get_terms_of_service, std::move(result), false);
}));
}
td_api::object_ptr<td_api::updateTermsOfService> TermsOfServiceManager::get_update_terms_of_service_object() const {
auto terms_of_service = pending_terms_of_service_.get_terms_of_service_object();
if (terms_of_service == nullptr) {
return nullptr;
}
return td_api::make_object<td_api::updateTermsOfService>(pending_terms_of_service_.get_id().str(),
std::move(terms_of_service));
}
void TermsOfServiceManager::on_get_terms_of_service(Result<std::pair<int32, TermsOfService>> result, bool dummy) {
if (G()->close_flag()) {
return;
}
CHECK(is_inited_);
int32 expires_in = 0;
if (result.is_error()) {
expires_in = Random::fast(10, 60);
} else {
auto terms = result.move_as_ok();
pending_terms_of_service_ = std::move(terms.second);
auto update = get_update_terms_of_service_object();
if (update == nullptr) {
expires_in = min(max(terms.first, G()->unix_time() + 3600) - G()->unix_time(), 86400);
} else {
send_closure(G()->td(), &Td::send_update, std::move(update));
}
}
if (expires_in > 0) {
schedule_get_terms_of_service(expires_in);
}
}
void TermsOfServiceManager::get_terms_of_service(Promise<std::pair<int32, TermsOfService>> promise) {
td_->create_handler<GetTermsOfServiceUpdateQuery>(std::move(promise))->send();
}
@ -104,4 +178,15 @@ void TermsOfServiceManager::accept_terms_of_service(string &&terms_of_service_id
td_->create_handler<AcceptTermsOfServiceQuery>(std::move(promise))->send(terms_of_service_id);
}
void TermsOfServiceManager::get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const {
if (!is_inited_) {
return;
}
auto update_terms_of_service_object = get_update_terms_of_service_object();
if (update_terms_of_service_object != nullptr) {
updates.push_back(std::move(update_terms_of_service_object));
}
}
} // namespace td

View File

@ -6,6 +6,7 @@
//
#pragma once
#include "td/telegram/td_api.h"
#include "td/telegram/TermsOfService.h"
#include "td/actor/actor.h"
@ -23,15 +24,33 @@ class TermsOfServiceManager final : public Actor {
public:
TermsOfServiceManager(Td *td, ActorShared<> parent);
void get_terms_of_service(Promise<std::pair<int32, TermsOfService>> promise);
void init();
void accept_terms_of_service(string &&terms_of_service_id, Promise<Unit> &&promise);
void schedule_get_terms_of_service(int32 expires_in);
void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const;
private:
void tear_down() final;
void start_up() final;
void timeout_expired() final;
void get_terms_of_service(Promise<std::pair<int32, TermsOfService>> promise);
td_api::object_ptr<td_api::updateTermsOfService> get_update_terms_of_service_object() const;
void on_get_terms_of_service(Result<std::pair<int32, TermsOfService>> result, bool dummy);
Td *td_;
ActorShared<> parent_;
TermsOfService pending_terms_of_service_;
bool is_inited_ = false;
};
} // namespace td