From ce679e7b78435edad34436d4d8e966b5a0403bc4 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 9 Aug 2023 13:19:54 +0300 Subject: [PATCH] Move default reaction handling to ReactionManager. --- td/telegram/ReactionManager.cpp | 68 ++++++++++++++++++++++++++++++++ td/telegram/ReactionManager.h | 4 ++ td/telegram/ReactionType.cpp | 70 --------------------------------- td/telegram/ReactionType.h | 6 --- td/telegram/Td.cpp | 6 +-- 5 files changed, 73 insertions(+), 81 deletions(-) diff --git a/td/telegram/ReactionManager.cpp b/td/telegram/ReactionManager.cpp index 830d496dd..aaf6a6afd 100644 --- a/td/telegram/ReactionManager.cpp +++ b/td/telegram/ReactionManager.cpp @@ -7,6 +7,7 @@ #include "td/telegram/ReactionManager.h" #include "td/telegram/AuthManager.h" +#include "td/telegram/ConfigManager.h" #include "td/telegram/Global.h" #include "td/telegram/logevent/LogEvent.h" #include "td/telegram/MessagesManager.h" @@ -22,6 +23,7 @@ #include "td/utils/FlatHashSet.h" #include "td/utils/logging.h" #include "td/utils/ScopeGuard.h" +#include "td/utils/Status.h" #include @@ -126,6 +128,45 @@ class ClearRecentReactionsQuery final : public Td::ResultHandler { } }; +class SetDefaultReactionQuery final : public Td::ResultHandler { + ReactionType reaction_type_; + + public: + void send(const ReactionType &reaction_type) { + reaction_type_ = reaction_type; + send_query( + G()->net_query_creator().create(telegram_api::messages_setDefaultReaction(reaction_type.get_input_reaction()))); + } + + 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()); + } + + if (!result_ptr.ok()) { + return on_error(Status::Error(400, "Receive false")); + } + + auto default_reaction = td_->option_manager_->get_option_string("default_reaction", "-"); + if (default_reaction != reaction_type_.get_string()) { + td_->reaction_manager_->send_set_default_reaction_query(); + } else { + td_->option_manager_->set_option_empty("default_reaction_needs_sync"); + } + } + + void on_error(Status status) final { + if (G()->close_flag()) { + return; + } + + LOG(INFO) << "Receive error for SetDefaultReactionQuery: " << status; + td_->option_manager_->set_option_empty("default_reaction_needs_sync"); + send_closure(G()->config_manager(), &ConfigManager::request_config, false); + } +}; + ReactionManager::ReactionManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { } @@ -148,6 +189,10 @@ void ReactionManager::init() { td_->stickers_manager_->init(); load_active_reactions(); + + if (td_->option_manager_->get_option_boolean("default_reaction_needs_sync")) { + send_set_default_reaction_query(); + } } td_api::object_ptr ReactionManager::get_emoji_reaction_object(const string &emoji) const { @@ -641,6 +686,29 @@ bool ReactionManager::is_active_reaction(const ReactionType &reaction_type) cons return false; } +void ReactionManager::set_default_reaction(ReactionType reaction_type, Promise &&promise) { + if (reaction_type.is_empty()) { + return promise.set_error(Status::Error(400, "Default reaction must be non-empty")); + } + if (!reaction_type.is_custom_reaction() && !is_active_reaction(reaction_type)) { + return promise.set_error(Status::Error(400, "Can't set incative reaction as default")); + } + + if (td_->option_manager_->get_option_string("default_reaction", "-") != reaction_type.get_string()) { + td_->option_manager_->set_option_string("default_reaction", reaction_type.get_string()); + if (!td_->option_manager_->get_option_boolean("default_reaction_needs_sync")) { + td_->option_manager_->set_option_boolean("default_reaction_needs_sync", true); + send_set_default_reaction_query(); + } + } + promise.set_value(Unit()); +} + +void ReactionManager::send_set_default_reaction_query() { + td_->create_handler()->send( + ReactionType(td_->option_manager_->get_option_string("default_reaction"))); +} + void ReactionManager::get_current_state(vector> &updates) const { if (td_->auth_manager_->is_bot()) { return; diff --git a/td/telegram/ReactionManager.h b/td/telegram/ReactionManager.h index 59e3d440a..4cb6feb80 100644 --- a/td/telegram/ReactionManager.h +++ b/td/telegram/ReactionManager.h @@ -56,6 +56,10 @@ class ReactionManager final : public Actor { void on_get_top_reactions(tl_object_ptr &&reactions_ptr); + void set_default_reaction(ReactionType reaction_type, Promise &&promise); + + void send_set_default_reaction_query(); + void get_current_state(vector> &updates) const; private: diff --git a/td/telegram/ReactionType.cpp b/td/telegram/ReactionType.cpp index f8401e2da..87ff2cdaa 100644 --- a/td/telegram/ReactionType.cpp +++ b/td/telegram/ReactionType.cpp @@ -6,67 +6,20 @@ // #include "td/telegram/ReactionType.h" -#include "td/telegram/ConfigManager.h" -#include "td/telegram/Global.h" #include "td/telegram/misc.h" -#include "td/telegram/OptionManager.h" -#include "td/telegram/ReactionManager.h" -#include "td/telegram/Td.h" #include "td/actor/actor.h" #include "td/utils/as.h" #include "td/utils/base64.h" -#include "td/utils/buffer.h" #include "td/utils/crypto.h" #include "td/utils/emoji.h" -#include "td/utils/logging.h" #include "td/utils/Slice.h" #include "td/utils/SliceBuilder.h" -#include "td/utils/Status.h" #include "td/utils/utf8.h" namespace td { -class SetDefaultReactionQuery final : public Td::ResultHandler { - ReactionType reaction_type_; - - public: - void send(const ReactionType &reaction_type) { - reaction_type_ = reaction_type; - send_query( - G()->net_query_creator().create(telegram_api::messages_setDefaultReaction(reaction_type.get_input_reaction()))); - } - - 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()); - } - - if (!result_ptr.ok()) { - return on_error(Status::Error(400, "Receive false")); - } - - auto default_reaction = td_->option_manager_->get_option_string("default_reaction", "-"); - if (default_reaction != reaction_type_.get_string()) { - send_set_default_reaction_query(td_); - } else { - td_->option_manager_->set_option_empty("default_reaction_needs_sync"); - } - } - - void on_error(Status status) final { - if (G()->close_flag()) { - return; - } - - LOG(INFO) << "Receive error for SetDefaultReactionQuery: " << status; - td_->option_manager_->set_option_empty("default_reaction_needs_sync"); - send_closure(G()->config_manager(), &ConfigManager::request_config, false); - } -}; - static int64 get_custom_emoji_id(const string &reaction) { auto r_decoded = base64_decode(Slice(&reaction[1], reaction.size() - 1)); CHECK(r_decoded.is_ok()); @@ -185,29 +138,6 @@ StringBuilder &operator<<(StringBuilder &string_builder, const ReactionType &rea return string_builder << "reaction " << reaction_type.reaction_; } -void set_default_reaction(Td *td, ReactionType reaction_type, Promise &&promise) { - if (reaction_type.is_empty()) { - return promise.set_error(Status::Error(400, "Default reaction must be non-empty")); - } - if (!reaction_type.is_custom_reaction() && !td->reaction_manager_->is_active_reaction(reaction_type)) { - return promise.set_error(Status::Error(400, "Can't set incative reaction as default")); - } - - if (td->option_manager_->get_option_string("default_reaction", "-") != reaction_type.get_string()) { - td->option_manager_->set_option_string("default_reaction", reaction_type.get_string()); - if (!td->option_manager_->get_option_boolean("default_reaction_needs_sync")) { - td->option_manager_->set_option_boolean("default_reaction_needs_sync", true); - send_set_default_reaction_query(td); - } - } - promise.set_value(Unit()); -} - -void send_set_default_reaction_query(Td *td) { - td->create_handler()->send( - ReactionType(td->option_manager_->get_option_string("default_reaction"))); -} - int64 get_reaction_types_hash(const vector &reaction_types) { vector numbers; for (auto &reaction_type : reaction_types) { diff --git a/td/telegram/ReactionType.h b/td/telegram/ReactionType.h index b551214fc..20eaff94c 100644 --- a/td/telegram/ReactionType.h +++ b/td/telegram/ReactionType.h @@ -19,8 +19,6 @@ namespace td { struct ReactionTypeHash; -class Td; - class ReactionType { string reaction_; @@ -82,10 +80,6 @@ inline bool operator!=(const ReactionType &lhs, const ReactionType &rhs) { StringBuilder &operator<<(StringBuilder &string_builder, const ReactionType &reaction_type); -void set_default_reaction(Td *td, ReactionType reaction_type, Promise &&promise); - -void send_set_default_reaction_query(Td *td); - int64 get_reaction_types_hash(const vector &reaction_types); } // namespace td diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 0236eaa0f..31cc5f952 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -3692,10 +3692,6 @@ void Td::init(Parameters parameters, Result r_opened_datab on_save_app_log_binlog_event(this, std::move(event)); } - if (option_manager_->get_option_boolean("default_reaction_needs_sync")) { - send_set_default_reaction_query(this); - } - if (is_online_) { on_online_updated(true, true); } @@ -5386,7 +5382,7 @@ void Td::on_request(uint64 id, td_api::getMessageAddedReactions &request) { void Td::on_request(uint64 id, td_api::setDefaultReactionType &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); - set_default_reaction(this, ReactionType(request.reaction_type_), std::move(promise)); + reaction_manager_->set_default_reaction(ReactionType(request.reaction_type_), std::move(promise)); } void Td::on_request(uint64 id, td_api::getMessagePublicForwards &request) {