tdlight/td/telegram/ChatReactions.cpp

150 lines
5.6 KiB
C++
Raw Normal View History

2022-08-30 10:45:27 +02:00
//
2024-01-01 01:07:21 +01:00
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
2022-08-30 10:45:27 +02: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)
//
#include "td/telegram/ChatReactions.h"
2024-04-22 02:19:40 +02:00
#include "td/telegram/OptionManager.h"
#include "td/telegram/Td.h"
2022-08-30 10:45:27 +02:00
#include "td/utils/algorithm.h"
namespace td {
bool ChatReactions::remove_paid_reactions() {
return td::remove_if(reaction_types_,
[&](const ReactionType &reaction_type) { return reaction_type.is_paid_reaction(); });
}
2024-04-22 02:19:40 +02:00
ChatReactions::ChatReactions(telegram_api::object_ptr<telegram_api::ChatReactions> &&chat_reactions_ptr,
int32 reactions_limit, bool paid_reactions_available)
: paid_reactions_available_(paid_reactions_available) {
2022-08-30 10:45:27 +02:00
if (chat_reactions_ptr == nullptr) {
return;
}
switch (chat_reactions_ptr->get_id()) {
case telegram_api::chatReactionsNone::ID:
break;
case telegram_api::chatReactionsAll::ID: {
auto chat_reactions = move_tl_object_as<telegram_api::chatReactionsAll>(chat_reactions_ptr);
2023-11-19 00:03:19 +01:00
allow_all_regular_ = true;
allow_all_custom_ = chat_reactions->allow_custom_;
2022-08-30 10:45:27 +02:00
break;
}
case telegram_api::chatReactionsSome::ID: {
auto chat_reactions = move_tl_object_as<telegram_api::chatReactionsSome>(chat_reactions_ptr);
reaction_types_ = ReactionType::get_reaction_types(chat_reactions->reactions_);
if (remove_paid_reactions()) {
LOG(ERROR) << "Receive paid reaction allowed";
}
2022-08-30 10:45:27 +02:00
break;
}
default:
UNREACHABLE();
}
2024-04-22 02:19:40 +02:00
reactions_limit_ = reactions_limit;
2022-08-30 10:45:27 +02:00
}
ChatReactions::ChatReactions(td_api::object_ptr<td_api::ChatAvailableReactions> &&chat_reactions_ptr,
2023-11-19 00:03:19 +01:00
bool allow_all_custom) {
2022-08-30 10:45:27 +02:00
if (chat_reactions_ptr == nullptr) {
return;
}
switch (chat_reactions_ptr->get_id()) {
2024-04-22 02:19:40 +02:00
case td_api::chatAvailableReactionsAll::ID: {
auto chat_reactions = move_tl_object_as<td_api::chatAvailableReactionsAll>(chat_reactions_ptr);
2023-11-19 00:03:19 +01:00
allow_all_regular_ = true;
allow_all_custom_ = allow_all_custom;
2024-04-22 02:19:40 +02:00
reactions_limit_ = chat_reactions->max_reaction_count_;
2022-08-30 10:45:27 +02:00
break;
2024-04-22 02:19:40 +02:00
}
2022-08-30 10:45:27 +02:00
case td_api::chatAvailableReactionsSome::ID: {
auto chat_reactions = move_tl_object_as<td_api::chatAvailableReactionsSome>(chat_reactions_ptr);
reaction_types_ = ReactionType::get_reaction_types(chat_reactions->reactions_);
2024-04-22 02:19:40 +02:00
reactions_limit_ = chat_reactions->max_reaction_count_;
paid_reactions_available_ = remove_paid_reactions();
2022-08-30 10:45:27 +02:00
break;
}
default:
UNREACHABLE();
}
}
2023-08-04 17:39:07 +02:00
ChatReactions ChatReactions::get_active_reactions(
const FlatHashMap<ReactionType, size_t, ReactionTypeHash> &active_reaction_pos) const {
2022-09-08 20:04:23 +02:00
ChatReactions result = *this;
2023-08-04 17:39:07 +02:00
if (!reaction_types_.empty()) {
2023-11-19 00:03:19 +01:00
CHECK(!allow_all_regular_);
CHECK(!allow_all_custom_);
2023-08-04 17:39:07 +02:00
td::remove_if(result.reaction_types_, [&](const ReactionType &reaction_type) {
return !reaction_type.is_active_reaction(active_reaction_pos);
});
}
2022-09-08 20:04:23 +02:00
return result;
}
2023-08-04 17:39:07 +02:00
bool ChatReactions::is_allowed_reaction_type(const ReactionType &reaction_type) const {
2023-11-19 00:03:19 +01:00
CHECK(!allow_all_regular_);
if (allow_all_custom_ && reaction_type.is_custom_reaction()) {
return true;
}
if (reaction_type.is_paid_reaction()) {
return paid_reactions_available_;
}
2023-08-04 17:39:07 +02:00
return td::contains(reaction_types_, reaction_type);
}
2024-04-22 02:19:40 +02:00
td_api::object_ptr<td_api::ChatAvailableReactions> ChatReactions::get_chat_available_reactions_object(Td *td) const {
2024-04-30 19:48:24 +02:00
auto reactions_uniq_max = static_cast<int32>(td->option_manager_->get_option_integer("reactions_uniq_max", 11));
2024-04-22 02:19:40 +02:00
if (0 < reactions_limit_ && reactions_limit_ < reactions_uniq_max) {
reactions_uniq_max = reactions_limit_;
}
2023-11-19 00:03:19 +01:00
if (allow_all_regular_) {
LOG_IF(ERROR, paid_reactions_available_) << "Have paid reaction in a non-channel chat";
2024-04-22 02:19:40 +02:00
return td_api::make_object<td_api::chatAvailableReactionsAll>(reactions_uniq_max);
2022-08-30 10:45:27 +02:00
}
return td_api::make_object<td_api::chatAvailableReactionsSome>(
ReactionType::get_reaction_types_object(reaction_types_, paid_reactions_available_), reactions_uniq_max);
2022-08-30 10:45:27 +02:00
}
telegram_api::object_ptr<telegram_api::ChatReactions> ChatReactions::get_input_chat_reactions() const {
2023-11-19 00:03:19 +01:00
if (allow_all_regular_) {
2022-08-30 10:45:27 +02:00
int32 flags = 0;
2023-11-19 00:03:19 +01:00
if (allow_all_custom_) {
2022-08-30 10:45:27 +02:00
flags |= telegram_api::chatReactionsAll::ALLOW_CUSTOM_MASK;
}
2023-11-19 00:03:19 +01:00
return telegram_api::make_object<telegram_api::chatReactionsAll>(flags, allow_all_custom_);
2022-08-30 10:45:27 +02:00
}
2023-08-04 17:39:07 +02:00
if (!reaction_types_.empty()) {
return telegram_api::make_object<telegram_api::chatReactionsSome>(
ReactionType::get_input_reactions(reaction_types_));
2022-08-30 10:45:27 +02:00
}
return telegram_api::make_object<telegram_api::chatReactionsNone>();
}
bool operator==(const ChatReactions &lhs, const ChatReactions &rhs) {
2023-11-19 00:03:19 +01:00
// don't compare allow_all_custom_
2024-04-22 02:19:40 +02:00
return lhs.reaction_types_ == rhs.reaction_types_ && lhs.allow_all_regular_ == rhs.allow_all_regular_ &&
lhs.reactions_limit_ == rhs.reactions_limit_ && lhs.paid_reactions_available_ == rhs.paid_reactions_available_;
2022-08-30 10:45:27 +02:00
}
StringBuilder &operator<<(StringBuilder &string_builder, const ChatReactions &reactions) {
2024-04-22 02:19:40 +02:00
if (reactions.reactions_limit_ != 0) {
string_builder << '[' << reactions.reactions_limit_ << "] ";
}
if (reactions.paid_reactions_available_) {
string_builder << "Paid";
}
2023-11-19 00:03:19 +01:00
if (reactions.allow_all_regular_) {
if (reactions.allow_all_custom_) {
2022-08-30 10:45:27 +02:00
return string_builder << "AllReactions";
}
return string_builder << "AllRegularReactions";
}
2023-08-04 17:39:07 +02:00
return string_builder << '[' << reactions.reaction_types_ << ']';
2022-08-30 10:45:27 +02:00
}
} // namespace td