Add and use MessageQuote(td_api::inputTextQuote).

This commit is contained in:
levlam 2024-04-25 16:18:07 +03:00
parent 1f6fdd554a
commit 02a99f3c50
4 changed files with 29 additions and 42 deletions

View File

@ -633,24 +633,7 @@ MessageInputReplyTo BusinessConnectionManager::create_business_message_input_rep
if (reply_to_message->chat_id_ != 0) { if (reply_to_message->chat_id_ != 0) {
return {}; return {};
} }
FormattedText quote; return MessageInputReplyTo{message_id, DialogId(), MessageQuote(td_, std::move(reply_to_message->quote_))};
int32 quote_position = 0;
if (reply_to_message->quote_ != nullptr) {
int32 ltrim_count = 0;
auto r_quote = get_formatted_text(td_, td_->dialog_manager_->get_my_dialog_id(),
std::move(reply_to_message->quote_->text_), td_->auth_manager_->is_bot(),
true, true, false, &ltrim_count);
if (r_quote.is_ok() && !r_quote.ok().text.empty()) {
quote = r_quote.move_as_ok();
quote_position = reply_to_message->quote_->position_;
if (0 <= quote_position && quote_position <= 1000000) { // some unreasonably big bound
quote_position += ltrim_count;
} else {
quote_position = 0;
}
}
}
return MessageInputReplyTo{message_id, DialogId(), {std::move(quote), quote_position}};
} }
default: default:
UNREACHABLE(); UNREACHABLE();

View File

@ -6,7 +6,9 @@
// //
#include "td/telegram/MessageQuote.h" #include "td/telegram/MessageQuote.h"
#include "td/telegram/AuthManager.h"
#include "td/telegram/Dependencies.h" #include "td/telegram/Dependencies.h"
#include "td/telegram/DialogManager.h"
#include "td/telegram/OptionManager.h" #include "td/telegram/OptionManager.h"
#include "td/telegram/Td.h" #include "td/telegram/Td.h"
#include "td/telegram/telegram_api.h" #include "td/telegram/telegram_api.h"
@ -43,6 +45,25 @@ MessageQuote::MessageQuote(Td *td, telegram_api::object_ptr<telegram_api::messag
is_manual_ = reply_header->quote_; is_manual_ = reply_header->quote_;
} }
MessageQuote::MessageQuote(Td *td, td_api::object_ptr<td_api::inputTextQuote> quote) {
if (quote == nullptr) {
return;
}
int32 ltrim_count = 0;
auto r_text = get_formatted_text(td, td->dialog_manager_->get_my_dialog_id(), std::move(quote->text_),
td->auth_manager_->is_bot(), true, true, false, &ltrim_count);
if (!r_text.is_ok() || r_text.ok().text.empty()) {
return;
}
text_ = r_text.move_as_ok();
position_ = quote->position_;
if (0 <= position_ && position_ <= 1000000) { // some unreasonably big bound
position_ += ltrim_count;
} else {
position_ = 0;
}
}
MessageQuote MessageQuote::clone(bool ignore_is_manual) const { MessageQuote MessageQuote::clone(bool ignore_is_manual) const {
return {FormattedText(text_), position_, ignore_is_manual ? true : is_manual_}; return {FormattedText(text_), position_, ignore_is_manual ? true : is_manual_};
} }

View File

