From da3b8f93a0a3fbbd607277d02d0234541dc1c7c3 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 4 Aug 2021 09:28:53 +0300 Subject: [PATCH] Hide too big media timestamps. --- td/telegram/Game.cpp | 2 +- td/telegram/InputMessageText.cpp | 3 ++- td/telegram/LinkManager.cpp | 4 +-- td/telegram/MessageContent.cpp | 43 +++++++++++++++++--------------- td/telegram/MessageContent.h | 3 ++- td/telegram/MessageEntity.cpp | 13 +++++++--- td/telegram/MessageEntity.h | 5 ++-- td/telegram/MessagesManager.cpp | 21 +++++++++------- td/telegram/PollManager.cpp | 2 +- td/telegram/Td.cpp | 10 +++++--- td/telegram/TermsOfService.h | 2 +- td/telegram/WebPagesManager.cpp | 11 +++++--- 12 files changed, 70 insertions(+), 49 deletions(-) diff --git a/td/telegram/Game.cpp b/td/telegram/Game.cpp index 6704dbc10..5a954f5fa 100644 --- a/td/telegram/Game.cpp +++ b/td/telegram/Game.cpp @@ -93,7 +93,7 @@ const FormattedText &Game::get_text() const { tl_object_ptr Game::get_game_object(Td *td, bool skip_bot_commands) const { return make_tl_object( - id_, short_name_, title_, get_formatted_text_object(text_, skip_bot_commands), description_, + id_, short_name_, title_, get_formatted_text_object(text_, skip_bot_commands, -1), description_, get_photo_object(td->file_manager_.get(), photo_), td->animations_manager_->get_animation_object(animation_file_id_, "get_game_object")); } diff --git a/td/telegram/InputMessageText.cpp b/td/telegram/InputMessageText.cpp index d8262a66b..8d596fecd 100644 --- a/td/telegram/InputMessageText.cpp +++ b/td/telegram/InputMessageText.cpp @@ -54,8 +54,9 @@ Result process_input_message_text(const ContactsManager *conta return std::move(result); } +// used only for draft td_api::object_ptr get_input_message_text_object(const InputMessageText &input_message_text) { - return td_api::make_object(get_formatted_text_object(input_message_text.text, false), + return td_api::make_object(get_formatted_text_object(input_message_text.text, false, -1), input_message_text.disable_web_page_preview, input_message_text.clear_draft); } diff --git a/td/telegram/LinkManager.cpp b/td/telegram/LinkManager.cpp index 8b40734ff..26dd976d5 100644 --- a/td/telegram/LinkManager.cpp +++ b/td/telegram/LinkManager.cpp @@ -192,7 +192,7 @@ class LinkManager::InternalLinkMessageDraft final : public InternalLink { bool contains_link_ = false; td_api::object_ptr get_internal_link_type_object() const final { - return td_api::make_object(get_formatted_text_object(text_, true), + return td_api::make_object(get_formatted_text_object(text_, true, -1), contains_link_); } @@ -367,7 +367,7 @@ class GetDeepLinkInfoQuery final : public Td::ResultHandler { } FormattedText text{std::move(info->message_), std::move(entities)}; return promise_.set_value( - td_api::make_object(get_formatted_text_object(text, true), need_update)); + td_api::make_object(get_formatted_text_object(text, true, -1), need_update)); } default: UNREACHABLE(); diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index bc3159ba2..48b0e0ffb 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -2914,9 +2914,9 @@ void merge_message_contents(Td *td, const MessageContent *old_content, MessageCo if (old_->text.text != new_->text.text) { if (need_message_changed_warning && need_message_text_changed_warning(old_, new_)) { LOG(ERROR) << "Message text has changed from " - << to_string(get_message_content_object(old_content, td, dialog_id, -1, false, false)) + << to_string(get_message_content_object(old_content, td, dialog_id, -1, false, false, -1)) << ". New content is " - << to_string(get_message_content_object(new_content, td, dialog_id, -1, false, false)); + << to_string(get_message_content_object(new_content, td, dialog_id, -1, false, false, -1)); } need_update = true; } @@ -2926,9 +2926,9 @@ void merge_message_contents(Td *td, const MessageContent *old_content, MessageCo old_->text.entities.size() <= MAX_CUSTOM_ENTITIES_COUNT && need_message_entities_changed_warning(old_->text.entities, new_->text.entities)) { LOG(WARNING) << "Entities has changed from " - << to_string(get_message_content_object(old_content, td, dialog_id, -1, false, false)) + << to_string(get_message_content_object(old_content, td, dialog_id, -1, false, false, -1)) << ". New content is " - << to_string(get_message_content_object(new_content, td, dialog_id, -1, false, false)); + << to_string(get_message_content_object(new_content, td, dialog_id, -1, false, false, -1)); } need_update = true; } @@ -4647,19 +4647,21 @@ unique_ptr get_action_message_content(Td *td, tl_object_ptr get_message_content_object(const MessageContent *content, Td *td, DialogId dialog_id, int32 message_date, - bool is_content_secret, bool skip_bot_commands) { + bool is_content_secret, bool skip_bot_commands, + int32 max_media_timestamp) { CHECK(content != nullptr); switch (content->get_type()) { case MessageContentType::Animation: { const MessageAnimation *m = static_cast(content); return make_tl_object( td->animations_manager_->get_animation_object(m->file_id, "get_message_content_object"), - get_formatted_text_object(m->caption, skip_bot_commands), is_content_secret); + get_formatted_text_object(m->caption, skip_bot_commands, max_media_timestamp), is_content_secret); } case MessageContentType::Audio: { const MessageAudio *m = static_cast(content); - return make_tl_object(td->audios_manager_->get_audio_object(m->file_id), - get_formatted_text_object(m->caption, skip_bot_commands)); + return make_tl_object( + td->audios_manager_->get_audio_object(m->file_id), + get_formatted_text_object(m->caption, skip_bot_commands, max_media_timestamp)); } case MessageContentType::Contact: { const MessageContact *m = static_cast(content); @@ -4669,7 +4671,7 @@ tl_object_ptr get_message_content_object(const MessageCo const MessageDocument *m = static_cast(content); return make_tl_object( td->documents_manager_->get_document_object(m->file_id, PhotoFormat::Jpeg), - get_formatted_text_object(m->caption, skip_bot_commands)); + get_formatted_text_object(m->caption, skip_bot_commands, max_media_timestamp)); } case MessageContentType::Game: { const MessageGame *m = static_cast(content); @@ -4694,9 +4696,9 @@ tl_object_ptr get_message_content_object(const MessageCo } case MessageContentType::Photo: { const MessagePhoto *m = static_cast(content); - return make_tl_object(get_photo_object(td->file_manager_.get(), m->photo), - get_formatted_text_object(m->caption, skip_bot_commands), - is_content_secret); + return make_tl_object( + get_photo_object(td->file_manager_.get(), m->photo), + get_formatted_text_object(m->caption, skip_bot_commands, max_media_timestamp), is_content_secret); } case MessageContentType::Sticker: { const MessageSticker *m = static_cast(content); @@ -4704,8 +4706,9 @@ tl_object_ptr get_message_content_object(const MessageCo } case MessageContentType::Text: { const MessageText *m = static_cast(content); - return make_tl_object(get_formatted_text_object(m->text, skip_bot_commands), - td->web_pages_manager_->get_web_page_object(m->web_page_id)); + return make_tl_object( + get_formatted_text_object(m->text, skip_bot_commands, max_media_timestamp), + td->web_pages_manager_->get_web_page_object(m->web_page_id)); } case MessageContentType::Unsupported: return make_tl_object(); @@ -4715,9 +4718,9 @@ tl_object_ptr get_message_content_object(const MessageCo } case MessageContentType::Video: { const MessageVideo *m = static_cast(content); - return make_tl_object(td->videos_manager_->get_video_object(m->file_id), - get_formatted_text_object(m->caption, skip_bot_commands), - is_content_secret); + return make_tl_object( + td->videos_manager_->get_video_object(m->file_id), + get_formatted_text_object(m->caption, skip_bot_commands, max_media_timestamp), is_content_secret); } case MessageContentType::VideoNote: { const MessageVideoNote *m = static_cast(content); @@ -4726,9 +4729,9 @@ tl_object_ptr get_message_content_object(const MessageCo } case MessageContentType::VoiceNote: { const MessageVoiceNote *m = static_cast(content); - return make_tl_object(td->voice_notes_manager_->get_voice_note_object(m->file_id), - get_formatted_text_object(m->caption, skip_bot_commands), - m->is_listened); + return make_tl_object( + td->voice_notes_manager_->get_voice_note_object(m->file_id), + get_formatted_text_object(m->caption, skip_bot_commands, max_media_timestamp), m->is_listened); } case MessageContentType::ChatCreate: { const MessageChatCreate *m = static_cast(content); diff --git a/td/telegram/MessageContent.h b/td/telegram/MessageContent.h index d127710d6..f41ac3346 100644 --- a/td/telegram/MessageContent.h +++ b/td/telegram/MessageContent.h @@ -198,7 +198,8 @@ unique_ptr get_action_message_content(Td *td, tl_object_ptr get_message_content_object(const MessageContent *content, Td *td, DialogId dialog_id, int32 message_date, - bool is_content_secret, bool skip_bot_commands); + bool is_content_secret, bool skip_bot_commands, + int32 max_media_timestamp); const FormattedText *get_message_content_text(const MessageContent *content); diff --git a/td/telegram/MessageEntity.cpp b/td/telegram/MessageEntity.cpp index f86e6f525..3c0b2da28 100644 --- a/td/telegram/MessageEntity.cpp +++ b/td/telegram/MessageEntity.cpp @@ -145,7 +145,7 @@ tl_object_ptr MessageEntity::get_text_entity_object() const } vector> get_text_entities_object(const vector &entities, - bool skip_bot_commands) { + bool skip_bot_commands, int32 max_media_timestamp) { vector> result; result.reserve(entities.size()); @@ -153,6 +153,10 @@ 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))) { + continue; + } auto entity_object = entity.get_text_entity_object(); if (entity_object->type_ != nullptr) { result.push_back(std::move(entity_object)); @@ -166,9 +170,10 @@ StringBuilder &operator<<(StringBuilder &string_builder, const FormattedText &te return string_builder << '"' << text.text << "\" with entities " << text.entities; } -td_api::object_ptr get_formatted_text_object(const FormattedText &text, bool skip_bot_commands) { - return td_api::make_object(text.text, - get_text_entities_object(text.entities, skip_bot_commands)); +td_api::object_ptr get_formatted_text_object(const FormattedText &text, bool skip_bot_commands, + int32 max_media_timestamp) { + return td_api::make_object( + text.text, get_text_entities_object(text.entities, skip_bot_commands, max_media_timestamp)); } static bool is_word_character(uint32 code) { diff --git a/td/telegram/MessageEntity.h b/td/telegram/MessageEntity.h index f9e682fb1..470f560f3 100644 --- a/td/telegram/MessageEntity.h +++ b/td/telegram/MessageEntity.h @@ -133,9 +133,10 @@ Result> get_message_entities(const ContactsManager *contac bool allow_all = false); vector> get_text_entities_object(const vector &entities, - bool skip_bot_commands); + bool skip_bot_commands, int32 max_media_timestamp); -td_api::object_ptr get_formatted_text_object(const FormattedText &text, bool skip_bot_commands); +td_api::object_ptr get_formatted_text_object(const FormattedText &text, bool skip_bot_commands, + int32 max_media_timestamp); vector find_entities(Slice text, bool skip_bot_commands, bool skip_media_timestamps); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 438be4c8f..6c11041ee 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -6534,8 +6534,8 @@ void MessagesManager::on_update_service_notification(tl_object_ptrflags_ & telegram_api::updateServiceNotification::POPUP_MASK) != 0) { send_closure(G()->td(), &Td::send_update, td_api::make_object( - update->type_, - get_message_content_object(content.get(), td_, owner_dialog_id, date, is_content_secret, true))); + update->type_, get_message_content_object(content.get(), td_, owner_dialog_id, date, + is_content_secret, true, -1))); } if (has_date && is_user) { Dialog *d = get_service_notifications_dialog(); @@ -13512,7 +13512,7 @@ std::pair> MessagesManager::creat LOG(ERROR) << "Receive media group identifier " << message_info.media_album_id << " in " << message_id << " from " << dialog_id << " with content " << oneline(to_string(get_message_content_object(message->content.get(), td_, dialog_id, message->date, - is_content_secret, false))); + is_content_secret, false, -1))); } else { message->media_album_id = message_info.media_album_id; } @@ -22840,7 +22840,8 @@ tl_object_ptr MessagesManager::get_message_object(DialogId dial auto date = is_scheduled ? 0 : m->date; auto edit_date = m->hide_edit_date ? 0 : m->edit_date; auto is_pinned = for_event_log || is_scheduled ? false : m->is_pinned; - bool skip_bot_commands = for_event_log || need_skip_bot_commands(dialog_id, m); + bool skip_bot_commands = for_event_log ? true : need_skip_bot_commands(dialog_id, m); + int32 max_media_timestamp = for_event_log ? -1 : get_message_content_duration(m->content.get(), td_); string source = PSTRING() << dialog_id << ' ' << m->message_id; return make_tl_object( m->message_id.get(), get_message_sender_object_const(m->sender_user_id, m->sender_dialog_id, source.c_str()), @@ -22851,7 +22852,7 @@ tl_object_ptr MessagesManager::get_message_object(DialogId dial reply_in_dialog_id.get(), reply_to_message_id, top_thread_message_id, ttl, ttl_expires_in, via_bot_user_id, m->author_signature, media_album_id, get_restriction_reason_description(m->restriction_reasons), get_message_content_object(m->content.get(), td_, dialog_id, live_location_date, m->is_content_secret, - skip_bot_commands), + skip_bot_commands, max_media_timestamp), get_reply_markup_object(m->reply_markup)); } @@ -24042,7 +24043,7 @@ void MessagesManager::do_send_message_group(int64 media_album_id) { << file_view.has_active_download_remote_location() << " " << file_view.is_encrypted() << " " << is_web << " " << file_view.has_url() << " " << to_string(get_message_content_object(m->content.get(), td_, dialog_id, m->date, - m->is_content_secret, false)); + m->is_content_secret, false, -1)); } auto entities = get_input_message_entities(td_->contacts_manager_.get(), caption, "do_send_message_group"); int32 input_single_media_flags = 0; @@ -28144,8 +28145,10 @@ void MessagesManager::send_update_message_content(DialogId dialog_id, const Mess void MessagesManager::send_update_message_content_impl(DialogId dialog_id, const Message *m, const char *source) const { CHECK(m != nullptr); LOG(INFO) << "Send updateMessageContent for " << m->message_id << " in " << dialog_id << " from " << source; - auto content_object = get_message_content_object(m->content.get(), td_, dialog_id, m->is_failed_to_send ? 0 : m->date, - m->is_content_secret, need_skip_bot_commands(dialog_id, m)); + int32 max_media_timestamp = get_message_content_duration(m->content.get(), td_); + auto content_object = + get_message_content_object(m->content.get(), td_, dialog_id, m->is_failed_to_send ? 0 : m->date, + m->is_content_secret, need_skip_bot_commands(dialog_id, m), max_media_timestamp); send_closure(G()->td(), &Td::send_update, td_api::make_object(dialog_id.get(), m->message_id.get(), std::move(content_object))); @@ -30486,7 +30489,7 @@ void MessagesManager::on_send_dialog_action_timeout(DialogId dialog_id) { if (!file_id.is_valid()) { LOG(ERROR) << "Have no file in " << to_string(get_message_content_object(m->content.get(), td_, dialog_id, m->date, m->is_content_secret, - false)); + false, -1)); return; } auto file_view = td_->file_manager_->get_file_view(file_id); diff --git a/td/telegram/PollManager.cpp b/td/telegram/PollManager.cpp index 266a6fe4e..c7e62846c 100644 --- a/td/telegram/PollManager.cpp +++ b/td/telegram/PollManager.cpp @@ -558,7 +558,7 @@ td_api::object_ptr PollManager::get_poll_object(PollId poll_id, co auto correct_option_id = is_local_poll_id(poll_id) ? -1 : poll->correct_option_id; poll_type = td_api::make_object( correct_option_id, - get_formatted_text_object(is_local_poll_id(poll_id) ? FormattedText() : poll->explanation, true)); + get_formatted_text_object(is_local_poll_id(poll_id) ? FormattedText() : poll->explanation, true, -1)); } else { poll_type = td_api::make_object(poll->allow_multiple_answers); } diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 78ff4e1e6..961ad8baf 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -8320,7 +8320,8 @@ td_api::object_ptr Td::do_static_request(const td_api::getTextEn return make_error(400, "Text must be encoded in UTF-8"); } auto text_entities = find_entities(request.text_, false, false); - return make_tl_object(get_text_entities_object(text_entities, false)); + return make_tl_object( + get_text_entities_object(text_entities, false, std::numeric_limits::max())); } td_api::object_ptr Td::do_static_request(td_api::parseTextEntities &request) { @@ -8355,7 +8356,7 @@ td_api::object_ptr Td::do_static_request(td_api::parseTextEntiti } return make_tl_object(std::move(request.text_), - get_text_entities_object(r_entities.ok(), false)); + get_text_entities_object(r_entities.ok(), false, -1)); } td_api::object_ptr Td::do_static_request(td_api::parseMarkdown &request) { @@ -8375,7 +8376,7 @@ td_api::object_ptr Td::do_static_request(td_api::parseMarkdown & 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, true).ensure(); - return get_formatted_text_object(parsed_text, true); + return get_formatted_text_object(parsed_text, false, std::numeric_limits::max()); } td_api::object_ptr Td::do_static_request(td_api::getMarkdownText &request) { @@ -8393,7 +8394,8 @@ td_api::object_ptr Td::do_static_request(td_api::getMarkdownText return make_error(400, status.error().message()); } - return get_formatted_text_object(get_markdown_v3({std::move(request.text_->text_), std::move(entities)}), true); + return get_formatted_text_object(get_markdown_v3({std::move(request.text_->text_), std::move(entities)}), false, + std::numeric_limits::max()); } td_api::object_ptr Td::do_static_request(const td_api::getFileMimeType &request) { diff --git a/td/telegram/TermsOfService.h b/td/telegram/TermsOfService.h index 4c3cc4fcf..344c57e0a 100644 --- a/td/telegram/TermsOfService.h +++ b/td/telegram/TermsOfService.h @@ -42,7 +42,7 @@ class TermsOfService { return nullptr; } - return td_api::make_object(get_formatted_text_object(text_, true), min_user_age_, + return td_api::make_object(get_formatted_text_object(text_, true, -1), min_user_age_, show_popup_); } diff --git a/td/telegram/WebPagesManager.cpp b/td/telegram/WebPagesManager.cpp index 4bd43d72b..0ff25cd3b 100644 --- a/td/telegram/WebPagesManager.cpp +++ b/td/telegram/WebPagesManager.cpp @@ -1254,11 +1254,16 @@ tl_object_ptr WebPagesManager::get_web_page_object(WebPageId we } } + int32 max_media_timestamp = -1; + if (web_page->document.type == Document::Type::Audio || web_page->document.type == Document::Type::Video || + web_page->document.type == Document::Type::VideoNote || web_page->document.type == Document::Type::VoiceNote) { + max_media_timestamp = web_page->duration == 0 ? std::numeric_limits::max() : web_page->duration; + } return make_tl_object( web_page->url, web_page->display_url, web_page->type, web_page->site_name, web_page->title, - get_formatted_text_object(description, true), get_photo_object(td_->file_manager_.get(), web_page->photo), - web_page->embed_url, web_page->embed_type, web_page->embed_dimensions.width, web_page->embed_dimensions.height, - web_page->duration, web_page->author, + get_formatted_text_object(description, true, max_media_timestamp), + get_photo_object(td_->file_manager_.get(), web_page->photo), web_page->embed_url, web_page->embed_type, + web_page->embed_dimensions.width, web_page->embed_dimensions.height, web_page->duration, web_page->author, web_page->document.type == Document::Type::Animation ? td_->animations_manager_->get_animation_object(web_page->document.file_id, "get_web_page_object") : nullptr,