Add class AvailableReaction.

This commit is contained in:
levlam 2022-06-09 01:07:53 +03:00
parent 2570044eba
commit cb98c0a10a
7 changed files with 81 additions and 31 deletions

View File

@ -283,6 +283,7 @@ set(TDLIB_SOURCE
td/telegram/AudiosManager.cpp td/telegram/AudiosManager.cpp
td/telegram/AuthManager.cpp td/telegram/AuthManager.cpp
td/telegram/AutoDownloadSettings.cpp td/telegram/AutoDownloadSettings.cpp
td/telegram/AvailableReaction.cpp
td/telegram/BackgroundManager.cpp td/telegram/BackgroundManager.cpp
td/telegram/BackgroundType.cpp td/telegram/BackgroundType.cpp
td/telegram/BotCommand.cpp td/telegram/BotCommand.cpp
@ -480,6 +481,7 @@ set(TDLIB_SOURCE
td/telegram/AudiosManager.h td/telegram/AudiosManager.h
td/telegram/AuthManager.h td/telegram/AuthManager.h
td/telegram/AutoDownloadSettings.h td/telegram/AutoDownloadSettings.h
td/telegram/AvailableReaction.h
td/telegram/BackgroundId.h td/telegram/BackgroundId.h
td/telegram/BackgroundManager.h td/telegram/BackgroundManager.h
td/telegram/BackgroundType.h td/telegram/BackgroundType.h

View File

@ -0,0 +1,19 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// 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/AvailableReaction.h"
namespace td {
td_api::object_ptr<td_api::availableReaction> AvailableReaction::get_available_reaction_object() const {
return td_api::make_object<td_api::availableReaction>(reaction_, is_premium_);
}
bool operator==(const AvailableReaction &lhs, const AvailableReaction &rhs) {
return lhs.reaction_ == rhs.reaction_ && lhs.is_premium_ == rhs.is_premium_;
}
} // namespace td

View File

@ -0,0 +1,31 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// 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/td_api.h"
#include "td/utils/common.h"
namespace td {
struct AvailableReaction {
string reaction_;
bool is_premium_;
AvailableReaction(const string &reaction, bool is_premium) : reaction_(reaction), is_premium_(is_premium) {
}
td_api::object_ptr<td_api::availableReaction> get_available_reaction_object() const;
};
bool operator==(const AvailableReaction &lhs, const AvailableReaction &rhs);
inline bool operator!=(const AvailableReaction &lhs, const AvailableReaction &rhs) {
return !(lhs == rhs);
}
} // namespace td

View File

@ -8199,7 +8199,7 @@ void MessagesManager::hide_dialog_message_reactions(Dialog *d) {
} }
} }
void MessagesManager::set_active_reactions(vector<std::pair<string, bool>> active_reactions) { void MessagesManager::set_active_reactions(vector<AvailableReaction> active_reactions) {
if (active_reactions == active_reactions_) { if (active_reactions == active_reactions_) {
return; return;
} }
@ -8209,8 +8209,8 @@ void MessagesManager::set_active_reactions(vector<std::pair<string, bool>> activ
active_reaction_pos_.clear(); active_reaction_pos_.clear();
bool is_changed = old_active_reactions.size() != active_reactions_.size(); bool is_changed = old_active_reactions.size() != active_reactions_.size();
for (size_t i = 0; i < active_reactions_.size(); i++) { for (size_t i = 0; i < active_reactions_.size(); i++) {
active_reaction_pos_[active_reactions_[i].first] = i; active_reaction_pos_[active_reactions_[i].reaction_] = i;
if (!is_changed && active_reactions_[i].first != old_active_reactions[i].first) { if (!is_changed && active_reactions_[i].reaction_ != old_active_reactions[i].reaction_) {
is_changed = true; is_changed = true;
} }
} }
@ -8253,7 +8253,7 @@ vector<string> MessagesManager::get_active_reactions(const vector<string> &avail
} }
vector<string> MessagesManager::get_active_reactions(const vector<string> &available_reactions, vector<string> MessagesManager::get_active_reactions(const vector<string> &available_reactions,
const vector<std::pair<string, bool>> &active_reactions) { const vector<AvailableReaction> &active_reactions) {
if (available_reactions.empty()) { if (available_reactions.empty()) {
// fast path // fast path
return available_reactions; return available_reactions;
@ -8261,7 +8261,7 @@ vector<string> MessagesManager::get_active_reactions(const vector<string> &avail
if (available_reactions.size() == active_reactions.size()) { if (available_reactions.size() == active_reactions.size()) {
size_t i; size_t i;
for (i = 0; i < available_reactions.size(); i++) { for (i = 0; i < available_reactions.size(); i++) {
if (available_reactions[i] != active_reactions[i].first) { if (available_reactions[i] != active_reactions[i].reaction_) {
break; break;
} }
} }
@ -8273,18 +8273,18 @@ vector<string> MessagesManager::get_active_reactions(const vector<string> &avail
vector<string> result; vector<string> result;
for (const auto &active_reaction : active_reactions) { for (const auto &active_reaction : active_reactions) {
if (td::contains(available_reactions, active_reaction.first)) { if (td::contains(available_reactions, active_reaction.reaction_)) {
result.push_back(active_reaction.first); result.push_back(active_reaction.reaction_);
} }
} }
return result; return result;
} }
MessagesManager::AvailableReactionType MessagesManager::get_reaction_type( MessagesManager::AvailableReactionType MessagesManager::get_reaction_type(
const vector<std::pair<string, bool>> &reactions, const string &reaction) { const vector<AvailableReaction> &available_reactions, const string &reaction) {
for (auto &reaction_premium : reactions) { for (auto &available_reaction : available_reactions) {
if (reaction_premium.first == reaction) { if (available_reaction.reaction_ == reaction) {
if (reaction_premium.second) { if (available_reaction.is_premium_) {
return AvailableReactionType::NeedsPremium; return AvailableReactionType::NeedsPremium;
} }
return AvailableReactionType::Available; return AvailableReactionType::Available;
@ -8297,7 +8297,7 @@ vector<string> MessagesManager::get_dialog_active_reactions(const Dialog *d) con
CHECK(d != nullptr); CHECK(d != nullptr);
switch (d->dialog_id.get_type()) { switch (d->dialog_id.get_type()) {
case DialogType::User: case DialogType::User:
return transform(active_reactions_, [](auto &reaction) { return reaction.first; }); return transform(active_reactions_, [](auto &reaction) { return reaction.reaction_; });
case DialogType::Chat: case DialogType::Chat:
case DialogType::Channel: case DialogType::Channel:
return get_active_reactions(d->available_reactions); return get_active_reactions(d->available_reactions);
@ -24264,8 +24264,7 @@ void MessagesManager::on_get_scheduled_messages_from_database(DialogId dialog_id
set_promises(promises); set_promises(promises);
} }
Result<vector<std::pair<string, bool>>> MessagesManager::get_message_available_reactions( Result<vector<AvailableReaction>> MessagesManager::get_message_available_reactions(FullMessageId full_message_id) {
FullMessageId full_message_id) {
auto dialog_id = full_message_id.get_dialog_id(); auto dialog_id = full_message_id.get_dialog_id();
Dialog *d = get_dialog_force(dialog_id, "get_message_available_reactions"); Dialog *d = get_dialog_force(dialog_id, "get_message_available_reactions");
if (d == nullptr) { if (d == nullptr) {
@ -24279,7 +24278,7 @@ Result<vector<std::pair<string, bool>>> MessagesManager::get_message_available_r
return get_message_available_reactions(d, m); return get_message_available_reactions(d, m);
} }
vector<std::pair<string, bool>> MessagesManager::get_message_available_reactions(const Dialog *d, const Message *m) { vector<AvailableReaction> MessagesManager::get_message_available_reactions(const Dialog *d, const Message *m) {
CHECK(d != nullptr); CHECK(d != nullptr);
CHECK(m != nullptr); CHECK(m != nullptr);
auto active_reactions = get_message_active_reactions(d, m); auto active_reactions = get_message_active_reactions(d, m);
@ -24288,16 +24287,16 @@ vector<std::pair<string, bool>> MessagesManager::get_message_available_reactions
} }
bool is_premium = G()->shared_config().get_option_boolean("is_premium"); bool is_premium = G()->shared_config().get_option_boolean("is_premium");
vector<std::pair<string, bool>> result; vector<AvailableReaction> result;
int64 reactions_uniq_max = G()->shared_config().get_option_integer("reactions_uniq_max", 11); int64 reactions_uniq_max = G()->shared_config().get_option_integer("reactions_uniq_max", 11);
bool can_add_new_reactions = bool can_add_new_reactions =
m->reactions == nullptr || static_cast<int64>(m->reactions->reactions_.size()) < reactions_uniq_max; m->reactions == nullptr || static_cast<int64>(m->reactions->reactions_.size()) < reactions_uniq_max;
// can add only active available reactions or remove previously set reaction // can add only active available reactions or remove previously set reaction
for (const auto &active_reaction : active_reactions_) { for (const auto &active_reaction : active_reactions_) {
// can add the reaction if it has already been used for the message or is available in the chat // can add the reaction if it has already been used for the message or is available in the chat
bool is_set = (m->reactions != nullptr && m->reactions->get_reaction(active_reaction.first) != nullptr); bool is_set = (m->reactions != nullptr && m->reactions->get_reaction(active_reaction.reaction_) != nullptr);
if (is_set || (can_add_new_reactions && td::contains(active_reactions, active_reaction.first))) { if (is_set || (can_add_new_reactions && td::contains(active_reactions, active_reaction.reaction_))) {
result.emplace_back(active_reaction.first, !is_premium && active_reaction.second && !is_set); result.emplace_back(active_reaction.reaction_, !is_premium && active_reaction.is_premium_ && !is_set);
} }
} }
if (m->reactions != nullptr) { if (m->reactions != nullptr) {

View File

@ -8,6 +8,7 @@
#include "td/telegram/AccessRights.h" #include "td/telegram/AccessRights.h"
#include "td/telegram/AffectedHistory.h" #include "td/telegram/AffectedHistory.h"
#include "td/telegram/AvailableReaction.h"
#include "td/telegram/ChannelId.h" #include "td/telegram/ChannelId.h"
#include "td/telegram/DialogAction.h" #include "td/telegram/DialogAction.h"
#include "td/telegram/DialogDate.h" #include "td/telegram/DialogDate.h"
@ -535,7 +536,7 @@ class MessagesManager final : public Actor {
void set_dialog_description(DialogId dialog_id, const string &description, Promise<Unit> &&promise); void set_dialog_description(DialogId dialog_id, const string &description, Promise<Unit> &&promise);
void set_active_reactions(vector<std::pair<string, bool>> active_reactions); void set_active_reactions(vector<AvailableReaction> active_reactions);
void set_dialog_available_reactions(DialogId dialog_id, vector<string> available_reactions, Promise<Unit> &&promise); void set_dialog_available_reactions(DialogId dialog_id, vector<string> available_reactions, Promise<Unit> &&promise);
@ -801,7 +802,7 @@ class MessagesManager final : public Actor {
vector<MessageId> get_dialog_scheduled_messages(DialogId dialog_id, bool force, bool ignore_result, vector<MessageId> get_dialog_scheduled_messages(DialogId dialog_id, bool force, bool ignore_result,
Promise<Unit> &&promise); Promise<Unit> &&promise);
Result<vector<std::pair<string, bool>>> get_message_available_reactions(FullMessageId full_message_id); Result<vector<AvailableReaction>> get_message_available_reactions(FullMessageId full_message_id);
void set_message_reaction(FullMessageId full_message_id, string reaction, bool is_big, Promise<Unit> &&promise); void set_message_reaction(FullMessageId full_message_id, string reaction, bool is_big, Promise<Unit> &&promise);
@ -2646,7 +2647,7 @@ class MessagesManager final : public Actor {
bool update_dialog_silent_send_message(Dialog *d, bool silent_send_message); bool update_dialog_silent_send_message(Dialog *d, bool silent_send_message);
vector<std::pair<string, bool>> get_message_available_reactions(const Dialog *d, const Message *m); vector<AvailableReaction> get_message_available_reactions(const Dialog *d, const Message *m);
void on_set_message_reaction(FullMessageId full_message_id, Result<Unit> result, Promise<Unit> promise); void on_set_message_reaction(FullMessageId full_message_id, Result<Unit> result, Promise<Unit> promise);
@ -2659,11 +2660,10 @@ class MessagesManager final : public Actor {
vector<string> get_active_reactions(const vector<string> &available_reactions) const; vector<string> get_active_reactions(const vector<string> &available_reactions) const;
static vector<string> get_active_reactions(const vector<string> &available_reactions, static vector<string> get_active_reactions(const vector<string> &available_reactions,
const vector<std::pair<string, bool>> &active_reactions); const vector<AvailableReaction> &active_reactions);
enum class AvailableReactionType { Unavailable, Available, NeedsPremium }; enum class AvailableReactionType { Unavailable, Available, NeedsPremium };
static AvailableReactionType get_reaction_type(const vector<std::pair<string, bool>> &reactions, static AvailableReactionType get_reaction_type(const vector<AvailableReaction> &reactions, const string &reaction);
const string &reaction);
vector<string> get_dialog_active_reactions(const Dialog *d) const; vector<string> get_dialog_active_reactions(const Dialog *d) const;
@ -3690,7 +3690,7 @@ class MessagesManager final : public Actor {
}; };
FlatHashMap<FullMessageId, PendingReaction, FullMessageIdHash> pending_reactions_; FlatHashMap<FullMessageId, PendingReaction, FullMessageIdHash> pending_reactions_;
vector<std::pair<string, bool>> active_reactions_; vector<AvailableReaction> active_reactions_;
FlatHashMap<string, size_t> active_reaction_pos_; FlatHashMap<string, size_t> active_reaction_pos_;
uint32 scheduled_messages_sync_generation_ = 1; uint32 scheduled_messages_sync_generation_ = 1;

View File

@ -8,6 +8,7 @@
#include "td/telegram/AccessRights.h" #include "td/telegram/AccessRights.h"
#include "td/telegram/AuthManager.h" #include "td/telegram/AuthManager.h"
#include "td/telegram/AvailableReaction.h"
#include "td/telegram/ConfigManager.h" #include "td/telegram/ConfigManager.h"
#include "td/telegram/ConfigShared.h" #include "td/telegram/ConfigShared.h"
#include "td/telegram/ContactsManager.h" #include "td/telegram/ContactsManager.h"
@ -65,7 +66,6 @@
#include <cmath> #include <cmath>
#include <limits> #include <limits>
#include <type_traits> #include <type_traits>
#include <utility>
namespace td { namespace td {
@ -3278,7 +3278,7 @@ void StickersManager::load_reactions() {
} }
void StickersManager::update_active_reactions() { void StickersManager::update_active_reactions() {
vector<std::pair<string, bool>> active_reactions; vector<AvailableReaction> active_reactions;
for (auto &reaction : reactions_.reactions_) { for (auto &reaction : reactions_.reactions_) {
if (reaction.is_active_) { if (reaction.is_active_) {
active_reactions.emplace_back(reaction.reaction_, reaction.is_premium_); active_reactions.emplace_back(reaction.reaction_, reaction.is_premium_);

View File

@ -5319,9 +5319,8 @@ void Td::on_request(uint64 id, const td_api::getMessageAvailableReactions &reque
if (r_reactions.is_error()) { if (r_reactions.is_error()) {
send_closure(actor_id(this), &Td::send_error, id, r_reactions.move_as_error()); send_closure(actor_id(this), &Td::send_error, id, r_reactions.move_as_error());
} else { } else {
auto reactions = transform(r_reactions.ok(), [](auto &reaction) { auto reactions =
return td_api::make_object<td_api::availableReaction>(reaction.first, reaction.second); transform(r_reactions.ok(), [](auto &reaction) { return reaction.get_available_reaction_object(); });
});
send_closure(actor_id(this), &Td::send_result, id, send_closure(actor_id(this), &Td::send_result, id,
td_api::make_object<td_api::availableReactions>(std::move(reactions))); td_api::make_object<td_api::availableReactions>(std::move(reactions)));
} }