tdlight/td/telegram/ReactionManager.h

260 lines
8.5 KiB
C
Raw Normal View History

//
2024-01-01 01:07:21 +01:00
// 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"
2024-01-18 18:53:43 +01:00
#include "td/telegram/ReactionListType.h"
#include "td/telegram/ReactionType.h"
#include "td/telegram/ReactionUnavailabilityReason.h"
2024-01-29 17:11:14 +01:00
#include "td/telegram/SavedMessagesTopicId.h"
#include "td/telegram/td_api.h"
#include "td/telegram/telegram_api.h"
#include "td/actor/actor.h"
#include "td/utils/common.h"
2024-02-07 15:50:57 +01:00
#include "td/utils/FlatHashMap.h"
#include "td/utils/Promise.h"
2024-01-25 18:06:07 +01:00
#include "td/utils/Status.h"
#include "td/utils/StringBuilder.h"
2023-08-09 12:31:34 +02:00
#include <utility>
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;
2024-01-18 18:53:43 +01:00
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<td_api::object_ptr<td_api::emojiReaction>> &&promise);
td_api::object_ptr<td_api::availableReactions> get_sorted_available_reactions(
ChatReactions available_reactions, ChatReactions active_reactions, int32 row_size, bool is_tag,
ReactionUnavailabilityReason unavailability_reason);
td_api::object_ptr<td_api::availableReactions> get_available_reactions(int32 row_size);
void add_recent_reaction(const ReactionType &reaction_type);
void clear_recent_reactions(Promise<Unit> &&promise);
vector<ReactionType> get_default_tag_reactions();
void reload_reactions();
2024-01-18 18:53:43 +01:00
void reload_reaction_list(ReactionListType reaction_list_type);
2024-01-18 18:53:43 +01:00
void on_get_reaction_list(ReactionListType reaction_list_type,
tl_object_ptr<telegram_api::messages_Reactions> &&reactions_ptr);
2024-01-18 18:08:37 +01:00
void on_get_available_reactions(tl_object_ptr<telegram_api::messages_AvailableReactions> &&available_reactions_ptr);
void set_default_reaction(ReactionType reaction_type, Promise<Unit> &&promise);
void send_set_default_reaction_query();
2024-01-29 17:11:14 +01:00
void get_saved_messages_tags(SavedMessagesTopicId saved_messages_topic_id,
Promise<td_api::object_ptr<td_api::savedMessagesTags>> &&promise);
2024-01-23 14:01:27 +01:00
2024-01-23 17:04:31 +01:00
void on_update_saved_reaction_tags(Promise<Unit> &&promise);
2024-01-29 17:11:14 +01:00
void update_saved_messages_tags(SavedMessagesTopicId saved_messages_topic_id, const vector<ReactionType> &old_tags,
const vector<ReactionType> &new_tags);
2024-01-23 17:04:31 +01:00
void set_saved_messages_tag_title(ReactionType reaction_type, string title, Promise<Unit> &&promise);
void get_current_state(vector<td_api::object_ptr<td_api::Update>> &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 <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
void parse(ParserT &parser);
};
struct Reactions {
int32 hash_ = 0;
bool are_being_reloaded_ = false;
vector<Reaction> reactions_;
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
void parse(ParserT &parser);
};
struct ReactionList {
int64 hash_ = 0;
bool is_loaded_from_database_ = false;
bool is_being_reloaded_ = false;
vector<ReactionType> reaction_types_;
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
void parse(ParserT &parser);
};
2024-01-23 17:04:31 +01:00
static constexpr int32 MAX_TAG_TITLE_LENGTH = 12;
2024-01-23 14:01:27 +01:00
struct SavedReactionTag {
ReactionType reaction_type_;
2024-01-25 12:29:22 +01:00
uint64 hash_ = 0;
2024-01-23 14:01:27 +01:00
string title_;
int32 count_ = 0;
SavedReactionTag() = default;
explicit SavedReactionTag(telegram_api::object_ptr<telegram_api::savedReactionTag> &&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());
}
2024-01-23 14:01:27 +01:00
td_api::object_ptr<td_api::savedMessagesTag> get_saved_messages_tag_object() const;
2024-02-01 22:06:59 +01:00
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
void parse(ParserT &parser);
2024-01-23 14:01:27 +01:00
};
2024-01-23 14:23:35 +01:00
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);
2024-01-23 14:01:27 +01:00
struct SavedReactionTags {
vector<SavedReactionTag> tags_;
int64 hash_ = 0;
bool is_inited_ = false;
td_api::object_ptr<td_api::savedMessagesTags> get_saved_messages_tags_object() const;
bool update_saved_messages_tags(const vector<ReactionType> &old_tags, const vector<ReactionType> &new_tags);
2024-01-25 15:12:02 +01:00
bool set_tag_title(const ReactionType &reaction_type, const string &title);
int64 calc_hash() const;
2024-02-01 22:06:59 +01:00
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
void parse(ParserT &parser);
2024-01-23 14:01:27 +01:00
};
td_api::object_ptr<td_api::emojiReaction> get_emoji_reaction_object(const string &emoji) const;
2024-01-18 18:53:43 +01:00
ReactionList &get_reaction_list(ReactionListType reaction_list_type);
void start_up() final;
void tear_down() final;
void save_active_reactions();
void save_reactions();
2024-01-18 18:53:43 +01:00
void save_reaction_list(ReactionListType reaction_list_type);
2024-01-18 18:08:37 +01:00
void load_active_reactions();
void load_reactions();
2024-01-18 18:53:43 +01:00
void load_reaction_list(ReactionListType reaction_list_type);
2024-01-18 18:08:37 +01:00
void update_active_reactions();
td_api::object_ptr<td_api::updateActiveEmojiReactions> get_update_active_emoji_reactions_object() const;
2024-01-29 17:11:14 +01:00
SavedReactionTags *get_saved_reaction_tags(SavedMessagesTopicId saved_messages_topic_id);
2024-01-29 17:11:14 +01:00
void reget_saved_messages_tags(SavedMessagesTopicId saved_messages_topic_id,
Promise<td_api::object_ptr<td_api::savedMessagesTags>> &&promise);
2024-01-23 14:23:35 +01:00
2024-01-29 17:11:14 +01:00
void on_get_saved_messages_tags(SavedMessagesTopicId saved_messages_topic_id,
Result<telegram_api::object_ptr<telegram_api::messages_SavedReactionTags>> &&r_tags);
2024-02-01 22:06:59 +01:00
string get_saved_messages_tags_database_key(SavedMessagesTopicId saved_messages_topic_id);
void load_all_saved_reaction_tags_from_database();
void load_saved_reaction_tags_from_database(SavedMessagesTopicId saved_messages_topic_id, SavedReactionTags *tags);
2024-01-29 17:11:14 +01:00
td_api::object_ptr<td_api::updateSavedMessagesTags> get_update_saved_messages_tags_object(
SavedMessagesTopicId saved_messages_topic_id, const SavedReactionTags *tags) const;
2024-02-01 22:06:59 +01:00
void send_update_saved_messages_tags(SavedMessagesTopicId saved_messages_topic_id, const SavedReactionTags *tags,
bool from_database = false);
2024-01-23 14:23:35 +01:00
Td *td_;
ActorShared<> parent_;
bool is_inited_ = false;
2024-01-29 17:11:14 +01:00
bool are_reactions_loaded_from_database_ = false;
bool are_all_tags_loaded_from_database_ = false;
vector<std::pair<string, Promise<td_api::object_ptr<td_api::emojiReaction>>>> pending_get_emoji_reaction_queries_;
Reactions reactions_;
vector<ReactionType> active_reaction_types_;
2024-01-18 18:53:43 +01:00
ReactionList reaction_lists_[MAX_REACTION_LIST_TYPE];
2024-01-29 17:11:14 +01:00
SavedReactionTags all_tags_;
FlatHashMap<SavedMessagesTopicId, unique_ptr<SavedReactionTags>, SavedMessagesTopicIdHash> topic_tags_;
2024-01-23 14:01:27 +01:00
2024-01-29 17:11:14 +01:00
vector<Promise<td_api::object_ptr<td_api::savedMessagesTags>>> pending_get_all_saved_reaction_tags_queries_;
FlatHashMap<SavedMessagesTopicId, vector<Promise<td_api::object_ptr<td_api::savedMessagesTags>>>,
SavedMessagesTopicIdHash>
pending_get_topic_saved_reaction_tags_queries_;
};
} // namespace td