2023-06-08 16:27:24 +02:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2023-06-08 16:27:24 +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/MessageInputReplyTo.h"
|
|
|
|
|
2023-10-30 17:04:14 +01:00
|
|
|
#include "td/telegram/AccessRights.h"
|
2023-10-29 09:26:21 +01:00
|
|
|
#include "td/telegram/Dependencies.h"
|
2023-07-01 15:22:01 +02:00
|
|
|
#include "td/telegram/DialogId.h"
|
2024-01-03 21:07:50 +01:00
|
|
|
#include "td/telegram/DialogManager.h"
|
2023-10-25 01:07:37 +02:00
|
|
|
#include "td/telegram/InputDialogId.h"
|
2023-10-27 12:06:38 +02:00
|
|
|
#include "td/telegram/misc.h"
|
2023-10-30 17:04:14 +01:00
|
|
|
#include "td/telegram/ServerMessageId.h"
|
2023-07-01 15:22:01 +02:00
|
|
|
#include "td/telegram/StoryId.h"
|
2023-06-08 16:27:24 +02:00
|
|
|
#include "td/telegram/Td.h"
|
2024-01-22 11:11:04 +01:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2023-06-08 16:27:24 +02:00
|
|
|
|
2023-07-01 15:22:01 +02:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
|
2023-06-08 16:27:24 +02:00
|
|
|
namespace td {
|
2023-10-27 02:37:26 +02:00
|
|
|
|
|
|
|
MessageInputReplyTo::~MessageInputReplyTo() = default;
|
2023-10-25 01:07:37 +02:00
|
|
|
|
|
|
|
MessageInputReplyTo::MessageInputReplyTo(Td *td,
|
|
|
|
telegram_api::object_ptr<telegram_api::InputReplyTo> &&input_reply_to) {
|
|
|
|
if (input_reply_to == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (input_reply_to->get_id()) {
|
|
|
|
case telegram_api::inputReplyToStory::ID: {
|
|
|
|
auto reply_to = telegram_api::move_object_as<telegram_api::inputReplyToStory>(input_reply_to);
|
2024-02-07 18:34:28 +01:00
|
|
|
auto dialog_id = InputDialogId(reply_to->peer_).get_dialog_id();
|
2023-10-25 01:07:37 +02:00
|
|
|
auto story_id = StoryId(reply_to->story_id_);
|
2024-02-07 18:34:28 +01:00
|
|
|
if (dialog_id.is_valid() && story_id.is_valid()) {
|
2024-01-04 13:38:01 +01:00
|
|
|
td->dialog_manager_->force_create_dialog(dialog_id, "MessageInputReplyTo", true);
|
2023-10-25 01:07:37 +02:00
|
|
|
story_full_id_ = {dialog_id, story_id};
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::inputReplyToMessage::ID: {
|
|
|
|
auto reply_to = telegram_api::move_object_as<telegram_api::inputReplyToMessage>(input_reply_to);
|
|
|
|
MessageId message_id(ServerMessageId(reply_to->reply_to_msg_id_));
|
2023-10-25 12:07:25 +02:00
|
|
|
if (!message_id.is_valid() && !message_id_.is_valid_scheduled()) {
|
2023-10-25 01:07:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
DialogId dialog_id;
|
|
|
|
if (reply_to->reply_to_peer_id_ != nullptr) {
|
|
|
|
dialog_id = InputDialogId(reply_to->reply_to_peer_id_).get_dialog_id();
|
2024-01-03 21:07:50 +01:00
|
|
|
if (!dialog_id.is_valid() || !td->dialog_manager_->have_input_peer(dialog_id, AccessRights::Read)) {
|
2023-10-25 01:07:37 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-01-04 13:38:01 +01:00
|
|
|
td->dialog_manager_->force_create_dialog(dialog_id, "inputReplyToMessage");
|
2023-10-25 01:07:37 +02:00
|
|
|
}
|
|
|
|
message_id_ = message_id;
|
2023-10-28 23:50:33 +02:00
|
|
|
dialog_id_ = dialog_id;
|
2023-10-27 12:06:38 +02:00
|
|
|
|
|
|
|
if (!reply_to->quote_text_.empty()) {
|
|
|
|
auto entities = get_message_entities(td->contacts_manager_.get(), std::move(reply_to->quote_entities_),
|
|
|
|
"inputReplyToMessage");
|
|
|
|
auto status = fix_formatted_text(reply_to->quote_text_, entities, true, true, true, true, false);
|
|
|
|
if (status.is_error()) {
|
|
|
|
if (!clean_input_string(reply_to->quote_text_)) {
|
|
|
|
reply_to->quote_text_.clear();
|
|
|
|
}
|
|
|
|
entities.clear();
|
|
|
|
}
|
|
|
|
quote_ = FormattedText{std::move(reply_to->quote_text_), std::move(entities)};
|
2023-10-29 00:25:21 +02:00
|
|
|
remove_unallowed_quote_entities(quote_);
|
2023-11-10 11:29:37 +01:00
|
|
|
quote_position_ = max(0, reply_to->quote_offset_);
|
2023-10-27 12:06:38 +02:00
|
|
|
}
|
2023-10-25 01:07:37 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-29 09:26:21 +01:00
|
|
|
void MessageInputReplyTo::add_dependencies(Dependencies &dependencies) const {
|
|
|
|
dependencies.add_dialog_and_dependencies(dialog_id_);
|
|
|
|
add_formatted_text_dependencies(dependencies, "e_); // just in case
|
|
|
|
dependencies.add_dialog_and_dependencies(story_full_id_.get_dialog_id()); // just in case
|
|
|
|
}
|
|
|
|
|
2023-06-08 16:27:24 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::InputReplyTo> MessageInputReplyTo::get_input_reply_to(
|
|
|
|
Td *td, MessageId top_thread_message_id) const {
|
|
|
|
if (story_full_id_.is_valid()) {
|
|
|
|
auto dialog_id = story_full_id_.get_dialog_id();
|
2024-02-07 18:34:28 +01:00
|
|
|
auto input_peer = td->dialog_manager_->get_input_peer(dialog_id, AccessRights::Read);
|
|
|
|
if (input_peer == nullptr) {
|
|
|
|
LOG(INFO) << "Failed to get input peer for " << story_full_id_;
|
2023-06-08 16:27:24 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2024-02-07 18:34:28 +01:00
|
|
|
return telegram_api::make_object<telegram_api::inputReplyToStory>(std::move(input_peer),
|
2023-06-08 16:27:24 +02:00
|
|
|
story_full_id_.get_story_id().get());
|
|
|
|
}
|
|
|
|
auto reply_to_message_id = message_id_;
|
|
|
|
if (reply_to_message_id == MessageId()) {
|
|
|
|
if (top_thread_message_id == MessageId()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
reply_to_message_id = top_thread_message_id;
|
|
|
|
}
|
|
|
|
CHECK(reply_to_message_id.is_server());
|
|
|
|
int32 flags = 0;
|
|
|
|
if (top_thread_message_id != MessageId()) {
|
|
|
|
CHECK(top_thread_message_id.is_server());
|
|
|
|
flags |= telegram_api::inputReplyToMessage::TOP_MSG_ID_MASK;
|
|
|
|
}
|
2023-10-28 23:50:33 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::InputPeer> input_peer;
|
|
|
|
if (dialog_id_ != DialogId()) {
|
2024-01-03 21:07:50 +01:00
|
|
|
input_peer = td->dialog_manager_->get_input_peer(dialog_id_, AccessRights::Read);
|
2023-10-28 23:50:33 +02:00
|
|
|
if (input_peer == nullptr) {
|
|
|
|
LOG(INFO) << "Failed to get input peer for " << dialog_id_;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
flags |= telegram_api::inputReplyToMessage::REPLY_TO_PEER_ID_MASK;
|
|
|
|
}
|
2023-10-27 12:06:38 +02:00
|
|
|
if (!quote_.text.empty()) {
|
|
|
|
flags |= telegram_api::inputReplyToMessage::QUOTE_TEXT_MASK;
|
|
|
|
}
|
|
|
|
auto quote_entities = get_input_message_entities(td->contacts_manager_.get(), quote_.entities, "get_input_reply_to");
|
|
|
|
if (!quote_entities.empty()) {
|
|
|
|
flags |= telegram_api::inputReplyToMessage::QUOTE_ENTITIES_MASK;
|
|
|
|
}
|
2023-11-10 11:29:37 +01:00
|
|
|
if (quote_position_ != 0) {
|
|
|
|
flags |= telegram_api::inputReplyToMessage::QUOTE_OFFSET_MASK;
|
|
|
|
}
|
2023-06-08 16:27:24 +02:00
|
|
|
return telegram_api::make_object<telegram_api::inputReplyToMessage>(
|
2023-09-28 15:13:36 +02:00
|
|
|
flags, reply_to_message_id.get_server_message_id().get(), top_thread_message_id.get_server_message_id().get(),
|
2023-11-10 11:29:37 +01:00
|
|
|
std::move(input_peer), quote_.text, std::move(quote_entities), quote_position_);
|
2023-06-08 16:27:24 +02:00
|
|
|
}
|
|
|
|
|
2023-10-28 23:50:33 +02:00
|
|
|
// only for draft messages
|
|
|
|
td_api::object_ptr<td_api::InputMessageReplyTo> MessageInputReplyTo::get_input_message_reply_to_object(Td *td) const {
|
2023-10-24 14:02:57 +02:00
|
|
|
if (story_full_id_.is_valid()) {
|
|
|
|
return td_api::make_object<td_api::inputMessageReplyToStory>(
|
2024-01-04 14:13:20 +01:00
|
|
|
td->dialog_manager_->get_chat_id_object(story_full_id_.get_dialog_id(), "inputMessageReplyToStory"),
|
2023-10-24 14:02:57 +02:00
|
|
|
story_full_id_.get_story_id().get());
|
|
|
|
}
|
2023-10-25 12:07:25 +02:00
|
|
|
if (!message_id_.is_valid() && !message_id_.is_valid_scheduled()) {
|
2023-10-24 14:02:57 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2023-11-28 16:16:04 +01:00
|
|
|
td_api::object_ptr<td_api::inputTextQuote> quote;
|
2023-10-27 12:06:38 +02:00
|
|
|
if (!quote_.text.empty()) {
|
2023-11-28 16:16:04 +01:00
|
|
|
quote = td_api::make_object<td_api::inputTextQuote>(get_formatted_text_object(quote_, true, -1), quote_position_);
|
2023-10-27 12:06:38 +02:00
|
|
|
}
|
2023-10-28 23:50:33 +02:00
|
|
|
return td_api::make_object<td_api::inputMessageReplyToMessage>(
|
2024-01-04 14:13:20 +01:00
|
|
|
td->dialog_manager_->get_chat_id_object(dialog_id_, "inputMessageReplyToMessage"), message_id_.get(),
|
2023-11-28 16:16:04 +01:00
|
|
|
std::move(quote));
|
2023-10-24 14:02:57 +02:00
|
|
|
}
|
|
|
|
|
2023-10-27 02:03:52 +02:00
|
|
|
MessageId MessageInputReplyTo::get_same_chat_reply_to_message_id() const {
|
2023-10-28 23:50:33 +02:00
|
|
|
return dialog_id_ == DialogId() && (message_id_.is_valid() || message_id_.is_valid_scheduled()) ? message_id_
|
|
|
|
: MessageId();
|
2023-10-27 02:03:52 +02:00
|
|
|
}
|
|
|
|
|
2023-10-24 18:03:10 +02:00
|
|
|
MessageFullId MessageInputReplyTo::get_reply_message_full_id(DialogId owner_dialog_id) const {
|
2023-10-25 12:07:25 +02:00
|
|
|
if (!message_id_.is_valid() && !message_id_.is_valid_scheduled()) {
|
2023-10-24 18:03:10 +02:00
|
|
|
return {};
|
|
|
|
}
|
2023-10-28 23:50:33 +02:00
|
|
|
return {dialog_id_ != DialogId() ? dialog_id_ : owner_dialog_id, message_id_};
|
2023-10-24 18:03:10 +02:00
|
|
|
}
|
|
|
|
|
2023-10-24 14:02:57 +02:00
|
|
|
bool operator==(const MessageInputReplyTo &lhs, const MessageInputReplyTo &rhs) {
|
2023-10-28 23:50:33 +02:00
|
|
|
return lhs.message_id_ == rhs.message_id_ && lhs.dialog_id_ == rhs.dialog_id_ &&
|
2023-11-10 11:29:37 +01:00
|
|
|
lhs.story_full_id_ == rhs.story_full_id_ && lhs.quote_ == rhs.quote_ &&
|
|
|
|
lhs.quote_position_ == rhs.quote_position_;
|
2023-10-24 14:02:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const MessageInputReplyTo &lhs, const MessageInputReplyTo &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2023-06-08 16:27:24 +02:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const MessageInputReplyTo &input_reply_to) {
|
2023-10-25 12:07:25 +02:00
|
|
|
if (input_reply_to.message_id_.is_valid() || input_reply_to.message_id_.is_valid_scheduled()) {
|
2023-10-27 12:06:38 +02:00
|
|
|
string_builder << input_reply_to.message_id_;
|
2023-10-28 23:50:33 +02:00
|
|
|
if (input_reply_to.dialog_id_ != DialogId()) {
|
|
|
|
string_builder << " in " << input_reply_to.dialog_id_;
|
|
|
|
}
|
2023-10-27 12:06:38 +02:00
|
|
|
if (!input_reply_to.quote_.text.empty()) {
|
|
|
|
string_builder << " with " << input_reply_to.quote_.text.size() << " quoted bytes";
|
2023-11-10 11:29:37 +01:00
|
|
|
if (input_reply_to.quote_position_ != 0) {
|
|
|
|
string_builder << " at position " << input_reply_to.quote_position_;
|
|
|
|
}
|
2023-10-27 12:06:38 +02:00
|
|
|
}
|
|
|
|
return string_builder;
|
2023-06-08 16:27:24 +02:00
|
|
|
}
|
|
|
|
if (input_reply_to.story_full_id_.is_valid()) {
|
|
|
|
return string_builder << input_reply_to.story_full_id_;
|
|
|
|
}
|
|
|
|
return string_builder << "nothing";
|
|
|
|
}
|
|
|
|
|
2024-01-18 12:59:53 +01:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const MessageInputReplyTo *input_reply_to_ptr) {
|
|
|
|
if (input_reply_to_ptr == nullptr) {
|
|
|
|
return string_builder << "nothing";
|
|
|
|
}
|
|
|
|
return string_builder << *input_reply_to_ptr;
|
|
|
|
}
|
|
|
|
|
2023-06-08 16:27:24 +02:00
|
|
|
} // namespace td
|