From 2098f10441a0e98d4e46839b817bd2e19360d41b Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 6 Aug 2021 09:14:52 +0300 Subject: [PATCH] Store media_timestamp in a separate field. --- td/telegram/MessageEntity.cpp | 23 ++++++++++------------- td/telegram/MessageEntity.h | 17 +++++++++++------ td/telegram/MessageEntity.hpp | 6 ++++++ 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/td/telegram/MessageEntity.cpp b/td/telegram/MessageEntity.cpp index b9f213542..6ac590870 100644 --- a/td/telegram/MessageEntity.cpp +++ b/td/telegram/MessageEntity.cpp @@ -133,7 +133,7 @@ tl_object_ptr MessageEntity::get_text_entity_type_object case MessageEntity::Type::BankCardNumber: return make_tl_object(); case MessageEntity::Type::MediaTimestamp: - return make_tl_object(to_integer(argument)); + return make_tl_object(media_timestamp); default: UNREACHABLE(); return nullptr; @@ -153,8 +153,7 @@ vector> get_text_entities_object(const vector< if (skip_bot_commands && entity.type == MessageEntity::Type::BotCommand) { continue; } - if (entity.type == MessageEntity::Type::MediaTimestamp && - (max_media_timestamp < 0 || max_media_timestamp < to_integer(entity.argument))) { + if (entity.type == MessageEntity::Type::MediaTimestamp && max_media_timestamp < entity.media_timestamp) { continue; } auto entity_object = entity.get_text_entity_object(); @@ -1632,7 +1631,7 @@ vector find_entities(Slice text, bool skip_bot_commands, bool ski for (auto &entity : media_timestamps) { auto offset = narrow_cast(entity.first.begin() - text.begin()); auto length = narrow_cast(entity.first.size()); - entities.emplace_back(MessageEntity::Type::MediaTimestamp, offset, length, to_string(entity.second)); + entities.emplace_back(MessageEntity::Type::MediaTimestamp, offset, length, entity.second); } } @@ -1648,7 +1647,7 @@ static vector find_media_timestamp_entities(Slice text) { for (auto &entity : media_timestamps) { auto offset = narrow_cast(entity.first.begin() - text.begin()); auto length = narrow_cast(entity.first.size()); - entities.emplace_back(MessageEntity::Type::MediaTimestamp, offset, length, to_string(entity.second)); + entities.emplace_back(MessageEntity::Type::MediaTimestamp, offset, length, entity.second); } fix_entity_offsets(text, entities); @@ -3253,11 +3252,11 @@ Result> get_message_entities(const ContactsManager *contac } case td_api::textEntityTypeMediaTimestamp::ID: { auto entity_media_timestamp = static_cast(entity->type_.get()); - if (entity_media_timestamp->media_timestamp_ <= 0) { + if (entity_media_timestamp->media_timestamp_ < 0) { return Status::Error(400, "Invalid media timestamp specified"); } entities.emplace_back(MessageEntity::Type::MediaTimestamp, entity->offset_, entity->length_, - to_string(entity_media_timestamp->media_timestamp_)); + entity_media_timestamp->media_timestamp_); break; } default: @@ -4096,16 +4095,14 @@ void add_formatted_text_dependencies(Dependencies &dependencies, const Formatted } } -bool has_media_timestamps(const FormattedText *text, int32 min_timestamp, int32 max_timestamp) { +bool has_media_timestamps(const FormattedText *text, int32 min_media_timestamp, int32 max_media_timestamp) { if (text == nullptr) { return false; } for (auto &entity : text->entities) { - if (entity.type == MessageEntity::Type::MediaTimestamp) { - int32 timestamp = to_integer(entity.argument); - if (min_timestamp <= timestamp && timestamp <= max_timestamp) { - return true; - } + if (entity.type == MessageEntity::Type::MediaTimestamp && min_media_timestamp <= entity.media_timestamp && + entity.media_timestamp <= max_media_timestamp) { + return true; } } return false; diff --git a/td/telegram/MessageEntity.h b/td/telegram/MessageEntity.h index 78023238f..ff3d54bc3 100644 --- a/td/telegram/MessageEntity.h +++ b/td/telegram/MessageEntity.h @@ -51,19 +51,24 @@ class MessageEntity { MediaTimestamp, Size }; - Type type; - int32 offset; - int32 length; + Type type = Type::Size; + int32 offset = -1; + int32 length = -1; + int32 media_timestamp = -1; string argument; UserId user_id; MessageEntity() = default; MessageEntity(Type type, int32 offset, int32 length, string argument = "") - : type(type), offset(offset), length(length), argument(std::move(argument)), user_id() { + : type(type), offset(offset), length(length), media_timestamp(-1), argument(std::move(argument)), user_id() { } MessageEntity(int32 offset, int32 length, UserId user_id) - : type(Type::MentionName), offset(offset), length(length), argument(), user_id(user_id) { + : type(Type::MentionName), offset(offset), length(length), media_timestamp(-1), argument(), user_id(user_id) { + } + MessageEntity(Type type, int32 offset, int32 length, int32 media_timestamp) + : type(type), offset(offset), length(length), media_timestamp(media_timestamp), argument(), user_id(user_id) { + CHECK(type == Type::MediaTimestamp); } tl_object_ptr get_text_entity_object() const; @@ -196,7 +201,7 @@ Result process_input_caption(const ContactsManager *contacts_mana void add_formatted_text_dependencies(Dependencies &dependencies, const FormattedText *text); -bool has_media_timestamps(const FormattedText *text, int32 min_timestamp, int32 max_timestamp); +bool has_media_timestamps(const FormattedText *text, int32 min_media_timestamp, int32 max_media_timestamp); bool has_bot_commands(const FormattedText *text); diff --git a/td/telegram/MessageEntity.hpp b/td/telegram/MessageEntity.hpp index 23aa58313..e81aa4c89 100644 --- a/td/telegram/MessageEntity.hpp +++ b/td/telegram/MessageEntity.hpp @@ -24,6 +24,9 @@ void MessageEntity::store(StorerT &storer) const { if (type == Type::MentionName) { store(user_id, storer); } + if (type == Type::MediaTimestamp) { + store(media_timestamp, storer); + } } template @@ -38,6 +41,9 @@ void MessageEntity::parse(ParserT &parser) { if (type == Type::MentionName) { parse(user_id, parser); } + if (type == Type::MediaTimestamp) { + parse(media_timestamp, parser); + } } template