Add helpers for sticker search.

This commit is contained in:
levlam 2022-09-27 18:29:10 +03:00
parent ee04923ce1
commit 4e78b4b65f
2 changed files with 41 additions and 26 deletions

View File

@ -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<string, vector<FileId>> &StickersManager::get_sticker_set_keywords(StickerSet *sticker_set) {
const std::map<string, vector<FileId>> &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<string, vector<FileId>> &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<FileId> &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<FileId>, vector<FileId>> StickersManager::split_stickers_by_premium(
const vector<FileId> &sticker_ids) const {
CHECK(!td_->auth_manager_->is_bot());
@ -4420,11 +4449,7 @@ vector<FileId> 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<FileId> sorted;
@ -4445,24 +4470,9 @@ vector<FileId> 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) {

View File

@ -458,7 +458,7 @@ class StickersManager final : public Actor {
vector<int32> premium_sticker_positions_;
FlatHashMap<string, vector<FileId>> emoji_stickers_map_; // emoji -> stickers
FlatHashMap<FileId, vector<string>, FileIdHash> sticker_emojis_map_; // sticker -> emojis
std::map<string, vector<FileId>> keyword_stickers_map_; // keyword -> stickers
mutable std::map<string, vector<FileId>> keyword_stickers_map_; // keyword -> stickers
FlatHashMap<FileId, vector<string>, 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<Sticker> 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<int64> &&document_ids,
Promise<td_api::object_ptr<td_api::stickers>> &&promise);
static const std::map<string, vector<FileId>> &get_sticker_set_keywords(StickerSet *sticker_set);
static const std::map<string, vector<FileId>> &get_sticker_set_keywords(const StickerSet *sticker_set);
static void find_sticker_set_stickers(const StickerSet *sticker_set, const string &query, vector<FileId> &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);