From 9a77b26a8ac785f5bfae1fe5ea93fd28c149032d Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 19 Jun 2019 03:18:44 +0300 Subject: [PATCH] Add PhotoSizeSource.h. GitOrigin-RevId: 63330f66b3459ecf9b2fdbce4d35faca7542ec3b --- CMakeLists.txt | 3 + td/telegram/Photo.cpp | 71 +---------------- td/telegram/Photo.h | 100 +----------------------- td/telegram/Photo.hpp | 66 ---------------- td/telegram/PhotoSizeSource.cpp | 81 ++++++++++++++++++++ td/telegram/PhotoSizeSource.h | 117 +++++++++++++++++++++++++++++ td/telegram/PhotoSizeSource.hpp | 81 ++++++++++++++++++++ td/telegram/files/FileLocation.h | 4 +- td/telegram/files/FileLocation.hpp | 2 +- 9 files changed, 288 insertions(+), 237 deletions(-) create mode 100644 td/telegram/PhotoSizeSource.cpp create mode 100644 td/telegram/PhotoSizeSource.h create mode 100644 td/telegram/PhotoSizeSource.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e888a5d6..37b2ca2ec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -433,6 +433,7 @@ set(TDLIB_SOURCE td/telegram/PhoneNumberManager.cpp td/telegram/PrivacyManager.cpp td/telegram/Photo.cpp + td/telegram/PhotoSizeSource.cpp td/telegram/PollManager.cpp td/telegram/QueryCombiner.cpp td/telegram/ReplyMarkup.cpp @@ -591,6 +592,7 @@ set(TDLIB_SOURCE td/telegram/Payments.h td/telegram/PhoneNumberManager.h td/telegram/Photo.h + td/telegram/PhotoSizeSource.h td/telegram/PollId.h td/telegram/PollManager.h td/telegram/PrivacyManager.h @@ -647,6 +649,7 @@ set(TDLIB_SOURCE td/telegram/NotificationSettings.hpp td/telegram/Payments.hpp td/telegram/Photo.hpp + td/telegram/PhotoSizeSource.hpp td/telegram/PollId.hpp td/telegram/PollManager.hpp td/telegram/ReplyMarkup.hpp diff --git a/td/telegram/Photo.cpp b/td/telegram/Photo.cpp index b3e9beebf..63c71b1fb 100644 --- a/td/telegram/Photo.cpp +++ b/td/telegram/Photo.cpp @@ -9,13 +9,10 @@ #include "td/telegram/secret_api.h" #include "td/telegram/telegram_api.h" -#include "td/telegram/ChannelId.h" -#include "td/telegram/ChatId.h" #include "td/telegram/files/FileEncryptionKey.h" #include "td/telegram/files/FileLocation.h" #include "td/telegram/files/FileManager.h" #include "td/telegram/net/DcId.h" -#include "td/telegram/UserId.h" #include "td/utils/base64.h" #include "td/utils/common.h" @@ -30,70 +27,6 @@ namespace td { -tl_object_ptr PhotoSizeSource::DialogPhoto::get_input_peer() const { - switch (dialog_id.get_type()) { - case DialogType::User: { - UserId user_id = dialog_id.get_user_id(); - return make_tl_object(user_id.get(), dialog_access_hash); - } - case DialogType::Chat: { - ChatId chat_id = dialog_id.get_chat_id(); - return make_tl_object(chat_id.get()); - } - case DialogType::Channel: { - ChannelId channel_id = dialog_id.get_channel_id(); - return make_tl_object(channel_id.get(), dialog_access_hash); - } - case DialogType::SecretChat: - return nullptr; - case DialogType::None: - return make_tl_object(); - default: - UNREACHABLE(); - return nullptr; - } -} - -FileType get_photo_size_source_file_type(const PhotoSizeSource &source) { - switch (source.get_type()) { - case PhotoSizeSource::Type::Thumbnail: - return source.thumbnail().file_type; - case PhotoSizeSource::Type::DialogPhoto: - return FileType::ProfilePhoto; - case PhotoSizeSource::Type::StickerSetThumbnail: - return FileType::Thumbnail; - case PhotoSizeSource::Type::Empty: - default: - UNREACHABLE(); - return FileType::Thumbnail; - } -} - -bool operator==(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs) { - if (lhs.get_type() != rhs.get_type()) { - return false; - } - switch (lhs.get_type()) { - case PhotoSizeSource::Type::Thumbnail: - return lhs.thumbnail().file_type == rhs.thumbnail().file_type && - lhs.thumbnail().thumbnail_type == rhs.thumbnail().thumbnail_type; - case PhotoSizeSource::Type::DialogPhoto: - return lhs.dialog_photo().dialog_id == rhs.dialog_photo().dialog_id && - lhs.dialog_photo().dialog_access_hash == rhs.dialog_photo().dialog_access_hash && - lhs.dialog_photo().is_big == rhs.dialog_photo().is_big; - case PhotoSizeSource::Type::StickerSetThumbnail: - return lhs.sticker_set_thumbnail().sticker_set_id == rhs.sticker_set_thumbnail().sticker_set_id && - lhs.sticker_set_thumbnail().sticker_set_access_hash == rhs.sticker_set_thumbnail().sticker_set_access_hash; - case PhotoSizeSource::Type::Empty: - default: - return true; - } -} - -bool operator!=(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs) { - return !(lhs == rhs); -} - static uint16 get_dimension(int32 size) { if (size < 0 || size > 65535) { LOG(ERROR) << "Wrong image dimension = " << size; @@ -170,8 +103,8 @@ static FileId register_photo(FileManager *file_manager, const PhotoSizeSource &s int32 local_id = location->local_id_; int64 volume_id = location->volume_id_; LOG(DEBUG) << "Receive " << (is_webp ? "webp" : (is_png ? "png" : "jpeg")) << " photo of type " - << get_photo_size_source_file_type(source) << " in [" << dc_id << "," << volume_id << "," << local_id - << "]. Id: (" << id << ", " << access_hash << ")"; + << source.get_file_type() << " in [" << dc_id << "," << volume_id << "," << local_id << "]. Id: (" << id + << ", " << access_hash << ")"; auto suggested_name = PSTRING() << static_cast(volume_id) << "_" << static_cast(local_id) << (is_webp ? ".webp" : (is_png ? ".png" : ".jpg")); auto file_location_source = owner_dialog_id.get_type() == DialogType::SecretChat ? FileLocationSource::FromUser diff --git a/td/telegram/Photo.h b/td/telegram/Photo.h index f3448815f..a0142b98b 100644 --- a/td/telegram/Photo.h +++ b/td/telegram/Photo.h @@ -10,6 +10,7 @@ #include "td/telegram/files/FileId.h" #include "td/telegram/files/FileType.h" #include "td/telegram/net/DcId.h" +#include "td/telegram/PhotoSizeSource.h" #include "td/telegram/SecretInputMedia.h" #include "td/telegram/UserId.h" @@ -20,7 +21,6 @@ #include "td/utils/buffer.h" #include "td/utils/common.h" #include "td/utils/StringBuilder.h" -#include "td/utils/Variant.h" namespace td { @@ -47,99 +47,6 @@ struct PhotoSize { FileId file_id; }; -struct PhotoSizeSource { - enum class Type : int32 { Empty, Thumbnail, DialogPhoto, StickerSetThumbnail }; - - // for photos, document thumbnails, encrypted thumbnails - struct Thumbnail { - Thumbnail() = default; - Thumbnail(FileType file_type, int32 thumbnail_type) : file_type(file_type), thumbnail_type(thumbnail_type) { - } - - FileType file_type; - int32 thumbnail_type = 0; - - template - void store(StorerT &storer) const; - template - void parse(ParserT &parser); - }; - - // for dialog photos - struct DialogPhoto { - DialogPhoto() = default; - DialogPhoto(DialogId dialog_id, int64 dialog_access_hash, bool is_big) - : dialog_id(dialog_id), dialog_access_hash(dialog_access_hash), is_big(is_big) { - } - - tl_object_ptr get_input_peer() const; - - DialogId dialog_id; - int64 dialog_access_hash = 0; - bool is_big = false; - - template - void store(StorerT &storer) const; - template - void parse(ParserT &parser); - }; - - // for sticker set thumbnails - struct StickerSetThumbnail { - int64 sticker_set_id = 0; - int64 sticker_set_access_hash = 0; - - StickerSetThumbnail() = default; - StickerSetThumbnail(int64 sticker_set_id, int64 sticker_set_access_hash) - : sticker_set_id(sticker_set_id), sticker_set_access_hash(sticker_set_access_hash) { - } - - tl_object_ptr get_input_sticker_set() const { - return make_tl_object(sticker_set_id, sticker_set_access_hash); - } - - template - void store(StorerT &storer) const; - template - void parse(ParserT &parser); - }; - - PhotoSizeSource() = default; - PhotoSizeSource(FileType file_type, int32 thumbnail_type) : variant(Thumbnail(file_type, thumbnail_type)) { - } - PhotoSizeSource(DialogId dialog_id, int64 dialog_access_hash, bool is_big) - : variant(DialogPhoto(dialog_id, dialog_access_hash, is_big)) { - } - PhotoSizeSource(int64 sticker_set_id, int64 sticker_set_access_hash) - : variant(StickerSetThumbnail(sticker_set_id, sticker_set_access_hash)) { - } - - Type get_type() const { - return static_cast(variant.get_offset() + 1); - } - - Thumbnail &thumbnail() { - return variant.get(); - } - const Thumbnail &thumbnail() const { - return variant.get(); - } - const DialogPhoto &dialog_photo() const { - return variant.get(); - } - const StickerSetThumbnail &sticker_set_thumbnail() const { - return variant.get(); - } - - template - void store(StorerT &storer) const; - template - void parse(ParserT &parser); - - private: - Variant variant; -}; - struct Photo { int64 id = 0; int32 date = 0; @@ -150,11 +57,6 @@ struct Photo { vector sticker_file_ids; }; -FileType get_photo_size_source_file_type(const PhotoSizeSource &source); - -bool operator==(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs); -bool operator!=(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs); - Dimensions get_dimensions(int32 width, int32 height); bool operator==(const Dimensions &lhs, const Dimensions &rhs); diff --git a/td/telegram/Photo.hpp b/td/telegram/Photo.hpp index 0502f37c9..0c683c9cd 100644 --- a/td/telegram/Photo.hpp +++ b/td/telegram/Photo.hpp @@ -15,72 +15,6 @@ namespace td { -template -void PhotoSizeSource::Thumbnail::store(StorerT &storer) const { - using td::store; - store(file_type, storer); - store(thumbnail_type, storer); -} - -template -void PhotoSizeSource::Thumbnail::parse(ParserT &parser) { - using td::parse; - parse(file_type, parser); - parse(thumbnail_type, parser); - if (thumbnail_type < 0 || thumbnail_type > 255) { - parser.set_error("Wrong thumbnail type"); - } -} - -template -void PhotoSizeSource::StickerSetThumbnail::store(StorerT &storer) const { - using td::store; - store(sticker_set_id, storer); - store(sticker_set_access_hash, storer); -} - -template -void PhotoSizeSource::StickerSetThumbnail::parse(ParserT &parser) { - using td::parse; - parse(sticker_set_id, parser); - parse(sticker_set_access_hash, parser); -} - -template -void PhotoSizeSource::DialogPhoto::store(StorerT &storer) const { - using td::store; - store(dialog_id, storer); - store(dialog_access_hash, storer); - store(is_big, storer); -} - -template -void PhotoSizeSource::DialogPhoto::parse(ParserT &parser) { - using td::parse; - parse(dialog_id, parser); - parse(dialog_access_hash, parser); - parse(is_big, parser); - - switch (dialog_id.get_type()) { - case DialogType::SecretChat: - case DialogType::None: - parser.set_error("Invalid chat id"); - break; - default: - break; - } -} - -template -void PhotoSizeSource::store(StorerT &storer) const { - td::store(variant, storer); -} - -template -void PhotoSizeSource::parse(ParserT &parser) { - td::parse(variant, parser); -} - template void store(Dimensions dimensions, StorerT &storer) { store(static_cast((static_cast(dimensions.width) << 16) | dimensions.height), storer); diff --git a/td/telegram/PhotoSizeSource.cpp b/td/telegram/PhotoSizeSource.cpp new file mode 100644 index 000000000..3ff245dee --- /dev/null +++ b/td/telegram/PhotoSizeSource.cpp @@ -0,0 +1,81 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019 +// +// 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/PhotoSizeSource.h" + +#include "td/telegram/ChannelId.h" +#include "td/telegram/ChatId.h" +#include "td/telegram/UserId.h" + +#include "td/utils/common.h" + +namespace td { + +tl_object_ptr PhotoSizeSource::DialogPhoto::get_input_peer() const { + switch (dialog_id.get_type()) { + case DialogType::User: { + UserId user_id = dialog_id.get_user_id(); + return make_tl_object(user_id.get(), dialog_access_hash); + } + case DialogType::Chat: { + ChatId chat_id = dialog_id.get_chat_id(); + return make_tl_object(chat_id.get()); + } + case DialogType::Channel: { + ChannelId channel_id = dialog_id.get_channel_id(); + return make_tl_object(channel_id.get(), dialog_access_hash); + } + case DialogType::SecretChat: + return nullptr; + case DialogType::None: + return make_tl_object(); + default: + UNREACHABLE(); + return nullptr; + } +} + +FileType PhotoSizeSource::get_file_type() const { + switch (get_type()) { + case PhotoSizeSource::Type::Thumbnail: + return thumbnail().file_type; + case PhotoSizeSource::Type::DialogPhoto: + return FileType::ProfilePhoto; + case PhotoSizeSource::Type::StickerSetThumbnail: + return FileType::Thumbnail; + case PhotoSizeSource::Type::Empty: + default: + UNREACHABLE(); + return FileType::Thumbnail; + } +} + +bool operator==(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs) { + if (lhs.get_type() != rhs.get_type()) { + return false; + } + switch (lhs.get_type()) { + case PhotoSizeSource::Type::Thumbnail: + return lhs.thumbnail().file_type == rhs.thumbnail().file_type && + lhs.thumbnail().thumbnail_type == rhs.thumbnail().thumbnail_type; + case PhotoSizeSource::Type::DialogPhoto: + return lhs.dialog_photo().dialog_id == rhs.dialog_photo().dialog_id && + lhs.dialog_photo().dialog_access_hash == rhs.dialog_photo().dialog_access_hash && + lhs.dialog_photo().is_big == rhs.dialog_photo().is_big; + case PhotoSizeSource::Type::StickerSetThumbnail: + return lhs.sticker_set_thumbnail().sticker_set_id == rhs.sticker_set_thumbnail().sticker_set_id && + lhs.sticker_set_thumbnail().sticker_set_access_hash == rhs.sticker_set_thumbnail().sticker_set_access_hash; + case PhotoSizeSource::Type::Empty: + default: + return true; + } +} + +bool operator!=(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs) { + return !(lhs == rhs); +} + +} // namespace td diff --git a/td/telegram/PhotoSizeSource.h b/td/telegram/PhotoSizeSource.h new file mode 100644 index 000000000..7dccb81ff --- /dev/null +++ b/td/telegram/PhotoSizeSource.h @@ -0,0 +1,117 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019 +// +// 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) +// +#pragma once + +#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/Variant.h" + +namespace td { + +struct PhotoSizeSource { + enum class Type : int32 { Empty, Thumbnail, DialogPhoto, StickerSetThumbnail }; + + // for photos, document thumbnails, encrypted thumbnails + struct Thumbnail { + Thumbnail() = default; + Thumbnail(FileType file_type, int32 thumbnail_type) : file_type(file_type), thumbnail_type(thumbnail_type) { + } + + FileType file_type; + int32 thumbnail_type = 0; + + template + void store(StorerT &storer) const; + template + void parse(ParserT &parser); + }; + + // for dialog photos + struct DialogPhoto { + DialogPhoto() = default; + DialogPhoto(DialogId dialog_id, int64 dialog_access_hash, bool is_big) + : dialog_id(dialog_id), dialog_access_hash(dialog_access_hash), is_big(is_big) { + } + + tl_object_ptr get_input_peer() const; + + DialogId dialog_id; + int64 dialog_access_hash = 0; + bool is_big = false; + + template + void store(StorerT &storer) const; + template + void parse(ParserT &parser); + }; + + // for sticker set thumbnails + struct StickerSetThumbnail { + int64 sticker_set_id = 0; + int64 sticker_set_access_hash = 0; + + StickerSetThumbnail() = default; + StickerSetThumbnail(int64 sticker_set_id, int64 sticker_set_access_hash) + : sticker_set_id(sticker_set_id), sticker_set_access_hash(sticker_set_access_hash) { + } + + tl_object_ptr get_input_sticker_set() const { + return make_tl_object(sticker_set_id, sticker_set_access_hash); + } + + template + void store(StorerT &storer) const; + template + void parse(ParserT &parser); + }; + + PhotoSizeSource() = default; + PhotoSizeSource(FileType file_type, int32 thumbnail_type) : variant(Thumbnail(file_type, thumbnail_type)) { + } + PhotoSizeSource(DialogId dialog_id, int64 dialog_access_hash, bool is_big) + : variant(DialogPhoto(dialog_id, dialog_access_hash, is_big)) { + } + PhotoSizeSource(int64 sticker_set_id, int64 sticker_set_access_hash) + : variant(StickerSetThumbnail(sticker_set_id, sticker_set_access_hash)) { + } + + Type get_type() const { + return static_cast(variant.get_offset() + 1); + } + + FileType get_file_type() const; + + Thumbnail &thumbnail() { + return variant.get(); + } + const Thumbnail &thumbnail() const { + return variant.get(); + } + const DialogPhoto &dialog_photo() const { + return variant.get(); + } + const StickerSetThumbnail &sticker_set_thumbnail() const { + return variant.get(); + } + + template + void store(StorerT &storer) const; + template + void parse(ParserT &parser); + + private: + Variant variant; +}; + +bool operator==(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs); +bool operator!=(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs); + +} // namespace td diff --git a/td/telegram/PhotoSizeSource.hpp b/td/telegram/PhotoSizeSource.hpp new file mode 100644 index 000000000..394efdef9 --- /dev/null +++ b/td/telegram/PhotoSizeSource.hpp @@ -0,0 +1,81 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019 +// +// 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) +// +#pragma once + +#include "td/telegram/Photo.h" + +#include "td/utils/tl_helpers.h" + +namespace td { + +template +void PhotoSizeSource::Thumbnail::store(StorerT &storer) const { + using td::store; + store(file_type, storer); + store(thumbnail_type, storer); +} + +template +void PhotoSizeSource::Thumbnail::parse(ParserT &parser) { + using td::parse; + parse(file_type, parser); + parse(thumbnail_type, parser); + if (thumbnail_type < 0 || thumbnail_type > 255) { + parser.set_error("Wrong thumbnail type"); + } +} + +template +void PhotoSizeSource::StickerSetThumbnail::store(StorerT &storer) const { + using td::store; + store(sticker_set_id, storer); + store(sticker_set_access_hash, storer); +} + +template +void PhotoSizeSource::StickerSetThumbnail::parse(ParserT &parser) { + using td::parse; + parse(sticker_set_id, parser); + parse(sticker_set_access_hash, parser); +} + +template +void PhotoSizeSource::DialogPhoto::store(StorerT &storer) const { + using td::store; + store(dialog_id, storer); + store(dialog_access_hash, storer); + store(is_big, storer); +} + +template +void PhotoSizeSource::DialogPhoto::parse(ParserT &parser) { + using td::parse; + parse(dialog_id, parser); + parse(dialog_access_hash, parser); + parse(is_big, parser); + + switch (dialog_id.get_type()) { + case DialogType::SecretChat: + case DialogType::None: + parser.set_error("Invalid chat id"); + break; + default: + break; + } +} + +template +void PhotoSizeSource::store(StorerT &storer) const { + td::store(variant, storer); +} + +template +void PhotoSizeSource::parse(ParserT &parser) { + td::parse(variant, parser); +} + +} // namespace td diff --git a/td/telegram/files/FileLocation.h b/td/telegram/files/FileLocation.h index c68dbf4d2..af3129599 100644 --- a/td/telegram/files/FileLocation.h +++ b/td/telegram/files/FileLocation.h @@ -11,7 +11,7 @@ #include "td/telegram/files/FileBitmask.h" #include "td/telegram/files/FileType.h" #include "td/telegram/net/DcId.h" -#include "td/telegram/Photo.h" +#include "td/telegram/PhotoSizeSource.h" #include "td/utils/base64.h" #include "td/utils/buffer.h" @@ -475,7 +475,7 @@ class FullRemoteFileLocation { // photo FullRemoteFileLocation(const PhotoSizeSource &source, int64 id, int64 access_hash, int32 local_id, int64 volume_id, DcId dc_id, std::string file_reference) - : file_type_(get_photo_size_source_file_type(source)) + : file_type_(source.get_file_type()) , dc_id_(dc_id) , file_reference_(std::move(file_reference)) , variant_(PhotoRemoteFileLocation{id, access_hash, volume_id, 0, local_id, source}) { diff --git a/td/telegram/files/FileLocation.hpp b/td/telegram/files/FileLocation.hpp index bcb51df69..87e296e65 100644 --- a/td/telegram/files/FileLocation.hpp +++ b/td/telegram/files/FileLocation.hpp @@ -10,7 +10,7 @@ #include "td/telegram/files/FileType.h" #include "td/telegram/net/DcId.h" -#include "td/telegram/Photo.hpp" +#include "td/telegram/PhotoSizeSource.hpp" #include "td/telegram/Version.h" #include "td/utils/common.h"