Trim quote from the replied message.

This commit is contained in:
levlam 2023-12-04 16:27:15 +03:00
parent 99f9b78819
commit 9a9e3be8cf
4 changed files with 24 additions and 9 deletions

View File

@ -1248,7 +1248,7 @@ messageSendingStateFailed error:error can_retry:Bool need_another_sender:Bool ne
//@description Describes manually or automatically chosen quote from another message
//@text Text of the quote. Only Bold, Italic, Underline, Strikethrough, Spoiler, and CustomEmoji entities can be present in the text
//@position Approximate quote position in the original message in UTF-16 code units
//@position Approximate quote position in the original message in UTF-16 code units as specified by the message sender
//@is_manual True, if the quote was manually chosen by the message sender
textQuote text:formattedText position:int32 is_manual:Bool = TextQuote;

View File

@ -4371,7 +4371,7 @@ static void merge_new_entities(vector<MessageEntity> &entities, vector<MessageEn
}
Status fix_formatted_text(string &text, vector<MessageEntity> &entities, bool allow_empty, bool skip_new_entities,
bool skip_bot_commands, bool skip_media_timestamps, bool skip_trim) {
bool skip_bot_commands, bool skip_media_timestamps, bool skip_trim, int32 *ltrim_count) {
string result;
if (entities.empty()) {
// fast path
@ -4419,6 +4419,9 @@ Status fix_formatted_text(string &text, vector<MessageEntity> &entities, bool al
// some splittable entities may be needed to be concatenated
fix_entities(entities);
if (ltrim_count != nullptr) {
*ltrim_count = 0;
}
if (skip_trim) {
text = std::move(result);
} else {
@ -4450,6 +4453,9 @@ Status fix_formatted_text(string &text, vector<MessageEntity> &entities, bool al
}
if (first_non_whitespaces_pos > 0) {
auto offset = narrow_cast<int32>(first_non_whitespaces_pos);
if (ltrim_count != nullptr) {
*ltrim_count = offset;
}
text = result.substr(first_non_whitespaces_pos);
for (auto &entity : entities) {
entity.offset -= offset;
@ -4566,7 +4572,8 @@ td_api::object_ptr<td_api::formattedText> extract_input_caption(
Result<FormattedText> get_formatted_text(const Td *td, DialogId dialog_id,
td_api::object_ptr<td_api::formattedText> &&text, bool is_bot,
bool allow_empty, bool skip_media_timestamps, bool skip_trim) {
bool allow_empty, bool skip_media_timestamps, bool skip_trim,
int32 *ltrim_count) {
if (text == nullptr) {
if (allow_empty) {
return FormattedText();
@ -4581,13 +4588,13 @@ Result<FormattedText> get_formatted_text(const Td *td, DialogId dialog_id,
bool skip_new_entities = is_bot && td->option_manager_->get_option_integer("session_count") > 1;
TRY_STATUS(fix_formatted_text(text->text_, entities, allow_empty, skip_new_entities || parse_markdown,
skip_new_entities || need_skip_bot_commands,
is_bot || skip_media_timestamps || parse_markdown, skip_trim));
is_bot || skip_media_timestamps || parse_markdown, skip_trim, ltrim_count));
FormattedText result{std::move(text->text_), std::move(entities)};
if (parse_markdown) {
result = parse_markdown_v3(std::move(result));
fix_formatted_text(result.text, result.entities, allow_empty, false, need_skip_bot_commands,
is_bot || skip_media_timestamps, skip_trim)
is_bot || skip_media_timestamps, skip_trim, nullptr)
.ensure();
}
remove_unallowed_entities(td, result, dialog_id);

View File

@ -229,7 +229,8 @@ FormattedText get_formatted_text(const ContactsManager *contacts_manager,
// like clean_input_string but also validates entities
Status fix_formatted_text(string &text, vector<MessageEntity> &entities, bool allow_empty, bool skip_new_entities,
bool skip_bot_commands, bool skip_media_timestamps, bool skip_trim) TD_WARN_UNUSED_RESULT;
bool skip_bot_commands, bool skip_media_timestamps, bool skip_trim,
int32 *ltrim_count = nullptr) TD_WARN_UNUSED_RESULT;
FormattedText get_message_text(const ContactsManager *contacts_manager, string message_text,
vector<tl_object_ptr<telegram_api::MessageEntity>> &&server_entities,
@ -243,7 +244,8 @@ td_api::object_ptr<td_api::formattedText> extract_input_caption(
Result<FormattedText> get_formatted_text(const Td *td, DialogId dialog_id,
td_api::object_ptr<td_api::formattedText> &&text, bool is_bot,
bool allow_empty, bool skip_media_timestamps, bool skip_trim);
bool allow_empty, bool skip_media_timestamps, bool skip_trim,
int32 *ltrim_count = nullptr);
void add_formatted_text_dependencies(Dependencies &dependencies, const FormattedText *text);

View File

@ -24691,11 +24691,17 @@ MessageInputReplyTo MessagesManager::get_message_input_reply_to(
FormattedText quote;
int32 quote_position = 0;
if (reply_to_message->quote_ != nullptr) {
int32 ltrim_count = 0;
auto r_quote = get_formatted_text(td_, get_my_dialog_id(), std::move(reply_to_message->quote_->text_),
td_->auth_manager_->is_bot(), true, true, true);
if (r_quote.is_ok() && d->dialog_id.get_type() != DialogType::SecretChat) {
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, "get_message_input_reply_to 2");