From 9653cc9e3ee24e57052e62d25560703d0ef90696 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 5 Aug 2021 02:09:04 +0300 Subject: [PATCH] Add separate get_message_content_media_duration. --- td/telegram/MessageContent.cpp | 32 ++++++++++++++++++++++++++++---- td/telegram/MessageContent.h | 2 ++ td/telegram/MessagesManager.cpp | 8 ++++---- td/telegram/WebPagesManager.cpp | 23 ++++++++++++++--------- td/telegram/WebPagesManager.h | 4 +++- 5 files changed, 51 insertions(+), 18 deletions(-) diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 48b0e0ffb..08b0a7e30 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -4921,10 +4921,6 @@ int32 get_message_content_duration(const MessageContent *content, const Td *td) auto audio_file_id = static_cast(content)->file_id; return td->audios_manager_->get_audio_duration(audio_file_id); } - case MessageContentType::Text: { - auto web_page_id = static_cast(content)->web_page_id; - return td->web_pages_manager_->get_web_page_duration(web_page_id); - } case MessageContentType::Video: { auto video_file_id = static_cast(content)->file_id; return td->videos_manager_->get_video_duration(video_file_id); @@ -4942,6 +4938,34 @@ int32 get_message_content_duration(const MessageContent *content, const Td *td) } } +int32 get_message_content_media_duration(const MessageContent *content, const Td *td) { + CHECK(content != nullptr); + switch (content->get_type()) { + case MessageContentType::Audio: { + auto audio_file_id = static_cast(content)->file_id; + return td->audios_manager_->get_audio_duration(audio_file_id); + } + case MessageContentType::Text: { + auto web_page_id = static_cast(content)->web_page_id; + return td->web_pages_manager_->get_web_page_media_duration(web_page_id); + } + case MessageContentType::Video: { + auto video_file_id = static_cast(content)->file_id; + return td->videos_manager_->get_video_duration(video_file_id); + } + case MessageContentType::VideoNote: { + auto video_note_file_id = static_cast(content)->file_id; + return td->video_notes_manager_->get_video_note_duration(video_note_file_id); + } + case MessageContentType::VoiceNote: { + auto voice_file_id = static_cast(content)->file_id; + return td->voice_notes_manager_->get_voice_note_duration(voice_file_id); + } + default: + return -1; + } +} + FileId get_message_content_upload_file_id(const MessageContent *content) { switch (content->get_type()) { case MessageContentType::Animation: diff --git a/td/telegram/MessageContent.h b/td/telegram/MessageContent.h index f41ac3346..6f66670a5 100644 --- a/td/telegram/MessageContent.h +++ b/td/telegram/MessageContent.h @@ -207,6 +207,8 @@ const FormattedText *get_message_content_caption(const MessageContent *content); int32 get_message_content_duration(const MessageContent *content, const Td *td); +int32 get_message_content_media_duration(const MessageContent *content, const Td *td); + FileId get_message_content_upload_file_id(const MessageContent *content); FileId get_message_content_any_file_id(const MessageContent *content); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 6c11041ee..7ce98a167 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -17369,7 +17369,7 @@ Result> MessagesManager::get_message_link(FullMessageId } if (media_timestamp != 0) { for_group = false; - auto duration = get_message_content_duration(m->content.get(), td_); + auto duration = get_message_content_media_duration(m->content.get(), td_); if (duration != 0 && media_timestamp > duration) { media_timestamp = 0; } @@ -17630,7 +17630,7 @@ td_api::object_ptr MessagesManager::get_message_link_in for_album = !info.is_single && m->media_album_id != 0; for_comment = (info.comment_dialog_id.is_valid() || info.for_comment) && m->top_thread_message_id.is_valid(); if (can_message_content_have_media_timestamp(m->content.get())) { - auto duration = get_message_content_duration(m->content.get(), td_); + auto duration = get_message_content_media_duration(m->content.get(), td_); if (duration == 0 || info.media_timestamp <= duration) { media_timestamp = info.media_timestamp; } @@ -22841,7 +22841,7 @@ tl_object_ptr MessagesManager::get_message_object(DialogId dial 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 ? true : need_skip_bot_commands(dialog_id, m); - int32 max_media_timestamp = for_event_log ? -1 : get_message_content_duration(m->content.get(), td_); + int32 max_media_timestamp = for_event_log ? -1 : get_message_content_media_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()), @@ -28145,7 +28145,7 @@ 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; - int32 max_media_timestamp = get_message_content_duration(m->content.get(), td_); + int32 max_media_timestamp = get_message_content_media_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); diff --git a/td/telegram/WebPagesManager.cpp b/td/telegram/WebPagesManager.cpp index 0ff25cd3b..097634f02 100644 --- a/td/telegram/WebPagesManager.cpp +++ b/td/telegram/WebPagesManager.cpp @@ -1254,14 +1254,10 @@ 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; - } + auto duration = get_web_page_media_duration(web_page); 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, max_media_timestamp), + get_formatted_text_object(description, true, duration == 0 ? std::numeric_limits::max() : duration), 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 @@ -1695,12 +1691,21 @@ string WebPagesManager::get_web_page_search_text(WebPageId web_page_id) const { return PSTRING() << web_page->title + " " + web_page->description; } -int32 WebPagesManager::get_web_page_duration(WebPageId web_page_id) const { +int32 WebPagesManager::get_web_page_media_duration(WebPageId web_page_id) const { const WebPage *web_page = get_web_page(web_page_id); if (web_page == nullptr) { - return 0; + return -1; } - return web_page->duration; + return get_web_page_media_duration(web_page); +} + +int32 WebPagesManager::get_web_page_media_duration(const WebPage *web_page) { + 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 || web_page->embed_type == "iframe") { + return web_page->duration; + } + + return -1; } vector WebPagesManager::get_web_page_file_ids(const WebPage *web_page) const { diff --git a/td/telegram/WebPagesManager.h b/td/telegram/WebPagesManager.h index 4d5e582ef..c7beff080 100644 --- a/td/telegram/WebPagesManager.h +++ b/td/telegram/WebPagesManager.h @@ -87,7 +87,7 @@ class WebPagesManager final : public Actor { string get_web_page_search_text(WebPageId web_page_id) const; - int32 get_web_page_duration(WebPageId web_page_id) const; + int32 get_web_page_media_duration(WebPageId web_page_id) const; private: static constexpr int32 WEBPAGE_FLAG_HAS_TYPE = 1 << 0; @@ -170,6 +170,8 @@ class WebPagesManager final : public Actor { void tear_down() final; + static int32 get_web_page_media_duration(const WebPage *web_page); + FileSourceId get_web_page_file_source_id(WebPage *web_page); vector get_web_page_file_ids(const WebPage *web_page) const;