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);
date_ = draft_message->date_;
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 status = fix_formatted_text(draft_message->message_, entities, true, true, true, true, true);
if (status.is_error()) {
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);
}
auto draft_text =
get_formatted_text(td->user_manager_.get(), std::move(draft_message->message_),
std::move(draft_message->entities_), true, true, true, true, true, "DraftMessage");
string web_page_url;
bool force_small_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_;
}
}
input_message_text_ = InputMessageText(FormattedText{std::move(draft_message->message_), std::move(entities)},
std::move(web_page_url), draft_message->no_webpage_, force_small_media,
force_large_media, draft_message->invert_media_, false);
input_message_text_ = InputMessageText(std::move(draft_text), std::move(web_page_url), draft_message->no_webpage_,
force_small_media, force_large_media, draft_message->invert_media_, false);
}
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);
case telegram_api::help_deepLinkInfo::ID: {
auto info = telegram_api::move_object_as<telegram_api::help_deepLinkInfo>(result);
auto entities = get_message_entities(nullptr, std::move(info->entities_), "GetDeepLinkInfoQuery");
auto status = fix_formatted_text(info->message_, entities, true, true, true, true, true);
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)};
auto text = get_formatted_text(nullptr, std::move(info->message_), std::move(info->entities_), true, true, true,
true, true, "GetDeepLinkInfoQuery");
return promise_.set_value(
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()) {
case telegram_api::botInlineMessageText::ID: {
auto inline_message = move_tl_object_as<telegram_api::botInlineMessageText>(bot_inline_message);
auto entities =
get_message_entities(td->user_manager_.get(), std::move(inline_message->entities_), "botInlineMessageText");
auto status = fix_formatted_text(inline_message->message_, entities, false, true, true, false, false);
if (status.is_error()) {
LOG(ERROR) << "Receive error " << status << " while parsing botInlineMessageText " << inline_message->message_;
break;
}
auto text = get_formatted_text(td->user_manager_.get(), std::move(inline_message->message_),
std::move(inline_message->entities_), false, true, true, false, false,
"botInlineMessageText");
result.disable_web_page_preview = inline_message->no_webpage_;
result.invert_media = inline_message->invert_media_;
FormattedText text{std::move(inline_message->message_), std::move(entities)};
WebPageId web_page_id;
if (!result.disable_web_page_preview) {
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_) {
web_page_url = std::move(inline_message->url_);
}
auto entities = get_message_entities(td->user_manager_.get(), std::move(inline_message->entities_),
"botInlineMessageMediaWebPage");
auto status =
fix_formatted_text(inline_message->message_, entities, !web_page_url.empty(), true, true, false, false);
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 =
auto text = get_formatted_text(td->user_manager_.get(), std::move(inline_message->message_),
std::move(inline_message->entities_), !web_page_url.empty(), true, true, false,
false, "botInlineMessageMediaWebPage");
auto web_page_id =
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>(
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,
skip_media_timestamps, skip_trim);
if (status.is_error()) {
LOG(ERROR) << "Receive error " << status << " from " << source << " while parsing " << text;
if (!clean_input_string(text)) {
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(
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,
bool is_premium, MultiPromiseActor &load_data_multipromise);

View File

@ -57,16 +57,9 @@ MessageInputReplyTo::MessageInputReplyTo(Td *td,
dialog_id_ = dialog_id;
if (!reply_to->quote_text_.empty()) {
auto entities =
get_message_entities(td->user_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)};
quote_ = get_formatted_text(td->user_manager_.get(), std::move(reply_to->quote_text_),
std::move(reply_to->quote_entities_), true, true, true, true, false,
"inputReplyToMessage");
remove_unallowed_quote_entities(quote_);
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 status = fix_formatted_text(poll_results->solution_, entities, true, true, true, true, false);
if (status.is_error()) {
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)};
auto explanation =
get_formatted_text(td_->user_manager_.get(), std::move(poll_results->solution_),
std::move(poll_results->solution_entities_), true, true, true, true, false, source);
if (poll->is_quiz_) {
if (poll->correct_option_id_ != correct_option_id) {
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()) {
is_quote_manual_ = reply_header->quote_;
auto entities =
get_message_entities(td->user_manager_.get(), std::move(reply_header->quote_entities_), "RepliedMessageInfo");
auto status = fix_formatted_text(reply_header->quote_text_, entities, true, true, true, true, false);
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_);
quote_ = get_formatted_text(td->user_manager_.get(), std::move(reply_header->quote_text_),
std::move(reply_header->quote_entities_), true, true, true, true, false,
"RepliedMessageInfo");
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_);
auto entities = get_message_entities(nullptr, std::move(terms->entities_), "TermsOfService");
auto status = fix_formatted_text(terms->text_, entities, true, true, true, true, false);
if (status.is_error()) {
if (!clean_input_string(terms->text_)) {
terms->text_.clear();
}
entities = find_entities(terms->text_, true, true);
}
if (terms->text_.empty()) {
text_ = get_formatted_text(nullptr, std::move(terms->text_), std::move(terms->entities_), true, true, true, true,
false, "TermsOfService");
if (text_.text.empty()) {
id_.clear();
}
text_ = FormattedText{std::move(terms->text_), std::move(entities)};
min_user_age_ = terms->min_age_confirm_;
show_popup_ = terms->popup_;
}