diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 0ffcf9458..464a0acb0 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -22738,16 +22738,22 @@ void MessagesManager::add_message_reaction(MessageFullId message_full_id, Reacti LOG(INFO) << "Have message with " << *m->reactions; bool is_tag = can_add_message_tag(dialog_id, m->reactions.get()); + vector old_chosen_tags; + if (is_tag) { + old_chosen_tags = m->reactions->get_chosen_reaction_types(); + } if (!m->reactions->add_my_reaction(reaction_type, is_big, get_my_reaction_dialog_id(d), have_recent_choosers, is_tag)) { return promise.set_value(Unit()); } - if (!is_tag && add_to_recent) { + set_message_reactions(d, m, is_big, add_to_recent, std::move(promise)); + + if (is_tag) { + td_->reaction_manager_->update_saved_messages_tags(old_chosen_tags, m->reactions->get_chosen_reaction_types()); + } else if (add_to_recent) { td_->reaction_manager_->add_recent_reaction(reaction_type); } - - set_message_reactions(d, m, is_big, add_to_recent, std::move(promise)); } void MessagesManager::remove_message_reaction(MessageFullId message_full_id, ReactionType reaction_type, @@ -22772,11 +22778,20 @@ void MessagesManager::remove_message_reaction(MessageFullId message_full_id, Rea } LOG(INFO) << "Have message with " << *m->reactions; + bool is_tag = can_add_message_tag(dialog_id, m->reactions.get()); + vector old_chosen_tags; + if (is_tag) { + old_chosen_tags = m->reactions->get_chosen_reaction_types(); + } if (!m->reactions->remove_my_reaction(reaction_type, get_my_reaction_dialog_id(d))) { return promise.set_value(Unit()); } set_message_reactions(d, m, false, false, std::move(promise)); + + if (is_tag) { + td_->reaction_manager_->update_saved_messages_tags(old_chosen_tags, m->reactions->get_chosen_reaction_types()); + } } void MessagesManager::set_message_reactions(Dialog *d, Message *m, bool is_big, bool add_to_recent, diff --git a/td/telegram/ReactionManager.cpp b/td/telegram/ReactionManager.cpp index 8fa7145c0..562a27fba 100644 --- a/td/telegram/ReactionManager.cpp +++ b/td/telegram/ReactionManager.cpp @@ -256,6 +256,53 @@ td_api::object_ptr ReactionManager::SavedReactionTags transform(tags_, [](const SavedReactionTag &tag) { return tag.get_saved_messages_tag_object(); })); } +void ReactionManager::SavedReactionTags::update_saved_messages_tags(const vector &old_tags, + const vector &new_tags, + bool &is_changed, bool &need_reload_title) { + if (!is_inited_) { + return; + } + is_changed = false; + for (const auto &old_tag : old_tags) { + if (!td::contains(new_tags, old_tag)) { + for (auto it = tags_.begin(); it != tags_.end(); ++it) { + auto &tag = *it; + if (tag.reaction_type_ == old_tag) { + tag.count_--; + if (tag.count_ <= 0) { + tags_.erase(it); + } + is_changed = true; + break; + } + } + } + } + for (const auto &new_tag : new_tags) { + if (!td::contains(old_tags, new_tag)) { + is_changed = true; + bool is_found = false; + for (auto &tag : tags_) { + if (tag.reaction_type_ == new_tag) { + tag.count_++; + is_found = true; + break; + } + } + if (!is_found) { + SavedReactionTag saved_reaction_tag; + saved_reaction_tag.reaction_type_ = new_tag; + saved_reaction_tag.count_ = 1; + tags_.push_back(std::move(saved_reaction_tag)); + need_reload_title = true; + } + } + } + if (is_changed) { + std::sort(tags_.begin(), tags_.end()); + } +} + ReactionManager::ReactionManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { } @@ -757,7 +804,7 @@ void ReactionManager::send_set_default_reaction_query() { void ReactionManager::get_saved_messages_tags(Promise> &&promise) { if (tags_.is_inited_) { - // return promise.set_value(tags_.get_saved_messages_tags_object()); + return promise.set_value(tags_.get_saved_messages_tags_object()); } auto &promises = pending_get_saved_reaction_tags_queries_; @@ -831,6 +878,22 @@ void ReactionManager::on_update_saved_reaction_tags(Promise &&promise) { })); } +void ReactionManager::update_saved_messages_tags(const vector &old_tags, + const vector &new_tags) { + if (old_tags == new_tags) { + return; + } + bool is_changed = false; + bool need_reload_title = false; + tags_.update_saved_messages_tags(old_tags, new_tags, is_changed, need_reload_title); + if (is_changed) { + send_update_saved_messages_tags(); + } + if (need_reload_title && td_->option_manager_->get_option_boolean("is_premium")) { + on_update_saved_reaction_tags(Auto()); + } +} + void ReactionManager::set_saved_messages_tag_title(ReactionType reaction_type, string title, Promise &&promise) { if (reaction_type.is_empty()) { return promise.set_error(Status::Error(400, "Reaction type must be non-empty")); diff --git a/td/telegram/ReactionManager.h b/td/telegram/ReactionManager.h index eeee58f77..8650b5a11 100644 --- a/td/telegram/ReactionManager.h +++ b/td/telegram/ReactionManager.h @@ -70,6 +70,8 @@ class ReactionManager final : public Actor { void on_update_saved_reaction_tags(Promise &&promise); + void update_saved_messages_tags(const vector &old_tags, const vector &new_tags); + void set_saved_messages_tag_title(ReactionType reaction_type, string title, Promise &&promise); void get_current_state(vector> &updates) const; @@ -132,6 +134,8 @@ class ReactionManager final : public Actor { string title_; int32 count_ = 0; + SavedReactionTag() = default; + explicit SavedReactionTag(telegram_api::object_ptr &&tag); td_api::object_ptr get_saved_messages_tag_object() const; @@ -149,6 +153,9 @@ class ReactionManager final : public Actor { bool is_inited_ = false; td_api::object_ptr get_saved_messages_tags_object() const; + + void update_saved_messages_tags(const vector &old_tags, const vector &new_tags, + bool &is_changed, bool &need_reload_title); }; td_api::object_ptr get_emoji_reaction_object(const string &emoji) const;