@ -45,6 +45,8 @@ class MessageQuote {
MessageQuote(Td *td, telegram_api::object_ptr<telegram_api::messageReplyHeader> &reply_header); MessageQuote(Td *td, telegram_api::object_ptr<telegram_api::messageReplyHeader> &reply_header);
MessageQuote(Td *td, td_api::object_ptr<td_api::inputTextQuote> quote);
static MessageQuote create_automatic_quote(Td *td, FormattedText &&text); static MessageQuote create_automatic_quote(Td *td, FormattedText &&text);
static int need_quote_changed_warning(const MessageQuote &old_quote, const MessageQuote &new_quote); static int need_quote_changed_warning(const MessageQuote &old_quote, const MessageQuote &new_quote);

View File

@ -23145,23 +23145,6 @@ MessageInputReplyTo MessagesManager::create_message_input_reply_to(
if (message_id == MessageId(ServerMessageId(1)) && reply_d->dialog_id.get_type() == DialogType::Channel) { if (message_id == MessageId(ServerMessageId(1)) && reply_d->dialog_id.get_type() == DialogType::Channel) {
return {}; return {};
} }
FormattedText quote;
int32 quote_position = 0;
if (reply_to_message->quote_ != nullptr) {
int32 ltrim_count = 0;
auto r_quote = get_formatted_text(td_, td_->dialog_manager_->get_my_dialog_id(),
std::move(reply_to_message->quote_->text_), td_->auth_manager_->is_bot(),
true, true, false, &ltrim_count);
if (r_quote.is_ok() && d->dialog_id.get_type() != DialogType::SecretChat && !r_quote.ok().text.empty()) {
quote = r_quote.move_as_ok();
quote_position = reply_to_message->quote_->position_;
if (0 <= quote_position && quote_position <= 1000000) { // some unreasonably big bound
quote_position += ltrim_count;
} else {
quote_position = 0;
}
}
}
const Message *m = get_message_force(reply_d, message_id, "create_message_input_reply_to 2"); const Message *m = get_message_force(reply_d, message_id, "create_message_input_reply_to 2");
if (m == nullptr || m->message_id.is_yet_unsent() || if (m == nullptr || m->message_id.is_yet_unsent() ||
(m->message_id.is_local() && reply_d->dialog_id.get_type() != DialogType::SecretChat)) { (m->message_id.is_local() && reply_d->dialog_id.get_type() != DialogType::SecretChat)) {
@ -23170,7 +23153,8 @@ MessageInputReplyTo MessagesManager::create_message_input_reply_to(
(reply_d->notification_info != nullptr && (reply_d->notification_info != nullptr &&
message_id <= reply_d->notification_info->max_push_notification_message_id_)) { message_id <= reply_d->notification_info->max_push_notification_message_id_)) {
// allow to reply yet unreceived server message in the same chat // allow to reply yet unreceived server message in the same chat
return MessageInputReplyTo{message_id, reply_dialog_id, MessageQuote{std::move(quote), quote_position}}; return MessageInputReplyTo{message_id, reply_dialog_id,
MessageQuote{td_, std::move(reply_to_message->quote_)}};
} }
if (!for_draft && top_thread_message_id.is_valid() && top_thread_message_id.is_server()) { if (!for_draft && top_thread_message_id.is_valid() && top_thread_message_id.is_server()) {
return MessageInputReplyTo{top_thread_message_id, DialogId(), MessageQuote()}; return MessageInputReplyTo{top_thread_message_id, DialogId(), MessageQuote()};
@ -23185,7 +23169,8 @@ MessageInputReplyTo MessagesManager::create_message_input_reply_to(
LOG(INFO) << "Can't reply in another chat " << m->message_id << " in " << reply_d->dialog_id; LOG(INFO) << "Can't reply in another chat " << m->message_id << " in " << reply_d->dialog_id;
return {}; return {};
} }
return MessageInputReplyTo{m->message_id, reply_dialog_id, MessageQuote{std::move(quote), quote_position}}; return MessageInputReplyTo{m->message_id, reply_dialog_id,
MessageQuote{td_, std::move(reply_to_message->quote_)}};
} }
default: default:
UNREACHABLE(); UNREACHABLE();
@ -26793,11 +26778,7 @@ Result<vector<MessageId>> MessagesManager::resend_messages(DialogId dialog_id, v
if (need_another_reply_quote && message_ids.size() == 1 && quote != nullptr) { if (need_another_reply_quote && message_ids.size() == 1 && quote != nullptr) {
CHECK(message->input_reply_to.is_valid()); CHECK(message->input_reply_to.is_valid());
CHECK(message->input_reply_to.has_quote()); // checked in on_send_message_fail CHECK(message->input_reply_to.has_quote()); // checked in on_send_message_fail
auto r_quote = get_formatted_text(td_, td_->dialog_manager_->get_my_dialog_id(), std::move(quote->text_), message->input_reply_to.set_quote(MessageQuote{td_, std::move(quote)});
td_->auth_manager_->is_bot(), true, true, true);
if (r_quote.is_ok()) {
message->input_reply_to.set_quote(MessageQuote{r_quote.move_as_ok(), quote->position_});
}
} else if (need_drop_reply) { } else if (need_drop_reply) {
message->input_reply_to = {}; message->input_reply_to = {};
} }