Add static methods for processing of vector<ReactionType>.

This commit is contained in:
levlam 2023-12-14 18:46:52 +03:00
parent 0a53e5ad0e
commit 0072ff225b
5 changed files with 45 additions and 21 deletions

View File

@ -25,9 +25,7 @@ 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_ = transform(
chat_reactions->reactions_,
[](const telegram_api::object_ptr<telegram_api::Reaction> &reaction) { return ReactionType(reaction); });
reaction_types_ = ReactionType::get_reaction_types(chat_reactions->reactions_);
break;
}
default:
@ -47,9 +45,7 @@ ChatReactions::ChatReactions(td_api::object_ptr<td_api::ChatAvailableReactions>
break;
case td_api::chatAvailableReactionsSome::ID: {
auto chat_reactions = move_tl_object_as<td_api::chatAvailableReactionsSome>(chat_reactions_ptr);
reaction_types_ =
transform(chat_reactions->reactions_,
[](const td_api::object_ptr<td_api::ReactionType> &reaction) { return ReactionType(reaction); });
reaction_types_ = ReactionType::get_reaction_types(chat_reactions->reactions_);
break;
}
default:
@ -82,8 +78,8 @@ td_api::object_ptr<td_api::ChatAvailableReactions> ChatReactions::get_chat_avail
if (allow_all_regular_) {
return td_api::make_object<td_api::chatAvailableReactionsAll>();
}
return td_api::make_object<td_api::chatAvailableReactionsSome>(transform(
reaction_types_, [](const ReactionType &reaction_type) { return reaction_type.get_reaction_type_object(); }));
return td_api::make_object<td_api::chatAvailableReactionsSome>(
ReactionType::get_reaction_types_object(reaction_types_));
}
telegram_api::object_ptr<telegram_api::ChatReactions> ChatReactions::get_input_chat_reactions() const {
@ -95,8 +91,8 @@ telegram_api::object_ptr<telegram_api::ChatReactions> ChatReactions::get_input_c
return telegram_api::make_object<telegram_api::chatReactionsAll>(flags, allow_all_custom_);
}
if (!reaction_types_.empty()) {
return telegram_api::make_object<telegram_api::chatReactionsSome>(transform(
reaction_types_, [](const ReactionType &reaction_type) { return reaction_type.get_input_reaction(); }));
return telegram_api::make_object<telegram_api::chatReactionsSome>(
ReactionType::get_input_reactions(reaction_types_));
}
return telegram_api::make_object<telegram_api::chatReactionsNone>();
}

View File

@ -123,11 +123,9 @@ class SendReactionQuery final : public Td::ResultHandler {
}
send_query(G()->net_query_creator().create(
telegram_api::messages_sendReaction(
flags, false /*ignored*/, false /*ignored*/, std::move(input_peer),
message_full_id.get_message_id().get_server_message_id().get(),
transform(reaction_types,
[](const ReactionType &reaction_type) { return reaction_type.get_input_reaction(); })),
telegram_api::messages_sendReaction(flags, false /*ignored*/, false /*ignored*/, std::move(input_peer),
message_full_id.get_message_id().get_server_message_id().get(),
ReactionType::get_input_reactions(reaction_types)),
{{dialog_id_}, {message_full_id}}));
}

View File

@ -627,9 +627,7 @@ void ReactionManager::on_get_recent_reactions(tl_object_ptr<telegram_api::messag
CHECK(constructor_id == telegram_api::messages_reactions::ID);
auto reactions = move_tl_object_as<telegram_api::messages_reactions>(reactions_ptr);
auto new_reaction_types = transform(
reactions->reactions_,
[](const telegram_api::object_ptr<telegram_api::Reaction> &reaction) { return ReactionType(reaction); });
auto new_reaction_types = ReactionType::get_reaction_types(reactions->reactions_);
if (new_reaction_types == recent_reactions_.reaction_types_ && recent_reactions_.hash_ == reactions->hash_) {
LOG(INFO) << "Top reactions are not modified";
return;
@ -663,9 +661,7 @@ void ReactionManager::on_get_top_reactions(tl_object_ptr<telegram_api::messages_
CHECK(constructor_id == telegram_api::messages_reactions::ID);
auto reactions = move_tl_object_as<telegram_api::messages_reactions>(reactions_ptr);
auto new_reaction_types = transform(
reactions->reactions_,
[](const telegram_api::object_ptr<telegram_api::Reaction> &reaction) { return ReactionType(reaction); });
auto new_reaction_types = ReactionType::get_reaction_types(reactions->reactions_);
if (new_reaction_types == top_reactions_.reaction_types_ && top_reactions_.hash_ == reactions->hash_) {
LOG(INFO) << "Top reactions are not modified";
return;

View File

@ -8,6 +8,7 @@
#include "td/telegram/misc.h"
#include "td/utils/algorithm.h"
#include "td/utils/as.h"
#include "td/utils/base64.h"
#include "td/utils/crypto.h"
@ -84,6 +85,28 @@ ReactionType::ReactionType(const td_api::object_ptr<td_api::ReactionType> &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); });
}
vector<ReactionType> ReactionType::get_reaction_types(
const vector<td_api::object_ptr<td_api::ReactionType>> &reactions) {
return transform(reactions, [](const auto &reaction) { return ReactionType(reaction); });
}
vector<telegram_api::object_ptr<telegram_api::Reaction>> ReactionType::get_input_reactions(
const vector<ReactionType> &reaction_types) {
return transform(reaction_types,
[](const ReactionType &reaction_type) { return reaction_type.get_input_reaction(); });
}
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(); });
}
telegram_api::object_ptr<telegram_api::Reaction> ReactionType::get_input_reaction() const {
if (is_empty()) {
return telegram_api::make_object<telegram_api::reactionEmpty>();

View File

@ -38,6 +38,17 @@ class ReactionType {
explicit ReactionType(const td_api::object_ptr<td_api::ReactionType> &type);
static vector<ReactionType> get_reaction_types(
const vector<telegram_api::object_ptr<telegram_api::Reaction>> &reactions);
static vector<ReactionType> get_reaction_types(const vector<td_api::object_ptr<td_api::ReactionType>> &reactions);
static vector<telegram_api::object_ptr<telegram_api::Reaction>> get_input_reactions(
const vector<ReactionType> &reaction_types);
static vector<td_api::object_ptr<td_api::ReactionType>> get_reaction_types_object(
const vector<ReactionType> &reaction_types);
telegram_api::object_ptr<telegram_api::Reaction> get_input_reaction() const;
td_api::object_ptr<td_api::ReactionType> get_reaction_type_object() const;