From 6f34c5c6a198253840f25e711d34945847aa1db1 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 27 Oct 2021 16:52:22 +0300 Subject: [PATCH] Make PhotoSizeSource constructors named. --- td/telegram/DocumentsManager.cpp | 11 ++-- td/telegram/MessageContent.cpp | 3 +- td/telegram/Photo.cpp | 39 +++++++------ td/telegram/PhotoSizeSource.cpp | 2 +- td/telegram/PhotoSizeSource.h | 91 +++++++++++++++++------------- td/telegram/PhotoSizeSource.hpp | 4 +- td/telegram/StickersManager.cpp | 14 +++-- td/telegram/files/FileLocation.h | 4 +- td/telegram/files/FileLocation.hpp | 11 ++-- 9 files changed, 99 insertions(+), 80 deletions(-) diff --git a/td/telegram/DocumentsManager.cpp b/td/telegram/DocumentsManager.cpp index 9829f1e08..c40a210a3 100644 --- a/td/telegram/DocumentsManager.cpp +++ b/td/telegram/DocumentsManager.cpp @@ -270,9 +270,9 @@ Document DocumentsManager::on_get_document(RemoteDocument remote_document, Dialo if (document_type != Document::Type::VoiceNote) { for (auto &thumb : document->thumbs_) { - auto photo_size = - get_photo_size(td_->file_manager_.get(), {FileType::Thumbnail, 0}, id, access_hash, file_reference, - DcId::create(dc_id), owner_dialog_id, std::move(thumb), thumbnail_format); + auto photo_size = get_photo_size(td_->file_manager_.get(), PhotoSizeSource::thumbnail(FileType::Thumbnail, 0), + id, access_hash, file_reference, DcId::create(dc_id), owner_dialog_id, + std::move(thumb), thumbnail_format); if (photo_size.get_offset() == 0) { if (!thumbnail.file_id.is_valid()) { thumbnail = std::move(photo_size.get<0>()); @@ -284,8 +284,9 @@ Document DocumentsManager::on_get_document(RemoteDocument remote_document, Dialo } for (auto &thumb : document->video_thumbs_) { if (thumb->type_ == "v") { - animated_thumbnail = get_animation_size(td_->file_manager_.get(), {FileType::Thumbnail, 0}, id, access_hash, - file_reference, DcId::create(dc_id), owner_dialog_id, std::move(thumb)); + animated_thumbnail = + get_animation_size(td_->file_manager_.get(), PhotoSizeSource::thumbnail(FileType::Thumbnail, 0), id, + access_hash, file_reference, DcId::create(dc_id), owner_dialog_id, std::move(thumb)); if (animated_thumbnail.file_id.is_valid()) { break; } diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index dd4712ffd..0d38a6bd1 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -3159,7 +3159,8 @@ void merge_message_contents(Td *td, const MessageContent *old_content, MessageCo old_file_view.main_remote_location().get_access_hash() != new_file_view.remote_location().get_access_hash()) { FileId file_id = td->file_manager_->register_remote( - FullRemoteFileLocation({FileType::Photo, 'i'}, new_file_view.remote_location().get_id(), + FullRemoteFileLocation(PhotoSizeSource::thumbnail(FileType::Photo, 'i'), + new_file_view.remote_location().get_id(), new_file_view.remote_location().get_access_hash(), DcId::invalid(), new_file_view.remote_location().get_file_reference().str()), FileLocationSource::FromServer, dialog_id, old_photo->photos.back().size, 0, ""); diff --git a/td/telegram/Photo.cpp b/td/telegram/Photo.cpp index 61cf18f0a..fc44bf0f7 100644 --- a/td/telegram/Photo.cpp +++ b/td/telegram/Photo.cpp @@ -162,12 +162,12 @@ ProfilePhoto get_profile_photo(FileManager *file_manager, UserId user_id, int64 result.has_animation = (profile_photo->flags_ & telegram_api::userProfilePhoto::HAS_VIDEO_MASK) != 0; result.id = profile_photo->photo_id_; result.minithumbnail = profile_photo->stripped_thumb_.as_slice().str(); - result.small_file_id = - register_photo(file_manager, {DialogId(user_id), user_access_hash, false}, result.id, 0 /*access_hash*/, - "" /*file_reference*/, DialogId(), 0 /*file_size*/, dc_id, PhotoFormat::Jpeg); - result.big_file_id = - register_photo(file_manager, {DialogId(user_id), user_access_hash, true}, result.id, 0 /*access_hash*/, - "" /*file_reference*/, DialogId(), 0 /*file_size*/, dc_id, PhotoFormat::Jpeg); + result.small_file_id = register_photo( + file_manager, PhotoSizeSource::dialog_photo(DialogId(user_id), user_access_hash, false), result.id, + 0 /*access_hash*/, "" /*file_reference*/, DialogId(), 0 /*file_size*/, dc_id, PhotoFormat::Jpeg); + result.big_file_id = register_photo( + file_manager, PhotoSizeSource::dialog_photo(DialogId(user_id), user_access_hash, true), result.id, + 0 /*access_hash*/, "" /*file_reference*/, DialogId(), 0 /*file_size*/, dc_id, PhotoFormat::Jpeg); break; } default: @@ -223,10 +223,12 @@ DialogPhoto get_dialog_photo(FileManager *file_manager, DialogId dialog_id, int6 auto dc_id = DcId::create(chat_photo->dc_id_); result.has_animation = (chat_photo->flags_ & telegram_api::chatPhoto::HAS_VIDEO_MASK) != 0; result.minithumbnail = chat_photo->stripped_thumb_.as_slice().str(); - result.small_file_id = register_photo(file_manager, {dialog_id, dialog_access_hash, false}, chat_photo->photo_id_, - 0, "", DialogId(), 0, dc_id, PhotoFormat::Jpeg); - result.big_file_id = register_photo(file_manager, {dialog_id, dialog_access_hash, true}, chat_photo->photo_id_, 0, - "", DialogId(), 0, dc_id, PhotoFormat::Jpeg); + result.small_file_id = + register_photo(file_manager, PhotoSizeSource::dialog_photo(dialog_id, dialog_access_hash, false), + chat_photo->photo_id_, 0, "", DialogId(), 0, dc_id, PhotoFormat::Jpeg); + result.big_file_id = + register_photo(file_manager, PhotoSizeSource::dialog_photo(dialog_id, dialog_access_hash, true), + chat_photo->photo_id_, 0, "", DialogId(), 0, dc_id, PhotoFormat::Jpeg); break; } @@ -293,7 +295,7 @@ ProfilePhoto as_profile_photo(FileManager *file_manager, UserId user_id, int64 u auto remote = file_view.remote_location(); CHECK(remote.is_photo()); CHECK(!remote.is_web()); - remote.set_source({DialogId(user_id), user_access_hash, is_big}); + remote.set_source(PhotoSizeSource::dialog_photo(DialogId(user_id), user_access_hash, is_big)); return file_manager->register_remote(std::move(remote), FileLocationSource::FromServer, DialogId(), file_view.size(), file_view.expected_size(), file_view.remote_name()); }; @@ -335,7 +337,8 @@ PhotoSize get_secret_thumbnail_photo_size(FileManager *file_manager, BufferSlice auto photo_id = -(Random::secure_int64() & std::numeric_limits::max()); res.file_id = file_manager->register_remote( - FullRemoteFileLocation(PhotoSizeSource(FileType::EncryptedThumbnail, 't'), photo_id, 0, dc_id, string()), + FullRemoteFileLocation(PhotoSizeSource::thumbnail(FileType::EncryptedThumbnail, 't'), photo_id, 0, dc_id, + string()), FileLocationSource::FromServer, owner_dialog_id, res.size, 0, PSTRING() << static_cast(photo_id) << ".jpg"); file_manager->set_content(res.file_id, std::move(bytes)); @@ -708,9 +711,9 @@ Photo get_photo(FileManager *file_manager, tl_object_ptr && DcId dc_id = DcId::create(photo->dc_id_); for (auto &size_ptr : photo->sizes_) { - auto photo_size = get_photo_size(file_manager, {FileType::Photo, 0}, photo->id_, photo->access_hash_, - photo->file_reference_.as_slice().str(), dc_id, owner_dialog_id, - std::move(size_ptr), PhotoFormat::Jpeg); + auto photo_size = get_photo_size(file_manager, PhotoSizeSource::thumbnail(FileType::Photo, 0), photo->id_, + photo->access_hash_, photo->file_reference_.as_slice().str(), dc_id, + owner_dialog_id, std::move(size_ptr), PhotoFormat::Jpeg); if (photo_size.get_offset() == 0) { PhotoSize &size = photo_size.get<0>(); if (size.type == 0 || size.type == 't' || size.type == 'i' || size.type == 'u' || size.type == 'v') { @@ -724,9 +727,9 @@ Photo get_photo(FileManager *file_manager, tl_object_ptr && } for (auto &size_ptr : photo->video_sizes_) { - auto animation = - get_animation_size(file_manager, {FileType::Photo, 0}, photo->id_, photo->access_hash_, - photo->file_reference_.as_slice().str(), dc_id, owner_dialog_id, std::move(size_ptr)); + auto animation = get_animation_size(file_manager, PhotoSizeSource::thumbnail(FileType::Photo, 0), photo->id_, + photo->access_hash_, photo->file_reference_.as_slice().str(), dc_id, + owner_dialog_id, std::move(size_ptr)); if (animation.type != 0 && animation.dimensions.width == animation.dimensions.height) { res.animations.push_back(std::move(animation)); } diff --git a/td/telegram/PhotoSizeSource.cpp b/td/telegram/PhotoSizeSource.cpp index a3923a2fe..e7b57908d 100644 --- a/td/telegram/PhotoSizeSource.cpp +++ b/td/telegram/PhotoSizeSource.cpp @@ -218,7 +218,7 @@ static bool operator==(const PhotoSizeSource::StickerSetThumbnailVersion &lhs, } bool operator==(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs) { - return lhs.variant == rhs.variant; + return lhs.variant_ == rhs.variant_; } bool operator!=(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs) { diff --git a/td/telegram/PhotoSizeSource.h b/td/telegram/PhotoSizeSource.h index c67f5c69e..731f5ae85 100644 --- a/td/telegram/PhotoSizeSource.h +++ b/td/telegram/PhotoSizeSource.h @@ -8,15 +8,12 @@ #include "td/telegram/DialogId.h" #include "td/telegram/files/FileType.h" - #include "td/telegram/telegram_api.h" #include "td/utils/common.h" #include "td/utils/StringBuilder.h" #include "td/utils/Variant.h" -#include - namespace td { struct PhotoSizeSource { @@ -141,37 +138,47 @@ struct PhotoSizeSource { }; PhotoSizeSource() = default; - PhotoSizeSource(FileType file_type, int32 thumbnail_type) : variant(Thumbnail(file_type, thumbnail_type)) { + + static PhotoSizeSource thumbnail(FileType file_type, int32 thumbnail_type) { + return PhotoSizeSource(Thumbnail(file_type, thumbnail_type)); } - PhotoSizeSource(DialogId dialog_id, int64 dialog_access_hash, bool is_big) { + + static PhotoSizeSource dialog_photo(DialogId dialog_id, int64 dialog_access_hash, bool is_big) { if (is_big) { - variant = DialogPhotoBig(dialog_id, dialog_access_hash); + return PhotoSizeSource(DialogPhotoBig(dialog_id, dialog_access_hash)); } else { - variant = DialogPhotoSmall(dialog_id, dialog_access_hash); + return PhotoSizeSource(DialogPhotoSmall(dialog_id, dialog_access_hash)); } } - PhotoSizeSource(int64 sticker_set_id, int64 sticker_set_access_hash) - : variant(StickerSetThumbnail(sticker_set_id, sticker_set_access_hash)) { + + static PhotoSizeSource sticker_set_thumbnail(int64 sticker_set_id, int64 sticker_set_access_hash) { + return PhotoSizeSource(StickerSetThumbnail(sticker_set_id, sticker_set_access_hash)); } - PhotoSizeSource(std::nullptr_t, int64 volume_id, int32 local_id, int64 secret) - : variant(FullLegacy(volume_id, local_id, secret)) { + + static PhotoSizeSource full_legacy(int64 volume_id, int32 local_id, int64 secret) { + return PhotoSizeSource(FullLegacy(volume_id, local_id, secret)); } - PhotoSizeSource(DialogId dialog_id, int64 dialog_access_hash, bool is_big, int64 volume_id, int32 local_id) { + + static PhotoSizeSource dialog_photo_legacy(DialogId dialog_id, int64 dialog_access_hash, bool is_big, int64 volume_id, + int32 local_id) { if (is_big) { - variant = DialogPhotoBigLegacy(dialog_id, dialog_access_hash, volume_id, local_id); + return PhotoSizeSource(DialogPhotoBigLegacy(dialog_id, dialog_access_hash, volume_id, local_id)); } else { - variant = DialogPhotoSmallLegacy(dialog_id, dialog_access_hash, volume_id, local_id); + return PhotoSizeSource(DialogPhotoSmallLegacy(dialog_id, dialog_access_hash, volume_id, local_id)); } } - PhotoSizeSource(int64 sticker_set_id, int64 sticker_set_access_hash, int64 volume_id, int32 local_id) - : variant(StickerSetThumbnailLegacy(sticker_set_id, sticker_set_access_hash, volume_id, local_id)) { + + static PhotoSizeSource sticker_set_thumbnail_legacy(int64 sticker_set_id, int64 sticker_set_access_hash, + int64 volume_id, int32 local_id) { + return PhotoSizeSource(StickerSetThumbnailLegacy(sticker_set_id, sticker_set_access_hash, volume_id, local_id)); } - PhotoSizeSource(int64 sticker_set_id, int64 sticker_set_access_hash, int32 version) - : variant(StickerSetThumbnailVersion(sticker_set_id, sticker_set_access_hash, version)) { + + static PhotoSizeSource sticker_set_thumbnail(int64 sticker_set_id, int64 sticker_set_access_hash, int32 version) { + return PhotoSizeSource(StickerSetThumbnailVersion(sticker_set_id, sticker_set_access_hash, version)); } Type get_type() const { - auto offset = variant.get_offset(); + auto offset = variant_.get_offset(); CHECK(offset >= 0); return static_cast(offset); } @@ -179,58 +186,58 @@ struct PhotoSizeSource { FileType get_file_type() const; Thumbnail &thumbnail() { - return variant.get(); + return variant_.get(); } const Legacy &legacy() const { - return variant.get(); + return variant_.get(); } const Thumbnail &thumbnail() const { - return variant.get(); + return variant_.get(); } const DialogPhoto &dialog_photo() const { - switch (variant.get_offset()) { + switch (variant_.get_offset()) { case 2: - return variant.get(); + return variant_.get(); case 3: - return variant.get(); + return variant_.get(); case 6: - return variant.get(); + return variant_.get(); case 7: - return variant.get(); + return variant_.get(); default: UNREACHABLE(); - return variant.get(); + return variant_.get(); } } const StickerSetThumbnail &sticker_set_thumbnail() const { - switch (variant.get_offset()) { + switch (variant_.get_offset()) { case 4: - return variant.get(); + return variant_.get(); case 8: - return variant.get(); + return variant_.get(); case 9: - return variant.get(); + return variant_.get(); default: UNREACHABLE(); - return variant.get(); + return variant_.get(); } } const FullLegacy &full_legacy() const { - return variant.get(); + return variant_.get(); } const DialogPhotoLegacy &dialog_photo_legacy() const { - if (variant.get_offset() == 6) { - return variant.get(); + if (variant_.get_offset() == 6) { + return variant_.get(); } else { - return variant.get(); + return variant_.get(); } } const StickerSetThumbnailLegacy &sticker_set_thumbnail_legacy() const { - return variant.get(); + return variant_.get(); } const StickerSetThumbnailVersion &sticker_set_thumbnail_version() const { - return variant.get(); + return variant_.get(); } // returns unique representation of the source @@ -249,7 +256,11 @@ struct PhotoSizeSource { private: Variant - variant; + variant_; + + template + explicit PhotoSizeSource(const T &variant) : variant_(variant) { + } }; bool operator==(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs); diff --git a/td/telegram/PhotoSizeSource.hpp b/td/telegram/PhotoSizeSource.hpp index 838a70439..c1ce48207 100644 --- a/td/telegram/PhotoSizeSource.hpp +++ b/td/telegram/PhotoSizeSource.hpp @@ -180,12 +180,12 @@ void parse(PhotoSizeSource::StickerSetThumbnailVersion &source, ParserT &parser) template void PhotoSizeSource::store(StorerT &storer) const { - td::store(variant, storer); + td::store(variant_, storer); } template void PhotoSizeSource::parse(ParserT &parser) { - td::parse(variant, parser); + td::parse(variant_, parser); } } // namespace td diff --git a/td/telegram/StickersManager.cpp b/td/telegram/StickersManager.cpp index 0fcab4dcb..cbf549213 100644 --- a/td/telegram/StickersManager.cpp +++ b/td/telegram/StickersManager.cpp @@ -2164,9 +2164,9 @@ std::pair StickersManager::on_get_sticker_document( string minithumbnail; auto thumbnail_format = has_webp_thumbnail(document->thumbs_) ? PhotoFormat::Webp : PhotoFormat::Jpeg; for (auto &thumb : document->thumbs_) { - auto photo_size = get_photo_size(td_->file_manager_.get(), {FileType::Thumbnail, 0}, document_id, - document->access_hash_, document->file_reference_.as_slice().str(), dc_id, - DialogId(), std::move(thumb), thumbnail_format); + auto photo_size = get_photo_size(td_->file_manager_.get(), PhotoSizeSource::thumbnail(FileType::Thumbnail, 0), + document_id, document->access_hash_, document->file_reference_.as_slice().str(), + dc_id, DialogId(), std::move(thumb), thumbnail_format); if (photo_size.get_offset() == 0) { if (!thumbnail.file_id.is_valid()) { thumbnail = std::move(photo_size.get<0>()); @@ -2698,9 +2698,11 @@ StickerSetId StickersManager::on_get_sticker_set(tl_object_ptrthumbs_) { - auto photo_size = get_photo_size(td_->file_manager_.get(), {set_id.get(), s->access_hash, set->thumb_version_}, 0, - 0, "", DcId::create(set->thumb_dc_id_), DialogId(), std::move(thumb), - is_animated ? PhotoFormat::Tgs : PhotoFormat::Webp); + auto photo_size = + get_photo_size(td_->file_manager_.get(), + PhotoSizeSource::sticker_set_thumbnail(set_id.get(), s->access_hash, set->thumb_version_), 0, 0, + "", DcId::create(set->thumb_dc_id_), DialogId(), std::move(thumb), + is_animated ? PhotoFormat::Tgs : PhotoFormat::Webp); if (photo_size.get_offset() == 0) { if (!thumbnail.file_id.is_valid()) { thumbnail = std::move(photo_size.get<0>()); diff --git a/td/telegram/files/FileLocation.h b/td/telegram/files/FileLocation.h index 03f44129a..b3fdd6737 100644 --- a/td/telegram/files/FileLocation.h +++ b/td/telegram/files/FileLocation.h @@ -325,11 +325,11 @@ class FullRemoteFileLocation { return photo().source_; case LocationType::Common: case LocationType::Web: - return PhotoSizeSource(nullptr, 0, 0, 0); + return PhotoSizeSource::full_legacy(0, 0, 0); case LocationType::None: default: UNREACHABLE(); - return PhotoSizeSource(nullptr, 0, 0, 0); + return PhotoSizeSource::full_legacy(0, 0, 0); } } diff --git a/td/telegram/files/FileLocation.hpp b/td/telegram/files/FileLocation.hpp index 0c5c10cbf..06e5f72eb 100644 --- a/td/telegram/files/FileLocation.hpp +++ b/td/telegram/files/FileLocation.hpp @@ -66,7 +66,7 @@ void PhotoRemoteFileLocation::parse(ParserT &parser) { int64 secret; parse(secret, parser); parse(local_id, parser); - source = PhotoSizeSource(nullptr, volume_id, local_id, secret); + source = PhotoSizeSource::full_legacy(volume_id, local_id, secret); } if (parser.get_error() != nullptr) { @@ -75,7 +75,7 @@ void PhotoRemoteFileLocation::parse(ParserT &parser) { switch (source.get_type()) { case PhotoSizeSource::Type::Legacy: - source_ = PhotoSizeSource(nullptr, volume_id, local_id, source.legacy().secret); + source_ = PhotoSizeSource::full_legacy(volume_id, local_id, source.legacy().secret); break; case PhotoSizeSource::Type::FullLegacy: case PhotoSizeSource::Type::Thumbnail: @@ -85,13 +85,14 @@ void PhotoRemoteFileLocation::parse(ParserT &parser) { case PhotoSizeSource::Type::DialogPhotoBig: { auto &dialog_photo = source.dialog_photo(); bool is_big = source.get_type() == PhotoSizeSource::Type::DialogPhotoBig; - source_ = PhotoSizeSource(dialog_photo.dialog_id, dialog_photo.dialog_access_hash, is_big, volume_id, local_id); + source_ = PhotoSizeSource::dialog_photo_legacy(dialog_photo.dialog_id, dialog_photo.dialog_access_hash, is_big, + volume_id, local_id); break; } case PhotoSizeSource::Type::StickerSetThumbnail: { auto &sticker_set_thumbnail = source.sticker_set_thumbnail(); - source_ = PhotoSizeSource(sticker_set_thumbnail.sticker_set_id, sticker_set_thumbnail.sticker_set_access_hash, - volume_id, local_id); + source_ = PhotoSizeSource::sticker_set_thumbnail_legacy( + sticker_set_thumbnail.sticker_set_id, sticker_set_thumbnail.sticker_set_access_hash, volume_id, local_id); break; } default: