summaryrefslogtreecommitdiff
path: root/protocols/Telegram/tdlib/td/td/telegram/StickersManager.hpp
blob: 2bdb3593a79de15c58ce1a3c4fa082fee17ee825 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//
// 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/StickersManager.h"

#include "td/telegram/files/FileId.hpp"
#include "td/telegram/misc.h"
#include "td/telegram/Photo.hpp"

#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/tl_helpers.h"

namespace td {

template <class T>
void StickersManager::store_sticker(FileId file_id, bool in_sticker_set, T &storer) const {
  auto it = stickers_.find(file_id);
  CHECK(it != stickers_.end());
  const Sticker *sticker = it->second.get();
  bool has_sticker_set_access_hash = sticker->set_id != 0 && !in_sticker_set;
  BEGIN_STORE_FLAGS();
  STORE_FLAG(sticker->is_mask);
  STORE_FLAG(has_sticker_set_access_hash);
  STORE_FLAG(in_sticker_set);
  END_STORE_FLAGS();
  if (!in_sticker_set) {
    store(sticker->set_id, storer);
    if (has_sticker_set_access_hash) {
      auto sticker_set = get_sticker_set(sticker->set_id);
      CHECK(sticker_set != nullptr);
      store(sticker_set->access_hash, storer);
    }
  }
  store(sticker->alt, storer);
  store(sticker->dimensions, storer);
  store(sticker->message_thumbnail, storer);
  store(sticker->sticker_thumbnail, storer);
  store(file_id, storer);
  if (sticker->is_mask) {
    store(sticker->point, storer);
    store(sticker->x_shift, storer);
    store(sticker->y_shift, storer);
    store(sticker->scale, storer);
  }
}

template <class T>
FileId StickersManager::parse_sticker(bool in_sticker_set, T &parser) {
  auto sticker = make_unique<Sticker>();
  bool has_sticker_set_access_hash;
  bool in_sticker_set_stored;
  BEGIN_PARSE_FLAGS();
  PARSE_FLAG(sticker->is_mask);
  PARSE_FLAG(has_sticker_set_access_hash);
  PARSE_FLAG(in_sticker_set_stored);
  END_PARSE_FLAGS();
  CHECK(in_sticker_set_stored == in_sticker_set) << in_sticker_set << " " << in_sticker_set_stored;
  if (!in_sticker_set) {
    parse(sticker->set_id, parser);
    if (has_sticker_set_access_hash) {
      int64 sticker_set_access_hash;
      parse(sticker_set_access_hash, parser);
      add_sticker_set(sticker->set_id, sticker_set_access_hash);
    } else {
      // backward compatibility
      sticker->set_id = 0;
    }
  }
  parse(sticker->alt, parser);
  parse(sticker->dimensions, parser);
  parse(sticker->message_thumbnail, parser);
  parse(sticker->sticker_thumbnail, parser);
  parse(sticker->file_id, parser);
  if (sticker->is_mask) {
    parse(sticker->point, parser);
    parse(sticker->x_shift, parser);
    parse(sticker->y_shift, parser);
    parse(sticker->scale, parser);
  }
  return on_get_sticker(std::move(sticker), true);
}

template <class T>
void StickersManager::store_sticker_set(const StickerSet *sticker_set, bool with_stickers, T &storer) const {
  size_t stickers_limit = with_stickers ? sticker_set->sticker_ids.size() : 5;
  bool is_full = sticker_set->sticker_ids.size() <= stickers_limit;
  bool was_loaded = sticker_set->was_loaded && is_full;
  bool is_loaded = sticker_set->is_loaded && is_full;
  bool has_expires_at = !sticker_set->is_installed && sticker_set->expires_at != 0;
  BEGIN_STORE_FLAGS();
  STORE_FLAG(sticker_set->is_inited);
  STORE_FLAG(was_loaded);
  STORE_FLAG(is_loaded);
  STORE_FLAG(sticker_set->is_installed);
  STORE_FLAG(sticker_set->is_archived);
  STORE_FLAG(sticker_set->is_official);
  STORE_FLAG(sticker_set->is_masks);
  STORE_FLAG(sticker_set->is_viewed);
  STORE_FLAG(has_expires_at);
  END_STORE_FLAGS();
  store(sticker_set->id, storer);
  store(sticker_set->access_hash, storer);
  if (sticker_set->is_inited) {
    store(sticker_set->title, storer);
    store(sticker_set->short_name, storer);
    store(sticker_set->sticker_count, storer);
    store(sticker_set->hash, storer);
    if (has_expires_at) {
      store(sticker_set->expires_at, storer);
    }

    uint32 stored_sticker_count = narrow_cast<uint32>(is_full ? sticker_set->sticker_ids.size() : stickers_limit);
    store(stored_sticker_count, storer);
    for (uint32 i = 0; i < stored_sticker_count; i++) {
      auto sticker_id = sticker_set->sticker_ids[i];
      store_sticker(sticker_id, true, storer);

      if (was_loaded) {
        auto it = sticker_set->sticker_emojis_map_.find(sticker_id);
        if (it != sticker_set->sticker_emojis_map_.end()) {
          store(it->second, storer);
        } else {
          store(vector<string>(), storer);
        }
      }
    }
  }
}

template <class T>
void StickersManager::parse_sticker_set(StickerSet *sticker_set, T &parser) {
  CHECK(sticker_set != nullptr);
  CHECK(!sticker_set->was_loaded);
  bool was_inited = sticker_set->is_inited;
  bool is_installed;
  bool is_archived;
  bool is_official;
  bool is_masks;
  bool has_expires_at;
  BEGIN_PARSE_FLAGS();
  PARSE_FLAG(sticker_set->is_inited);
  PARSE_FLAG(sticker_set->was_loaded);
  PARSE_FLAG(sticker_set->is_loaded);
  PARSE_FLAG(is_installed);
  PARSE_FLAG(is_archived);
  PARSE_FLAG(is_official);
  PARSE_FLAG(is_masks);
  PARSE_FLAG(sticker_set->is_viewed);
  PARSE_FLAG(has_expires_at);
  END_PARSE_FLAGS();
  int64 sticker_set_id;
  int64 access_hash;
  parse(sticker_set_id, parser);
  parse(access_hash, parser);
  CHECK(sticker_set->id == sticker_set_id);
  if (sticker_set->access_hash != access_hash) {
    LOG(ERROR) << "Sticker set " << sticker_set_id << " access hash has changed from " << access_hash << " to "
               << sticker_set->access_hash;
  }

  if (sticker_set->is_inited) {
    string title;
    string short_name;
    int32 sticker_count;
    int32 hash;
    int32 expires_at = 0;
    parse(title, parser);
    parse(short_name, parser);
    parse(sticker_count, parser);
    parse(hash, parser);
    if (has_expires_at) {
      parse(expires_at, parser);
    }
    if (!was_inited) {
      sticker_set->title = std::move(title);
      sticker_set->short_name = std::move(short_name);
      sticker_set->sticker_count = sticker_count;
      sticker_set->hash = hash;
      sticker_set->expires_at = expires_at;
      sticker_set->is_official = is_official;
      sticker_set->is_masks = is_masks;

      short_name_to_sticker_set_id_.emplace(clean_username(sticker_set->short_name), sticker_set_id);
      on_update_sticker_set(sticker_set, is_installed, is_archived, false, true);
    } else {
      if (sticker_set->title != title) {
        LOG(INFO) << "Sticker set " << sticker_set_id << " title has changed";
      }
      if (sticker_set->short_name != short_name) {
        LOG(ERROR) << "Sticker set " << sticker_set_id << " short name has changed from \"" << short_name << "\" to \""
                   << sticker_set->short_name << "\"";
      }
      if (sticker_set->sticker_count != sticker_count || sticker_set->hash != hash) {
        sticker_set->is_loaded = false;
      }
    }

    uint32 stored_sticker_count;
    parse(stored_sticker_count, parser);
    sticker_set->sticker_ids.clear();
    if (sticker_set->was_loaded) {
      sticker_set->emoji_stickers_map_.clear();
      sticker_set->sticker_emojis_map_.clear();
    }
    for (uint32 i = 0; i < stored_sticker_count; i++) {
      auto sticker_id = parse_sticker(true, parser);
      sticker_set->sticker_ids.push_back(sticker_id);

      Sticker *sticker = get_sticker(sticker_id);
      CHECK(sticker != nullptr);
      if (sticker->set_id != sticker_set->id) {
        LOG_IF(ERROR, sticker->set_id != 0) << "Sticker " << sticker_id << " set_id has changed";
        sticker->set_id = sticker_set->id;
        sticker->is_changed = true;
      }

      if (sticker_set->was_loaded) {
        vector<string> emojis;
        parse(emojis, parser);
        for (auto &emoji : emojis) {
          sticker_set->emoji_stickers_map_[remove_emoji_modifiers(emoji)].push_back(sticker_id);
        }
        sticker_set->sticker_emojis_map_[sticker_id] = std::move(emojis);
      }
    }
    if (expires_at > sticker_set->expires_at) {
      sticker_set->expires_at = expires_at;
    }
  }
}

template <class T>
void StickersManager::store_sticker_set_id(int64 sticker_set_id, T &storer) const {
  CHECK(sticker_set_id != 0);
  const StickerSet *sticker_set = get_sticker_set(sticker_set_id);
  CHECK(sticker_set != nullptr);
  store(sticker_set_id, storer);
  store(sticker_set->access_hash, storer);
}

template <class T>
void StickersManager::parse_sticker_set_id(int64 &sticker_set_id, T &parser) {
  parse(sticker_set_id, parser);
  int64 sticker_set_access_hash;
  parse(sticker_set_access_hash, parser);
  add_sticker_set(sticker_set_id, sticker_set_access_hash);
}

}  // namespace td