Move more functions to ReactionType.

This commit is contained in:
levlam 2023-08-05 14:59:42 +03:00
parent 905287f1ec
commit 57b935f6a7
4 changed files with 92 additions and 89 deletions

View File

@ -7,7 +7,6 @@
#include "td/telegram/MessageReaction.h" #include "td/telegram/MessageReaction.h"
#include "td/telegram/AccessRights.h" #include "td/telegram/AccessRights.h"
#include "td/telegram/ConfigManager.h"
#include "td/telegram/ContactsManager.h" #include "td/telegram/ContactsManager.h"
#include "td/telegram/Dependencies.h" #include "td/telegram/Dependencies.h"
#include "td/telegram/Global.h" #include "td/telegram/Global.h"
@ -15,7 +14,6 @@
#include "td/telegram/MessagesManager.h" #include "td/telegram/MessagesManager.h"
#include "td/telegram/OptionManager.h" #include "td/telegram/OptionManager.h"
#include "td/telegram/ServerMessageId.h" #include "td/telegram/ServerMessageId.h"
#include "td/telegram/StickersManager.h"
#include "td/telegram/Td.h" #include "td/telegram/Td.h"
#include "td/telegram/telegram_api.h" #include "td/telegram/telegram_api.h"
#include "td/telegram/UpdatesManager.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<telegram_api::messages_setDefaultReaction>(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 { class ReportReactionQuery final : public Td::ResultHandler {
Promise<Unit> promise_; Promise<Unit> promise_;
DialogId dialog_id_; 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); ->send(full_message_id, std::move(reaction_type), std::move(offset), limit);
} }
void set_default_reaction(Td *td, ReactionType reaction_type, Promise<Unit> &&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<SetDefaultReactionQuery>()->send(
ReactionType(td->option_manager_->get_option_string("default_reaction")));
}
void report_message_reactions(Td *td, FullMessageId full_message_id, DialogId chooser_dialog_id, void report_message_reactions(Td *td, FullMessageId full_message_id, DialogId chooser_dialog_id,
Promise<Unit> &&promise) { Promise<Unit> &&promise) {
auto dialog_id = full_message_id.get_dialog_id(); 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<ReportReactionQuery>(std::move(promise))->send(dialog_id, message_id, chooser_dialog_id); td->create_handler<ReportReactionQuery>(std::move(promise))->send(dialog_id, message_id, chooser_dialog_id);
} }
vector<ReactionType> get_recent_reactions(Td *td) {
return td->stickers_manager_->get_recent_reactions();
}
vector<ReactionType> 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 } // namespace td

View File

@ -218,17 +218,7 @@ void send_message_reaction(Td *td, FullMessageId full_message_id, vector<Reactio
void get_message_added_reactions(Td *td, FullMessageId full_message_id, ReactionType reaction_type, string offset, void get_message_added_reactions(Td *td, FullMessageId full_message_id, ReactionType reaction_type, string offset,
int32 limit, Promise<td_api::object_ptr<td_api::addedReactions>> &&promise); int32 limit, Promise<td_api::object_ptr<td_api::addedReactions>> &&promise);
void set_default_reaction(Td *td, ReactionType reaction_type, Promise<Unit> &&promise);
void send_set_default_reaction_query(Td *td);
void report_message_reactions(Td *td, FullMessageId full_message_id, DialogId chooser_dialog_id, void report_message_reactions(Td *td, FullMessageId full_message_id, DialogId chooser_dialog_id,
Promise<Unit> &&promise); Promise<Unit> &&promise);
vector<ReactionType> get_recent_reactions(Td *td);
vector<ReactionType> get_top_reactions(Td *td);
void add_recent_reaction(Td *td, const ReactionType &reaction_type);
} // namespace td } // namespace td

View File

@ -6,17 +6,62 @@
// //
#include "td/telegram/ReactionType.h" #include "td/telegram/ReactionType.h"
#include "td/telegram/ConfigManager.h"
#include "td/telegram/misc.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/as.h"
#include "td/utils/base64.h" #include "td/utils/base64.h"
#include "td/utils/buffer.h"
#include "td/utils/crypto.h" #include "td/utils/crypto.h"
#include "td/utils/emoji.h" #include "td/utils/emoji.h"
#include "td/utils/SliceBuilder.h" #include "td/utils/SliceBuilder.h"
#include "td/utils/Status.h"
#include "td/utils/utf8.h" #include "td/utils/utf8.h"
namespace td { 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<telegram_api::messages_setDefaultReaction>(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) { static int64 get_custom_emoji_id(const string &reaction) {
auto r_decoded = base64_decode(Slice(&reaction[1], reaction.size() - 1)); auto r_decoded = base64_decode(Slice(&reaction[1], reaction.size() - 1));
CHECK(r_decoded.is_ok()); CHECK(r_decoded.is_ok());
@ -114,7 +159,7 @@ bool ReactionType::is_custom_reaction() const {
bool ReactionType::is_active_reaction( bool ReactionType::is_active_reaction(
const FlatHashMap<ReactionType, size_t, ReactionTypeHash> &active_reaction_pos) const { const FlatHashMap<ReactionType, size_t, ReactionTypeHash> &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) { 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_; return string_builder << "reaction " << reaction_type.reaction_;
} }
void set_default_reaction(Td *td, ReactionType reaction_type, Promise<Unit> &&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<SetDefaultReactionQuery>()->send(
ReactionType(td->option_manager_->get_option_string("default_reaction")));
}
vector<ReactionType> get_recent_reactions(Td *td) {
return td->stickers_manager_->get_recent_reactions();
}
vector<ReactionType> 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<ReactionType> &reaction_types) { int64 get_reaction_types_hash(const vector<ReactionType> &reaction_types) {
vector<uint64> numbers; vector<uint64> numbers;
for (auto &reaction_type : reaction_types) { for (auto &reaction_type : reaction_types) {

View File

@ -11,12 +11,11 @@
#include "td/utils/common.h" #include "td/utils/common.h"
#include "td/utils/FlatHashMap.h" #include "td/utils/FlatHashMap.h"
#include "td/utils/Promise.h"
#include "td/utils/StringBuilder.h" #include "td/utils/StringBuilder.h"
namespace td { namespace td {
class Dependencies;
class Td; class Td;
class ReactionType { class ReactionType {
@ -80,6 +79,16 @@ inline bool operator!=(const ReactionType &lhs, const ReactionType &rhs) {
StringBuilder &operator<<(StringBuilder &string_builder, const ReactionType &reaction_type); StringBuilder &operator<<(StringBuilder &string_builder, const ReactionType &reaction_type);
void set_default_reaction(Td *td, ReactionType reaction_type, Promise<Unit> &&promise);
void send_set_default_reaction_query(Td *td);
vector<ReactionType> get_recent_reactions(Td *td);
vector<ReactionType> get_top_reactions(Td *td);
void add_recent_reaction(Td *td, const ReactionType &reaction_type);
int64 get_reaction_types_hash(const vector<ReactionType> &reaction_types); int64 get_reaction_types_hash(const vector<ReactionType> &reaction_types);
} // namespace td } // namespace td