Move secret to PhotoSizeSource.

GitOrigin-RevId: 99506d685d672a790e9b276ed14b18ae66792703
This commit is contained in:
levlam 2019-06-19 03:53:11 +03:00
parent e6082852d9
commit 5022fa26aa
5 changed files with 45 additions and 40 deletions

View File

@ -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;
}

View File

@ -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<Type>(variant.get_offset() + 1);
auto offset = variant.get_offset();
CHECK(offset >= 0);
return static_cast<Type>(offset);
}
FileType get_file_type() const;
@ -77,6 +90,10 @@ struct PhotoSizeSource {
Thumbnail &thumbnail() {
return variant.get<Thumbnail>();
}
const Legacy &legacy() const {
return variant.get<Legacy>();
}
const Thumbnail &thumbnail() const {
return variant.get<Thumbnail>();
}
@ -93,7 +110,7 @@ struct PhotoSizeSource {
void parse(ParserT &parser);
private:
Variant<Thumbnail, DialogPhoto, StickerSetThumbnail> variant;
Variant<Legacy, Thumbnail, DialogPhoto, StickerSetThumbnail> variant;
};
bool operator==(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs);

View File

@ -12,6 +12,16 @@
namespace td {
template <class StorerT>
void store(const PhotoSizeSource::Legacy &source, StorerT &storer) {
store(source.secret, storer);
}
template <class ParserT>
void parse(PhotoSizeSource::Legacy &source, ParserT &parser) {
parse(source.secret, parser);
}
template <class StorerT>
void store(const PhotoSizeSource::Thumbnail &source, StorerT &storer) {
store(source.file_type, storer);

View File

@ -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<telegram_api::inputFileLocation>(photo().volume_id_, photo().local_id_,
photo().secret_, BufferSlice(file_reference_));
case PhotoSizeSource::Type::Legacy:
return make_tl_object<telegram_api::inputFileLocation>(
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();
}

View File

@ -42,49 +42,27 @@ void PartialRemoteFileLocation::parse(ParserT &parser) {
template <class StorerT>
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 <class ParserT>
void PhotoRemoteFileLocation::parse(ParserT &parser) {
using td::parse;
bool has_secret = true;
bool has_source = false;
if (parser.version() >= static_cast<int32>(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<int32>(Version::AddPhotoSizeSource)) {
parse(source_, parser);
} else {
source_ = PhotoSizeSource();
int64 secret;
parse(secret, parser);
source_ = PhotoSizeSource(secret);
}
parse(local_id_, parser);
}
template <class StorerT>