Add PhotoSizeSource.h.

GitOrigin-RevId: 63330f66b3459ecf9b2fdbce4d35faca7542ec3b
This commit is contained in:
levlam 2019-06-19 03:18:44 +03:00
parent 59fd30237e
commit 9a77b26a8a
9 changed files with 288 additions and 237 deletions

View File

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

View File

@ -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<telegram_api::InputPeer> 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<telegram_api::inputPeerUser>(user_id.get(), dialog_access_hash);
}
case DialogType::Chat: {
ChatId chat_id = dialog_id.get_chat_id();
return make_tl_object<telegram_api::inputPeerChat>(chat_id.get());
}
case DialogType::Channel: {
ChannelId channel_id = dialog_id.get_channel_id();
return make_tl_object<telegram_api::inputPeerChannel>(channel_id.get(), dialog_access_hash);
}
case DialogType::SecretChat:
return nullptr;
case DialogType::None:
return make_tl_object<telegram_api::inputPeerEmpty>();
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<uint64>(volume_id) << "_" << static_cast<uint64>(local_id)
<< (is_webp ? ".webp" : (is_png ? ".png" : ".jpg"));
auto file_location_source = owner_dialog_id.get_type() == DialogType::SecretChat ? FileLocationSource::FromUser

View File

@ -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 <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
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<telegram_api::InputPeer> get_input_peer() const;
DialogId dialog_id;
int64 dialog_access_hash = 0;
bool is_big = false;
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
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<telegram_api::InputStickerSet> get_input_sticker_set() const {
return make_tl_object<telegram_api::inputStickerSetID>(sticker_set_id, sticker_set_access_hash);
}
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
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<Type>(variant.get_offset() + 1);
}
Thumbnail &thumbnail() {
return variant.get<Thumbnail>();
}
const Thumbnail &thumbnail() const {
return variant.get<Thumbnail>();
}
const DialogPhoto &dialog_photo() const {
return variant.get<DialogPhoto>();
}
const StickerSetThumbnail &sticker_set_thumbnail() const {
return variant.get<StickerSetThumbnail>();
}
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
void parse(ParserT &parser);
private:
Variant<Thumbnail, DialogPhoto, StickerSetThumbnail> variant;
};
struct Photo {
int64 id = 0;
int32 date = 0;
@ -150,11 +57,6 @@ struct Photo {
vector<FileId> 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);

View File

@ -15,72 +15,6 @@
namespace td {
template <class StorerT>
void PhotoSizeSource::Thumbnail::store(StorerT &storer) const {
using td::store;
store(file_type, storer);
store(thumbnail_type, storer);
}
template <class ParserT>
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 <class StorerT>
void PhotoSizeSource::StickerSetThumbnail::store(StorerT &storer) const {
using td::store;
store(sticker_set_id, storer);
store(sticker_set_access_hash, storer);
}
template <class ParserT>
void PhotoSizeSource::StickerSetThumbnail::parse(ParserT &parser) {
using td::parse;
parse(sticker_set_id, parser);
parse(sticker_set_access_hash, parser);
}
template <class StorerT>
void PhotoSizeSource::DialogPhoto::store(StorerT &storer) const {
using td::store;
store(dialog_id, storer);
store(dialog_access_hash, storer);
store(is_big, storer);
}
template <class ParserT>
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 <class StorerT>
void PhotoSizeSource::store(StorerT &storer) const {
td::store(variant, storer);
}
template <class ParserT>
void PhotoSizeSource::parse(ParserT &parser) {
td::parse(variant, parser);
}
template <class StorerT>
void store(Dimensions dimensions, StorerT &storer) {
store(static_cast<uint32>((static_cast<uint32>(dimensions.width) << 16) | dimensions.height), storer);

View File

@ -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<telegram_api::InputPeer> 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<telegram_api::inputPeerUser>(user_id.get(), dialog_access_hash);
}
case DialogType::Chat: {
ChatId chat_id = dialog_id.get_chat_id();
return make_tl_object<telegram_api::inputPeerChat>(chat_id.get());
}
case DialogType::Channel: {
ChannelId channel_id = dialog_id.get_channel_id();
return make_tl_object<telegram_api::inputPeerChannel>(channel_id.get(), dialog_access_hash);
}
case DialogType::SecretChat:
return nullptr;
case DialogType::None:
return make_tl_object<telegram_api::inputPeerEmpty>();
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

View File

@ -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 <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
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<telegram_api::InputPeer> get_input_peer() const;
DialogId dialog_id;
int64 dialog_access_hash = 0;
bool is_big = false;
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
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<telegram_api::InputStickerSet> get_input_sticker_set() const {
return make_tl_object<telegram_api::inputStickerSetID>(sticker_set_id, sticker_set_access_hash);
}
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
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<Type>(variant.get_offset() + 1);
}
FileType get_file_type() const;
Thumbnail &thumbnail() {
return variant.get<Thumbnail>();
}
const Thumbnail &thumbnail() const {
return variant.get<Thumbnail>();
}
const DialogPhoto &dialog_photo() const {
return variant.get<DialogPhoto>();
}
const StickerSetThumbnail &sticker_set_thumbnail() const {
return variant.get<StickerSetThumbnail>();
}
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
void parse(ParserT &parser);
private:
Variant<Thumbnail, DialogPhoto, StickerSetThumbnail> variant;
};
bool operator==(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs);
bool operator!=(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs);
} // namespace td

View File

@ -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 <class StorerT>
void PhotoSizeSource::Thumbnail::store(StorerT &storer) const {
using td::store;
store(file_type, storer);
store(thumbnail_type, storer);
}
template <class ParserT>
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 <class StorerT>
void PhotoSizeSource::StickerSetThumbnail::store(StorerT &storer) const {
using td::store;
store(sticker_set_id, storer);
store(sticker_set_access_hash, storer);
}
template <class ParserT>
void PhotoSizeSource::StickerSetThumbnail::parse(ParserT &parser) {
using td::parse;
parse(sticker_set_id, parser);
parse(sticker_set_access_hash, parser);
}
template <class StorerT>
void PhotoSizeSource::DialogPhoto::store(StorerT &storer) const {
using td::store;
store(dialog_id, storer);
store(dialog_access_hash, storer);
store(is_big, storer);
}
template <class ParserT>
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 <class StorerT>
void PhotoSizeSource::store(StorerT &storer) const {
td::store(variant, storer);
}
template <class ParserT>
void PhotoSizeSource::parse(ParserT &parser) {
td::parse(variant, parser);
}
} // namespace td

View File

@ -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}) {

View File

@ -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"