From 5022fa26aad19cf87c3f99c1a452c4f51c4ea6a3 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 19 Jun 2019 03:53:11 +0300 Subject: [PATCH] Move secret to PhotoSizeSource. GitOrigin-RevId: 99506d685d672a790e9b276ed14b18ae66792703 --- td/telegram/PhotoSizeSource.cpp | 5 +++-- td/telegram/PhotoSizeSource.h | 23 +++++++++++++++++--- td/telegram/PhotoSizeSource.hpp | 10 +++++++++ td/telegram/files/FileLocation.h | 13 ++++++------ td/telegram/files/FileLocation.hpp | 34 ++++++------------------------ 5 files changed, 45 insertions(+), 40 deletions(-) diff --git a/td/telegram/PhotoSizeSource.cpp b/td/telegram/PhotoSizeSource.cpp index 3ff245de..9e9ca3c9 100644 --- a/td/telegram/PhotoSizeSource.cpp +++ b/td/telegram/PhotoSizeSource.cpp @@ -46,7 +46,7 @@ FileType PhotoSizeSource::get_file_type() const { return FileType::ProfilePhoto; case PhotoSizeSource::Type::StickerSetThumbnail: return FileType::Thumbnail; - case PhotoSizeSource::Type::Empty: + case PhotoSizeSource::Type::Legacy: default: UNREACHABLE(); return FileType::Thumbnail; @@ -68,7 +68,8 @@ bool operator==(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs) { 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: + case PhotoSizeSource::Type::Legacy: + return lhs.legacy().secret == rhs.legacy().secret; default: return true; } diff --git a/td/telegram/PhotoSizeSource.h b/td/telegram/PhotoSizeSource.h index 817eb792..dd088e3f 100644 --- a/td/telegram/PhotoSizeSource.h +++ b/td/telegram/PhotoSizeSource.h @@ -17,7 +17,16 @@ namespace td { struct PhotoSizeSource { - enum class Type : int32 { Empty, Thumbnail, DialogPhoto, StickerSetThumbnail }; + enum class Type : int32 { Legacy, Thumbnail, DialogPhoto, StickerSetThumbnail }; + + // for legacy photos with secret + struct Legacy { + Legacy() = default; + explicit Legacy(int64 secret) : secret(secret) { + } + + int64 secret = 0; + }; // for photos, document thumbnails, encrypted thumbnails struct Thumbnail { @@ -59,6 +68,8 @@ struct PhotoSizeSource { }; PhotoSizeSource() = default; + explicit PhotoSizeSource(int64 secret) : variant(Legacy(secret)) { + } PhotoSizeSource(FileType file_type, int32 thumbnail_type) : variant(Thumbnail(file_type, thumbnail_type)) { } PhotoSizeSource(DialogId dialog_id, int64 dialog_access_hash, bool is_big) @@ -69,7 +80,9 @@ struct PhotoSizeSource { } Type get_type() const { - return static_cast(variant.get_offset() + 1); + auto offset = variant.get_offset(); + CHECK(offset >= 0); + return static_cast(offset); } FileType get_file_type() const; @@ -77,6 +90,10 @@ struct PhotoSizeSource { Thumbnail &thumbnail() { return variant.get(); } + + const Legacy &legacy() const { + return variant.get(); + } const Thumbnail &thumbnail() const { return variant.get(); } @@ -93,7 +110,7 @@ struct PhotoSizeSource { void parse(ParserT &parser); private: - Variant variant; + Variant variant; }; bool operator==(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs); diff --git a/td/telegram/PhotoSizeSource.hpp b/td/telegram/PhotoSizeSource.hpp index 17a8fa6d..64633b05 100644 --- a/td/telegram/PhotoSizeSource.hpp +++ b/td/telegram/PhotoSizeSource.hpp @@ -12,6 +12,16 @@ namespace td { +template +void store(const PhotoSizeSource::Legacy &source, StorerT &storer) { + store(source.secret, storer); +} + +template +void parse(PhotoSizeSource::Legacy &source, ParserT &parser) { + parse(source.secret, parser); +} + template void store(const PhotoSizeSource::Thumbnail &source, StorerT &storer) { store(source.file_type, storer); diff --git a/td/telegram/files/FileLocation.h b/td/telegram/files/FileLocation.h index af312959..98084006 100644 --- a/td/telegram/files/FileLocation.h +++ b/td/telegram/files/FileLocation.h @@ -83,7 +83,6 @@ struct PhotoRemoteFileLocation { int64 id_; int64 access_hash_; int64 volume_id_; - int64 secret_; int32 local_id_; PhotoSizeSource source_; @@ -325,11 +324,11 @@ class FullRemoteFileLocation { return photo().source_; case LocationType::Common: case LocationType::Web: - return PhotoSizeSource(); + return PhotoSizeSource(0); case LocationType::None: default: UNREACHABLE(); - return PhotoSizeSource(); + return PhotoSizeSource(0); } } @@ -396,9 +395,9 @@ class FullRemoteFileLocation { switch (location_type()) { case LocationType::Photo: { switch (photo().source_.get_type()) { - case PhotoSizeSource::Type::Empty: - return make_tl_object(photo().volume_id_, photo().local_id_, - photo().secret_, BufferSlice(file_reference_)); + case PhotoSizeSource::Type::Legacy: + return make_tl_object( + photo().volume_id_, photo().local_id_, photo().source_.legacy().secret, BufferSlice(file_reference_)); case PhotoSizeSource::Type::Thumbnail: { auto &thumbnail = photo().source_.thumbnail(); switch (thumbnail.file_type) { @@ -478,7 +477,7 @@ class FullRemoteFileLocation { : 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}) { + , variant_(PhotoRemoteFileLocation{id, access_hash, volume_id, local_id, source}) { CHECK(is_photo()); check_file_reference(); } diff --git a/td/telegram/files/FileLocation.hpp b/td/telegram/files/FileLocation.hpp index 87e296e6..c1a463a9 100644 --- a/td/telegram/files/FileLocation.hpp +++ b/td/telegram/files/FileLocation.hpp @@ -42,49 +42,27 @@ void PartialRemoteFileLocation::parse(ParserT &parser) { template void PhotoRemoteFileLocation::store(StorerT &storer) const { using td::store; - bool has_secret = secret_ != 0; - bool has_source = source_.get_type() != PhotoSizeSource::Type::Empty; - BEGIN_STORE_FLAGS(); - STORE_FLAG(has_secret); - STORE_FLAG(has_source); - END_STORE_FLAGS(); store(id_, storer); store(access_hash_, storer); store(volume_id_, storer); - if (has_secret) { - store(secret_, storer); - } + store(source_, storer); store(local_id_, storer); - if (has_source) { - store(source_, storer); - } } template void PhotoRemoteFileLocation::parse(ParserT &parser) { using td::parse; - bool has_secret = true; - bool has_source = false; - if (parser.version() >= static_cast(Version::AddPhotoSizeSource)) { - BEGIN_PARSE_FLAGS(); - PARSE_FLAG(has_secret); - PARSE_FLAG(has_source); - END_PARSE_FLAGS(); - } parse(id_, parser); parse(access_hash_, parser); parse(volume_id_, parser); - if (has_secret) { - parse(secret_, parser); - } else { - secret_ = 0; - } - parse(local_id_, parser); - if (has_source) { + if (parser.version() >= static_cast(Version::AddPhotoSizeSource)) { parse(source_, parser); } else { - source_ = PhotoSizeSource(); + int64 secret; + parse(secret, parser); + source_ = PhotoSizeSource(secret); } + parse(local_id_, parser); } template