From 6417510d51104441ca6861cb0bc6d99639544f7a Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 10 Mar 2020 03:51:56 +0300 Subject: [PATCH] Return all entities in get_message_entities. GitOrigin-RevId: b08c7a9ab0cfa9a78e450d5cbf3567203abd7d67 --- td/telegram/MessageEntity.cpp | 45 +++++++++++++++++++++++++++++------ td/telegram/MessageEntity.h | 3 ++- td/telegram/Td.cpp | 2 +- test/message_entities.cpp | 3 ++- 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/td/telegram/MessageEntity.cpp b/td/telegram/MessageEntity.cpp index 08db8b95b..57b6c0046 100644 --- a/td/telegram/MessageEntity.cpp +++ b/td/telegram/MessageEntity.cpp @@ -1908,8 +1908,8 @@ static vector find_text_url_entities_v3(Slice text) { // entities must be valid for the text static FormattedText parse_text_url_entities_v3(Slice text, vector entities) { - // TextUrl and MentionName can't intersect TextUrl entities, - // so try to find new TextUrl entities only between the predetermined TextUrl and MentionName entities + // continuous entities can't intersect TextUrl entities, + // so try to find new TextUrl entities only between the predetermined continuous entities FormattedText result; int32 result_text_utf16_length = 0; @@ -2068,7 +2068,7 @@ static FormattedText parse_text_url_entities_v3(Slice text, vector max_end) { // found a gap from max_end to entity.offset between predetermined entities @@ -2137,7 +2137,7 @@ static vector find_splittable_entities_v3(Slice text, const vecto return result; } -// entities must be valid and can contain only splittable, TextUrl and MentionName entities +// entities must be valid and can contain only splittable and continuous entities // __italic__ ~~strikethrough~~ **bold** and [text_url](telegram.org) entities are left to be parsed static FormattedText parse_markdown_v3_without_pre(Slice text, vector entities) { check_is_sorted(entities); @@ -2386,8 +2386,7 @@ FormattedText parse_markdown_v3(FormattedText text) { for (size_t i = 0; i < text.entities.size(); i++) { auto &entity = text.entities[i]; - CHECK(is_splittable_entity(entity.type) || is_pre_entity(entity.type) || - entity.type == MessageEntity::Type::TextUrl || entity.type == MessageEntity::Type::MentionName); + CHECK(is_splittable_entity(entity.type) || is_pre_entity(entity.type) || is_continuous_entity(entity.type)); if (is_pre_entity(entity.type)) { CHECK(entity.offset >= max_end); CHECK(i + 1 == text.entities.size() || text.entities[i + 1].offset >= entity.offset + entity.length); @@ -2836,7 +2835,8 @@ vector> get_input_secret_message_entiti } Result> get_message_entities(const ContactsManager *contacts_manager, - vector> &&input_entities) { + vector> &&input_entities, + bool allow_all) { vector entities; for (auto &entity : input_entities) { if (entity == nullptr || entity->type_ == nullptr) { @@ -2845,13 +2845,44 @@ Result> get_message_entities(const ContactsManager *contac switch (entity->type_->get_id()) { case td_api::textEntityTypeMention::ID: + if (allow_all) { + entities.emplace_back(MessageEntity::Type::Mention, entity->offset_, entity->length_); + } + break; case td_api::textEntityTypeHashtag::ID: + if (allow_all) { + entities.emplace_back(MessageEntity::Type::Hashtag, entity->offset_, entity->length_); + } + break; case td_api::textEntityTypeBotCommand::ID: + if (allow_all) { + entities.emplace_back(MessageEntity::Type::BotCommand, entity->offset_, entity->length_); + } + break; case td_api::textEntityTypeUrl::ID: + if (allow_all) { + entities.emplace_back(MessageEntity::Type::Url, entity->offset_, entity->length_); + } + break; case td_api::textEntityTypeEmailAddress::ID: + if (allow_all) { + entities.emplace_back(MessageEntity::Type::EmailAddress, entity->offset_, entity->length_); + } + break; case td_api::textEntityTypeCashtag::ID: + if (allow_all) { + entities.emplace_back(MessageEntity::Type::Cashtag, entity->offset_, entity->length_); + } + break; case td_api::textEntityTypePhoneNumber::ID: + if (allow_all) { + entities.emplace_back(MessageEntity::Type::PhoneNumber, entity->offset_, entity->length_); + } + break; case td_api::textEntityTypeBankCardNumber::ID: + if (allow_all) { + entities.emplace_back(MessageEntity::Type::BankCardNumber, entity->offset_, entity->length_); + } break; case td_api::textEntityTypeBold::ID: entities.emplace_back(MessageEntity::Type::Bold, entity->offset_, entity->length_); diff --git a/td/telegram/MessageEntity.h b/td/telegram/MessageEntity.h index dc85dc4b5..437909d60 100644 --- a/td/telegram/MessageEntity.h +++ b/td/telegram/MessageEntity.h @@ -124,7 +124,8 @@ inline bool operator!=(const FormattedText &lhs, const FormattedText &rhs) { const std::unordered_set &get_valid_short_usernames(); Result> get_message_entities(const ContactsManager *contacts_manager, - vector> &&input_entities); + vector> &&input_entities, + bool allow_all = false); vector> get_text_entities_object(const vector &entities); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 2272964eb..d36a002c0 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -7560,7 +7560,7 @@ td_api::object_ptr Td::do_static_request(td_api::parseMarkdown & return make_error(400, "Text must be non-empty"); } - auto r_entities = get_message_entities(nullptr, std::move(request.text_->entities_)); + auto r_entities = get_message_entities(nullptr, std::move(request.text_->entities_), true); if (r_entities.is_error()) { return make_error(400, r_entities.error().message()); } diff --git a/test/message_entities.cpp b/test/message_entities.cpp index 427d84ad8..d2f47758c 100644 --- a/test/message_entities.cpp +++ b/test/message_entities.cpp @@ -1493,7 +1493,8 @@ TEST(MessageEntities, parse_markdown_v3) { td::vector types{ td::MessageEntity::Type::Bold, td::MessageEntity::Type::Italic, td::MessageEntity::Type::Underline, td::MessageEntity::Type::Strikethrough, td::MessageEntity::Type::Code, td::MessageEntity::Type::Pre, - td::MessageEntity::Type::PreCode, td::MessageEntity::Type::TextUrl, td::MessageEntity::Type::MentionName}; + td::MessageEntity::Type::PreCode, td::MessageEntity::Type::TextUrl, td::MessageEntity::Type::MentionName, + td::MessageEntity::Type::Cashtag}; for (size_t test_n = 0; test_n < 1000; test_n++) { td::string str; int part_n = td::Random::fast(1, 200);