Move set_default_reaction to MessageReaction.h.

This commit is contained in:
levlam 2022-08-15 22:47:02 +03:00
parent bbac7baed0
commit febf458091
4 changed files with 75 additions and 58 deletions

View File

@ -7,14 +7,19 @@
#include "td/telegram/MessageReaction.h"
#include "td/telegram/AccessRights.h"
#include "td/telegram/ConfigManager.h"
#include "td/telegram/ConfigShared.h"
#include "td/telegram/ContactsManager.h"
#include "td/telegram/Global.h"
#include "td/telegram/MessageSender.h"
#include "td/telegram/MessagesManager.h"
#include "td/telegram/ServerMessageId.h"
#include "td/telegram/StickersManager.h"
#include "td/telegram/Td.h"
#include "td/telegram/UpdatesManager.h"
#include "td/actor/actor.h"
#include "td/utils/algorithm.h"
#include "td/utils/buffer.h"
#include "td/utils/FlatHashSet.h"
@ -217,6 +222,46 @@ class GetMessageReactionsListQuery final : public Td::ResultHandler {
}
};
class SetDefaultReactionQuery final : public Td::ResultHandler {
string reaction_;
public:
void send(const string &reaction) {
reaction_ = reaction;
send_query(G()->net_query_creator().create(telegram_api::messages_setDefaultReaction(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 = G()->shared_config().get_option_string("default_reaction", "-");
LOG(INFO) << "Successfully set reaction " << reaction_ << " as default, current default is " << default_reaction;
if (default_reaction != reaction_) {
send_set_default_reaction_query(td_);
} else {
G()->shared_config().set_option_empty("default_reaction_needs_sync");
}
}
void on_error(Status status) final {
if (G()->close_flag()) {
return;
}
LOG(INFO) << "Failed to set default reaction: " << status;
G()->shared_config().set_option_empty("default_reaction_needs_sync");
send_closure(G()->config_manager(), &ConfigManager::reget_app_config, Promise<Unit>());
}
};
void MessageReaction::add_recent_chooser_dialog_id(DialogId dialog_id) {
recent_chooser_dialog_ids_.insert(recent_chooser_dialog_ids_.begin(), dialog_id);
if (recent_chooser_dialog_ids_.size() > MAX_RECENT_CHOOSERS + 1) {
@ -545,4 +590,22 @@ void get_message_added_reactions(Td *td, FullMessageId full_message_id, string r
->send(full_message_id, std::move(reaction), std::move(offset), limit);
}
void set_default_reaction(Td *td, string reaction, Promise<Unit> &&promise) {
if (!td->stickers_manager_->is_active_reaction(reaction)) {
return promise.set_error(Status::Error(400, "Can't set incative reaction as default"));
}
if (G()->shared_config().get_option_string("default_reaction", "-") != reaction) {
G()->shared_config().set_option_string("default_reaction", reaction);
if (!G()->shared_config().get_option_boolean("default_reaction_needs_sync")) {
G()->shared_config().set_option_boolean("default_reaction_needs_sync", true);
}
}
promise.set_value(Unit());
}
void send_set_default_reaction_query(Td *td) {
td->create_handler<SetDefaultReactionQuery>()->send(G()->shared_config().get_option_string("default_reaction"));
}
} // namespace td

View File

@ -180,4 +180,8 @@ void set_message_reaction(Td *td, FullMessageId full_message_id, string reaction
void get_message_added_reactions(Td *td, FullMessageId full_message_id, string reaction, string offset, int32 limit,
Promise<td_api::object_ptr<td_api::addedReactions>> &&promise);
void set_default_reaction(Td *td, string reaction, Promise<Unit> &&promise);
void send_set_default_reaction_query(Td *td);
} // namespace td

View File

@ -17,6 +17,7 @@
#include "td/telegram/Global.h"
#include "td/telegram/JsonValue.h"
#include "td/telegram/LanguagePackManager.h"
#include "td/telegram/MessageReaction.h"
#include "td/telegram/net/MtprotoHeader.h"
#include "td/telegram/net/NetQueryDispatcher.h"
#include "td/telegram/NotificationManager.h"
@ -43,36 +44,6 @@
namespace td {
class SetDefaultReactionQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
public:
explicit SetDefaultReactionQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(const string &reaction) {
send_query(G()->net_query_creator().create(telegram_api::messages_setDefaultReaction(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()) {
promise_.set_value(Unit());
} else {
on_error(Status::Error(400, "Receive false"));
}
}
void on_error(Status status) final {
LOG(INFO) << "Failed to set default reaction: " << status;
promise_.set_error(std::move(status));
}
};
OptionManager::OptionManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
send_unix_time_update();
@ -255,7 +226,7 @@ void OptionManager::on_option_updated(const string &name) {
break;
case 'd':
if (name == "default_reaction_needs_sync" && G()->shared_config().get_option_boolean(name)) {
set_default_reaction();
send_set_default_reaction_query(td_);
}
if (name == "dice_emojis") {
send_closure(td_->stickers_manager_actor_, &StickersManager::on_update_dice_emojis);
@ -566,10 +537,12 @@ void OptionManager::set_option(const string &name, td_api::object_ptr<td_api::Op
}
break;
case 'd':
if (!is_bot && set_string_option("default_reaction", [td = td_](Slice value) {
return td->stickers_manager_->is_active_reaction(value.str());
})) {
G()->shared_config().set_option_boolean("default_reaction_needs_sync", true);
if (!is_bot && name == "default_reaction") {
string reaction;
if (value_constructor_id == td_api::optionValueString::ID) {
reaction = static_cast<td_api::optionValueString *>(value.get())->value_;
}
set_default_reaction(td_, std::move(reaction), std::move(promise));
return;
}
if (!is_bot && set_boolean_option("disable_animated_emoji")) {
@ -805,23 +778,4 @@ void OptionManager::get_current_state(vector<td_api::object_ptr<td_api::Update>>
}
}
void OptionManager::set_default_reaction() {
auto promise = PromiseCreator::lambda([actor_id = actor_id(this)](Result<Unit> &&result) {
send_closure(actor_id, &OptionManager::on_set_default_reaction, result.is_ok());
});
td_->create_handler<SetDefaultReactionQuery>(std::move(promise))
->send(G()->shared_config().get_option_string("default_reaction"));
}
void OptionManager::on_set_default_reaction(bool success) {
if (G()->close_flag() && !success) {
return;
}
G()->shared_config().set_option_empty("default_reaction_needs_sync");
if (!success) {
send_closure(G()->config_manager(), &ConfigManager::reget_app_config, Promise<Unit>());
}
}
} // namespace td

View File

@ -59,10 +59,6 @@ class OptionManager final : public Actor {
void send_unix_time_update();
void set_default_reaction();
void on_set_default_reaction(bool success);
Td *td_;
ActorShared<> parent_;