Move default reaction handling to ReactionManager.

This commit is contained in:
levlam 2023-08-09 13:19:54 +03:00
parent 441482c0c4
commit ce679e7b78
5 changed files with 73 additions and 81 deletions

View File

@ -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 <algorithm>
@ -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<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()) {
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<td_api::emojiReaction> 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<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() && !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<SetDefaultReactionQuery>()->send(
ReactionType(td_->option_manager_->get_option_string("default_reaction")));
}
void ReactionManager::get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const {
if (td_->auth_manager_->is_bot()) {
return;

View File

@ -56,6 +56,10 @@ class ReactionManager final : public Actor {
void on_get_top_reactions(tl_object_ptr<telegram_api::messages_Reactions> &&reactions_ptr);
void set_default_reaction(ReactionType reaction_type, Promise<Unit> &&promise);
void send_set_default_reaction_query();
void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const;
private:

View File

@ -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<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) {
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<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->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<SetDefaultReactionQuery>()->send(
ReactionType(td->option_manager_->get_option_string("default_reaction")));
}
int64 get_reaction_types_hash(const vector<ReactionType> &reaction_types) {
vector<uint64> numbers;
for (auto &reaction_type : reaction_types) {

View File

@ -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<Unit> &&promise);
void send_set_default_reaction_query(Td *td);
int64 get_reaction_types_hash(const vector<ReactionType> &reaction_types);
} // namespace td

View File

@ -3692,10 +3692,6 @@ void Td::init(Parameters parameters, Result<TdDb::OpenedDatabase> 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) {