Allow to enable paid reaction in channels.

This commit is contained in:
levlam 2024-07-26 15:31:09 +03:00
parent 1438e1bd88
commit c583fb2fcf
11 changed files with 62 additions and 21 deletions

View File

@ -1963,7 +1963,8 @@ chatPosition list:ChatList order:int64 is_pinned:Bool source:ChatSource = ChatPo
//@class ChatAvailableReactions @description Describes reactions available in the chat
//@description All reactions are available in the chat @max_reaction_count The maximum allowed number of reactions per message; 1-11
//@description All reactions are available in the chat, excluding the paid reaction and custom reactions in channel chats
//@max_reaction_count The maximum allowed number of reactions per message; 1-11
chatAvailableReactionsAll max_reaction_count:int32 = ChatAvailableReactions;
//@description Only specific reactions are available in the chat @reactions The list of reactions @max_reaction_count The maximum allowed number of reactions per message; 1-11

View File

@ -5265,7 +5265,7 @@ void ChatManager::on_get_chat_full(tl_object_ptr<telegram_api::ChatFull> &&chat_
"on_get_chat_full");
td_->messages_manager_->on_update_dialog_available_reactions(
DialogId(chat_id), std::move(chat->available_reactions_), chat->reactions_limit_);
DialogId(chat_id), std::move(chat->available_reactions_), chat->reactions_limit_, false);
td_->messages_manager_->on_update_dialog_theme_name(DialogId(chat_id), std::move(chat->theme_emoticon_));
@ -5322,7 +5322,8 @@ void ChatManager::on_get_chat_full(tl_object_ptr<telegram_api::ChatFull> &&chat_
td_->messages_manager_->on_update_dialog_background(DialogId(channel_id), std::move(channel->wallpaper_));
td_->messages_manager_->on_update_dialog_available_reactions(
DialogId(channel_id), std::move(channel->available_reactions_), channel->reactions_limit_);
DialogId(channel_id), std::move(channel->available_reactions_), channel->reactions_limit_,
channel->paid_reactions_available_);
td_->messages_manager_->on_update_dialog_theme_name(DialogId(channel_id), std::move(channel->theme_emoticon_));

View File

