Add skip_media_timestamps parameter to get_formatted_text.
This commit is contained in:
parent
ab71d6a200
commit
389e92f1f4
@ -45,7 +45,7 @@ unique_ptr<DraftMessage> get_draft_message(ContactsManager *contacts_manager,
|
||||
}
|
||||
|
||||
auto entities = get_message_entities(contacts_manager, std::move(draft->entities_), "draftMessage");
|
||||
auto status = fix_formatted_text(draft->message_, entities, true, true, true, true);
|
||||
auto status = fix_formatted_text(draft->message_, entities, true, true, true, true, true);
|
||||
if (status.is_error()) {
|
||||
LOG(ERROR) << "Receive error " << status << " while parsing draft " << draft->message_;
|
||||
if (!clean_input_string(draft->message_)) {
|
||||
|
@ -39,15 +39,16 @@ Result<InputMessageText> process_input_message_text(const ContactsManager *conta
|
||||
}
|
||||
|
||||
TRY_RESULT(entities, get_message_entities(contacts_manager, std::move(input_message_text->text_->entities_)));
|
||||
auto need_skip_commands = need_always_skip_bot_commands(contacts_manager, dialog_id, is_bot);
|
||||
auto need_skip_bot_commands = need_always_skip_bot_commands(contacts_manager, dialog_id, is_bot);
|
||||
bool parse_markdown = G()->shared_config().get_option_boolean("always_parse_markdown");
|
||||
TRY_STATUS(fix_formatted_text(input_message_text->text_->text_, entities, for_draft, parse_markdown,
|
||||
need_skip_commands, for_draft));
|
||||
need_skip_bot_commands, is_bot || for_draft || parse_markdown, for_draft));
|
||||
InputMessageText result{FormattedText{std::move(input_message_text->text_->text_), std::move(entities)},
|
||||
input_message_text->disable_web_page_preview_, input_message_text->clear_draft_};
|
||||
if (G()->shared_config().get_option_boolean("always_parse_markdown")) {
|
||||
if (parse_markdown) {
|
||||
result.text = parse_markdown_v3(std::move(result.text));
|
||||
fix_formatted_text(result.text.text, result.text.entities, for_draft, false, need_skip_commands, for_draft)
|
||||
fix_formatted_text(result.text.text, result.text.entities, for_draft, false, need_skip_bot_commands,
|
||||
is_bot || for_draft, for_draft)
|
||||
.ensure();
|
||||
}
|
||||
return std::move(result);
|
||||
|
@ -357,7 +357,7 @@ class GetDeepLinkInfoQuery final : public Td::ResultHandler {
|
||||
bool need_update = (info->flags_ & telegram_api::help_deepLinkInfo::UPDATE_APP_MASK) != 0;
|
||||
|
||||
auto entities = get_message_entities(nullptr, std::move(info->entities_), "GetDeepLinkInfoQuery");
|
||||
auto status = fix_formatted_text(info->message_, entities, true, true, true, true);
|
||||
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_)) {
|
||||
@ -1005,7 +1005,7 @@ unique_ptr<LinkManager::InternalLink> LinkManager::get_internal_link_message_dra
|
||||
} else {
|
||||
full_text.text = url.str();
|
||||
}
|
||||
if (fix_formatted_text(full_text.text, full_text.entities, false, false, false, true).is_error()) {
|
||||
if (fix_formatted_text(full_text.text, full_text.entities, false, false, false, true, true).is_error()) {
|
||||
return nullptr;
|
||||
}
|
||||
if (full_text.text[0] == '@') {
|
||||
|
@ -1461,7 +1461,7 @@ InlineMessageContent create_inline_message_content(Td *td, FileId file_id,
|
||||
auto inline_message = move_tl_object_as<telegram_api::botInlineMessageText>(bot_inline_message);
|
||||
auto entities = get_message_entities(td->contacts_manager_.get(), std::move(inline_message->entities_),
|
||||
"botInlineMessageText");
|
||||
auto status = fix_formatted_text(inline_message->message_, entities, false, true, true, false);
|
||||
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;
|
||||
@ -1525,7 +1525,7 @@ InlineMessageContent create_inline_message_content(Td *td, FileId file_id,
|
||||
auto inline_message = move_tl_object_as<telegram_api::botInlineMessageMediaAuto>(bot_inline_message);
|
||||
auto caption =
|
||||
get_message_text(td->contacts_manager_.get(), inline_message->message_, std::move(inline_message->entities_),
|
||||
true, 0, false, "create_inline_message_content");
|
||||
true, false, 0, false, "create_inline_message_content");
|
||||
if (allowed_media_content_id == td_api::inputMessageAnimation::ID) {
|
||||
result.message_content = make_unique<MessageAnimation>(file_id, std::move(caption));
|
||||
} else if (allowed_media_content_id == td_api::inputMessageAudio::ID) {
|
||||
@ -3823,7 +3823,7 @@ unique_ptr<MessageContent> get_secret_message_content(
|
||||
}
|
||||
|
||||
auto entities = get_message_entities(std::move(secret_entities));
|
||||
auto status = fix_formatted_text(message_text, entities, true, false, true, false);
|
||||
auto status = fix_formatted_text(message_text, entities, true, false, true, true, false);
|
||||
if (status.is_error()) {
|
||||
LOG(WARNING) << "Receive error " << status << " while parsing secret message \"" << message_text
|
||||
<< "\" with entities " << format::as_array(entities);
|
||||
|
@ -3777,7 +3777,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 for_draft) {
|
||||
bool skip_bot_commands, bool skip_media_timestamps, bool for_draft) {
|
||||
string result;
|
||||
if (entities.empty()) {
|
||||
// fast path
|
||||
@ -3898,11 +3898,12 @@ Status fix_formatted_text(string &text, vector<MessageEntity> &entities, bool al
|
||||
|
||||
FormattedText get_message_text(const ContactsManager *contacts_manager, string message_text,
|
||||
vector<tl_object_ptr<telegram_api::MessageEntity>> &&server_entities,
|
||||
bool skip_new_entities, int32 send_date, bool from_album, const char *source) {
|
||||
bool skip_new_entities, bool skip_media_timestamps, int32 send_date, bool from_album,
|
||||
const char *source) {
|
||||
auto entities = get_message_entities(contacts_manager, std::move(server_entities), source);
|
||||
auto debug_message_text = message_text;
|
||||
auto debug_entities = entities;
|
||||
auto status = fix_formatted_text(message_text, entities, true, skip_new_entities, true, false);
|
||||
auto status = fix_formatted_text(message_text, entities, true, skip_new_entities, true, skip_media_timestamps, false);
|
||||
if (status.is_error()) {
|
||||
// message entities in media albums can be wrong because of a long time ago fixed server-side bug
|
||||
if (!from_album && (send_date == 0 || send_date > 1600340000)) { // approximate fix date
|
||||
@ -3957,7 +3958,7 @@ Result<FormattedText> process_input_caption(const ContactsManager *contacts_mana
|
||||
}
|
||||
TRY_RESULT(entities, get_message_entities(contacts_manager, std::move(caption->entities_)));
|
||||
TRY_STATUS(fix_formatted_text(caption->text_, entities, true, false,
|
||||
need_always_skip_bot_commands(contacts_manager, dialog_id, is_bot), false));
|
||||
need_always_skip_bot_commands(contacts_manager, dialog_id, is_bot), is_bot, false));
|
||||
return FormattedText{std::move(caption->text_), std::move(entities)};
|
||||
}
|
||||
|
||||
|
@ -179,11 +179,12 @@ vector<MessageEntity> get_message_entities(vector<tl_object_ptr<secret_api::Mess
|
||||
|
||||
// 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 for_draft) TD_WARN_UNUSED_RESULT;
|
||||
bool skip_bot_commands, bool skip_media_timestamps, bool for_draft) TD_WARN_UNUSED_RESULT;
|
||||
|
||||
FormattedText get_message_text(const ContactsManager *contacts_manager, string message_text,
|
||||
vector<tl_object_ptr<telegram_api::MessageEntity>> &&server_entities,
|
||||
bool skip_new_entities, int32 send_date, bool from_album, const char *source);
|
||||
bool skip_new_entities, bool skip_media_timestamps, int32 send_date, bool from_album,
|
||||
const char *source);
|
||||
|
||||
td_api::object_ptr<td_api::formattedText> extract_input_caption(
|
||||
tl_object_ptr<td_api::InputMessageContent> &input_message_content);
|
||||
|
@ -6523,7 +6523,7 @@ void MessagesManager::on_update_service_notification(tl_object_ptr<telegram_api:
|
||||
bool is_user = is_authorized && !td_->auth_manager_->is_bot();
|
||||
auto contacts_manager = is_authorized ? td_->contacts_manager_.get() : nullptr;
|
||||
auto message_text = get_message_text(contacts_manager, std::move(update->message_), std::move(update->entities_),
|
||||
skip_new_entities, date, false, "on_update_service_notification");
|
||||
skip_new_entities, !is_user, date, false, "on_update_service_notification");
|
||||
DialogId owner_dialog_id = is_user ? get_service_notifications_dialog()->dialog_id : DialogId();
|
||||
auto content = get_message_content(td_, std::move(message_text), std::move(update->media_), owner_dialog_id, false,
|
||||
UserId(), &ttl);
|
||||
@ -13213,7 +13213,8 @@ MessagesManager::MessageInfo MessagesManager::parse_telegram_api_message(
|
||||
message_info.content = get_message_content(
|
||||
td_,
|
||||
get_message_text(td_->contacts_manager_.get(), std::move(message->message_), std::move(message->entities_),
|
||||
true, message_info.forward_header ? message_info.forward_header->date_ : message_info.date,
|
||||
true, td_->auth_manager_->is_bot(),
|
||||
message_info.forward_header ? message_info.forward_header->date_ : message_info.date,
|
||||
message_info.media_album_id != 0, new_source.c_str()),
|
||||
std::move(message->media_), message_info.dialog_id, is_content_read, message_info.via_bot_user_id,
|
||||
&message_info.ttl);
|
||||
@ -14207,7 +14208,7 @@ void MessagesManager::on_update_sent_text_message(int64 random_id,
|
||||
const FormattedText *old_message_text = get_message_content_text(m->content.get());
|
||||
CHECK(old_message_text != nullptr);
|
||||
FormattedText new_message_text = get_message_text(
|
||||
td_->contacts_manager_.get(), old_message_text->text, std::move(entities), true,
|
||||
td_->contacts_manager_.get(), old_message_text->text, std::move(entities), true, td_->auth_manager_->is_bot(),
|
||||
m->forward_info ? m->forward_info->date : m->date, m->media_album_id != 0, "on_update_sent_text_message");
|
||||
auto new_content = get_message_content(td_, std::move(new_message_text), std::move(message_media), dialog_id,
|
||||
true /*likely ignored*/, UserId() /*likely ignored*/, nullptr /*ignored*/);
|
||||
|
@ -1544,7 +1544,7 @@ PollId PollManager::on_get_poll(PollId poll_id, tl_object_ptr<telegram_api::poll
|
||||
|
||||
auto entities =
|
||||
get_message_entities(td_->contacts_manager_.get(), std::move(poll_results->solution_entities_), "on_get_poll");
|
||||
auto status = fix_formatted_text(poll_results->solution_, entities, true, true, true, false);
|
||||
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();
|
||||
|
@ -8368,13 +8368,13 @@ td_api::object_ptr<td_api::Object> Td::do_static_request(td_api::parseMarkdown &
|
||||
return make_error(400, r_entities.error().message());
|
||||
}
|
||||
auto entities = r_entities.move_as_ok();
|
||||
auto status = fix_formatted_text(request.text_->text_, entities, true, true, true, true);
|
||||
auto status = fix_formatted_text(request.text_->text_, entities, true, true, true, true, true);
|
||||
if (status.is_error()) {
|
||||
return make_error(400, status.error().message());
|
||||
}
|
||||
|
||||
auto parsed_text = parse_markdown_v3({std::move(request.text_->text_), std::move(entities)});
|
||||
fix_formatted_text(parsed_text.text, parsed_text.entities, true, true, true, true).ensure();
|
||||
fix_formatted_text(parsed_text.text, parsed_text.entities, true, true, true, true, true).ensure();
|
||||
return get_formatted_text_object(parsed_text, true);
|
||||
}
|
||||
|
||||
@ -8388,7 +8388,7 @@ td_api::object_ptr<td_api::Object> Td::do_static_request(td_api::getMarkdownText
|
||||
return make_error(400, r_entities.error().message());
|
||||
}
|
||||
auto entities = r_entities.move_as_ok();
|
||||
auto status = fix_formatted_text(request.text_->text_, entities, true, true, true, true);
|
||||
auto status = fix_formatted_text(request.text_->text_, entities, true, true, true, true, true);
|
||||
if (status.is_error()) {
|
||||
return make_error(400, status.error().message());
|
||||
}
|
||||
|
@ -95,7 +95,7 @@ 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, false);
|
||||
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();
|
||||
|
@ -792,7 +792,7 @@ int64 WebPagesManager::get_web_page_preview(td_api::object_ptr<td_api::formatted
|
||||
}
|
||||
auto entities = r_entities.move_as_ok();
|
||||
|
||||
auto result = fix_formatted_text(text->text_, entities, true, false, true, false);
|
||||
auto result = fix_formatted_text(text->text_, entities, true, false, true, true, false);
|
||||
if (result.is_error() || text->text_.empty()) {
|
||||
promise.set_value(Unit());
|
||||
return 0;
|
||||
|
@ -655,16 +655,16 @@ static void check_fix_formatted_text(td::string str, td::vector<td::MessageEntit
|
||||
const td::vector<td::MessageEntity> &expected_entities, bool allow_empty = true,
|
||||
bool skip_new_entities = false, bool skip_bot_commands = false,
|
||||
bool for_draft = true) {
|
||||
ASSERT_TRUE(
|
||||
td::fix_formatted_text(str, entities, allow_empty, skip_new_entities, skip_bot_commands, for_draft).is_ok());
|
||||
ASSERT_TRUE(td::fix_formatted_text(str, entities, allow_empty, skip_new_entities, skip_bot_commands, true, for_draft)
|
||||
.is_ok());
|
||||
ASSERT_STREQ(expected_str, str);
|
||||
ASSERT_EQ(expected_entities, entities);
|
||||
}
|
||||
|
||||
static void check_fix_formatted_text(td::string str, td::vector<td::MessageEntity> entities, bool allow_empty,
|
||||
bool skip_new_entities, bool skip_bot_commands, bool for_draft) {
|
||||
ASSERT_TRUE(
|
||||
fix_formatted_text(str, entities, allow_empty, skip_new_entities, skip_bot_commands, for_draft).is_error());
|
||||
ASSERT_TRUE(td::fix_formatted_text(str, entities, allow_empty, skip_new_entities, skip_bot_commands, true, for_draft)
|
||||
.is_error());
|
||||
}
|
||||
|
||||
TEST(MessageEntities, fix_formatted_text) {
|
||||
@ -1064,7 +1064,7 @@ TEST(MessageEntities, fix_formatted_text) {
|
||||
return result;
|
||||
};
|
||||
auto old_type_mask = get_type_mask(str.size(), entities);
|
||||
ASSERT_TRUE(td::fix_formatted_text(str, entities, false, false, true, false).is_ok());
|
||||
ASSERT_TRUE(td::fix_formatted_text(str, entities, false, false, true, true, false).is_ok());
|
||||
auto new_type_mask = get_type_mask(str.size(), entities);
|
||||
auto splittable_mask = (1 << 5) | (1 << 6) | (1 << 14) | (1 << 15);
|
||||
auto pre_mask = (1 << 7) | (1 << 8) | (1 << 9);
|
||||
@ -1384,7 +1384,7 @@ static void check_parse_markdown_v3(td::string text, td::vector<td::MessageEntit
|
||||
bool fix = false) {
|
||||
auto parsed_text = td::parse_markdown_v3({std::move(text), std::move(entities)});
|
||||
if (fix) {
|
||||
ASSERT_TRUE(fix_formatted_text(parsed_text.text, parsed_text.entities, true, true, true, true).is_ok());
|
||||
ASSERT_TRUE(td::fix_formatted_text(parsed_text.text, parsed_text.entities, true, true, true, true, true).is_ok());
|
||||
}
|
||||
ASSERT_STREQ(result_text, parsed_text.text);
|
||||
ASSERT_EQ(result_entities, parsed_text.entities);
|
||||
@ -1676,9 +1676,9 @@ TEST(MessageEntities, parse_markdown_v3) {
|
||||
|
||||
td::FormattedText text{std::move(str), std::move(entities)};
|
||||
while (true) {
|
||||
ASSERT_TRUE(fix_formatted_text(text.text, text.entities, true, true, true, true).is_ok());
|
||||
ASSERT_TRUE(td::fix_formatted_text(text.text, text.entities, true, true, true, true, true).is_ok());
|
||||
auto parsed_text = td::parse_markdown_v3(text);
|
||||
ASSERT_TRUE(fix_formatted_text(parsed_text.text, parsed_text.entities, true, true, true, true).is_ok());
|
||||
ASSERT_TRUE(td::fix_formatted_text(parsed_text.text, parsed_text.entities, true, true, true, true, true).is_ok());
|
||||
if (parsed_text == text) {
|
||||
break;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user