1 //
2 // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 #pragma once
8 
9 #include "td/telegram/DialogId.h"
10 #include "td/telegram/EncryptedFile.h"
11 #include "td/telegram/files/FileId.h"
12 #include "td/telegram/files/FileType.h"
13 #include "td/telegram/net/DcId.h"
14 #include "td/telegram/PhotoSizeSource.h"
15 #include "td/telegram/secret_api.h"
16 #include "td/telegram/SecretInputMedia.h"
17 #include "td/telegram/td_api.h"
18 #include "td/telegram/telegram_api.h"
19 #include "td/telegram/UserId.h"
20 
21 #include "td/utils/buffer.h"
22 #include "td/utils/common.h"
23 #include "td/utils/MovableValue.h"
24 #include "td/utils/StringBuilder.h"
25 #include "td/utils/Variant.h"
26 
27 namespace td {
28 
29 class FileManager;
30 
31 struct Dimensions {
32   uint16 width = 0;
33   uint16 height = 0;
34 };
35 
36 struct DialogPhoto {
37   FileId small_file_id;
38   FileId big_file_id;
39   string minithumbnail;
40   bool has_animation = false;
41 };
42 
43 struct ProfilePhoto final : public DialogPhoto {
44   int64 id = 0;
45 };
46 
47 struct PhotoSize {
48   int32 type = 0;
49   Dimensions dimensions;
50   int32 size = 0;
51   FileId file_id;
52   vector<int32> progressive_sizes;
53 };
54 
55 struct AnimationSize final : public PhotoSize {
56   double main_frame_timestamp = 0.0;
57 };
58 
59 struct Photo {
60   MovableValue<int64, -2> id;
61   int32 date = 0;
62   string minithumbnail;
63   vector<PhotoSize> photos;
64 
65   vector<AnimationSize> animations;
66 
67   bool has_stickers = false;
68   vector<FileId> sticker_file_ids;
69 
is_emptyPhoto70   bool is_empty() const {
71     return id.get() == -2;
72   }
73 };
74 
75 Dimensions get_dimensions(int32 width, int32 height, const char *source);
76 
77 bool operator==(const Dimensions &lhs, const Dimensions &rhs);
78 bool operator!=(const Dimensions &lhs, const Dimensions &rhs);
79 
80 StringBuilder &operator<<(StringBuilder &string_builder, const Dimensions &dimensions);
81 
82 td_api::object_ptr<td_api::minithumbnail> get_minithumbnail_object(const string &packed);
83 
84 ProfilePhoto get_profile_photo(FileManager *file_manager, UserId user_id, int64 user_access_hash,
85                                tl_object_ptr<telegram_api::UserProfilePhoto> &&profile_photo_ptr);
86 tl_object_ptr<td_api::profilePhoto> get_profile_photo_object(FileManager *file_manager,
87                                                              const ProfilePhoto &profile_photo);
88 
89 bool operator==(const ProfilePhoto &lhs, const ProfilePhoto &rhs);
90 bool operator!=(const ProfilePhoto &lhs, const ProfilePhoto &rhs);
91 
92 StringBuilder &operator<<(StringBuilder &string_builder, const ProfilePhoto &profile_photo);
93 
94 DialogPhoto get_dialog_photo(FileManager *file_manager, DialogId dialog_id, int64 dialog_access_hash,
95                              tl_object_ptr<telegram_api::ChatPhoto> &&chat_photo_ptr);
96 tl_object_ptr<td_api::chatPhotoInfo> get_chat_photo_info_object(FileManager *file_manager,
97                                                                 const DialogPhoto *dialog_photo);
98 
99 DialogPhoto as_fake_dialog_photo(const Photo &photo, DialogId dialog_id);
100 
101 ProfilePhoto as_profile_photo(FileManager *file_manager, UserId user_id, int64 user_access_hash, const Photo &photo);
102 
103 vector<FileId> dialog_photo_get_file_ids(const DialogPhoto &dialog_photo);
104 
105 bool operator==(const DialogPhoto &lhs, const DialogPhoto &rhs);
106 bool operator!=(const DialogPhoto &lhs, const DialogPhoto &rhs);
107 
108 StringBuilder &operator<<(StringBuilder &string_builder, const DialogPhoto &dialog_photo);
109 
110 enum class PhotoFormat : int32 { Jpeg, Png, Webp, Gif, Tgs, Mpeg4 };
111 
112 PhotoSize get_secret_thumbnail_photo_size(FileManager *file_manager, BufferSlice bytes, DialogId owner_dialog_id,
113                                           int32 width, int32 height);
114 Variant<PhotoSize, string> get_photo_size(FileManager *file_manager, PhotoSizeSource source, int64 id,
115                                           int64 access_hash, string file_reference, DcId dc_id,
116                                           DialogId owner_dialog_id, tl_object_ptr<telegram_api::PhotoSize> &&size_ptr,
117                                           PhotoFormat format);
118 AnimationSize get_animation_size(FileManager *file_manager, PhotoSizeSource source, int64 id, int64 access_hash,
119                                  string file_reference, DcId dc_id, DialogId owner_dialog_id,
120                                  tl_object_ptr<telegram_api::videoSize> &&size);
121 PhotoSize get_web_document_photo_size(FileManager *file_manager, FileType file_type, DialogId owner_dialog_id,
122                                       tl_object_ptr<telegram_api::WebDocument> web_document_ptr);
123 td_api::object_ptr<td_api::thumbnail> get_thumbnail_object(FileManager *file_manager, const PhotoSize &photo_size,
124                                                            PhotoFormat format);
125 
126 bool operator==(const PhotoSize &lhs, const PhotoSize &rhs);
127 bool operator!=(const PhotoSize &lhs, const PhotoSize &rhs);
128 
129 bool operator<(const PhotoSize &lhs, const PhotoSize &rhs);
130 
131 StringBuilder &operator<<(StringBuilder &string_builder, const PhotoSize &photo_size);
132 
133 bool operator==(const AnimationSize &lhs, const AnimationSize &rhs);
134 bool operator!=(const AnimationSize &lhs, const AnimationSize &rhs);
135 
136 StringBuilder &operator<<(StringBuilder &string_builder, const AnimationSize &animation_size);
137 
138 Photo get_photo(FileManager *file_manager, tl_object_ptr<telegram_api::Photo> &&photo, DialogId owner_dialog_id);
139 Photo get_photo(FileManager *file_manager, tl_object_ptr<telegram_api::photo> &&photo, DialogId owner_dialog_id);
140 Photo get_encrypted_file_photo(FileManager *file_manager, unique_ptr<EncryptedFile> &&file,
141                                tl_object_ptr<secret_api::decryptedMessageMediaPhoto> &&photo, DialogId owner_dialog_id);
142 Photo get_web_document_photo(FileManager *file_manager, tl_object_ptr<telegram_api::WebDocument> web_document,
143                              DialogId owner_dialog_id);
144 tl_object_ptr<td_api::photo> get_photo_object(FileManager *file_manager, const Photo &photo);
145 tl_object_ptr<td_api::chatPhoto> get_chat_photo_object(FileManager *file_manager, const Photo &photo);
146 
147 void photo_delete_thumbnail(Photo &photo);
148 
149 bool photo_has_input_media(FileManager *file_manager, const Photo &photo, bool is_secret, bool is_bot);
150 
151 SecretInputMedia photo_get_secret_input_media(FileManager *file_manager, const Photo &photo,
152                                               tl_object_ptr<telegram_api::InputEncryptedFile> input_file,
153                                               const string &caption, BufferSlice thumbnail);
154 
155 tl_object_ptr<telegram_api::InputMedia> photo_get_input_media(FileManager *file_manager, const Photo &photo,
156                                                               tl_object_ptr<telegram_api::InputFile> input_file,
157                                                               int32 ttl);
158 
159 vector<FileId> photo_get_file_ids(const Photo &photo);
160 
161 bool operator==(const Photo &lhs, const Photo &rhs);
162 bool operator!=(const Photo &lhs, const Photo &rhs);
163 
164 StringBuilder &operator<<(StringBuilder &string_builder, const Photo &photo);
165 
166 tl_object_ptr<telegram_api::userProfilePhoto> convert_photo_to_profile_photo(
167     const tl_object_ptr<telegram_api::photo> &photo);
168 
169 }  // namespace td
170