// // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024 // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #pragma once #include "td/telegram/ChatReactions.h" #include "td/telegram/files/FileId.h" #include "td/telegram/ReactionListType.h" #include "td/telegram/ReactionType.h" #include "td/telegram/ReactionUnavailabilityReason.h" #include "td/telegram/td_api.h" #include "td/telegram/telegram_api.h" #include "td/actor/actor.h" #include "td/utils/common.h" #include "td/utils/Promise.h" #include "td/utils/StringBuilder.h" #include namespace td { class Td; class ReactionManager final : public Actor { public: ReactionManager(Td *td, ActorShared<> parent); ReactionManager(const ReactionManager &) = delete; ReactionManager &operator=(const ReactionManager &) = delete; ReactionManager(ReactionManager &&) = delete; ReactionManager &operator=(ReactionManager &&) = delete; ~ReactionManager() final; static constexpr size_t MAX_RECENT_REACTIONS = 100; // some reasonable value void init(); bool is_active_reaction(const ReactionType &reaction_type) const; void get_emoji_reaction(const string &emoji, Promise> &&promise); td_api::object_ptr get_sorted_available_reactions( ChatReactions available_reactions, ChatReactions active_reactions, int32 row_size, bool is_tag, ReactionUnavailabilityReason unavailability_reason); td_api::object_ptr get_available_reactions(int32 row_size); void add_recent_reaction(const ReactionType &reaction_type); void clear_recent_reactions(Promise &&promise); vector get_default_tag_reactions(); void reload_reactions(); void reload_reaction_list(ReactionListType reaction_list_type); void on_get_reaction_list(ReactionListType reaction_list_type, tl_object_ptr &&reactions_ptr); void on_get_available_reactions(tl_object_ptr &&available_reactions_ptr); void set_default_reaction(ReactionType reaction_type, Promise &&promise); void send_set_default_reaction_query(); void get_saved_messages_tags(Promise> &&promise); 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; private: struct Reaction { ReactionType reaction_type_; string title_; bool is_active_ = false; bool is_premium_ = false; FileId static_icon_; FileId appear_animation_; FileId select_animation_; FileId activate_animation_; FileId effect_animation_; FileId around_animation_; FileId center_animation_; bool is_valid() const { return static_icon_.is_valid() && appear_animation_.is_valid() && select_animation_.is_valid() && activate_animation_.is_valid() && effect_animation_.is_valid() && !reaction_type_.is_empty(); } template void store(StorerT &storer) const; template void parse(ParserT &parser); }; struct Reactions { int32 hash_ = 0; bool are_being_reloaded_ = false; vector reactions_; template void store(StorerT &storer) const; template void parse(ParserT &parser); }; struct ReactionList { int64 hash_ = 0; bool is_loaded_from_database_ = false; bool is_being_reloaded_ = false; vector reaction_types_; template void store(StorerT &storer) const; template void parse(ParserT &parser); }; static constexpr int32 MAX_TAG_TITLE_LENGTH = 12; struct SavedReactionTag { ReactionType reaction_type_; uint64 hash_ = 0; string title_; int32 count_ = 0; SavedReactionTag() = default; explicit SavedReactionTag(telegram_api::object_ptr &&tag); SavedReactionTag(const ReactionType &reaction_type, const string &title, int32 count); bool is_valid() const { return !reaction_type_.is_empty() && count_ >= 0 && (count_ > 0 || !title_.empty()); } td_api::object_ptr get_saved_messages_tag_object() const; }; friend bool operator==(const SavedReactionTag &lhs, const SavedReactionTag &rhs); friend bool operator!=(const SavedReactionTag &lhs, const SavedReactionTag &rhs); friend bool operator<(const SavedReactionTag &lhs, const SavedReactionTag &rhs); friend StringBuilder &operator<<(StringBuilder &string_builder, const SavedReactionTag &saved_reaction_tag); struct SavedReactionTags { vector tags_; int64 hash_ = 0; 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); bool set_tag_title(const ReactionType &reaction_type, const string &title); int64 calc_hash() const; }; td_api::object_ptr get_emoji_reaction_object(const string &emoji) const; ReactionList &get_reaction_list(ReactionListType reaction_list_type); void start_up() final; void tear_down() final; void save_active_reactions(); void save_reactions(); void save_reaction_list(ReactionListType reaction_list_type); void load_active_reactions(); void load_reactions(); void load_reaction_list(ReactionListType reaction_list_type); void update_active_reactions(); td_api::object_ptr get_update_active_emoji_reactions_object() const; void reget_saved_messages_tags(Promise> &&promise); void on_get_saved_messages_tags(Result> &&r_tags); td_api::object_ptr get_update_saved_messages_tags_object() const; void send_update_saved_messages_tags(); Td *td_; ActorShared<> parent_; bool is_inited_ = false; vector>>> pending_get_emoji_reaction_queries_; Reactions reactions_; vector active_reaction_types_; ReactionList reaction_lists_[MAX_REACTION_LIST_TYPE]; SavedReactionTags tags_; vector>> pending_get_saved_reaction_tags_queries_; bool are_reactions_loaded_from_database_ = false; }; } // namespace td