2022-01-20 20:54:34 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2022-01-20 20:54:34 +01:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2022-01-21 11:15:27 +01:00
|
|
|
#include "td/telegram/ChannelId.h"
|
|
|
|
#include "td/telegram/DialogId.h"
|
2022-01-21 14:46:16 +01:00
|
|
|
#include "td/telegram/FullMessageId.h"
|
2022-01-26 18:06:27 +01:00
|
|
|
#include "td/telegram/MessageId.h"
|
2022-01-21 11:15:27 +01:00
|
|
|
#include "td/telegram/MinChannel.h"
|
2023-08-04 17:39:07 +02:00
|
|
|
#include "td/telegram/ReactionType.h"
|
2022-01-20 20:54:34 +01:00
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
2022-09-24 19:58:54 +02:00
|
|
|
#include "td/telegram/UserId.h"
|
2022-01-20 20:54:34 +01:00
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2022-02-07 22:04:34 +01:00
|
|
|
#include "td/utils/FlatHashMap.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2022-01-20 20:54:34 +01:00
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
|
2022-01-21 11:15:27 +01:00
|
|
|
#include <utility>
|
|
|
|
|
2022-01-20 20:54:34 +01:00
|
|
|
namespace td {
|
|
|
|
|
2022-09-24 20:07:35 +02:00
|
|
|
class Dependencies;
|
|
|
|
|
2022-01-20 20:54:34 +01:00
|
|
|
class Td;
|
|
|
|
|
|
|
|
class MessageReaction {
|
2022-09-24 20:32:50 +02:00
|
|
|
static constexpr int32 MAX_CHOOSE_COUNT = 2147483640;
|
|
|
|
|
|
|
|
static constexpr size_t MAX_RECENT_CHOOSERS = 3;
|
|
|
|
|
2023-08-04 17:39:07 +02:00
|
|
|
ReactionType reaction_type_;
|
2022-01-20 20:54:34 +01:00
|
|
|
int32 choose_count_ = 0;
|
|
|
|
bool is_chosen_ = false;
|
2023-04-27 15:20:54 +02:00
|
|
|
DialogId my_recent_chooser_dialog_id_;
|
2022-01-21 11:15:27 +01:00
|
|
|
vector<DialogId> recent_chooser_dialog_ids_;
|
|
|
|
vector<std::pair<ChannelId, MinChannel>> recent_chooser_min_channels_;
|
2022-01-20 20:54:34 +01:00
|
|
|
|
|
|
|
friend bool operator==(const MessageReaction &lhs, const MessageReaction &rhs);
|
|
|
|
|
|
|
|
friend StringBuilder &operator<<(StringBuilder &string_builder, const MessageReaction &message_reaction);
|
|
|
|
|
2022-09-13 14:35:18 +02:00
|
|
|
friend struct MessageReactions;
|
2022-01-20 20:54:34 +01:00
|
|
|
|
2023-08-04 17:39:07 +02:00
|
|
|
MessageReaction(ReactionType reaction_type, int32 choose_count, bool is_chosen, DialogId my_recent_chooser_dialog_id,
|
2023-04-27 15:20:54 +02:00
|
|
|
vector<DialogId> &&recent_chooser_dialog_ids,
|
2023-07-13 12:54:58 +02:00
|
|
|
vector<std::pair<ChannelId, MinChannel>> &&recent_chooser_min_channels);
|
2022-01-20 20:54:34 +01:00
|
|
|
|
|
|
|
bool is_empty() const {
|
|
|
|
return choose_count_ <= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_chosen() const {
|
|
|
|
return is_chosen_;
|
|
|
|
}
|
|
|
|
|
2023-04-27 18:31:31 +02:00
|
|
|
void set_as_chosen(DialogId my_dialog_id, bool have_recent_choosers);
|
2023-04-27 15:42:38 +02:00
|
|
|
|
|
|
|
void unset_as_chosen();
|
2022-01-20 20:54:34 +01:00
|
|
|
|
2022-09-13 14:35:18 +02:00
|
|
|
void add_recent_chooser_dialog_id(DialogId dialog_id);
|
|
|
|
|
2023-04-27 15:20:54 +02:00
|
|
|
bool remove_recent_chooser_dialog_id();
|
2022-09-13 14:35:18 +02:00
|
|
|
|
2023-04-27 15:37:15 +02:00
|
|
|
void update_from(const MessageReaction &old_reaction);
|
|
|
|
|
2022-09-13 14:35:18 +02:00
|
|
|
void update_recent_chooser_dialog_ids(const MessageReaction &old_reaction);
|
|
|
|
|
2022-01-26 10:01:14 +01:00
|
|
|
int32 get_choose_count() const {
|
|
|
|
return choose_count_;
|
|
|
|
}
|
|
|
|
|
2023-04-27 18:31:31 +02:00
|
|
|
void set_my_recent_chooser_dialog_id(DialogId my_dialog_id);
|
|
|
|
|
2023-04-27 15:20:54 +02:00
|
|
|
DialogId get_my_recent_chooser_dialog_id() const {
|
|
|
|
return my_recent_chooser_dialog_id_;
|
|
|
|
}
|
|
|
|
|
2022-01-21 11:15:27 +01:00
|
|
|
const vector<DialogId> &get_recent_chooser_dialog_ids() const {
|
|
|
|
return recent_chooser_dialog_ids_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const vector<std::pair<ChannelId, MinChannel>> &get_recent_chooser_min_channels() const {
|
|
|
|
return recent_chooser_min_channels_;
|
2022-01-20 20:54:34 +01:00
|
|
|
}
|
|
|
|
|
2022-09-14 16:15:12 +02:00
|
|
|
td_api::object_ptr<td_api::messageReaction> get_message_reaction_object(Td *td, UserId my_user_id,
|
|
|
|
UserId peer_user_id) const;
|
2022-01-20 20:54:34 +01:00
|
|
|
|
2022-09-24 20:32:50 +02:00
|
|
|
public:
|
|
|
|
MessageReaction() = default;
|
|
|
|
|
2023-08-04 17:39:07 +02:00
|
|
|
const ReactionType &get_reaction_type() const {
|
|
|
|
return reaction_type_;
|
2022-09-24 20:32:50 +02:00
|
|
|
}
|
|
|
|
|
2022-01-20 20:54:34 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const;
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser);
|
|
|
|
};
|
|
|
|
|
|
|
|
bool operator==(const MessageReaction &lhs, const MessageReaction &rhs);
|
|
|
|
|
|
|
|
inline bool operator!=(const MessageReaction &lhs, const MessageReaction &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2022-01-28 15:10:17 +01:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const MessageReaction &reaction);
|
|
|
|
|
|
|
|
class UnreadMessageReaction {
|
2023-08-04 17:39:07 +02:00
|
|
|
ReactionType reaction_type_;
|
2022-01-28 15:10:17 +01:00
|
|
|
DialogId sender_dialog_id_;
|
|
|
|
bool is_big_ = false;
|
|
|
|
|
|
|
|
friend bool operator==(const UnreadMessageReaction &lhs, const UnreadMessageReaction &rhs);
|
|
|
|
|
|
|
|
friend StringBuilder &operator<<(StringBuilder &string_builder, const UnreadMessageReaction &message_reaction);
|
|
|
|
|
|
|
|
public:
|
|
|
|
UnreadMessageReaction() = default;
|
|
|
|
|
2023-08-04 17:39:07 +02:00
|
|
|
UnreadMessageReaction(ReactionType reaction_type, DialogId sender_dialog_id, bool is_big)
|
|
|
|
: reaction_type_(std::move(reaction_type)), sender_dialog_id_(sender_dialog_id), is_big_(is_big) {
|
2022-01-28 15:10:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::unreadReaction> get_unread_reaction_object(Td *td) const;
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const;
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser);
|
|
|
|
};
|
|
|
|
|
|
|
|
bool operator==(const UnreadMessageReaction &lhs, const UnreadMessageReaction &rhs);
|
|
|
|
|
|
|
|
inline bool operator!=(const UnreadMessageReaction &lhs, const UnreadMessageReaction &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const UnreadMessageReaction &unread_reaction);
|
2022-01-20 20:54:34 +01:00
|
|
|
|
|
|
|
struct MessageReactions {
|
|
|
|
vector<MessageReaction> reactions_;
|
2022-01-28 15:10:17 +01:00
|
|
|
vector<UnreadMessageReaction> unread_reactions_;
|
2023-08-04 17:39:07 +02:00
|
|
|
vector<ReactionType> chosen_reaction_order_;
|
2022-01-20 20:54:34 +01:00
|
|
|
bool is_min_ = false;
|
|
|
|
bool need_polling_ = true;
|
2022-01-30 11:24:27 +01:00
|
|
|
bool can_get_added_reactions_ = false;
|
2022-01-20 20:54:34 +01:00
|
|
|
|
|
|
|
MessageReactions() = default;
|
|
|
|
|
|
|
|
static unique_ptr<MessageReactions> get_message_reactions(Td *td,
|
|
|
|
tl_object_ptr<telegram_api::messageReactions> &&reactions,
|
|
|
|
bool is_bot);
|
|
|
|
|
2023-08-04 17:39:07 +02:00
|
|
|
MessageReaction *get_reaction(const ReactionType &reaction_type);
|
2022-01-26 13:25:03 +01:00
|
|
|
|
2023-08-04 17:39:07 +02:00
|
|
|
const MessageReaction *get_reaction(const ReactionType &reaction_type) const;
|
2022-01-26 14:51:47 +01:00
|
|
|
|
2022-01-20 20:54:34 +01:00
|
|
|
void update_from(const MessageReactions &old_reactions);
|
|
|
|
|
2023-08-04 17:39:07 +02:00
|
|
|
bool add_reaction(const ReactionType &reaction_type, bool is_big, DialogId my_dialog_id, bool have_recent_choosers);
|
2022-09-09 16:43:21 +02:00
|
|
|
|
2023-08-04 17:39:07 +02:00
|
|
|
bool remove_reaction(const ReactionType &reaction_type, DialogId my_dialog_id);
|
2022-09-09 16:43:21 +02:00
|
|
|
|
2023-08-04 17:39:07 +02:00
|
|
|
void sort_reactions(const FlatHashMap<ReactionType, size_t, ReactionTypeHash> &active_reaction_pos);
|
2022-01-26 10:01:14 +01:00
|
|
|
|
2023-04-27 15:20:54 +02:00
|
|
|
void fix_chosen_reaction();
|
|
|
|
|
|
|
|
void fix_my_recent_chooser_dialog_id(DialogId my_dialog_id);
|
2022-04-27 21:13:05 +02:00
|
|
|
|
2023-08-04 17:39:07 +02:00
|
|
|
vector<ReactionType> get_chosen_reaction_types() const;
|
2022-09-11 13:06:01 +02:00
|
|
|
|
2023-08-04 17:39:07 +02:00
|
|
|
bool are_consistent_with_list(const ReactionType &reaction_type,
|
|
|
|
FlatHashMap<ReactionType, vector<DialogId>, ReactionTypeHash> reaction_types,
|
2022-09-24 19:15:38 +02:00
|
|
|
int32 total_count) const;
|
|
|
|
|
2022-09-24 19:58:54 +02:00
|
|
|
vector<td_api::object_ptr<td_api::messageReaction>> get_message_reactions_object(Td *td, UserId my_user_id,
|
|
|
|
UserId peer_user_id) const;
|
|
|
|
|
2022-09-24 20:09:07 +02:00
|
|
|
void add_min_channels(Td *td) const;
|
|
|
|
|
2022-09-24 20:07:35 +02:00
|
|
|
void add_dependencies(Dependencies &dependencies) const;
|
|
|
|
|
2022-01-20 20:54:34 +01:00
|
|
|
static bool need_update_message_reactions(const MessageReactions *old_reactions,
|
|
|
|
const MessageReactions *new_reactions);
|
|
|
|
|
2022-01-28 15:10:17 +01:00
|
|
|
static bool need_update_unread_reactions(const MessageReactions *old_reactions,
|
|
|
|
const MessageReactions *new_reactions);
|
|
|
|
|
2022-01-20 20:54:34 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const;
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser);
|
2022-09-11 14:10:56 +02:00
|
|
|
|
|
|
|
private:
|
2023-08-04 17:39:07 +02:00
|
|
|
bool do_remove_reaction(const ReactionType &reaction_type);
|
2022-01-20 20:54:34 +01:00
|
|
|
};
|
|
|
|
|
2022-01-30 11:24:27 +01:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const MessageReactions &reactions);
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const unique_ptr<MessageReactions> &reactions);
|
|
|
|
|
2022-01-26 16:48:46 +01:00
|
|
|
void reload_message_reactions(Td *td, DialogId dialog_id, vector<MessageId> &&message_ids);
|
|
|
|
|
2023-08-04 17:39:07 +02:00
|
|
|
void send_message_reaction(Td *td, FullMessageId full_message_id, vector<ReactionType> reaction_types, bool is_big,
|
2022-09-09 15:21:00 +02:00
|
|
|
bool add_to_recent, Promise<Unit> &&promise);
|
2022-01-21 18:39:55 +01:00
|
|
|
|
2023-08-04 17:39:07 +02:00
|
|
|
void get_message_added_reactions(Td *td, FullMessageId full_message_id, ReactionType reaction_type, string offset,
|
|
|
|
int32 limit, Promise<td_api::object_ptr<td_api::addedReactions>> &&promise);
|
2022-01-21 14:46:16 +01:00
|
|
|
|
2023-08-04 17:39:07 +02:00
|
|
|
void set_default_reaction(Td *td, ReactionType reaction_type, Promise<Unit> &&promise);
|
2022-08-15 21:47:02 +02:00
|
|
|
|
|
|
|
void send_set_default_reaction_query(Td *td);
|
|
|
|
|
2022-08-23 13:39:10 +02:00
|
|
|
void report_message_reactions(Td *td, FullMessageId full_message_id, DialogId chooser_dialog_id,
|
|
|
|
Promise<Unit> &&promise);
|
|
|
|
|
2023-08-04 17:39:07 +02:00
|
|
|
vector<ReactionType> get_recent_reactions(Td *td);
|
2022-09-13 18:19:35 +02:00
|
|
|
|
2023-08-04 17:39:07 +02:00
|
|
|
vector<ReactionType> get_top_reactions(Td *td);
|
2022-09-12 19:17:14 +02:00
|
|
|
|
2023-08-04 17:39:07 +02:00
|
|
|
void add_recent_reaction(Td *td, const ReactionType &reaction_type);
|
2022-09-12 20:55:30 +02:00
|
|
|
|
2022-01-20 20:54:34 +01:00
|
|
|
} // namespace td
|