From f9f309d334712794320c5d40957442a0b8202cdb Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 11 May 2022 01:53:18 +0300 Subject: [PATCH] Simplify SecretInputMedia creation. --- CMakeLists.txt | 1 + td/telegram/AnimationsManager.cpp | 16 ++++++++-------- td/telegram/AudiosManager.cpp | 16 ++++++++-------- td/telegram/DocumentsManager.cpp | 16 ++++++++-------- td/telegram/SecretInputMedia.cpp | 25 +++++++++++++++++++++++++ td/telegram/SecretInputMedia.h | 10 ++++++++++ td/telegram/StickersManager.cpp | 14 +++++++------- td/telegram/VideoNotesManager.cpp | 17 +++++++++-------- td/telegram/VideosManager.cpp | 20 ++++++++++++-------- td/telegram/VoiceNotesManager.cpp | 11 ++++------- 10 files changed, 92 insertions(+), 54 deletions(-) create mode 100644 td/telegram/SecretInputMedia.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a569fcde..ae58a43fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -412,6 +412,7 @@ set(TDLIB_SOURCE td/telegram/SecretChatActor.cpp td/telegram/SecretChatDb.cpp td/telegram/SecretChatsManager.cpp + td/telegram/SecretInputMedia.cpp td/telegram/SecureManager.cpp td/telegram/SecureStorage.cpp td/telegram/SecureValue.cpp diff --git a/td/telegram/AnimationsManager.cpp b/td/telegram/AnimationsManager.cpp index 546232c1c..152b1af38 100644 --- a/td/telegram/AnimationsManager.cpp +++ b/td/telegram/AnimationsManager.cpp @@ -388,8 +388,7 @@ SecretInputMedia AnimationsManager::get_secret_input_media(FileId animation_file auto *animation = get_animation(animation_file_id); CHECK(animation != nullptr); auto file_view = td_->file_manager_->get_file_view(animation_file_id); - auto &encryption_key = file_view.encryption_key(); - if (!file_view.is_encrypted_secret() || encryption_key.empty()) { + if (!file_view.is_encrypted_secret() || file_view.encryption_key().empty()) { return SecretInputMedia{}; } if (file_view.has_remote_location()) { @@ -415,12 +414,13 @@ SecretInputMedia AnimationsManager::get_secret_input_media(FileId animation_file } attributes.push_back(make_tl_object()); - return SecretInputMedia{ - std::move(input_file), - make_tl_object( - std::move(thumbnail), animation->thumbnail.dimensions.width, animation->thumbnail.dimensions.height, - animation->mime_type, narrow_cast(file_view.size()), BufferSlice(encryption_key.key_slice()), - BufferSlice(encryption_key.iv_slice()), std::move(attributes), caption)}; + return {std::move(input_file), + std::move(thumbnail), + animation->thumbnail.dimensions, + animation->mime_type, + file_view, + std::move(attributes), + caption}; } void AnimationsManager::on_update_animation_search_emojis(string animation_search_emojis) { diff --git a/td/telegram/AudiosManager.cpp b/td/telegram/AudiosManager.cpp index 7b701772c..b05afbdcd 100644 --- a/td/telegram/AudiosManager.cpp +++ b/td/telegram/AudiosManager.cpp @@ -210,8 +210,7 @@ SecretInputMedia AudiosManager::get_secret_input_media(FileId audio_file_id, auto *audio = get_audio(audio_file_id); CHECK(audio != nullptr); auto file_view = td_->file_manager_->get_file_view(audio_file_id); - auto &encryption_key = file_view.encryption_key(); - if (!file_view.is_encrypted_secret() || encryption_key.empty()) { + if (!file_view.is_encrypted_secret() || file_view.encryption_key().empty()) { return SecretInputMedia{}; } if (file_view.has_remote_location()) { @@ -231,12 +230,13 @@ SecretInputMedia AudiosManager::get_secret_input_media(FileId audio_file_id, secret_api::documentAttributeAudio::TITLE_MASK | secret_api::documentAttributeAudio::PERFORMER_MASK, false /*ignored*/, audio->duration, audio->title, audio->performer, BufferSlice())); - return SecretInputMedia{ - std::move(input_file), - make_tl_object( - std::move(thumbnail), audio->thumbnail.dimensions.width, audio->thumbnail.dimensions.height, audio->mime_type, - narrow_cast(file_view.size()), BufferSlice(encryption_key.key_slice()), - BufferSlice(encryption_key.iv_slice()), std::move(attributes), caption)}; + return {std::move(input_file), + std::move(thumbnail), + audio->thumbnail.dimensions, + audio->mime_type, + file_view, + std::move(attributes), + caption}; } tl_object_ptr AudiosManager::get_input_media( diff --git a/td/telegram/DocumentsManager.cpp b/td/telegram/DocumentsManager.cpp index 352aed808..be2e34e4d 100644 --- a/td/telegram/DocumentsManager.cpp +++ b/td/telegram/DocumentsManager.cpp @@ -593,8 +593,7 @@ SecretInputMedia DocumentsManager::get_secret_input_media(FileId document_file_i const GeneralDocument *document = get_document(document_file_id); CHECK(document != nullptr); auto file_view = td_->file_manager_->get_file_view(document_file_id); - auto &encryption_key = file_view.encryption_key(); - if (!file_view.is_encrypted_secret() || encryption_key.empty()) { + if (!file_view.is_encrypted_secret() || file_view.encryption_key().empty()) { return SecretInputMedia{}; } if (file_view.has_remote_location()) { @@ -610,12 +609,13 @@ SecretInputMedia DocumentsManager::get_secret_input_media(FileId document_file_i if (!document->file_name.empty()) { attributes.push_back(make_tl_object(document->file_name)); } - return SecretInputMedia{ - std::move(input_file), - make_tl_object( - std::move(thumbnail), document->thumbnail.dimensions.width, document->thumbnail.dimensions.height, - document->mime_type, narrow_cast(file_view.size()), BufferSlice(encryption_key.key_slice()), - BufferSlice(encryption_key.iv_slice()), std::move(attributes), caption)}; + return {std::move(input_file), + std::move(thumbnail), + document->thumbnail.dimensions, + document->mime_type, + file_view, + std::move(attributes), + caption}; } tl_object_ptr DocumentsManager::get_input_media( diff --git a/td/telegram/SecretInputMedia.cpp b/td/telegram/SecretInputMedia.cpp new file mode 100644 index 000000000..9ad5290f7 --- /dev/null +++ b/td/telegram/SecretInputMedia.cpp @@ -0,0 +1,25 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#include "td/telegram/SecretInputMedia.h" + +#include "td/telegram/files/FileManager.h" + +namespace td { + +SecretInputMedia::SecretInputMedia(tl_object_ptr input_file, BufferSlice &&thumbnail, + Dimensions thumbnail_dimensions, const string &mime_type, const FileView &file_view, + vector> &&attributes, + const string &caption) + : input_file_(std::move(input_file)) { + auto &encryption_key = file_view.encryption_key(); + decrypted_media_ = secret_api::make_object( + std::move(thumbnail), thumbnail_dimensions.width, thumbnail_dimensions.height, mime_type, + narrow_cast(file_view.size()), BufferSlice(encryption_key.key_slice()), + BufferSlice(encryption_key.iv_slice()), std::move(attributes), caption); +} + +} // namespace td diff --git a/td/telegram/SecretInputMedia.h b/td/telegram/SecretInputMedia.h index cf36feac1..35c96ee6a 100644 --- a/td/telegram/SecretInputMedia.h +++ b/td/telegram/SecretInputMedia.h @@ -6,11 +6,17 @@ // #pragma once +#include "td/telegram/PhotoSize.h" #include "td/telegram/secret_api.h" #include "td/telegram/telegram_api.h" +#include "td/utils/buffer.h" +#include "td/utils/common.h" + namespace td { +class FileView; + struct SecretInputMedia { tl_object_ptr input_file_; tl_object_ptr decrypted_media_; @@ -22,6 +28,10 @@ struct SecretInputMedia { : input_file_(std::move(input_file)), decrypted_media_(std::move(decrypted_media)) { } + SecretInputMedia(tl_object_ptr input_file, BufferSlice &&thumbnail, + Dimensions thumbnail_dimensions, const string &mime_type, const FileView &file_view, + vector> &&attributes, const string &caption); + bool empty() const { return decrypted_media_ == nullptr; } diff --git a/td/telegram/StickersManager.cpp b/td/telegram/StickersManager.cpp index 817b0bfd1..5e8bf36a0 100644 --- a/td/telegram/StickersManager.cpp +++ b/td/telegram/StickersManager.cpp @@ -2748,13 +2748,13 @@ SecretInputMedia StickersManager::get_secret_input_media(FileId sticker_file_id, } if (file_view.is_encrypted_secret()) { - auto &encryption_key = file_view.encryption_key(); - return SecretInputMedia{std::move(input_file), - make_tl_object( - std::move(thumbnail), sticker->s_thumbnail.dimensions.width, - sticker->s_thumbnail.dimensions.height, get_sticker_format_mime_type(sticker->format), - narrow_cast(file_view.size()), BufferSlice(encryption_key.key_slice()), - BufferSlice(encryption_key.iv_slice()), std::move(attributes), "")}; + return {std::move(input_file), + std::move(thumbnail), + sticker->s_thumbnail.dimensions, + get_sticker_format_mime_type(sticker->format), + file_view, + std::move(attributes), + string()}; } else { CHECK(!file_view.is_encrypted()); auto &remote_location = file_view.remote_location(); diff --git a/td/telegram/VideoNotesManager.cpp b/td/telegram/VideoNotesManager.cpp index 7eb8e2824..5567ea303 100644 --- a/td/telegram/VideoNotesManager.cpp +++ b/td/telegram/VideoNotesManager.cpp @@ -159,8 +159,7 @@ SecretInputMedia VideoNotesManager::get_secret_input_media(FileId video_note_fil const VideoNote *video_note = get_video_note(video_note_file_id); CHECK(video_note != nullptr); auto file_view = td_->file_manager_->get_file_view(video_note_file_id); - auto &encryption_key = file_view.encryption_key(); - if (!file_view.is_encrypted_secret() || encryption_key.empty()) { + if (!file_view.is_encrypted_secret() || file_view.encryption_key().empty()) { return SecretInputMedia{}; } if (file_view.has_remote_location()) { @@ -176,12 +175,14 @@ SecretInputMedia VideoNotesManager::get_secret_input_media(FileId video_note_fil attributes.push_back(make_tl_object( secret_api::documentAttributeVideo66::ROUND_MESSAGE_MASK, true, video_note->duration, video_note->dimensions.width, video_note->dimensions.height)); - return SecretInputMedia{ - std::move(input_file), - make_tl_object( - std::move(thumbnail), video_note->thumbnail.dimensions.width, video_note->thumbnail.dimensions.height, - "video/mp4", narrow_cast(file_view.size()), BufferSlice(encryption_key.key_slice()), - BufferSlice(encryption_key.iv_slice()), std::move(attributes), "")}; + + return {std::move(input_file), + std::move(thumbnail), + video_note->thumbnail.dimensions, + "video/mp4", + file_view, + std::move(attributes), + string()}; } tl_object_ptr VideoNotesManager::get_input_media( diff --git a/td/telegram/VideosManager.cpp b/td/telegram/VideosManager.cpp index ac648a404..60b70adf5 100644 --- a/td/telegram/VideosManager.cpp +++ b/td/telegram/VideosManager.cpp @@ -205,8 +205,7 @@ SecretInputMedia VideosManager::get_secret_input_media(FileId video_file_id, const Video *video = get_video(video_file_id); CHECK(video != nullptr); auto file_view = td_->file_manager_->get_file_view(video_file_id); - auto &encryption_key = file_view.encryption_key(); - if (!file_view.is_encrypted_secret() || encryption_key.empty()) { + if (!file_view.is_encrypted_secret() || file_view.encryption_key().empty()) { return SecretInputMedia{}; } if (file_view.has_remote_location()) { @@ -218,12 +217,17 @@ SecretInputMedia VideosManager::get_secret_input_media(FileId video_file_id, if (video->thumbnail.file_id.is_valid() && thumbnail.empty()) { return {}; } - return SecretInputMedia{ - std::move(input_file), - make_tl_object( - std::move(thumbnail), video->thumbnail.dimensions.width, video->thumbnail.dimensions.height, video->duration, - video->mime_type, video->dimensions.width, video->dimensions.height, narrow_cast(file_view.size()), - BufferSlice(encryption_key.key_slice()), BufferSlice(encryption_key.iv_slice()), caption)}; + vector> attributes; + attributes.emplace_back(make_tl_object(video->duration, video->dimensions.width, + video->dimensions.height)); + + return {std::move(input_file), + std::move(thumbnail), + video->thumbnail.dimensions, + video->mime_type, + file_view, + std::move(attributes), + caption}; } tl_object_ptr VideosManager::get_input_media( diff --git a/td/telegram/VoiceNotesManager.cpp b/td/telegram/VoiceNotesManager.cpp index 6b05e09d4..5021007d3 100644 --- a/td/telegram/VoiceNotesManager.cpp +++ b/td/telegram/VoiceNotesManager.cpp @@ -133,8 +133,7 @@ SecretInputMedia VoiceNotesManager::get_secret_input_media(FileId voice_file_id, auto *voice_note = get_voice_note(voice_file_id); CHECK(voice_note != nullptr); auto file_view = td_->file_manager_->get_file_view(voice_file_id); - auto &encryption_key = file_view.encryption_key(); - if (!file_view.is_encrypted_secret() || encryption_key.empty()) { + if (!file_view.is_encrypted_secret() || file_view.encryption_key().empty()) { return SecretInputMedia{}; } if (file_view.has_remote_location()) { @@ -147,11 +146,9 @@ SecretInputMedia VoiceNotesManager::get_secret_input_media(FileId voice_file_id, attributes.push_back(make_tl_object( secret_api::documentAttributeAudio::VOICE_MASK | secret_api::documentAttributeAudio::WAVEFORM_MASK, false /*ignored*/, voice_note->duration, "", "", BufferSlice(voice_note->waveform))); - return SecretInputMedia{std::move(input_file), - make_tl_object( - BufferSlice(), 0, 0, voice_note->mime_type, narrow_cast(file_view.size()), - BufferSlice(encryption_key.key_slice()), BufferSlice(encryption_key.iv_slice()), - std::move(attributes), caption)}; + + return {std::move(input_file), BufferSlice(), Dimensions(), voice_note->mime_type, file_view, + std::move(attributes), caption}; } tl_object_ptr VoiceNotesManager::get_input_media(