summaryrefslogtreecommitdiff
path: root/libs/tdlib/td/td/telegram/AnimationsManager.h
blob: 3282d70521c6a03ee306b18f7d2fb06fe87dc1f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
//
// 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/td_api.h"
#include "td/telegram/telegram_api.h"

#include "td/actor/actor.h"

#include "td/telegram/files/FileId.h"
#include "td/telegram/Photo.h"
#include "td/telegram/SecretInputMedia.h"

#include "td/utils/buffer.h"
#include "td/utils/common.h"
#include "td/utils/Status.h"

#include <unordered_map>

namespace td {
class Td;
template <class T>
class Promise;
}  // namespace td

namespace td {

class AnimationsManager : public Actor {
 public:
  AnimationsManager(Td *td, ActorShared<> parent);

  int32 get_animation_duration(FileId file_id);

  tl_object_ptr<td_api::animation> get_animation_object(FileId file_id, const char *source);

  void create_animation(FileId file_id, PhotoSize thumbnail, string file_name, string mime_type, int32 duration,
                        Dimensions dimensions, bool replace);

  tl_object_ptr<telegram_api::InputMedia> get_input_media(FileId file_id,
                                                          tl_object_ptr<telegram_api::InputFile> input_file,
                                                          tl_object_ptr<telegram_api::InputFile> input_thumbnail) const;

  SecretInputMedia get_secret_input_media(FileId animation_file_id,
                                          tl_object_ptr<telegram_api::InputEncryptedFile> input_file,
                                          const string &caption, BufferSlice thumbnail) const;

  FileId get_animation_thumbnail_file_id(FileId file_id) const;

  void delete_animation_thumbnail(FileId file_id);

  FileId dup_animation(FileId new_id, FileId old_id);

  bool merge_animations(FileId new_id, FileId old_id, bool can_delete_old);

  void on_update_saved_animations_limit(int32 saved_animations_limit);

  void reload_saved_animations(bool force);

  void on_get_saved_animations(tl_object_ptr<telegram_api::messages_SavedGifs> &&saved_animations_ptr);

  void on_get_saved_animations_failed(Status error);

  vector<FileId> get_saved_animations(Promise<Unit> &&promise);

  void add_saved_animation(const tl_object_ptr<td_api::InputFile> &input_file, Promise<Unit> &&promise);

  void add_saved_animation_by_id(FileId animation_id);

  void remove_saved_animation(const tl_object_ptr<td_api::InputFile> &input_file, Promise<Unit> &&promise);

  template <class T>
  void store_animation(FileId file_id, T &storer) const;

  template <class T>
  FileId parse_animation(T &parser);

  string get_animation_search_text(FileId file_id) const;

 private:
  class Animation {
   public:
    string file_name;
    string mime_type;
    int32 duration = 0;
    Dimensions dimensions;
    PhotoSize thumbnail;

    FileId file_id;

    bool is_changed = true;
  };

  const Animation *get_animation(FileId file_id) const;

  FileId on_get_animation(std::unique_ptr<Animation> new_animation, bool replace);

  int32 get_saved_animations_hash() const;

  void add_saved_animation_inner(FileId animation_id, Promise<Unit> &&promise);

  bool add_saved_animation_impl(FileId animation_id, Promise<Unit> &promise);

  void load_saved_animations(Promise<Unit> &&promise);

  void on_load_saved_animations_from_database(const string &value);

  void on_load_saved_animations_finished(vector<FileId> &&saved_animation_ids, bool from_database = false);

  void send_update_saved_animations(bool from_database = false);

  void save_saved_animations_to_database();

  void tear_down() override;

  class AnimationListLogEvent;

  Td *td_;
  ActorShared<> parent_;

  std::unordered_map<FileId, unique_ptr<Animation>, FileIdHash> animations_;

  int32 saved_animations_limit_ = 200;
  vector<FileId> saved_animation_ids_;
  double next_saved_animations_load_time_ = 0;
  bool are_saved_animations_loaded_ = false;
  vector<Promise<Unit>> load_saved_animations_queries_;
};

}  // namespace td