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/files/FileId.h"
10 #include "td/telegram/files/FileSourceId.h"
11 #include "td/telegram/Photo.h"
12 #include "td/telegram/SecretInputMedia.h"
13 #include "td/telegram/td_api.h"
14 #include "td/telegram/telegram_api.h"
15 
16 #include "td/actor/actor.h"
17 #include "td/actor/PromiseFuture.h"
18 
19 #include "td/utils/buffer.h"
20 #include "td/utils/common.h"
21 #include "td/utils/Status.h"
22 
23 #include <unordered_map>
24 
25 namespace td {
26 
27 class Td;
28 
29 class AnimationsManager final : public Actor {
30  public:
31   AnimationsManager(Td *td, ActorShared<> parent);
32 
33   int32 get_animation_duration(FileId file_id) const;
34 
35   tl_object_ptr<td_api::animation> get_animation_object(FileId file_id) const;
36 
37   void create_animation(FileId file_id, string minithumbnail, PhotoSize thumbnail, AnimationSize animated_thumbnail,
38                         bool has_stickers, vector<FileId> &&sticker_file_ids, string file_name, string mime_type,
39                         int32 duration, Dimensions dimensions, bool replace);
40 
41   tl_object_ptr<telegram_api::InputMedia> get_input_media(FileId file_id,
42                                                           tl_object_ptr<telegram_api::InputFile> input_file,
43                                                           tl_object_ptr<telegram_api::InputFile> input_thumbnail) const;
44 
45   SecretInputMedia get_secret_input_media(FileId animation_file_id,
46                                           tl_object_ptr<telegram_api::InputEncryptedFile> input_file,
47                                           const string &caption, BufferSlice thumbnail) const;
48 
49   FileId get_animation_thumbnail_file_id(FileId file_id) const;
50 
51   FileId get_animation_animated_thumbnail_file_id(FileId file_id) const;
52 
53   void delete_animation_thumbnail(FileId file_id);
54 
55   FileId dup_animation(FileId new_id, FileId old_id);
56 
57   void merge_animations(FileId new_id, FileId old_id, bool can_delete_old);
58 
59   void on_update_animation_search_emojis(string animation_search_emojis);
60 
61   void on_update_animation_search_provider(string animation_search_provider);
62 
63   void on_update_saved_animations_limit(int32 saved_animations_limit);
64 
65   void reload_saved_animations(bool force);
66 
67   void repair_saved_animations(Promise<Unit> &&promise);
68 
69   void on_get_saved_animations(bool is_repair, tl_object_ptr<telegram_api::messages_SavedGifs> &&saved_animations_ptr);
70 
71   void on_get_saved_animations_failed(bool is_repair, Status error);
72 
73   vector<FileId> get_saved_animations(Promise<Unit> &&promise);
74 
75   FileSourceId get_saved_animations_file_source_id();
76 
77   void send_save_gif_query(FileId animation_id, bool unsave, Promise<Unit> &&promise);
78 
79   void add_saved_animation(const tl_object_ptr<td_api::InputFile> &input_file, Promise<Unit> &&promise);
80 
81   void add_saved_animation_by_id(FileId animation_id);
82 
83   void remove_saved_animation(const tl_object_ptr<td_api::InputFile> &input_file, Promise<Unit> &&promise);
84 
85   template <class StorerT>
86   void store_animation(FileId file_id, StorerT &storer) const;
87 
88   template <class ParserT>
89   FileId parse_animation(ParserT &parser);
90 
91   string get_animation_search_text(FileId file_id) const;
92 
93   void after_get_difference();
94 
95   void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const;
96 
97  private:
98   class Animation {
99    public:
100     string file_name;
101     string mime_type;
102     int32 duration = 0;
103     Dimensions dimensions;
104     string minithumbnail;
105     PhotoSize thumbnail;
106     AnimationSize animated_thumbnail;
107 
108     bool has_stickers = false;
109     vector<FileId> sticker_file_ids;
110 
111     FileId file_id;
112   };
113 
114   const Animation *get_animation(FileId file_id) const;
115 
116   FileId on_get_animation(unique_ptr<Animation> new_animation, bool replace);
117 
118   int64 get_saved_animations_hash(const char *source) const;
119 
120   void add_saved_animation_impl(FileId animation_id, bool add_on_server, Promise<Unit> &&promise);
121 
122   void load_saved_animations(Promise<Unit> &&promise);
123 
124   void on_load_saved_animations_from_database(const string &value);
125 
126   void on_load_saved_animations_finished(vector<FileId> &&saved_animation_ids, bool from_database = false);
127 
128   void try_send_update_animation_search_parameters() const;
129 
130   td_api::object_ptr<td_api::updateAnimationSearchParameters> get_update_animation_search_parameters_object() const;
131 
132   td_api::object_ptr<td_api::updateSavedAnimations> get_update_saved_animations_object() const;
133 
134   void send_update_saved_animations(bool from_database = false);
135 
136   void save_saved_animations_to_database();
137 
138   void tear_down() final;
139 
140   class AnimationListLogEvent;
141 
142   Td *td_;
143   ActorShared<> parent_;
144 
145   std::unordered_map<FileId, unique_ptr<Animation>, FileIdHash> animations_;
146 
147   int32 saved_animations_limit_ = 200;
148   vector<FileId> saved_animation_ids_;
149   vector<FileId> saved_animation_file_ids_;
150   double next_saved_animations_load_time_ = 0;
151   bool are_saved_animations_being_loaded_ = false;
152   bool are_saved_animations_loaded_ = false;
153   vector<Promise<Unit>> load_saved_animations_queries_;
154   vector<Promise<Unit>> repair_saved_animations_queries_;
155   FileSourceId saved_animations_file_source_id_;
156 
157   string animation_search_emojis_;
158   string animation_search_provider_;
159   bool is_animation_search_emojis_inited_ = false;
160   bool is_animation_search_provider_inited_ = false;
161 };
162 
163 }  // namespace td
164