Update get_message_available_reactions.

This commit is contained in:
levlam 2022-09-08 20:04:32 +03:00
parent ac5e8e7eda
commit 148f1bf794
5 changed files with 38 additions and 26 deletions

View File

@ -84,6 +84,11 @@ td_api::object_ptr<td_api::ChatAvailableReactions> ChatReactions::get_chat_avail
return td_api::make_object<td_api::chatAvailableReactionsSome>(transform(reactions_, get_reaction_type_object));
}
td_api::object_ptr<td_api::availableReactions> ChatReactions::get_available_reactions_object() const {
CHECK(!allow_all_);
return td_api::make_object<td_api::availableReactions>(transform(reactions_, get_reaction_type_object));
}
telegram_api::object_ptr<telegram_api::ChatReactions> ChatReactions::get_input_chat_reactions() const {
if (allow_all_) {
int32 flags = 0;

View File

@ -38,6 +38,8 @@ struct ChatReactions {
td_api::object_ptr<td_api::ChatAvailableReactions> get_chat_available_reactions_object() const;
td_api::object_ptr<td_api::availableReactions> get_available_reactions_object() const;
bool empty() const {
return reactions_.empty() && !allow_all_;
}

View File

@ -24458,7 +24458,11 @@ void MessagesManager::on_get_scheduled_messages_from_database(DialogId dialog_id
set_promises(promises);
}
Result<vector<string>> MessagesManager::get_message_available_reactions(FullMessageId full_message_id) {
bool MessagesManager::is_active_reaction(const string &reaction) const {
return !reaction.empty() && (reaction[0] == '#' || active_reaction_pos_.count(reaction) > 0);
}
Result<ChatReactions> MessagesManager::get_message_available_reactions(FullMessageId full_message_id) {
auto dialog_id = full_message_id.get_dialog_id();
Dialog *d = get_dialog_force(dialog_id, "get_message_available_reactions");
if (d == nullptr) {
@ -24472,7 +24476,7 @@ Result<vector<string>> MessagesManager::get_message_available_reactions(FullMess
return get_message_available_reactions(d, m);
}
vector<string> MessagesManager::get_message_available_reactions(const Dialog *d, const Message *m) {
ChatReactions MessagesManager::get_message_available_reactions(const Dialog *d, const Message *m) {
CHECK(d != nullptr);
CHECK(m != nullptr);
auto active_reactions = get_message_active_reactions(d, m);
@ -24490,30 +24494,31 @@ vector<string> MessagesManager::get_message_available_reactions(const Dialog *d,
}
}
vector<string> result;
if (can_use_reactions) {
int64 reactions_uniq_max = td_->option_manager_->get_option_integer("reactions_uniq_max", 11);
bool can_add_new_reactions =
m->reactions == nullptr || static_cast<int64>(m->reactions->reactions_.size()) < reactions_uniq_max;
// can add only active available reactions or remove previously set reaction
for (const auto &active_reaction : active_reactions_) {
// can add the reaction if it has already been used for the message or is available in the chat
bool is_set = (m->reactions != nullptr && m->reactions->get_reaction(active_reaction) != nullptr);
if (is_set || (can_add_new_reactions &&
(active_reactions.allow_all_ || td::contains(active_reactions.reactions_, active_reaction)))) {
result.push_back(active_reaction);
}
}
int64 reactions_uniq_max = td_->option_manager_->get_option_integer("reactions_uniq_max", 11);
bool can_add_new_reactions =
m->reactions == nullptr || static_cast<int64>(m->reactions->reactions_.size()) < reactions_uniq_max;
if (!can_use_reactions || !can_add_new_reactions) {
active_reactions = ChatReactions();
}
if (active_reactions.allow_all_) {
active_reactions.reactions_ = active_reactions_;
active_reactions.allow_all_ = false;
}
if (m->reactions != nullptr) {
for (const auto &reaction : m->reactions->reactions_) {
if (reaction.is_chosen() && !td::contains(result, reaction.get_reaction())) {
CHECK(!can_use_reactions || !td::contains(active_reactions_, reaction.get_reaction()));
result.push_back(reaction.get_reaction());
// we always can remove a currently chosen reaction
// an already used reaction can be added if it is an active reaction
const string &reaction_str = reaction.get_reaction();
if (reaction.is_chosen() || (can_use_reactions && is_active_reaction(reaction_str))) {
if (!td::contains(active_reactions.reactions_, reaction_str)) {
active_reactions.reactions_.push_back(reaction_str);
}
}
}
}
return result;
return active_reactions;
}
void MessagesManager::set_message_reaction(FullMessageId full_message_id, string reaction, bool is_big,
@ -24529,7 +24534,7 @@ void MessagesManager::set_message_reaction(FullMessageId full_message_id, string
return promise.set_error(Status::Error(400, "Message not found"));
}
if (!reaction.empty() && !td::contains(get_message_available_reactions(d, m), reaction)) {
if (!reaction.empty() && !td::contains(get_message_available_reactions(d, m).reactions_, reaction)) {
return promise.set_error(Status::Error(400, "The reaction isn't available for the message"));
}

View File

@ -805,7 +805,7 @@ class MessagesManager final : public Actor {
vector<MessageId> get_dialog_scheduled_messages(DialogId dialog_id, bool force, bool ignore_result,
Promise<Unit> &&promise);
Result<vector<string>> get_message_available_reactions(FullMessageId full_message_id);
Result<ChatReactions> get_message_available_reactions(FullMessageId full_message_id);
void set_message_reaction(FullMessageId full_message_id, string reaction, bool is_big, bool add_to_recent,
Promise<Unit> &&promise);
@ -2687,7 +2687,9 @@ class MessagesManager final : public Actor {
bool update_dialog_silent_send_message(Dialog *d, bool silent_send_message);
vector<string> get_message_available_reactions(const Dialog *d, const Message *m);
bool is_active_reaction(const string &reaction) const;
ChatReactions get_message_available_reactions(const Dialog *d, const Message *m);
void on_set_message_reaction(FullMessageId full_message_id, Result<Unit> result, Promise<Unit> promise);

View File

@ -5206,9 +5206,7 @@ void Td::on_request(uint64 id, const td_api::getMessageAvailableReactions &reque
if (r_reactions.is_error()) {
send_closure(actor_id(this), &Td::send_error, id, r_reactions.move_as_error());
} else {
auto reactions = transform(r_reactions.ok(), [](auto &reaction) { return get_reaction_type_object(reaction); });
send_closure(actor_id(this), &Td::send_result, id,
td_api::make_object<td_api::availableReactions>(std::move(reactions)));
send_closure(actor_id(this), &Td::send_result, id, r_reactions.ok().get_available_reactions_object());
}
}