Use get_formatted_text.

This commit is contained in:
levlam 2024-04-23 19:43:12 +03:00
parent e9beb42444
commit 8e2713ca93
9 changed files with 28 additions and 88 deletions

View File

@ -409,15 +409,9 @@ DraftMessage::DraftMessage(Td *td, telegram_api::object_ptr<telegram_api::draftM
CHECK(draft_message != nullptr); CHECK(draft_message != nullptr);
date_ = draft_message->date_; date_ = draft_message->date_;
message_input_reply_to_ = MessageInputReplyTo(td, std::move(draft_message->reply_to_)); message_input_reply_to_ = MessageInputReplyTo(td, std::move(draft_message->reply_to_));
auto entities = get_message_entities(td->user_manager_.get(), std::move(draft_message->entities_), "draftMessage"); auto draft_text =
auto status = fix_formatted_text(draft_message->message_, entities, true, true, true, true, true); get_formatted_text(td->user_manager_.get(), std::move(draft_message->message_),
if (status.is_error()) { std::move(draft_message->entities_), true, true, true, true, true, "DraftMessage");
LOG(ERROR) << "Receive error " << status << " while parsing draft " << draft_message->message_;
if (!clean_input_string(draft_message->message_)) {
draft_message->message_.clear();
}
entities = find_entities(draft_message->message_, false, true);
}
string web_page_url; string web_page_url;
bool force_small_media = false; bool force_small_media = false;
bool force_large_media = false; bool force_large_media = false;
@ -434,9 +428,8 @@ DraftMessage::DraftMessage(Td *td, telegram_api::object_ptr<telegram_api::draftM
force_large_media = media->force_large_media_; force_large_media = media->force_large_media_;
} }
} }
input_message_text_ = InputMessageText(FormattedText{std::move(draft_message->message_), std::move(entities)}, input_message_text_ = InputMessageText(std::move(draft_text), std::move(web_page_url), draft_message->no_webpage_,
std::move(web_page_url), draft_message->no_webpage_, force_small_media, force_small_media, force_large_media, draft_message->invert_media_, false);
force_large_media, draft_message->invert_media_, false);
} }
Result<unique_ptr<DraftMessage>> DraftMessage::get_draft_message( Result<unique_ptr<DraftMessage>> DraftMessage::get_draft_message(

View File

@ -848,16 +848,8 @@ class GetDeepLinkInfoQuery final : public Td::ResultHandler {
return promise_.set_value(nullptr); return promise_.set_value(nullptr);
case telegram_api::help_deepLinkInfo::ID: { case telegram_api::help_deepLinkInfo::ID: {
auto info = telegram_api::move_object_as<telegram_api::help_deepLinkInfo>(result); auto info = telegram_api::move_object_as<telegram_api::help_deepLinkInfo>(result);
auto entities = get_message_entities(nullptr, std::move(info->entities_), "GetDeepLinkInfoQuery"); auto text = get_formatted_text(nullptr, std::move(info->message_), std::move(info->entities_), true, true, true,
auto status = fix_formatted_text(info->message_, entities, true, true, true, true, true); true, true, "GetDeepLinkInfoQuery");
if (status.is_error()) {
LOG(ERROR) << "Receive error " << status << " while parsing deep link info " << info->message_;
if (!clean_input_string(info->message_)) {
info->message_.clear();
}
entities = find_entities(info->message_, true, true);
}
FormattedText text{std::move(info->message_), std::move(entities)};
return promise_.set_value( return promise_.set_value(
td_api::make_object<td_api::deepLinkInfo>(get_formatted_text_object(text, true, -1), info->update_app_)); td_api::make_object<td_api::deepLinkInfo>(get_formatted_text_object(text, true, -1), info->update_app_));
} }

View File

@ -2471,17 +2471,11 @@ InlineMessageContent create_inline_message_content(Td *td, FileId file_id,
switch (bot_inline_message->get_id()) { switch (bot_inline_message->get_id()) {
case telegram_api::botInlineMessageText::ID: { case telegram_api::botInlineMessageText::ID: {
auto inline_message = move_tl_object_as<telegram_api::botInlineMessageText>(bot_inline_message); auto inline_message = move_tl_object_as<telegram_api::botInlineMessageText>(bot_inline_message);
auto entities = auto text = get_formatted_text(td->user_manager_.get(), std::move(inline_message->message_),
get_message_entities(td->user_manager_.get(), std::move(inline_message->entities_), "botInlineMessageText"); std::move(inline_message->entities_), false, true, true, false, false,
auto status = fix_formatted_text(inline_message->message_, entities, false, true, true, false, false); "botInlineMessageText");
if (status.is_error()) {
LOG(ERROR) << "Receive error " << status << " while parsing botInlineMessageText " << inline_message->message_;
break;
}
result.disable_web_page_preview = inline_message->no_webpage_; result.disable_web_page_preview = inline_message->no_webpage_;
result.invert_media = inline_message->invert_media_; result.invert_media = inline_message->invert_media_;
FormattedText text{std::move(inline_message->message_), std::move(entities)};
WebPageId web_page_id; WebPageId web_page_id;
if (!result.disable_web_page_preview) { if (!result.disable_web_page_preview) {
web_page_id = td->web_pages_manager_->get_web_page_by_url(get_first_url(text).str()); web_page_id = td->web_pages_manager_->get_web_page_by_url(get_first_url(text).str());
@ -2497,18 +2491,10 @@ InlineMessageContent create_inline_message_content(Td *td, FileId file_id,
if (inline_message->manual_) { if (inline_message->manual_) {
web_page_url = std::move(inline_message->url_); web_page_url = std::move(inline_message->url_);
} }
auto entities = get_message_entities(td->user_manager_.get(), std::move(inline_message->entities_), auto text = get_formatted_text(td->user_manager_.get(), std::move(inline_message->message_),
"botInlineMessageMediaWebPage"); std::move(inline_message->entities_), !web_page_url.empty(), true, true, false,
auto status = false, "botInlineMessageMediaWebPage");
fix_formatted_text(inline_message->message_, entities, !web_page_url.empty(), true, true, false, false); auto web_page_id =
if (status.is_error()) {
LOG(ERROR) << "Receive error " << status << " while parsing botInlineMessageMediaWebPage "
<< inline_message->message_;
break;
}
FormattedText text{std::move(inline_message->message_), std::move(entities)};
WebPageId web_page_id =
td->web_pages_manager_->get_web_page_by_url(web_page_url.empty() ? get_first_url(text).str() : web_page_url); td->web_pages_manager_->get_web_page_by_url(web_page_url.empty() ? get_first_url(text).str() : web_page_url);
result.message_content = td::make_unique<MessageText>( result.message_content = td::make_unique<MessageText>(
std::move(text), web_page_id, inline_message->force_small_media_, inline_message->force_large_media_, std::move(text), web_page_id, inline_message->force_small_media_, inline_message->force_large_media_,

View File

@ -3957,6 +3957,7 @@ FormattedText get_formatted_text(const UserManager *user_manager, string &&text,
auto status = fix_formatted_text(text, entities, allow_empty, skip_new_entities, skip_bot_commands, auto status = fix_formatted_text(text, entities, allow_empty, skip_new_entities, skip_bot_commands,
skip_media_timestamps, skip_trim); skip_media_timestamps, skip_trim);
if (status.is_error()) { if (status.is_error()) {
LOG(ERROR) << "Receive error " << status << " from " << source << " while parsing " << text;
if (!clean_input_string(text)) { if (!clean_input_string(text)) {
text.clear(); text.clear();
} }

View File

@ -212,10 +212,6 @@ vector<tl_object_ptr<telegram_api::MessageEntity>> get_input_message_entities(co
vector<tl_object_ptr<secret_api::MessageEntity>> get_input_secret_message_entities( vector<tl_object_ptr<secret_api::MessageEntity>> get_input_secret_message_entities(
const vector<MessageEntity> &entities, int32 layer); const vector<MessageEntity> &entities, int32 layer);
vector<MessageEntity> get_message_entities(const UserManager *user_manager,
vector<tl_object_ptr<telegram_api::MessageEntity>> &&server_entities,
const char *source);
vector<MessageEntity> get_message_entities(Td *td, vector<tl_object_ptr<secret_api::MessageEntity>> &&secret_entities, vector<MessageEntity> get_message_entities(Td *td, vector<tl_object_ptr<secret_api::MessageEntity>> &&secret_entities,
bool is_premium, MultiPromiseActor &load_data_multipromise); bool is_premium, MultiPromiseActor &load_data_multipromise);

View File

@ -57,16 +57,9 @@ MessageInputReplyTo::MessageInputReplyTo(Td *td,
dialog_id_ = dialog_id; dialog_id_ = dialog_id;
if (!reply_to->quote_text_.empty()) { if (!reply_to->quote_text_.empty()) {
auto entities = quote_ = get_formatted_text(td->user_manager_.get(), std::move(reply_to->quote_text_),
get_message_entities(td->user_manager_.get(), std::move(reply_to->quote_entities_), "inputReplyToMessage"); std::move(reply_to->quote_entities_), true, true, true, true, false,
auto status = fix_formatted_text(reply_to->quote_text_, entities, true, true, true, true, false); "inputReplyToMessage");
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)};
remove_unallowed_quote_entities(quote_); remove_unallowed_quote_entities(quote_);
quote_position_ = max(0, reply_to->quote_offset_); quote_position_ = max(0, reply_to->quote_offset_);
} }

View File

@ -1812,16 +1812,9 @@ PollId PollManager::on_get_poll(PollId poll_id, tl_object_ptr<telegram_api::poll
} }
} }
auto entities = get_message_entities(td_->user_manager_.get(), std::move(poll_results->solution_entities_), source); auto explanation =
auto status = fix_formatted_text(poll_results->solution_, entities, true, true, true, true, false); get_formatted_text(td_->user_manager_.get(), std::move(poll_results->solution_),
if (status.is_error()) { std::move(poll_results->solution_entities_), true, true, true, true, false, source);
if (!clean_input_string(poll_results->solution_)) {
poll_results->solution_.clear();
}
entities = find_entities(poll_results->solution_, true, true);
}
FormattedText explanation{std::move(poll_results->solution_), std::move(entities)};
if (poll->is_quiz_) { if (poll->is_quiz_) {
if (poll->correct_option_id_ != correct_option_id) { if (poll->correct_option_id_ != correct_option_id) {
if (correct_option_id == -1 && poll->correct_option_id_ != -1) { if (correct_option_id == -1 && poll->correct_option_id_ != -1) {

View File

@ -117,18 +117,11 @@ RepliedMessageInfo::RepliedMessageInfo(Td *td, tl_object_ptr<telegram_api::messa
} }
if ((!origin_.is_empty() || message_id_ != MessageId()) && !reply_header->quote_text_.empty()) { if ((!origin_.is_empty() || message_id_ != MessageId()) && !reply_header->quote_text_.empty()) {
is_quote_manual_ = reply_header->quote_; is_quote_manual_ = reply_header->quote_;
auto entities = quote_ = get_formatted_text(td->user_manager_.get(), std::move(reply_header->quote_text_),
get_message_entities(td->user_manager_.get(), std::move(reply_header->quote_entities_), "RepliedMessageInfo"); std::move(reply_header->quote_entities_), true, true, true, true, false,
auto status = fix_formatted_text(reply_header->quote_text_, entities, true, true, true, true, false); "RepliedMessageInfo");
if (status.is_error()) {
if (!clean_input_string(reply_header->quote_text_)) {
reply_header->quote_text_.clear();
}
entities.clear();
}
quote_ = FormattedText{std::move(reply_header->quote_text_), std::move(entities)};
quote_position_ = max(0, reply_header->quote_offset_);
remove_unallowed_quote_entities(quote_); remove_unallowed_quote_entities(quote_);
quote_position_ = max(0, reply_header->quote_offset_);
} }
} }

View File

@ -95,18 +95,11 @@ TermsOfService::TermsOfService(telegram_api::object_ptr<telegram_api::help_terms
} }
id_ = std::move(terms->id_->data_); id_ = std::move(terms->id_->data_);
auto entities = get_message_entities(nullptr, std::move(terms->entities_), "TermsOfService"); text_ = get_formatted_text(nullptr, std::move(terms->text_), std::move(terms->entities_), true, true, true, true,
auto status = fix_formatted_text(terms->text_, entities, true, true, true, true, false); false, "TermsOfService");
if (status.is_error()) { if (text_.text.empty()) {
if (!clean_input_string(terms->text_)) {
terms->text_.clear();
}
entities = find_entities(terms->text_, true, true);
}
if (terms->text_.empty()) {
id_.clear(); id_.clear();
} }
text_ = FormattedText{std::move(terms->text_), std::move(entities)};
min_user_age_ = terms->min_age_confirm_; min_user_age_ = terms->min_age_confirm_;
show_popup_ = terms->popup_; show_popup_ = terms->popup_;
} }