diff --git a/td/telegram/StickersManager.cpp b/td/telegram/StickersManager.cpp index 57c855a67..e4a516dd8 100644 --- a/td/telegram/StickersManager.cpp +++ b/td/telegram/StickersManager.cpp @@ -4186,7 +4186,7 @@ void StickersManager::on_get_installed_sticker_sets_failed(StickerType sticker_t fail_promises(load_installed_sticker_sets_queries_[type], std::move(error)); } -const std::map> &StickersManager::get_sticker_set_keywords(StickerSet *sticker_set) { +const std::map> &StickersManager::get_sticker_set_keywords(const StickerSet *sticker_set) { if (sticker_set->keyword_stickers_map_.empty()) { for (auto &sticker_id_keywords : sticker_set->sticker_keywords_map_) { for (auto &keyword : Hints::fix_words(transform(sticker_id_keywords.second, utf8_prepare_search_string))) { @@ -4198,6 +4198,35 @@ const std::map> &StickersManager::get_sticker_set_keyword return sticker_set->keyword_stickers_map_; } +void StickersManager::find_sticker_set_stickers(const StickerSet *sticker_set, const string &query, + vector &result) { + auto it = sticker_set->emoji_stickers_map_.find(query); + if (it != sticker_set->emoji_stickers_map_.end()) { + LOG(INFO) << "Add " << it->second << " stickers from " << sticker_set->id_; + append(result, it->second); + } +} + +bool StickersManager::can_found_sticker_by_query(FileId sticker_id, const string &query) const { + const Sticker *s = get_sticker(sticker_id); + CHECK(s != nullptr); + if (remove_emoji_modifiers(s->alt_) == query) { + // fast path + return true; + } + const StickerSet *sticker_set = get_sticker_set(s->set_id_); + if (sticker_set == nullptr || !sticker_set->was_loaded_) { + return false; + } + auto map_it = sticker_set->emoji_stickers_map_.find(query); + if (map_it != sticker_set->emoji_stickers_map_.end()) { + if (td::contains(map_it->second, sticker_id)) { + return true; + } + } + return false; +} + std::pair, vector> StickersManager::split_stickers_by_premium( const vector &sticker_ids) const { CHECK(!td_->auth_manager_->is_bot()); @@ -4420,11 +4449,7 @@ vector StickersManager::get_stickers(StickerType sticker_type, string em return is_sticker_format_animated(lhs->sticker_format_) && !is_sticker_format_animated(rhs->sticker_format_); }); for (auto sticker_set : examined_sticker_sets) { - auto it = sticker_set->emoji_stickers_map_.find(emoji); - if (it != sticker_set->emoji_stickers_map_.end()) { - LOG(INFO) << "Add " << it->second << " stickers from " << sticker_set->id_; - append(result, it->second); - } + find_sticker_set_stickers(sticker_set, emoji, result); } vector sorted; @@ -4445,24 +4470,9 @@ vector StickersManager::get_stickers(StickerType sticker_type, string em << (it - result.begin()); *it = FileId(); is_good = true; - } else { - const Sticker *s = get_sticker(sticker_id); - CHECK(s != nullptr); - if (remove_emoji_modifiers(s->alt_) == emoji) { - LOG(INFO) << "Found prepend sticker " << sticker_id << " main emoji matches"; - is_good = true; - } else if (s->set_id_.is_valid()) { - const StickerSet *sticker_set = get_sticker_set(s->set_id_); - if (sticker_set != nullptr && sticker_set->was_loaded_) { - auto map_it = sticker_set->emoji_stickers_map_.find(emoji); - if (map_it != sticker_set->emoji_stickers_map_.end()) { - if (td::contains(map_it->second, sticker_id)) { - LOG(INFO) << "Found prepend sticker " << sticker_id << " has matching emoji"; - is_good = true; - } - } - } - } + } else if (can_found_sticker_by_query(sticker_id, emoji)) { + LOG(INFO) << "Found prepend sticker " << sticker_id << " has matching emoji"; + is_good = true; } if (is_good) { diff --git a/td/telegram/StickersManager.h b/td/telegram/StickersManager.h index 7bff0915d..719f3e2c0 100644 --- a/td/telegram/StickersManager.h +++ b/td/telegram/StickersManager.h @@ -458,7 +458,7 @@ class StickersManager final : public Actor { vector premium_sticker_positions_; FlatHashMap> emoji_stickers_map_; // emoji -> stickers FlatHashMap, FileIdHash> sticker_emojis_map_; // sticker -> emojis - std::map> keyword_stickers_map_; // keyword -> stickers + mutable std::map> keyword_stickers_map_; // keyword -> stickers FlatHashMap, FileIdHash> sticker_keywords_map_; // sticker -> keywords bool is_installed_ = false; @@ -625,6 +625,7 @@ class StickersManager final : public Actor { FileId on_get_sticker(unique_ptr new_sticker, bool replace); StickerSet *get_sticker_set(StickerSetId sticker_set_id); + const StickerSet *get_sticker_set(StickerSetId sticker_set_id) const; StickerSet *add_sticker_set(StickerSetId sticker_set_id, int64 access_hash); @@ -900,7 +901,11 @@ class StickersManager final : public Actor { vector &&document_ids, Promise> &&promise); - static const std::map> &get_sticker_set_keywords(StickerSet *sticker_set); + static const std::map> &get_sticker_set_keywords(const StickerSet *sticker_set); + + static void find_sticker_set_stickers(const StickerSet *sticker_set, const string &query, vector &result); + + bool can_found_sticker_by_query(FileId sticker_id, const string &query) const; static string get_emoji_language_code_version_database_key(const string &language_code);