From 57b935f6a737a5b912713dc24154f87df4c95501 Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 5 Aug 2023 14:59:42 +0300 Subject: [PATCH] Move more functions to ReactionType. --- td/telegram/MessageReaction.cpp | 76 ------------------------------ td/telegram/MessageReaction.h | 10 ---- td/telegram/ReactionType.cpp | 82 ++++++++++++++++++++++++++++++++- td/telegram/ReactionType.h | 13 +++++- 4 files changed, 92 insertions(+), 89 deletions(-) diff --git a/td/telegram/MessageReaction.cpp b/td/telegram/MessageReaction.cpp index 5554fc2fd..d394fb2fc 100644 --- a/td/telegram/MessageReaction.cpp +++ b/td/telegram/MessageReaction.cpp @@ -7,7 +7,6 @@ #include "td/telegram/MessageReaction.h" #include "td/telegram/AccessRights.h" -#include "td/telegram/ConfigManager.h" #include "td/telegram/ContactsManager.h" #include "td/telegram/Dependencies.h" #include "td/telegram/Global.h" @@ -15,7 +14,6 @@ #include "td/telegram/MessagesManager.h" #include "td/telegram/OptionManager.h" #include "td/telegram/ServerMessageId.h" -#include "td/telegram/StickersManager.h" #include "td/telegram/Td.h" #include "td/telegram/telegram_api.h" #include "td/telegram/UpdatesManager.h" @@ -248,45 +246,6 @@ class GetMessageReactionsListQuery 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()) { - 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); - } -}; - class ReportReactionQuery final : public Td::ResultHandler { Promise promise_; DialogId dialog_id_; @@ -943,29 +902,6 @@ void get_message_added_reactions(Td *td, FullMessageId full_message_id, Reaction ->send(full_message_id, std::move(reaction_type), std::move(offset), limit); } -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->stickers_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"))); -} - void report_message_reactions(Td *td, FullMessageId full_message_id, DialogId chooser_dialog_id, Promise &&promise) { auto dialog_id = full_message_id.get_dialog_id(); @@ -997,16 +933,4 @@ void report_message_reactions(Td *td, FullMessageId full_message_id, DialogId ch td->create_handler(std::move(promise))->send(dialog_id, message_id, chooser_dialog_id); } -vector get_recent_reactions(Td *td) { - return td->stickers_manager_->get_recent_reactions(); -} - -vector get_top_reactions(Td *td) { - return td->stickers_manager_->get_top_reactions(); -} - -void add_recent_reaction(Td *td, const ReactionType &reaction_type) { - td->stickers_manager_->add_recent_reaction(reaction_type); -} - } // namespace td diff --git a/td/telegram/MessageReaction.h b/td/telegram/MessageReaction.h index f9c761694..d39d540b1 100644 --- a/td/telegram/MessageReaction.h +++ b/td/telegram/MessageReaction.h @@ -218,17 +218,7 @@ void send_message_reaction(Td *td, FullMessageId full_message_id, vector> &&promise); -void set_default_reaction(Td *td, ReactionType reaction_type, Promise &&promise); - -void send_set_default_reaction_query(Td *td); - void report_message_reactions(Td *td, FullMessageId full_message_id, DialogId chooser_dialog_id, Promise &&promise); -vector get_recent_reactions(Td *td); - -vector get_top_reactions(Td *td); - -void add_recent_reaction(Td *td, const ReactionType &reaction_type); - } // namespace td diff --git a/td/telegram/ReactionType.cpp b/td/telegram/ReactionType.cpp index 0f85c037b..bd2fba8ec 100644 --- a/td/telegram/ReactionType.cpp +++ b/td/telegram/ReactionType.cpp @@ -6,17 +6,62 @@ // #include "td/telegram/ReactionType.h" +#include "td/telegram/ConfigManager.h" #include "td/telegram/misc.h" +#include "td/telegram/OptionManager.h" +#include "td/telegram/StickersManager.h" +#include "td/telegram/Td.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/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()); @@ -114,7 +159,7 @@ bool ReactionType::is_custom_reaction() const { bool ReactionType::is_active_reaction( const FlatHashMap &active_reaction_pos) const { - return !reaction_.empty() && (is_custom_reaction() || active_reaction_pos.count(*this) > 0); + return !is_empty() && (is_custom_reaction() || active_reaction_pos.count(*this) > 0); } bool operator<(const ReactionType &lhs, const ReactionType &rhs) { @@ -135,6 +180,41 @@ 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->stickers_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"))); +} + +vector get_recent_reactions(Td *td) { + return td->stickers_manager_->get_recent_reactions(); +} + +vector get_top_reactions(Td *td) { + return td->stickers_manager_->get_top_reactions(); +} + +void add_recent_reaction(Td *td, const ReactionType &reaction_type) { + td->stickers_manager_->add_recent_reaction(reaction_type); +} + 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 30873ddea..51a7a3c3c 100644 --- a/td/telegram/ReactionType.h +++ b/td/telegram/ReactionType.h @@ -11,12 +11,11 @@ #include "td/utils/common.h" #include "td/utils/FlatHashMap.h" +#include "td/utils/Promise.h" #include "td/utils/StringBuilder.h" namespace td { -class Dependencies; - class Td; class ReactionType { @@ -80,6 +79,16 @@ 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); + +vector get_recent_reactions(Td *td); + +vector get_top_reactions(Td *td); + +void add_recent_reaction(Td *td, const ReactionType &reaction_type); + int64 get_reaction_types_hash(const vector &reaction_types); } // namespace td