2018-12-31 20:04:05 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// 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
|
2018-07-03 21:29:04 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#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"
|
2018-08-10 20:54:17 +02:00
|
|
|
#include "td/utils/Slice.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/tl_helpers.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2019-02-21 18:54:20 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void StickersManager::store_sticker(FileId file_id, bool in_sticker_set, StorerT &storer) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto it = stickers_.find(file_id);
|
2020-05-23 21:27:24 +02:00
|
|
|
if (it == stickers_.end() || it->second == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
const Sticker *sticker = it->second.get();
|
2019-09-18 05:55:43 +02:00
|
|
|
bool has_sticker_set_access_hash = sticker->set_id.is_valid() && !in_sticker_set;
|
2020-11-09 21:41:23 +01:00
|
|
|
bool has_minithumbnail = !sticker->minithumbnail.empty();
|
2018-12-31 20:04:05 +01:00
|
|
|
BEGIN_STORE_FLAGS();
|
|
|
|
STORE_FLAG(sticker->is_mask);
|
|
|
|
STORE_FLAG(has_sticker_set_access_hash);
|
|
|
|
STORE_FLAG(in_sticker_set);
|
2019-07-15 02:43:05 +02:00
|
|
|
STORE_FLAG(sticker->is_animated);
|
2020-11-09 21:41:23 +01:00
|
|
|
STORE_FLAG(has_minithumbnail);
|
2018-12-31 20:04:05 +01:00
|
|
|
END_STORE_FLAGS();
|
|
|
|
if (!in_sticker_set) {
|
2019-09-18 05:55:43 +02:00
|
|
|
store(sticker->set_id.get(), storer);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (has_sticker_set_access_hash) {
|
|
|
|
auto sticker_set = get_sticker_set(sticker->set_id);
|
2020-05-23 21:27:24 +02:00
|
|
|
if (sticker_set != nullptr) {
|
|
|
|
store(sticker_set->access_hash, storer);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
store(sticker->alt, storer);
|
|
|
|
store(sticker->dimensions, storer);
|
2019-07-15 03:01:26 +02:00
|
|
|
store(sticker->s_thumbnail, storer);
|
|
|
|
store(sticker->m_thumbnail, storer);
|
2018-03-08 20:43:12 +01:00
|
|
|
store(file_id, storer);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (sticker->is_mask) {
|
|
|
|
store(sticker->point, storer);
|
|
|
|
store(sticker->x_shift, storer);
|
|
|
|
store(sticker->y_shift, storer);
|
|
|
|
store(sticker->scale, storer);
|
|
|
|
}
|
2020-11-09 21:41:23 +01:00
|
|
|
if (has_minithumbnail) {
|
|
|
|
store(sticker->minithumbnail, storer);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-21 18:54:20 +01:00
|
|
|
template <class ParserT>
|
|
|
|
FileId StickersManager::parse_sticker(bool in_sticker_set, ParserT &parser) {
|
2019-05-30 00:35:19 +02:00
|
|
|
if (parser.get_error() != nullptr) {
|
|
|
|
return FileId();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
auto sticker = make_unique<Sticker>();
|
|
|
|
bool has_sticker_set_access_hash;
|
|
|
|
bool in_sticker_set_stored;
|
2020-11-09 21:41:23 +01:00
|
|
|
bool has_minithumbnail;
|
2018-12-31 20:04:05 +01:00
|
|
|
BEGIN_PARSE_FLAGS();
|
|
|
|
PARSE_FLAG(sticker->is_mask);
|
|
|
|
PARSE_FLAG(has_sticker_set_access_hash);
|
|
|
|
PARSE_FLAG(in_sticker_set_stored);
|
2019-07-15 02:43:05 +02:00
|
|
|
PARSE_FLAG(sticker->is_animated);
|
2020-11-09 21:41:23 +01:00
|
|
|
PARSE_FLAG(has_minithumbnail);
|
2018-12-31 20:04:05 +01:00
|
|
|
END_PARSE_FLAGS();
|
2019-05-30 00:35:19 +02:00
|
|
|
if (in_sticker_set_stored != in_sticker_set) {
|
|
|
|
Slice data = parser.template fetch_string_raw<Slice>(parser.get_left_len());
|
|
|
|
for (auto c : data) {
|
|
|
|
if (c != '\0') {
|
2020-04-21 10:07:50 +02:00
|
|
|
parser.set_error("Invalid sticker set is stored in the database");
|
|
|
|
break;
|
2019-05-30 00:35:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
parser.set_error("Zero sticker set is stored in the database");
|
|
|
|
return FileId();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!in_sticker_set) {
|
2019-09-18 05:55:43 +02:00
|
|
|
int64 set_id;
|
|
|
|
parse(set_id, parser);
|
|
|
|
sticker->set_id = StickerSetId(set_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
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
|
2019-09-18 05:55:43 +02:00
|
|
|
sticker->set_id = StickerSetId();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
parse(sticker->alt, parser);
|
|
|
|
parse(sticker->dimensions, parser);
|
2019-07-15 03:01:26 +02:00
|
|
|
PhotoSize thumbnail;
|
|
|
|
parse(thumbnail, parser);
|
|
|
|
add_sticker_thumbnail(sticker.get(), thumbnail);
|
|
|
|
parse(thumbnail, parser);
|
|
|
|
add_sticker_thumbnail(sticker.get(), thumbnail);
|
2018-12-31 20:04:05 +01:00
|
|
|
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);
|
|
|
|
}
|
2020-11-09 21:41:23 +01:00
|
|
|
if (has_minithumbnail) {
|
|
|
|
parse(sticker->minithumbnail, parser);
|
|
|
|
}
|
2019-08-01 02:58:49 +02:00
|
|
|
if (parser.get_error() != nullptr || !sticker->file_id.is_valid()) {
|
|
|
|
return FileId();
|
|
|
|
}
|
2018-11-06 12:37:07 +01:00
|
|
|
return on_get_sticker(std::move(sticker), false); // data in the database is always outdated
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-21 18:54:20 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void StickersManager::store_sticker_set(const StickerSet *sticker_set, bool with_stickers, StorerT &storer) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
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;
|
2019-05-17 16:35:45 +02:00
|
|
|
bool has_thumbnail = sticker_set->thumbnail.file_id.is_valid();
|
2020-11-09 21:41:23 +01:00
|
|
|
bool has_minithumbnail = !sticker_set->minithumbnail.empty();
|
2018-12-31 20:04:05 +01:00
|
|
|
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);
|
2019-05-17 16:35:45 +02:00
|
|
|
STORE_FLAG(has_thumbnail);
|
2019-05-17 17:08:17 +02:00
|
|
|
STORE_FLAG(sticker_set->is_thumbnail_reloaded);
|
2019-07-15 02:43:05 +02:00
|
|
|
STORE_FLAG(sticker_set->is_animated);
|
2020-11-09 21:41:23 +01:00
|
|
|
STORE_FLAG(sticker_set->are_legacy_sticker_thumbnails_reloaded);
|
|
|
|
STORE_FLAG(has_minithumbnail);
|
2018-12-31 20:04:05 +01:00
|
|
|
END_STORE_FLAGS();
|
2019-09-18 05:55:43 +02:00
|
|
|
store(sticker_set->id.get(), storer);
|
2018-12-31 20:04:05 +01:00
|
|
|
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);
|
|
|
|
}
|
2019-05-17 16:35:45 +02:00
|
|
|
if (has_thumbnail) {
|
|
|
|
store(sticker_set->thumbnail, storer);
|
|
|
|
}
|
2020-11-09 21:41:23 +01:00
|
|
|
if (has_minithumbnail) {
|
|
|
|
store(sticker_set->minithumbnail, storer);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 18:54:20 +01:00
|
|
|
template <class ParserT>
|
|
|
|
void StickersManager::parse_sticker_set(StickerSet *sticker_set, ParserT &parser) {
|
2020-05-23 21:27:24 +02:00
|
|
|
if (sticker_set == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
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;
|
2019-05-17 16:35:45 +02:00
|
|
|
bool has_thumbnail;
|
2020-11-09 21:41:23 +01:00
|
|
|
bool is_animated;
|
|
|
|
bool has_minithumbnail;
|
2018-12-31 20:04:05 +01:00
|
|
|
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);
|
2019-05-17 16:35:45 +02:00
|
|
|
PARSE_FLAG(has_thumbnail);
|
2019-05-17 17:08:17 +02:00
|
|
|
PARSE_FLAG(sticker_set->is_thumbnail_reloaded);
|
2019-07-15 02:43:05 +02:00
|
|
|
PARSE_FLAG(is_animated);
|
2020-11-09 21:41:23 +01:00
|
|
|
PARSE_FLAG(sticker_set->are_legacy_sticker_thumbnails_reloaded);
|
|
|
|
PARSE_FLAG(has_minithumbnail);
|
2018-12-31 20:04:05 +01:00
|
|
|
END_PARSE_FLAGS();
|
|
|
|
int64 sticker_set_id;
|
|
|
|
int64 access_hash;
|
|
|
|
parse(sticker_set_id, parser);
|
|
|
|
parse(access_hash, parser);
|
2019-09-18 05:55:43 +02:00
|
|
|
CHECK(sticker_set->id.get() == sticker_set_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (sticker_set->access_hash != access_hash) {
|
2020-03-23 23:36:43 +01:00
|
|
|
LOG(ERROR) << "Access hash of " << sticker_set->id << " has changed from " << access_hash << " to "
|
2018-01-25 17:01:10 +01:00
|
|
|
<< sticker_set->access_hash;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sticker_set->is_inited) {
|
|
|
|
string title;
|
|
|
|
string short_name;
|
2020-11-09 21:41:23 +01:00
|
|
|
string minithumbnail;
|
|
|
|
PhotoSize thumbnail;
|
2018-12-31 20:04:05 +01:00
|
|
|
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);
|
|
|
|
}
|
2019-05-17 16:35:45 +02:00
|
|
|
if (has_thumbnail) {
|
2020-11-09 21:41:23 +01:00
|
|
|
parse(thumbnail, parser);
|
|
|
|
}
|
|
|
|
if (has_minithumbnail) {
|
|
|
|
parse(minithumbnail, parser);
|
2019-05-17 16:35:45 +02:00
|
|
|
}
|
2020-11-09 21:41:23 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!was_inited) {
|
|
|
|
sticker_set->title = std::move(title);
|
|
|
|
sticker_set->short_name = std::move(short_name);
|
2020-11-09 21:41:23 +01:00
|
|
|
sticker_set->minithumbnail = std::move(minithumbnail);
|
|
|
|
sticker_set->thumbnail = std::move(thumbnail);
|
2018-12-31 20:04:05 +01:00
|
|
|
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;
|
2019-07-15 02:43:05 +02:00
|
|
|
sticker_set->is_animated = is_animated;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-01-23 23:08:21 +01:00
|
|
|
short_name_to_sticker_set_id_.emplace(clean_username(sticker_set->short_name), sticker_set->id);
|
2018-12-31 20:04:05 +01:00
|
|
|
on_update_sticker_set(sticker_set, is_installed, is_archived, false, true);
|
|
|
|
} else {
|
|
|
|
if (sticker_set->title != title) {
|
2020-01-23 23:08:21 +01:00
|
|
|
LOG(INFO) << "Title of " << sticker_set->id << " has changed";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (sticker_set->short_name != short_name) {
|
2020-01-23 23:08:21 +01:00
|
|
|
LOG(ERROR) << "Short name of " << sticker_set->id << " has changed from \"" << short_name << "\" to \""
|
2018-12-31 20:04:05 +01:00
|
|
|
<< sticker_set->short_name << "\"";
|
|
|
|
}
|
|
|
|
if (sticker_set->sticker_count != sticker_count || sticker_set->hash != hash) {
|
|
|
|
sticker_set->is_loaded = false;
|
|
|
|
}
|
2019-07-15 02:43:05 +02:00
|
|
|
if (sticker_set->is_animated != is_animated) {
|
2020-01-23 23:08:21 +01:00
|
|
|
LOG(ERROR) << "Is animated of " << sticker_set->id << " has changed from \"" << is_animated << "\" to \""
|
2019-09-18 05:55:43 +02:00
|
|
|
<< sticker_set->is_animated << "\"";
|
2019-07-15 02:43:05 +02:00
|
|
|
}
|
|
|
|
if (sticker_set->is_masks != is_masks) {
|
2020-01-23 23:08:21 +01:00
|
|
|
LOG(ERROR) << "Is masks of " << sticker_set->id << " has changed from \"" << is_masks << "\" to \""
|
2019-07-15 02:43:05 +02:00
|
|
|
<< sticker_set->is_masks << "\"";
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2019-05-30 00:35:19 +02:00
|
|
|
if (parser.get_error() != nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-03 17:16:11 +02:00
|
|
|
if (!sticker_id.is_valid()) {
|
|
|
|
return parser.set_error("Receive invalid sticker in a sticker set");
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
sticker_set->sticker_ids.push_back(sticker_id);
|
|
|
|
|
|
|
|
Sticker *sticker = get_sticker(sticker_id);
|
2020-05-23 21:27:24 +02:00
|
|
|
if (sticker == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (sticker->set_id != sticker_set->id) {
|
2019-09-18 05:55:43 +02:00
|
|
|
LOG_IF(ERROR, sticker->set_id.is_valid()) << "Sticker " << sticker_id << " set_id has changed";
|
2018-12-31 20:04:05 +01:00
|
|
|
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) {
|
2018-07-31 22:48:54 +02:00
|
|
|
auto &sticker_ids = sticker_set->emoji_stickers_map_[remove_emoji_modifiers(emoji)];
|
|
|
|
if (sticker_ids.empty() || sticker_ids.back() != sticker_id) {
|
|
|
|
sticker_ids.push_back(sticker_id);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
sticker_set->sticker_emojis_map_[sticker_id] = std::move(emojis);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (expires_at > sticker_set->expires_at) {
|
|
|
|
sticker_set->expires_at = expires_at;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 18:54:20 +01:00
|
|
|
template <class StorerT>
|
2019-09-18 05:55:43 +02:00
|
|
|
void StickersManager::store_sticker_set_id(StickerSetId sticker_set_id, StorerT &storer) const {
|
|
|
|
CHECK(sticker_set_id.is_valid());
|
2018-12-31 20:04:05 +01:00
|
|
|
const StickerSet *sticker_set = get_sticker_set(sticker_set_id);
|
2020-05-23 21:27:24 +02:00
|
|
|
if (sticker_set == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-18 05:55:43 +02:00
|
|
|
store(sticker_set_id.get(), storer);
|
2018-12-31 20:04:05 +01:00
|
|
|
store(sticker_set->access_hash, storer);
|
|
|
|
}
|
|
|
|
|
2019-02-21 18:54:20 +01:00
|
|
|
template <class ParserT>
|
2019-09-18 05:55:43 +02:00
|
|
|
void StickersManager::parse_sticker_set_id(StickerSetId &sticker_set_id, ParserT &parser) {
|
|
|
|
int64 set_id;
|
|
|
|
parse(set_id, parser);
|
|
|
|
sticker_set_id = StickerSetId(set_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 sticker_set_access_hash;
|
|
|
|
parse(sticker_set_access_hash, parser);
|
|
|
|
add_sticker_set(sticker_set_id, sticker_set_access_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|