Store media_timestamp in a separate field.

This commit is contained in:
levlam 2021-08-06 09:14:52 +03:00
parent eee773901c
commit 2098f10441
3 changed files with 27 additions and 19 deletions

View File

@ -133,7 +133,7 @@ tl_object_ptr<td_api::TextEntityType> MessageEntity::get_text_entity_type_object
case MessageEntity::Type::BankCardNumber: case MessageEntity::Type::BankCardNumber:
return make_tl_object<td_api::textEntityTypeBankCardNumber>(); return make_tl_object<td_api::textEntityTypeBankCardNumber>();
case MessageEntity::Type::MediaTimestamp: case MessageEntity::Type::MediaTimestamp:
return make_tl_object<td_api::textEntityTypeMediaTimestamp>(to_integer<int32>(argument)); return make_tl_object<td_api::textEntityTypeMediaTimestamp>(media_timestamp);
default: default:
UNREACHABLE(); UNREACHABLE();
return nullptr; return nullptr;
@ -153,8 +153,7 @@ vector<tl_object_ptr<td_api::textEntity>> get_text_entities_object(const vector<
if (skip_bot_commands && entity.type == MessageEntity::Type::BotCommand) { if (skip_bot_commands && entity.type == MessageEntity::Type::BotCommand) {
continue; continue;
} }
if (entity.type == MessageEntity::Type::MediaTimestamp && if (entity.type == MessageEntity::Type::MediaTimestamp && max_media_timestamp < entity.media_timestamp) {
(max_media_timestamp < 0 || max_media_timestamp < to_integer<int32>(entity.argument))) {
continue; continue;
} }
auto entity_object = entity.get_text_entity_object(); auto entity_object = entity.get_text_entity_object();
@ -1632,7 +1631,7 @@ vector<MessageEntity> find_entities(Slice text, bool skip_bot_commands, bool ski
for (auto &entity : media_timestamps) { for (auto &entity : media_timestamps) {
auto offset = narrow_cast<int32>(entity.first.begin() - text.begin()); auto offset = narrow_cast<int32>(entity.first.begin() - text.begin());
auto length = narrow_cast<int32>(entity.first.size()); auto length = narrow_cast<int32>(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<MessageEntity> find_media_timestamp_entities(Slice text) {
for (auto &entity : media_timestamps) { for (auto &entity : media_timestamps) {
auto offset = narrow_cast<int32>(entity.first.begin() - text.begin()); auto offset = narrow_cast<int32>(entity.first.begin() - text.begin());
auto length = narrow_cast<int32>(entity.first.size()); auto length = narrow_cast<int32>(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); fix_entity_offsets(text, entities);
@ -3253,11 +3252,11 @@ Result<vector<MessageEntity>> get_message_entities(const ContactsManager *contac
} }
case td_api::textEntityTypeMediaTimestamp::ID: { case td_api::textEntityTypeMediaTimestamp::ID: {
auto entity_media_timestamp = static_cast<td_api::textEntityTypeMediaTimestamp *>(entity->type_.get()); auto entity_media_timestamp = static_cast<td_api::textEntityTypeMediaTimestamp *>(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"); return Status::Error(400, "Invalid media timestamp specified");
} }
entities.emplace_back(MessageEntity::Type::MediaTimestamp, entity->offset_, entity->length_, entities.emplace_back(MessageEntity::Type::MediaTimestamp, entity->offset_, entity->length_,
to_string(entity_media_timestamp->media_timestamp_)); entity_media_timestamp->media_timestamp_);
break; break;
} }
default: default:
@ -4096,18 +4095,16 @@ 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) { if (text == nullptr) {
return false; return false;
} }
for (auto &entity : text->entities) { for (auto &entity : text->entities) {
if (entity.type == MessageEntity::Type::MediaTimestamp) { if (entity.type == MessageEntity::Type::MediaTimestamp && min_media_timestamp <= entity.media_timestamp &&
int32 timestamp = to_integer<int32>(entity.argument); entity.media_timestamp <= max_media_timestamp) {
if (min_timestamp <= timestamp && timestamp <= max_timestamp) {
return true; return true;
} }
} }
}
return false; return false;
} }

View File

@ -51,19 +51,24 @@ class MessageEntity {
MediaTimestamp, MediaTimestamp,
Size Size
}; };
Type type; Type type = Type::Size;
int32 offset; int32 offset = -1;
int32 length; int32 length = -1;
int32 media_timestamp = -1;
string argument; string argument;
UserId user_id; UserId user_id;
MessageEntity() = default; MessageEntity() = default;
MessageEntity(Type type, int32 offset, int32 length, string argument = "") 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) 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<td_api::textEntity> get_text_entity_object() const; tl_object_ptr<td_api::textEntity> get_text_entity_object() const;
@ -196,7 +201,7 @@ Result<FormattedText> process_input_caption(const ContactsManager *contacts_mana
void add_formatted_text_dependencies(Dependencies &dependencies, const FormattedText *text); 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); bool has_bot_commands(const FormattedText *text);

View File

@ -24,6 +24,9 @@ void MessageEntity::store(StorerT &storer) const {
if (type == Type::MentionName) { if (type == Type::MentionName) {
store(user_id, storer); store(user_id, storer);
} }
if (type == Type::MediaTimestamp) {
store(media_timestamp, storer);
}
} }
template <class ParserT> template <class ParserT>
@ -38,6 +41,9 @@ void MessageEntity::parse(ParserT &parser) {
if (type == Type::MentionName) { if (type == Type::MentionName) {
parse(user_id, parser); parse(user_id, parser);
} }
if (type == Type::MediaTimestamp) {
parse(media_timestamp, parser);
}
} }
template <class StorerT> template <class StorerT>