Save sticker keywords.

This commit is contained in:
levlam 2022-09-27 00:20:13 +03:00
parent 7ccaaf3b70
commit 0c21d0d596
3 changed files with 43 additions and 8 deletions

View File

@ -3673,16 +3673,14 @@ StickerSetId StickersManager::on_get_messages_sticker_set(StickerSetId sticker_s
s->was_loaded_ = true;
s->is_loaded_ = true;
s->is_changed_ = true;
vector<tl_object_ptr<telegram_api::stickerPack>> packs = std::move(set->packs_);
vector<tl_object_ptr<telegram_api::Document>> documents = std::move(set->documents_);
s->are_keywords_loaded_ = true;
FlatHashMap<int64, FileId> document_id_to_sticker_id;
s->sticker_ids_.clear();
s->premium_sticker_positions_.clear();
bool is_bot = td_->auth_manager_->is_bot();
for (auto &document_ptr : documents) {
for (auto &document_ptr : set->documents_) {
auto sticker_id = on_get_sticker_document(std::move(document_ptr), s->sticker_format_);
if (!sticker_id.second.is_valid() || sticker_id.first == 0) {
continue;
@ -3705,7 +3703,8 @@ StickerSetId StickersManager::on_get_messages_sticker_set(StickerSetId sticker_s
if (!is_bot) {
s->emoji_stickers_map_.clear();
s->sticker_emojis_map_.clear();
for (auto &pack : packs) {
s->sticker_keywords_map_.clear();
for (auto &pack : set->packs_) {
auto cleaned_emoji = remove_emoji_modifiers(pack->emoticon_);
if (cleaned_emoji.empty()) {
LOG(ERROR) << "Receive empty emoji in " << set_id << "/" << s->short_name_ << " from " << source;
@ -3733,6 +3732,21 @@ StickerSetId StickersManager::on_get_messages_sticker_set(StickerSetId sticker_s
}
}
}
for (auto &keywords : set->keywords_) {
auto document_id = keywords->document_id_;
auto it = document_id_to_sticker_id.find(document_id);
if (it == document_id_to_sticker_id.end()) {
LOG(ERROR) << "Can't find document with ID " << document_id << " in " << set_id << "/" << s->short_name_
<< " from " << source;
continue;
}
bool is_inserted = s->sticker_keywords_map_.emplace(it->second, std::move(keywords->keyword_)).second;
if (!is_inserted) {
LOG(ERROR) << "Receive twice document with ID " << document_id << " in " << set_id << "/" << s->short_name_
<< " from " << source;
}
}
}
update_sticker_set(s, "on_get_messages_sticker_set 2");
@ -5276,7 +5290,8 @@ void StickersManager::on_load_sticker_set_from_database(StickerSetId sticker_set
<< format::as_hex_dump<4>(Slice(value));
}
}
if (!sticker_set->is_thumbnail_reloaded_ || !sticker_set->are_legacy_sticker_thumbnails_reloaded_) {
if (!sticker_set->are_keywords_loaded_ || !sticker_set->is_thumbnail_reloaded_ ||
!sticker_set->are_legacy_sticker_thumbnails_reloaded_) {
do_reload_sticker_set(sticker_set_id, get_input_sticker_set(sticker_set), 0, Auto());
}

View File

@ -438,6 +438,7 @@ class StickersManager final : public Actor {
bool is_inited_ = false; // basic information about the set
bool was_loaded_ = false;
bool is_loaded_ = false;
bool are_keywords_loaded_ = false;
StickerSetId id_;
int64 access_hash_ = 0;
@ -455,8 +456,9 @@ class StickersManager final : public Actor {
vector<FileId> sticker_ids_;
vector<int32> premium_sticker_positions_;
FlatHashMap<string, vector<FileId>> emoji_stickers_map_; // emoji -> stickers
FlatHashMap<FileId, vector<string>, FileIdHash> sticker_emojis_map_; // sticker -> emojis
FlatHashMap<string, vector<FileId>> emoji_stickers_map_; // emoji -> stickers
FlatHashMap<FileId, vector<string>, FileIdHash> sticker_emojis_map_; // sticker -> emojis
FlatHashMap<FileId, vector<string>, FileIdHash> sticker_keywords_map_; // sticker -> keywords
bool is_installed_ = false;
bool is_archived_ = false;

View File

@ -204,6 +204,7 @@ void StickersManager::store_sticker_set(const StickerSet *sticker_set, bool with
STORE_FLAG(is_webm);
STORE_FLAG(is_emojis);
STORE_FLAG(has_thumbnail_document_id);
STORE_FLAG(sticker_set->are_keywords_loaded_);
END_STORE_FLAGS();
store(sticker_set->id_.get(), storer);
store(sticker_set->access_hash_, storer);
@ -239,6 +240,14 @@ void StickersManager::store_sticker_set(const StickerSet *sticker_set, bool with
store(vector<string>(), storer);
}
}
if (sticker_set->are_keywords_loaded_) {
auto it = sticker_set->sticker_keywords_map_.find(sticker_id);
if (it != sticker_set->sticker_keywords_map_.end()) {
store(it->second, storer);
} else {
store(vector<string>(), storer);
}
}
}
}
}
@ -277,6 +286,7 @@ void StickersManager::parse_sticker_set(StickerSet *sticker_set, ParserT &parser
PARSE_FLAG(is_webm);
PARSE_FLAG(is_emojis);
PARSE_FLAG(has_thumbnail_document_id);
PARSE_FLAG(sticker_set->are_keywords_loaded_);
END_PARSE_FLAGS();
int64 sticker_set_id;
int64 access_hash;
@ -367,6 +377,7 @@ void StickersManager::parse_sticker_set(StickerSet *sticker_set, ParserT &parser
if (sticker_set->was_loaded_) {
sticker_set->emoji_stickers_map_.clear();
sticker_set->sticker_emojis_map_.clear();
sticker_set->sticker_keywords_map_.clear();
}
for (uint32 i = 0; i < stored_sticker_count; i++) {
auto sticker_id = parse_sticker(true, parser);
@ -405,6 +416,13 @@ void StickersManager::parse_sticker_set(StickerSet *sticker_set, ParserT &parser
}
sticker_set->sticker_emojis_map_[sticker_id] = std::move(emojis);
}
if (sticker_set->are_keywords_loaded_) {
vector<string> keywords;
parse(keywords, parser);
if (!keywords.empty()) {
sticker_set->sticker_keywords_map_.emplace(sticker_id, std::move(keywords));
}
}
}
if (expires_at > sticker_set->expires_at_) {
sticker_set->expires_at_ = expires_at;