2022-01-20 20:54:34 +01:00
|
|
|
//
|
|
|
|
// 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/MessageReaction.h"
|
|
|
|
|
2022-01-31 13:56:44 +01:00
|
|
|
#include "td/telegram/AccessRights.h"
|
2022-01-20 20:54:34 +01:00
|
|
|
#include "td/telegram/ContactsManager.h"
|
2022-01-31 13:56:44 +01:00
|
|
|
#include "td/telegram/Global.h"
|
2022-01-22 21:17:41 +01:00
|
|
|
#include "td/telegram/MessageSender.h"
|
2022-01-21 11:15:27 +01:00
|
|
|
#include "td/telegram/MessagesManager.h"
|
2022-01-31 13:56:44 +01:00
|
|
|
#include "td/telegram/ServerMessageId.h"
|
2022-01-20 20:54:34 +01:00
|
|
|
#include "td/telegram/Td.h"
|
2022-01-21 18:39:55 +01:00
|
|
|
#include "td/telegram/UpdatesManager.h"
|
2022-01-20 20:54:34 +01:00
|
|
|
|
|
|
|
#include "td/utils/algorithm.h"
|
2022-01-21 14:46:16 +01:00
|
|
|
#include "td/utils/buffer.h"
|
2022-01-20 20:54:34 +01:00
|
|
|
#include "td/utils/logging.h"
|
2022-01-31 13:56:44 +01:00
|
|
|
#include "td/utils/Status.h"
|
2022-01-20 20:54:34 +01:00
|
|
|
|
2022-01-31 13:56:44 +01:00
|
|
|
#include <algorithm>
|
2022-01-20 20:54:34 +01:00
|
|
|
#include <unordered_set>
|
2022-01-31 13:56:44 +01:00
|
|
|
#include <utility>
|
2022-01-20 20:54:34 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2022-01-26 16:48:46 +01:00
|
|
|
class GetMessagesReactionsQuery final : public Td::ResultHandler {
|
|
|
|
DialogId dialog_id_;
|
2022-01-26 18:06:27 +01:00
|
|
|
vector<MessageId> message_ids_;
|
2022-01-26 16:48:46 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
void send(DialogId dialog_id, vector<MessageId> &&message_ids) {
|
|
|
|
dialog_id_ = dialog_id;
|
2022-01-26 18:06:27 +01:00
|
|
|
message_ids_ = std::move(message_ids);
|
2022-01-26 16:48:46 +01:00
|
|
|
|
|
|
|
auto input_peer = td_->messages_manager_->get_input_peer(dialog_id_, AccessRights::Read);
|
|
|
|
CHECK(input_peer != nullptr);
|
|
|
|
|
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_getMessagesReactions(
|
2022-01-26 18:06:27 +01:00
|
|
|
std::move(input_peer), MessagesManager::get_server_message_ids(message_ids_))));
|
2022-01-26 16:48:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getMessagesReactions>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
|
|
|
LOG(INFO) << "Receive result for GetMessagesReactionsQuery: " << to_string(ptr);
|
2022-01-26 18:06:27 +01:00
|
|
|
if (ptr->get_id() == telegram_api::updates::ID) {
|
|
|
|
auto &updates = static_cast<telegram_api::updates *>(ptr.get())->updates_;
|
|
|
|
std::unordered_set<MessageId, MessageIdHash> skipped_message_ids(message_ids_.begin(), message_ids_.end());
|
|
|
|
for (const auto &update : updates) {
|
|
|
|
if (update->get_id() == telegram_api::updateMessageReactions::ID) {
|
|
|
|
auto update_message_reactions = static_cast<const telegram_api::updateMessageReactions *>(update.get());
|
|
|
|
if (DialogId(update_message_reactions->peer_) == dialog_id_) {
|
|
|
|
skipped_message_ids.erase(MessageId(ServerMessageId(update_message_reactions->msg_id_)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto message_id : skipped_message_ids) {
|
2022-02-12 12:15:14 +01:00
|
|
|
td_->messages_manager_->update_message_reactions({dialog_id_, message_id}, nullptr);
|
2022-01-26 18:06:27 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-26 16:48:46 +01:00
|
|
|
td_->updates_manager_->on_get_updates(std::move(ptr), Promise<Unit>());
|
2022-03-07 12:27:42 +01:00
|
|
|
td_->messages_manager_->try_reload_message_reactions(dialog_id_, true);
|
2022-01-26 16:48:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
td_->messages_manager_->on_get_dialog_error(dialog_id_, status, "GetMessagesReactionsQuery");
|
2022-03-07 12:27:42 +01:00
|
|
|
td_->messages_manager_->try_reload_message_reactions(dialog_id_, true);
|
2022-01-26 16:48:46 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-21 18:39:55 +01:00
|
|
|
class SendReactionQuery final : public Td::ResultHandler {
|
|
|
|
Promise<Unit> promise_;
|
|
|
|
DialogId dialog_id_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit SendReactionQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2022-01-26 14:41:41 +01:00
|
|
|
void send(FullMessageId full_message_id, string reaction, bool is_big) {
|
2022-01-21 18:39:55 +01:00
|
|
|
dialog_id_ = full_message_id.get_dialog_id();
|
|
|
|
|
|
|
|
auto input_peer = td_->messages_manager_->get_input_peer(dialog_id_, AccessRights::Read);
|
|
|
|
if (input_peer == nullptr) {
|
|
|
|
return on_error(Status::Error(400, "Can't access the chat"));
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 flags = 0;
|
|
|
|
if (!reaction.empty()) {
|
|
|
|
flags |= telegram_api::messages_sendReaction::REACTION_MASK;
|
2022-01-26 14:41:41 +01:00
|
|
|
|
|
|
|
if (is_big) {
|
|
|
|
flags |= telegram_api::messages_sendReaction::BIG_MASK;
|
|
|
|
}
|
2022-01-21 18:39:55 +01:00
|
|
|
}
|
|
|
|
|
2022-02-16 16:36:55 +01:00
|
|
|
send_query(G()->net_query_creator().create(
|
|
|
|
telegram_api::messages_sendReaction(flags, false /*ignored*/, std::move(input_peer),
|
|
|
|
full_message_id.get_message_id().get_server_message_id().get(), reaction),
|
|
|
|
{{dialog_id_}, {full_message_id}}));
|
2022-01-21 18:39:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_sendReaction>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
|
|
|
LOG(INFO) << "Receive result for SendReactionQuery: " << to_string(ptr);
|
|
|
|
td_->updates_manager_->on_get_updates(std::move(ptr), std::move(promise_));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
td_->messages_manager_->on_get_dialog_error(dialog_id_, status, "SendReactionQuery");
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-21 14:46:16 +01:00
|
|
|
class GetMessageReactionsListQuery final : public Td::ResultHandler {
|
2022-01-26 23:52:21 +01:00
|
|
|
Promise<td_api::object_ptr<td_api::addedReactions>> promise_;
|
2022-01-21 14:46:16 +01:00
|
|
|
DialogId dialog_id_;
|
|
|
|
MessageId message_id_;
|
|
|
|
string reaction_;
|
|
|
|
string offset_;
|
|
|
|
|
|
|
|
public:
|
2022-01-26 23:52:21 +01:00
|
|
|
explicit GetMessageReactionsListQuery(Promise<td_api::object_ptr<td_api::addedReactions>> &&promise)
|
2022-01-21 14:46:16 +01:00
|
|
|
: promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(FullMessageId full_message_id, string reaction, string offset, int32 limit) {
|
|
|
|
dialog_id_ = full_message_id.get_dialog_id();
|
|
|
|
message_id_ = full_message_id.get_message_id();
|
|
|
|
reaction_ = std::move(reaction);
|
|
|
|
offset_ = std::move(offset);
|
|
|
|
|
|
|
|
auto input_peer = td_->messages_manager_->get_input_peer(dialog_id_, AccessRights::Read);
|
|
|
|
if (input_peer == nullptr) {
|
|
|
|
return on_error(Status::Error(400, "Can't access the chat"));
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 flags = 0;
|
|
|
|
if (!reaction_.empty()) {
|
|
|
|
flags |= telegram_api::messages_getMessageReactionsList::REACTION_MASK;
|
|
|
|
}
|
|
|
|
if (!offset_.empty()) {
|
|
|
|
flags |= telegram_api::messages_getMessageReactionsList::OFFSET_MASK;
|
|
|
|
}
|
|
|
|
|
2022-02-16 16:36:55 +01:00
|
|
|
send_query(G()->net_query_creator().create(
|
|
|
|
telegram_api::messages_getMessageReactionsList(
|
|
|
|
flags, std::move(input_peer), message_id_.get_server_message_id().get(), reaction_, offset_, limit),
|
|
|
|
{{full_message_id}}));
|
2022-01-21 14:46:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getMessageReactionsList>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
|
|
|
LOG(INFO) << "Receive result for GetMessageReactionsListQuery: " << to_string(ptr);
|
|
|
|
|
|
|
|
td_->contacts_manager_->on_get_users(std::move(ptr->users_), "GetMessageReactionsListQuery");
|
2022-01-25 10:36:30 +01:00
|
|
|
td_->contacts_manager_->on_get_chats(std::move(ptr->chats_), "GetMessageReactionsListQuery");
|
2022-01-21 14:46:16 +01:00
|
|
|
|
|
|
|
int32 total_count = ptr->count_;
|
2022-02-12 12:15:14 +01:00
|
|
|
auto received_reaction_count = static_cast<int32>(ptr->reactions_.size());
|
|
|
|
if (total_count < received_reaction_count) {
|
2022-01-21 14:46:16 +01:00
|
|
|
LOG(ERROR) << "Receive invalid total_count in " << to_string(ptr);
|
2022-02-12 12:15:14 +01:00
|
|
|
total_count = received_reaction_count;
|
2022-01-21 14:46:16 +01:00
|
|
|
}
|
|
|
|
|
2022-01-26 23:52:21 +01:00
|
|
|
vector<td_api::object_ptr<td_api::addedReaction>> reactions;
|
2022-02-14 09:34:12 +01:00
|
|
|
FlatHashMap<string, vector<DialogId>> recent_reactions;
|
2022-02-12 12:15:14 +01:00
|
|
|
for (const auto &reaction : ptr->reactions_) {
|
2022-01-25 10:36:30 +01:00
|
|
|
DialogId dialog_id(reaction->peer_id_);
|
2022-02-12 12:15:14 +01:00
|
|
|
if (!dialog_id.is_valid() ||
|
|
|
|
(reaction_.empty() ? reaction->reaction_.empty() : reaction_ != reaction->reaction_)) {
|
2022-01-21 14:46:16 +01:00
|
|
|
LOG(ERROR) << "Receive unexpected " << to_string(reaction);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-02-14 09:34:12 +01:00
|
|
|
if (offset_.empty()) {
|
|
|
|
recent_reactions[reaction->reaction_].push_back(dialog_id);
|
|
|
|
}
|
|
|
|
|
2022-01-25 10:36:30 +01:00
|
|
|
auto message_sender = get_min_message_sender_object(td_, dialog_id, "GetMessageReactionsListQuery");
|
2022-01-22 21:17:41 +01:00
|
|
|
if (message_sender != nullptr) {
|
2022-01-26 23:52:21 +01:00
|
|
|
reactions.push_back(td_api::make_object<td_api::addedReaction>(reaction->reaction_, std::move(message_sender)));
|
2022-01-22 21:17:41 +01:00
|
|
|
}
|
2022-01-21 14:46:16 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 09:34:12 +01:00
|
|
|
if (offset_.empty()) {
|
|
|
|
td_->messages_manager_->on_get_message_reaction_list({dialog_id_, message_id_}, reaction_,
|
|
|
|
std::move(recent_reactions), total_count);
|
|
|
|
}
|
|
|
|
|
2022-01-21 14:46:16 +01:00
|
|
|
promise_.set_value(
|
2022-01-26 23:52:21 +01:00
|
|
|
td_api::make_object<td_api::addedReactions>(total_count, std::move(reactions), ptr->next_offset_));
|
2022-01-21 14:46:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
td_->messages_manager_->on_get_dialog_error(dialog_id_, status, "GetMessageReactionsListQuery");
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-30 11:24:27 +01:00
|
|
|
void MessageReaction::set_is_chosen(bool is_chosen, DialogId chooser_dialog_id, bool can_get_added_reactions) {
|
2022-01-21 18:39:55 +01:00
|
|
|
if (is_chosen_ == is_chosen) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
is_chosen_ = is_chosen;
|
|
|
|
|
|
|
|
if (chooser_dialog_id.is_valid()) {
|
|
|
|
choose_count_ += is_chosen_ ? 1 : -1;
|
2022-01-30 11:24:27 +01:00
|
|
|
if (can_get_added_reactions) {
|
2022-01-26 10:50:41 +01:00
|
|
|
td::remove(recent_chooser_dialog_ids_, chooser_dialog_id);
|
|
|
|
if (is_chosen_) {
|
|
|
|
recent_chooser_dialog_ids_.insert(recent_chooser_dialog_ids_.begin(), chooser_dialog_id);
|
|
|
|
if (recent_chooser_dialog_ids_.size() > MAX_RECENT_CHOOSERS) {
|
|
|
|
recent_chooser_dialog_ids_.resize(MAX_RECENT_CHOOSERS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-21 18:39:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 20:54:34 +01:00
|
|
|
td_api::object_ptr<td_api::messageReaction> MessageReaction::get_message_reaction_object(Td *td) const {
|
|
|
|
CHECK(!is_empty());
|
|
|
|
|
2022-01-21 11:15:27 +01:00
|
|
|
vector<td_api::object_ptr<td_api::MessageSender>> recent_choosers;
|
|
|
|
for (auto dialog_id : recent_chooser_dialog_ids_) {
|
2022-01-22 21:17:41 +01:00
|
|
|
auto recent_chooser = get_min_message_sender_object(td, dialog_id, "get_message_reaction_object");
|
|
|
|
if (recent_chooser != nullptr) {
|
|
|
|
recent_choosers.push_back(std::move(recent_chooser));
|
2022-01-20 20:54:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return td_api::make_object<td_api::messageReaction>(reaction_, choose_count_, is_chosen_, std::move(recent_choosers));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const MessageReaction &lhs, const MessageReaction &rhs) {
|
|
|
|
return lhs.reaction_ == rhs.reaction_ && lhs.choose_count_ == rhs.choose_count_ && lhs.is_chosen_ == rhs.is_chosen_ &&
|
2022-01-21 11:15:27 +01:00
|
|
|
lhs.recent_chooser_dialog_ids_ == rhs.recent_chooser_dialog_ids_;
|
2022-01-20 20:54:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const MessageReaction &reaction) {
|
|
|
|
string_builder << '[' << reaction.reaction_ << (reaction.is_chosen_ ? " X " : " x ") << reaction.choose_count_;
|
2022-01-21 11:15:27 +01:00
|
|
|
if (!reaction.recent_chooser_dialog_ids_.empty()) {
|
|
|
|
string_builder << " by " << reaction.recent_chooser_dialog_ids_;
|
2022-01-20 20:54:34 +01:00
|
|
|
}
|
|
|
|
return string_builder << ']';
|
|
|
|
}
|
|
|
|
|
2022-01-28 15:10:17 +01:00
|
|
|
td_api::object_ptr<td_api::unreadReaction> UnreadMessageReaction::get_unread_reaction_object(Td *td) const {
|
|
|
|
auto sender_id = get_min_message_sender_object(td, sender_dialog_id_, "get_unread_reaction_object");
|
|
|
|
if (sender_id == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return td_api::make_object<td_api::unreadReaction>(reaction_, std::move(sender_id), is_big_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const UnreadMessageReaction &lhs, const UnreadMessageReaction &rhs) {
|
|
|
|
return lhs.reaction_ == rhs.reaction_ && lhs.sender_dialog_id_ == rhs.sender_dialog_id_ && lhs.is_big_ == rhs.is_big_;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const UnreadMessageReaction &unread_reaction) {
|
|
|
|
return string_builder << '[' << unread_reaction.reaction_ << (unread_reaction.is_big_ ? " BY " : " by ")
|
|
|
|
<< unread_reaction.sender_dialog_id_ << ']';
|
|
|
|
}
|
|
|
|
|
2022-01-20 20:54:34 +01:00
|
|
|
unique_ptr<MessageReactions> MessageReactions::get_message_reactions(
|
|
|
|
Td *td, tl_object_ptr<telegram_api::messageReactions> &&reactions, bool is_bot) {
|
|
|
|
if (reactions == nullptr || is_bot) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = make_unique<MessageReactions>();
|
2022-01-30 11:24:27 +01:00
|
|
|
result->can_get_added_reactions_ = reactions->can_see_list_;
|
2022-01-20 20:54:34 +01:00
|
|
|
result->is_min_ = reactions->min_;
|
|
|
|
|
|
|
|
std::unordered_set<string> reaction_strings;
|
2022-01-21 11:15:27 +01:00
|
|
|
std::unordered_set<DialogId, DialogIdHash> recent_choosers;
|
2022-01-20 20:54:34 +01:00
|
|
|
for (auto &reaction_count : reactions->results_) {
|
|
|
|
if (reaction_count->count_ <= 0 || reaction_count->count_ >= MessageReaction::MAX_CHOOSE_COUNT) {
|
|
|
|
LOG(ERROR) << "Receive reaction " << reaction_count->reaction_ << " with invalid count "
|
|
|
|
<< reaction_count->count_;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!reaction_strings.insert(reaction_count->reaction_).second) {
|
|
|
|
LOG(ERROR) << "Receive duplicate reaction " << reaction_count->reaction_;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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-25 10:36:30 +01:00
|
|
|
for (auto &peer_reaction : reactions->recent_reactions_) {
|
|
|
|
if (peer_reaction->reaction_ == reaction_count->reaction_) {
|
|
|
|
DialogId dialog_id(peer_reaction->peer_id_);
|
2022-01-21 11:15:27 +01:00
|
|
|
if (!dialog_id.is_valid()) {
|
|
|
|
LOG(ERROR) << "Receive invalid " << dialog_id << " as a recent chooser";
|
2022-01-20 20:54:34 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-01-21 11:15:27 +01:00
|
|
|
if (!recent_choosers.insert(dialog_id).second) {
|
|
|
|
LOG(ERROR) << "Receive duplicate " << dialog_id << " as a recent chooser";
|
2022-01-20 20:54:34 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-01-21 11:15:27 +01:00
|
|
|
if (!td->messages_manager_->have_dialog_info(dialog_id)) {
|
|
|
|
auto dialog_type = dialog_id.get_type();
|
|
|
|
if (dialog_type == DialogType::User) {
|
|
|
|
auto user_id = dialog_id.get_user_id();
|
|
|
|
if (!td->contacts_manager_->have_min_user(user_id)) {
|
|
|
|
LOG(ERROR) << "Have no info about " << user_id;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (dialog_type == DialogType::Channel) {
|
|
|
|
auto channel_id = dialog_id.get_channel_id();
|
|
|
|
auto min_channel = td->contacts_manager_->get_min_channel(channel_id);
|
|
|
|
if (min_channel == nullptr) {
|
|
|
|
LOG(ERROR) << "Have no info about reacted " << channel_id;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
recent_chooser_min_channels.emplace_back(channel_id, *min_channel);
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Have no info about reacted " << dialog_id;
|
|
|
|
continue;
|
|
|
|
}
|
2022-01-20 20:54:34 +01:00
|
|
|
}
|
|
|
|
|
2022-01-21 11:15:27 +01:00
|
|
|
recent_chooser_dialog_ids.push_back(dialog_id);
|
2022-01-28 15:10:17 +01:00
|
|
|
if (peer_reaction->unread_) {
|
|
|
|
result->unread_reactions_.emplace_back(std::move(peer_reaction->reaction_), dialog_id, peer_reaction->big_);
|
|
|
|
}
|
2022-01-21 11:15:27 +01:00
|
|
|
if (recent_chooser_dialog_ids.size() == MessageReaction::MAX_RECENT_CHOOSERS) {
|
2022-01-20 20:54:34 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result->reactions_.emplace_back(std::move(reaction_count->reaction_), reaction_count->count_,
|
2022-01-21 11:15:27 +01:00
|
|
|
reaction_count->chosen_, std::move(recent_chooser_dialog_ids),
|
|
|
|
std::move(recent_chooser_min_channels));
|
2022-01-20 20:54:34 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-01-26 13:25:03 +01:00
|
|
|
MessageReaction *MessageReactions::get_reaction(const string &reaction) {
|
2022-01-26 23:52:21 +01:00
|
|
|
for (auto &added_reaction : reactions_) {
|
|
|
|
if (added_reaction.get_reaction() == reaction) {
|
|
|
|
return &added_reaction;
|
2022-01-26 13:25:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-01-26 14:51:47 +01:00
|
|
|
const MessageReaction *MessageReactions::get_reaction(const string &reaction) const {
|
2022-01-26 23:52:21 +01:00
|
|
|
for (auto &added_reaction : reactions_) {
|
|
|
|
if (added_reaction.get_reaction() == reaction) {
|
|
|
|
return &added_reaction;
|
2022-01-26 14:51:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-01-20 20:54:34 +01:00
|
|
|
void MessageReactions::update_from(const MessageReactions &old_reactions) {
|
|
|
|
if (is_min_ && !old_reactions.is_min_) {
|
|
|
|
// chosen reaction was known, keep it
|
|
|
|
is_min_ = false;
|
|
|
|
for (const auto &old_reaction : old_reactions.reactions_) {
|
|
|
|
if (old_reaction.is_chosen()) {
|
2022-01-26 13:25:03 +01:00
|
|
|
auto *reaction = get_reaction(old_reaction.get_reaction());
|
|
|
|
if (reaction != nullptr) {
|
|
|
|
reaction->set_is_chosen(true, DialogId(), false);
|
2022-01-20 20:54:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-12 12:15:14 +01:00
|
|
|
unread_reactions_ = old_reactions.unread_reactions_;
|
2022-01-20 20:54:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 20:41:07 +01:00
|
|
|
void MessageReactions::sort_reactions(const FlatHashMap<string, size_t> &active_reaction_pos) {
|
2022-01-26 10:50:41 +01:00
|
|
|
std::sort(reactions_.begin(), reactions_.end(),
|
|
|
|
[&active_reaction_pos](const MessageReaction &lhs, const MessageReaction &rhs) {
|
|
|
|
if (lhs.get_choose_count() != rhs.get_choose_count()) {
|
|
|
|
return lhs.get_choose_count() > rhs.get_choose_count();
|
|
|
|
}
|
|
|
|
auto lhs_it = active_reaction_pos.find(lhs.get_reaction());
|
|
|
|
auto lhs_pos = lhs_it != active_reaction_pos.end() ? lhs_it->second : active_reaction_pos.size();
|
|
|
|
auto rhs_it = active_reaction_pos.find(rhs.get_reaction());
|
|
|
|
auto rhs_pos = rhs_it != active_reaction_pos.end() ? rhs_it->second : active_reaction_pos.size();
|
|
|
|
if (lhs_pos != rhs_pos) {
|
|
|
|
return lhs_pos < rhs_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lhs.get_reaction() < rhs.get_reaction();
|
|
|
|
});
|
2022-01-26 10:01:14 +01:00
|
|
|
}
|
|
|
|
|
2022-01-20 20:54:34 +01:00
|
|
|
bool MessageReactions::need_update_message_reactions(const MessageReactions *old_reactions,
|
|
|
|
const MessageReactions *new_reactions) {
|
|
|
|
if (old_reactions == nullptr) {
|
|
|
|
// add reactions
|
|
|
|
return new_reactions != nullptr;
|
|
|
|
}
|
|
|
|
if (new_reactions == nullptr) {
|
|
|
|
// remove reactions when they are disabled
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-28 15:10:17 +01:00
|
|
|
// unread_reactions_ are updated independently; compare all other fields
|
2022-01-20 20:54:34 +01:00
|
|
|
return old_reactions->reactions_ != new_reactions->reactions_ || old_reactions->is_min_ != new_reactions->is_min_ ||
|
2022-01-30 11:24:27 +01:00
|
|
|
old_reactions->can_get_added_reactions_ != new_reactions->can_get_added_reactions_ ||
|
2022-01-20 20:54:34 +01:00
|
|
|
old_reactions->need_polling_ != new_reactions->need_polling_;
|
|
|
|
}
|
|
|
|
|
2022-01-28 15:10:17 +01:00
|
|
|
bool MessageReactions::need_update_unread_reactions(const MessageReactions *old_reactions,
|
|
|
|
const MessageReactions *new_reactions) {
|
|
|
|
if (old_reactions == nullptr || old_reactions->unread_reactions_.empty()) {
|
|
|
|
return !(new_reactions == nullptr || new_reactions->unread_reactions_.empty());
|
|
|
|
}
|
|
|
|
return new_reactions == nullptr || old_reactions->unread_reactions_ != new_reactions->unread_reactions_;
|
|
|
|
}
|
|
|
|
|
2022-01-30 11:24:27 +01:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const MessageReactions &reactions) {
|
|
|
|
return string_builder << (reactions.is_min_ ? "Min" : "") << "MessageReactions{" << reactions.reactions_
|
|
|
|
<< " with unread " << reactions.unread_reactions_
|
|
|
|
<< " and can_get_added_reactions = " << reactions.can_get_added_reactions_;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const unique_ptr<MessageReactions> &reactions) {
|
|
|
|
if (reactions == nullptr) {
|
|
|
|
return string_builder << "null";
|
|
|
|
}
|
|
|
|
return string_builder << *reactions;
|
|
|
|
}
|
|
|
|
|
2022-01-26 16:48:46 +01:00
|
|
|
void reload_message_reactions(Td *td, DialogId dialog_id, vector<MessageId> &&message_ids) {
|
2022-01-26 18:06:27 +01:00
|
|
|
if (!td->messages_manager_->have_input_peer(dialog_id, AccessRights::Read) || message_ids.empty()) {
|
2022-01-26 16:48:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &message_id : message_ids) {
|
|
|
|
CHECK(message_id.is_valid());
|
|
|
|
CHECK(message_id.is_server());
|
|
|
|
}
|
|
|
|
|
|
|
|
td->create_handler<GetMessagesReactionsQuery>()->send(dialog_id, std::move(message_ids));
|
|
|
|
}
|
|
|
|
|
2022-01-26 14:41:41 +01:00
|
|
|
void set_message_reaction(Td *td, FullMessageId full_message_id, string reaction, bool is_big,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
td->create_handler<SendReactionQuery>(std::move(promise))->send(full_message_id, std::move(reaction), is_big);
|
2022-01-21 18:39:55 +01:00
|
|
|
}
|
|
|
|
|
2022-01-26 23:52:21 +01:00
|
|
|
void get_message_added_reactions(Td *td, FullMessageId full_message_id, string reaction, string offset, int32 limit,
|
|
|
|
Promise<td_api::object_ptr<td_api::addedReactions>> &&promise) {
|
|
|
|
if (!td->messages_manager_->have_message_force(full_message_id, "get_message_added_reactions")) {
|
2022-01-21 14:46:16 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Message not found"));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto message_id = full_message_id.get_message_id();
|
2022-01-21 18:39:55 +01:00
|
|
|
if (full_message_id.get_dialog_id().get_type() == DialogType::SecretChat || !message_id.is_valid() ||
|
|
|
|
!message_id.is_server()) {
|
2022-01-26 23:52:21 +01:00
|
|
|
return promise.set_value(td_api::make_object<td_api::addedReactions>(0, Auto(), string()));
|
2022-01-21 14:46:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (limit <= 0) {
|
|
|
|
return promise.set_error(Status::Error(400, "Parameter limit must be positive"));
|
|
|
|
}
|
2022-01-26 23:52:21 +01:00
|
|
|
static constexpr int32 MAX_GET_ADDED_REACTIONS = 100; // server side limit
|
|
|
|
if (limit > MAX_GET_ADDED_REACTIONS) {
|
|
|
|
limit = MAX_GET_ADDED_REACTIONS;
|
2022-01-21 14:46:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
td->create_handler<GetMessageReactionsListQuery>(std::move(promise))
|
|
|
|
->send(full_message_id, std::move(reaction), std::move(offset), limit);
|
|
|
|
}
|
|
|
|
|
2022-01-20 20:54:34 +01:00
|
|
|
} // namespace td
|