@ -13,8 +13,14 @@
namespace td {
bool ChatReactions::remove_paid_reactions() {
return td::remove_if(reaction_types_,
[&](const ReactionType &reaction_type) { return reaction_type.is_paid_reaction(); });
}
ChatReactions::ChatReactions(telegram_api::object_ptr<telegram_api::ChatReactions> &&chat_reactions_ptr,
int32 reactions_limit) {
int32 reactions_limit, bool paid_reactions_available)
: paid_reactions_available_(paid_reactions_available) {
if (chat_reactions_ptr == nullptr) {
return;
}
@ -30,6 +36,9 @@ ChatReactions::ChatReactions(telegram_api::object_ptr<telegram_api::ChatReaction
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";
}
break;
}
default:
@ -55,6 +64,7 @@ ChatReactions::ChatReactions(td_api::object_ptr<td_api::ChatAvailableReactions>
auto chat_reactions = move_tl_object_as<td_api::chatAvailableReactionsSome>(chat_reactions_ptr);
reaction_types_ = ReactionType::get_reaction_types(chat_reactions->reactions_);
reactions_limit_ = chat_reactions->max_reaction_count_;
paid_reactions_available_ = remove_paid_reactions();
break;
}
default:
@ -80,6 +90,9 @@ bool ChatReactions::is_allowed_reaction_type(const ReactionType &reaction_type)
if (allow_all_custom_ && reaction_type.is_custom_reaction()) {
return true;
}
if (reaction_type.is_paid_reaction()) {
return paid_reactions_available_;
}
return td::contains(reaction_types_, reaction_type);
}
@ -89,10 +102,11 @@ td_api::object_ptr<td_api::ChatAvailableReactions> ChatReactions::get_chat_avail
reactions_uniq_max = reactions_limit_;
}
if (allow_all_regular_) {
LOG_IF(ERROR, paid_reactions_available_) << "Have paid reaction in a non-channel chat";
return td_api::make_object<td_api::chatAvailableReactionsAll>(reactions_uniq_max);
}
return td_api::make_object<td_api::chatAvailableReactionsSome>(
ReactionType::get_reaction_types_object(reaction_types_), reactions_uniq_max);
ReactionType::get_reaction_types_object(reaction_types_, paid_reactions_available_), reactions_uniq_max);
}
telegram_api::object_ptr<telegram_api::ChatReactions> ChatReactions::get_input_chat_reactions() const {
@ -113,13 +127,16 @@ telegram_api::object_ptr<telegram_api::ChatReactions> ChatReactions::get_input_c
bool operator==(const ChatReactions &lhs, const ChatReactions &rhs) {
// don't compare allow_all_custom_
return lhs.reaction_types_ == rhs.reaction_types_ && lhs.allow_all_regular_ == rhs.allow_all_regular_ &&
lhs.reactions_limit_ == rhs.reactions_limit_;
lhs.reactions_limit_ == rhs.reactions_limit_ && lhs.paid_reactions_available_ == rhs.paid_reactions_available_;
}
StringBuilder &operator<<(StringBuilder &string_builder, const ChatReactions &reactions) {
if (reactions.reactions_limit_ != 0) {
string_builder << '[' << reactions.reactions_limit_ << "] ";
}
if (reactions.paid_reactions_available_) {
string_builder << "Paid";
}
if (reactions.allow_all_regular_) {
if (reactions.allow_all_custom_) {
return string_builder << "AllReactions";

View File

@ -23,6 +23,7 @@ struct ChatReactions {
bool allow_all_regular_ = false; // implies empty reaction_types_
bool allow_all_custom_ = false; // implies allow_all_regular_
int32 reactions_limit_ = 0;
bool paid_reactions_available_ = false;
ChatReactions() = default;
@ -32,7 +33,8 @@ struct ChatReactions {
return result;
}
ChatReactions(telegram_api::object_ptr<telegram_api::ChatReactions> &&chat_reactions_ptr, int32 reactions_limit);
ChatReactions(telegram_api::object_ptr<telegram_api::ChatReactions> &&chat_reactions_ptr, int32 reactions_limit,
bool paid_reactions_available);
ChatReactions(td_api::object_ptr<td_api::ChatAvailableReactions> &&chat_reactions_ptr, bool allow_all_custom);
@ -50,9 +52,11 @@ struct ChatReactions {
td_api::object_ptr<td_api::ChatAvailableReactions> get_chat_available_reactions_object(Td *td) const;
bool empty() const {
return reaction_types_.empty() && !allow_all_regular_;
return reaction_types_.empty() && !allow_all_regular_ && !paid_reactions_available_;
}
bool remove_paid_reactions();
template <class StorerT>
void store(StorerT &storer) const;

View File

@ -23,6 +23,7 @@ void ChatReactions::store(StorerT &storer) const {
STORE_FLAG(allow_all_custom_);
STORE_FLAG(has_reactions);
STORE_FLAG(has_reactions_limit);
STORE_FLAG(paid_reactions_available_);
END_STORE_FLAGS();
if (has_reactions) {
td::store(reaction_types_, storer);
@ -41,6 +42,7 @@ void ChatReactions::parse(ParserT &parser) {
PARSE_FLAG(allow_all_custom_);
PARSE_FLAG(has_reactions);
PARSE_FLAG(has_reactions_limit);
PARSE_FLAG(paid_reactions_available_);
END_PARSE_FLAGS();
if (has_reactions) {
td::parse(reaction_types_, parser);

View File

@ -372,8 +372,8 @@ static td_api::object_ptr<td_api::ChatEventAction> get_chat_event_action_object(
}
case telegram_api::channelAdminLogEventActionChangeAvailableReactions::ID: {
auto action = move_tl_object_as<telegram_api::channelAdminLogEventActionChangeAvailableReactions>(action_ptr);
ChatReactions old_available_reactions(std::move(action->prev_value_), 0);
ChatReactions new_available_reactions(std::move(action->new_value_), 0);
ChatReactions old_available_reactions(std::move(action->prev_value_), 0, false);
ChatReactions new_available_reactions(std::move(action->new_value_), 0, false);
return td_api::make_object<td_api::chatEventAvailableReactionsChanged>(
old_available_reactions.get_chat_available_reactions_object(td),
new_available_reactions.get_chat_available_reactions_object(td));

View File

@ -811,13 +811,13 @@ class SetChatAvailableReactionsQuery final : public Td::ResultHandler {
if (input_peer == nullptr) {
return on_error(Status::Error(400, "Can't access the chat"));
}
int32 flags = 0;
int32 flags = telegram_api::messages_setChatAvailableReactions::PAID_ENABLED_MASK;
if (available_reactions.reactions_limit_ != 0) {
flags |= telegram_api::messages_setChatAvailableReactions::REACTIONS_LIMIT_MASK;
}
send_query(G()->net_query_creator().create(telegram_api::messages_setChatAvailableReactions(
flags, std::move(input_peer), available_reactions.get_input_chat_reactions(),
available_reactions.reactions_limit_, false)));
available_reactions.reactions_limit_, available_reactions.paid_reactions_available_)));
}
void on_result(BufferSlice packet) final {
@ -7611,13 +7611,14 @@ void MessagesManager::on_update_dialog_notify_settings(
void MessagesManager::on_update_dialog_available_reactions(
DialogId dialog_id, telegram_api::object_ptr<telegram_api::ChatReactions> &&available_reactions,
int32 reactions_limit) {
int32 reactions_limit, bool paid_reactions_available) {
Dialog *d = get_dialog_force(dialog_id, "on_update_dialog_available_reactions");
if (d == nullptr) {
return;
}
set_dialog_available_reactions(d, ChatReactions(std::move(available_reactions), reactions_limit));
set_dialog_available_reactions(
d, ChatReactions(std::move(available_reactions), reactions_limit, paid_reactions_available));
}
void MessagesManager::set_dialog_available_reactions(Dialog *d, ChatReactions &&available_reactions) {

View File

@ -849,7 +849,7 @@ class MessagesManager final : public Actor {
void on_update_dialog_available_reactions(DialogId dialog_id,
telegram_api::object_ptr<telegram_api::ChatReactions> &&available_reactions,
int32 reactions_limit);
int32 reactions_limit, bool paid_reactions_available);
void hide_dialog_action_bar(DialogId dialog_id);

View File

@ -91,6 +91,12 @@ ReactionType::ReactionType(const td_api::object_ptr<td_api::ReactionType> &type)
}
}
ReactionType ReactionType::paid() {
ReactionType reaction_type;
reaction_type.reaction_ = "$";
return reaction_type;
}
vector<ReactionType> ReactionType::get_reaction_types(
const vector<telegram_api::object_ptr<telegram_api::Reaction>> &reactions) {
return transform(reactions, [](const auto &reaction) { return ReactionType(reaction); });
@ -108,9 +114,16 @@ vector<telegram_api::object_ptr<telegram_api::Reaction>> ReactionType::get_input
}
vector<td_api::object_ptr<td_api::ReactionType>> ReactionType::get_reaction_types_object(
const vector<ReactionType> &reaction_types) {
return transform(reaction_types,
[](const ReactionType &reaction_type) { return reaction_type.get_reaction_type_object(); });
const vector<ReactionType> &reaction_types, bool paid_reactions_available) {
vector<td_api::object_ptr<td_api::ReactionType>> result;
result.reserve(reaction_types.size() + (paid_reactions_available ? 1 : 0));
if (paid_reactions_available) {
result.push_back(paid().get_reaction_type_object());
}
for (auto &reaction_type : reaction_types) {
result.push_back(reaction_type.get_reaction_type_object());
}
return result;
}
telegram_api::object_ptr<telegram_api::Reaction> ReactionType::get_input_reaction() const {

View File

@ -38,6 +38,8 @@ class ReactionType {
explicit ReactionType(const td_api::object_ptr<td_api::ReactionType> &type);
static ReactionType paid();
static vector<ReactionType> get_reaction_types(
const vector<telegram_api::object_ptr<telegram_api::Reaction>> &reactions);
@ -47,7 +49,7 @@ class ReactionType {
const vector<ReactionType> &reaction_types);
static vector<td_api::object_ptr<td_api::ReactionType>> get_reaction_types_object(
const vector<ReactionType> &reaction_types);
const vector<ReactionType> &reaction_types, bool paid_reactions_available);
telegram_api::object_ptr<telegram_api::Reaction> get_input_reaction() const;

View File

@ -3059,8 +3059,8 @@ void UpdatesManager::process_qts_update(tl_object_ptr<telegram_api::Update> &&up
td_api::make_object<td_api::updateMessageReaction>(
td_->dialog_manager_->get_chat_id_object(dialog_id, "updateMessageReaction"), message_id.get(),
get_message_sender_object(td_, actor_dialog_id, "updateMessageReaction"), date,
ReactionType::get_reaction_types_object(old_reaction_types),
ReactionType::get_reaction_types_object(new_reaction_types)));
ReactionType::get_reaction_types_object(old_reaction_types, false),
ReactionType::get_reaction_types_object(new_reaction_types, false)));
break;
}
case telegram_api::updateBotMessageReactions::ID: {