Consistently use is_active_reaction.

This commit is contained in:
levlam 2022-09-08 21:04:23 +03:00
parent 148f1bf794
commit 4ae5b9a533
6 changed files with 23 additions and 30 deletions

View File

@ -60,21 +60,15 @@ ChatReactions::ChatReactions(td_api::object_ptr<td_api::ChatAvailableReactions>
}
}
ChatReactions ChatReactions::get_active_reactions(const vector<string> &active_reactions) const {
if (reactions_.empty()) {
// fast path
return *this;
ChatReactions ChatReactions::get_active_reactions(const FlatHashMap<string, size_t> &active_reaction_pos) const {
ChatReactions result = *this;
if (!reactions_.empty()) {
CHECK(!allow_all_);
CHECK(!allow_custom_);
td::remove_if(result.reactions_,
[&](const string &reaction) { return !is_active_reaction(reaction, active_reaction_pos); });
}
CHECK(!allow_all_);
CHECK(!allow_custom_);
vector<string> result;
for (const auto &active_reaction : active_reactions) {
if (td::contains(reactions_, active_reaction)) {
result.push_back(active_reaction);
}
}
return ChatReactions(std::move(result));
return result;
}
td_api::object_ptr<td_api::ChatAvailableReactions> ChatReactions::get_chat_available_reactions_object() const {

View File

@ -10,6 +10,7 @@
#include "td/telegram/telegram_api.h"
#include "td/utils/common.h"
#include "td/utils/FlatHashMap.h"
#include "td/utils/StringBuilder.h"
#include "td/utils/tl_helpers.h"
@ -32,7 +33,7 @@ struct ChatReactions {
ChatReactions(bool allow_all, bool allow_custom) : allow_all_(allow_all), allow_custom_(allow_custom) {
}
ChatReactions get_active_reactions(const vector<string> &active_reactions) const;
ChatReactions get_active_reactions(const FlatHashMap<string, size_t> &active_reaction_pos) const;
telegram_api::object_ptr<telegram_api::ChatReactions> get_input_chat_reactions() const;

View File

@ -680,6 +680,10 @@ StringBuilder &operator<<(StringBuilder &string_builder, const unique_ptr<Messag
return string_builder << *reactions;
}
bool is_active_reaction(const string &reaction, const FlatHashMap<string, size_t> &active_reaction_pos) {
return !reaction.empty() && (reaction[0] == '#' || active_reaction_pos.count(reaction) > 0);
}
void reload_message_reactions(Td *td, DialogId dialog_id, vector<MessageId> &&message_ids) {
if (!td->messages_manager_->have_input_peer(dialog_id, AccessRights::Read) || message_ids.empty()) {
return;

View File

@ -181,6 +181,8 @@ string get_message_reaction_string(const telegram_api::object_ptr<telegram_api::
string get_message_reaction_string(const td_api::object_ptr<td_api::ReactionType> &type);
bool is_active_reaction(const string &reaction, const FlatHashMap<string, size_t> &active_reaction_pos);
void reload_message_reactions(Td *td, DialogId dialog_id, vector<MessageId> &&message_ids);
void set_message_reaction(Td *td, FullMessageId full_message_id, string reaction, bool is_big, bool add_to_recent,

View File

@ -8241,15 +8241,13 @@ void MessagesManager::set_active_reactions(vector<string> active_reactions) {
return;
}
auto old_active_reactions = std::move(active_reactions_);
bool is_changed = active_reactions != active_reactions_;
active_reactions_ = std::move(active_reactions);
auto old_active_reaction_pos_ = std::move(active_reaction_pos_);
active_reaction_pos_.clear();
bool is_changed = old_active_reactions.size() != active_reactions_.size();
for (size_t i = 0; i < active_reactions_.size(); i++) {
active_reaction_pos_[active_reactions_[i]] = i;
if (!is_changed && active_reactions_[i] != old_active_reactions[i]) {
is_changed = true;
}
}
dialogs_.foreach([&](const DialogId &dialog_id, unique_ptr<Dialog> &dialog) {
@ -8262,8 +8260,8 @@ void MessagesManager::set_active_reactions(vector<string> active_reactions) {
break;
case DialogType::Chat:
case DialogType::Channel: {
auto old_reactions = d->available_reactions.get_active_reactions(old_active_reactions);
auto new_reactions = d->available_reactions.get_active_reactions(active_reactions_);
auto old_reactions = d->available_reactions.get_active_reactions(old_active_reaction_pos_);
auto new_reactions = d->available_reactions.get_active_reactions(active_reaction_pos_);
if (old_reactions != new_reactions) {
if (old_reactions.empty() != new_reactions.empty()) {
if (!old_reactions.empty()) {
@ -8286,7 +8284,7 @@ void MessagesManager::set_active_reactions(vector<string> active_reactions) {
}
ChatReactions MessagesManager::get_active_reactions(const ChatReactions &available_reactions) const {
return available_reactions.get_active_reactions(active_reactions_);
return available_reactions.get_active_reactions(active_reaction_pos_);
}
ChatReactions MessagesManager::get_dialog_active_reactions(const Dialog *d) const {
@ -24458,10 +24456,6 @@ void MessagesManager::on_get_scheduled_messages_from_database(DialogId dialog_id
set_promises(promises);
}
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");
@ -24511,7 +24505,7 @@ ChatReactions MessagesManager::get_message_available_reactions(const Dialog *d,
// 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 (reaction.is_chosen() || (can_use_reactions && is_active_reaction(reaction_str, active_reaction_pos_))) {
if (!td::contains(active_reactions.reactions_, reaction_str)) {
active_reactions.reactions_.push_back(reaction_str);
}

View File

@ -2687,8 +2687,6 @@ class MessagesManager final : public Actor {
bool update_dialog_silent_send_message(Dialog *d, bool silent_send_message);
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);