From 05325437e8351168d934f774654013eaa2f2eb4d Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 1 Aug 2024 14:13:48 +0300 Subject: [PATCH] Move promo data polling to PromoDataManager. --- td/telegram/AuthManager.cpp | 3 +- td/telegram/PromoDataManager.cpp | 128 ++++++++++++++++++++++++++ td/telegram/PromoDataManager.h | 19 ++++ td/telegram/Td.cpp | 100 -------------------- td/telegram/Td.h | 10 -- td/telegram/net/ConnectionCreator.cpp | 6 +- 6 files changed, 152 insertions(+), 114 deletions(-) diff --git a/td/telegram/AuthManager.cpp b/td/telegram/AuthManager.cpp index 9eb3f6d07..3b14ad005 100644 --- a/td/telegram/AuthManager.cpp +++ b/td/telegram/AuthManager.cpp @@ -21,6 +21,7 @@ #include "td/telegram/NotificationManager.h" #include "td/telegram/OptionManager.h" #include "td/telegram/PasswordManager.h" +#include "td/telegram/PromoDataManager.h" #include "td/telegram/ReactionManager.h" #include "td/telegram/SendCodeHelper.hpp" #include "td/telegram/StateManager.h" @@ -1267,6 +1268,7 @@ void AuthManager::on_get_authorization(tl_object_ptrdialog_filter_manager_->on_authorization_success(); // must be after MessagesManager::on_authorization_success() // to have folders created td_->notification_manager_->init(); + td_->promo_data_manager_->init(); td_->reaction_manager_->init(); td_->stickers_manager_->init(); td_->terms_of_service_manager_->init(); @@ -1275,7 +1277,6 @@ void AuthManager::on_get_authorization(tl_object_ptrupdates_manager_->get_difference("on_get_authorization"); if (!is_bot()) { td_->on_online_updated(false, true); - td_->reload_promo_data(); G()->td_db()->get_binlog_pmc()->set("fetched_marks_as_unread", "1"); } else { td_->set_is_bot_online(true); diff --git a/td/telegram/PromoDataManager.cpp b/td/telegram/PromoDataManager.cpp index 765f80b03..77a6572fa 100644 --- a/td/telegram/PromoDataManager.cpp +++ b/td/telegram/PromoDataManager.cpp @@ -6,8 +6,45 @@ // #include "td/telegram/PromoDataManager.h" +#include "td/telegram/AuthManager.h" +#include "td/telegram/DialogSource.h" +#include "td/telegram/Global.h" +#include "td/telegram/MessagesManager.h" +#include "td/telegram/Td.h" +#include "td/telegram/telegram_api.h" + +#include "td/utils/buffer.h" +#include "td/utils/misc.h" + namespace td { +class GetPromoDataQuery final : public Td::ResultHandler { + Promise> promise_; + + public: + explicit GetPromoDataQuery(Promise> &&promise) + : promise_(std::move(promise)) { + } + + void send() { + // we don't poll promo data before authorization + send_query(G()->net_query_creator().create(telegram_api::help_getPromoData())); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + promise_.set_value(result_ptr.move_as_ok()); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + PromoDataManager::PromoDataManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { } @@ -15,4 +52,95 @@ void PromoDataManager::tear_down() { parent_.reset(); } +void PromoDataManager::start_up() { + init(); +} + +void PromoDataManager::init() { + if (G()->close_flag()) { + return; + } + if (is_inited_ || !td_->auth_manager_->is_authorized() || td_->auth_manager_->is_bot()) { + return; + } + is_inited_ = true; + + reload_promo_data(); +} + +void PromoDataManager::reload_promo_data() { + if (reloading_promo_data_) { + need_reload_promo_data_ = true; + return; + } + schedule_get_promo_data(0); +} + +void PromoDataManager::schedule_get_promo_data(int32 expires_in) { + if (!is_inited_) { + return; + } + + expires_in = expires_in <= 0 ? 0 : clamp(expires_in, 60, 86400); + LOG(INFO) << "Schedule getPromoData in " << expires_in; + set_timeout_in(expires_in); +} + +void PromoDataManager::timeout_expired() { + if (G()->close_flag() || !is_inited_) { + return; + } + + reloading_promo_data_ = true; + auto promise = PromiseCreator::lambda( + [actor_id = actor_id(this)](Result> result) { + send_closure(actor_id, &PromoDataManager::on_get_promo_data, std::move(result), false); + }); + td_->create_handler(std::move(promise))->send(); +} + +void PromoDataManager::on_get_promo_data(Result> r_promo_data, + bool dummy) { + if (G()->close_flag()) { + return; + } + reloading_promo_data_ = false; + + if (r_promo_data.is_error()) { + LOG(ERROR) << "Receive error for GetPromoData: " << r_promo_data.error(); + return schedule_get_promo_data(60); + } + + auto promo_data_ptr = r_promo_data.move_as_ok(); + CHECK(promo_data_ptr != nullptr); + LOG(DEBUG) << "Receive " << to_string(promo_data_ptr); + int32 expires_at = 0; + switch (promo_data_ptr->get_id()) { + case telegram_api::help_promoDataEmpty::ID: { + auto promo = telegram_api::move_object_as(promo_data_ptr); + expires_at = promo->expires_; + td_->messages_manager_->remove_sponsored_dialog(); + break; + } + case telegram_api::help_promoData::ID: { + auto promo = telegram_api::move_object_as(promo_data_ptr); + expires_at = promo->expires_; + bool is_proxy = promo->proxy_; + td_->messages_manager_->on_get_sponsored_dialog( + std::move(promo->peer_), + is_proxy ? DialogSource::mtproto_proxy() + : DialogSource::public_service_announcement(promo->psa_type_, promo->psa_message_), + std::move(promo->users_), std::move(promo->chats_)); + break; + } + default: + UNREACHABLE(); + } + if (need_reload_promo_data_) { + need_reload_promo_data_ = false; + expires_at = 0; + } + schedule_get_promo_data(expires_at == 0 ? 0 : expires_at - G()->unix_time()); +} + } // namespace td diff --git a/td/telegram/PromoDataManager.h b/td/telegram/PromoDataManager.h index 09e6baace..e749477da 100644 --- a/td/telegram/PromoDataManager.h +++ b/td/telegram/PromoDataManager.h @@ -6,9 +6,12 @@ // #pragma once +#include "td/telegram/telegram_api.h" + #include "td/actor/actor.h" #include "td/utils/common.h" +#include "td/utils/Status.h" namespace td { @@ -18,11 +21,27 @@ class PromoDataManager final : public Actor { public: PromoDataManager(Td *td, ActorShared<> parent); + void init(); + + void reload_promo_data(); + private: void tear_down() final; + void start_up() final; + + void timeout_expired() final; + + void on_get_promo_data(Result> r_promo_data, bool dummy); + + void schedule_get_promo_data(int32 expires_in); + Td *td_; ActorShared<> parent_; + + bool is_inited_ = false; + bool reloading_promo_data_ = false; + bool need_reload_promo_data_ = false; }; } // namespace td diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 8129d6b47..f2b520025 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -57,7 +57,6 @@ #include "td/telegram/DialogParticipant.h" #include "td/telegram/DialogParticipantFilter.h" #include "td/telegram/DialogParticipantManager.h" -#include "td/telegram/DialogSource.h" #include "td/telegram/DocumentsManager.h" #include "td/telegram/DownloadManager.h" #include "td/telegram/DownloadManagerCallback.h" @@ -210,33 +209,6 @@ void Td::ResultHandler::send_query(NetQueryPtr query) { G()->net_query_dispatcher().dispatch(std::move(query)); } -class GetPromoDataQuery final : public Td::ResultHandler { - Promise> promise_; - - public: - explicit GetPromoDataQuery(Promise> &&promise) - : promise_(std::move(promise)) { - } - - void send() { - // we don't poll promo data before authorization - send_query(G()->net_query_creator().create(telegram_api::help_getPromoData())); - } - - void on_result(BufferSlice packet) final { - auto result_ptr = fetch_result(packet); - if (result_ptr.is_error()) { - return on_error(result_ptr.move_as_error()); - } - - promise_.set_value(result_ptr.move_as_ok()); - } - - void on_error(Status status) final { - promise_.set_error(std::move(status)); - } -}; - class GetRecentMeUrlsQuery final : public Td::ResultHandler { Promise> promise_; @@ -2256,17 +2228,6 @@ void Td::on_alarm_timeout(int64 alarm_id) { } return; } - if (alarm_id == PROMO_DATA_ALARM_ID) { - if (!close_flag_ && !auth_manager_->is_bot()) { - reloading_promo_data_ = true; - auto promise = PromiseCreator::lambda( - [actor_id = actor_id(this)](Result> result) { - send_closure(actor_id, &Td::on_get_promo_data, std::move(result), false); - }); - create_handler(std::move(promise))->send(); - } - return; - } if (close_flag_ >= 2) { // pending_alarms_ was already cleared return; @@ -2308,65 +2269,6 @@ void Td::on_update_status_success(bool is_online) { } } -void Td::on_get_promo_data(Result> r_promo_data, bool dummy) { - if (G()->close_flag()) { - return; - } - reloading_promo_data_ = false; - - if (r_promo_data.is_error()) { - LOG(ERROR) << "Receive error for GetPromoData: " << r_promo_data.error(); - return schedule_get_promo_data(60); - } - - auto promo_data_ptr = r_promo_data.move_as_ok(); - CHECK(promo_data_ptr != nullptr); - LOG(DEBUG) << "Receive " << to_string(promo_data_ptr); - int32 expires_at = 0; - switch (promo_data_ptr->get_id()) { - case telegram_api::help_promoDataEmpty::ID: { - auto promo = telegram_api::move_object_as(promo_data_ptr); - expires_at = promo->expires_; - messages_manager_->remove_sponsored_dialog(); - break; - } - case telegram_api::help_promoData::ID: { - auto promo = telegram_api::move_object_as(promo_data_ptr); - expires_at = promo->expires_; - bool is_proxy = promo->proxy_; - messages_manager_->on_get_sponsored_dialog( - std::move(promo->peer_), - is_proxy ? DialogSource::mtproto_proxy() - : DialogSource::public_service_announcement(promo->psa_type_, promo->psa_message_), - std::move(promo->users_), std::move(promo->chats_)); - break; - } - default: - UNREACHABLE(); - } - if (need_reload_promo_data_) { - need_reload_promo_data_ = false; - expires_at = 0; - } - schedule_get_promo_data(expires_at == 0 ? 0 : expires_at - G()->unix_time()); -} - -void Td::reload_promo_data() { - if (reloading_promo_data_) { - need_reload_promo_data_ = true; - return; - } - schedule_get_promo_data(0); -} - -void Td::schedule_get_promo_data(int32 expires_in) { - expires_in = expires_in <= 0 ? 0 : clamp(expires_in, 60, 86400); - if (!close_flag_ && auth_manager_->is_authorized() && !auth_manager_->is_bot()) { - LOG(INFO) << "Schedule getPromoData in " << expires_in; - alarm_timeout_.set_timeout_in(PROMO_DATA_ALARM_ID, expires_in); - } -} - bool Td::is_online() const { return is_online_; } @@ -2924,7 +2826,6 @@ void Td::clear() { alarm_timeout_.cancel_timeout(ONLINE_ALARM_ID); } alarm_timeout_.cancel_timeout(PING_SERVER_ALARM_ID); - alarm_timeout_.cancel_timeout(PROMO_DATA_ALARM_ID); auto reset_actor = [&timer](ActorOwn actor) { if (!actor.empty()) { @@ -3232,7 +3133,6 @@ void Td::init(Parameters parameters, Result r_opened_datab country_info_manager_->get_current_country_code(Promise()); } else { updates_manager_->get_difference("init"); - reload_promo_data(); } complete_pending_preauthentication_requests([](int32 id) { return true; }); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 47d6e6ef9..6abf046df 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -137,8 +137,6 @@ class Td final : public Actor { void destroy(); - void reload_promo_data(); - void on_update(telegram_api::object_ptr updates, uint64 auth_key_id); void on_result(NetQueryPtr query); @@ -339,7 +337,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 PROMO_DATA_ALARM_ID = -3; void on_connection_state_changed(ConnectionState new_state); @@ -389,9 +386,6 @@ class Td final : public Actor { bool can_ignore_background_updates_ = false; - bool reloading_promo_data_ = false; - bool need_reload_promo_data_ = false; - bool is_online_ = false; bool is_bot_online_ = false; NetQueryRef update_status_query_; @@ -424,8 +418,6 @@ class Td final : public Actor { void on_alarm_timeout(int64 alarm_id); - void on_get_promo_data(Result> r_promo_data, bool dummy); - template friend class RequestActor; // uses send_result/send_error friend class AuthManager; // uses send_result/send_error, TODO pass Promise<> @@ -449,8 +441,6 @@ class Td final : public Actor { std::shared_ptr old_context_; - void schedule_get_promo_data(int32 expires_in); - static int *get_log_verbosity_level(Slice name); template diff --git a/td/telegram/net/ConnectionCreator.cpp b/td/telegram/net/ConnectionCreator.cpp index 68018bc66..bc33ef27e 100644 --- a/td/telegram/net/ConnectionCreator.cpp +++ b/td/telegram/net/ConnectionCreator.cpp @@ -15,8 +15,8 @@ #include "td/telegram/net/NetQueryDispatcher.h" #include "td/telegram/net/NetType.h" #include "td/telegram/net/PublicRsaKeySharedMain.h" +#include "td/telegram/PromoDataManager.h" #include "td/telegram/StateManager.h" -#include "td/telegram/Td.h" #include "td/telegram/TdDb.h" #include "td/mtproto/DhCallback.h" @@ -559,7 +559,7 @@ void ConnectionCreator::enable_proxy_impl(int32 proxy_id) { void ConnectionCreator::disable_proxy_impl() { if (active_proxy_id_ == 0) { send_closure(G()->messages_manager(), &MessagesManager::remove_sponsored_dialog); - send_closure(G()->td(), &Td::reload_promo_data); + send_closure(G()->promo_data_manager(), &PromoDataManager::reload_promo_data); return; } CHECK(proxies_.count(active_proxy_id_) == 1); @@ -594,7 +594,7 @@ void ConnectionCreator::on_proxy_changed(bool from_db) { if (active_proxy_id_ == 0 || !from_db) { send_closure(G()->messages_manager(), &MessagesManager::remove_sponsored_dialog); } - send_closure(G()->td(), &Td::reload_promo_data); + send_closure(G()->promo_data_manager(), &PromoDataManager::reload_promo_data); loop(); }