From 30cdbe69b6d921ed78f3d63e10cf09ea5231ff47 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 25 Jan 2023 16:23:34 +0300 Subject: [PATCH] Store emoji groups in the database. --- td/telegram/EmojiGroup.cpp | 2 +- td/telegram/EmojiGroup.h | 2 +- td/telegram/StickersManager.cpp | 77 +++++++++++++++++++++++++++++---- td/telegram/StickersManager.h | 6 +++ 4 files changed, 77 insertions(+), 10 deletions(-) diff --git a/td/telegram/EmojiGroup.cpp b/td/telegram/EmojiGroup.cpp index e7d501de2..e2ac6b9f5 100644 --- a/td/telegram/EmojiGroup.cpp +++ b/td/telegram/EmojiGroup.cpp @@ -21,7 +21,7 @@ td_api::object_ptr EmojiGroup::get_emoji_category_object( return td_api::make_object(title_, icon_custom_emoji_id_.get(), vector(emojis_)); } -EmojiGroupList::EmojiGroupList(string &&used_language_codes, int32 hash, +EmojiGroupList::EmojiGroupList(string used_language_codes, int32 hash, vector> &&emoji_groups) : used_language_codes_(std::move(used_language_codes)) , hash_(hash) diff --git a/td/telegram/EmojiGroup.h b/td/telegram/EmojiGroup.h index c93f47a40..2ee02ae35 100644 --- a/td/telegram/EmojiGroup.h +++ b/td/telegram/EmojiGroup.h @@ -42,7 +42,7 @@ class EmojiGroupList { public: EmojiGroupList() = default; - EmojiGroupList(string &&used_language_codes, int32 hash, + EmojiGroupList(string used_language_codes, int32 hash, vector> &&emoji_groups); td_api::object_ptr get_emoji_categories_object() const; diff --git a/td/telegram/StickersManager.cpp b/td/telegram/StickersManager.cpp index d156fff05..8fd431410 100644 --- a/td/telegram/StickersManager.cpp +++ b/td/telegram/StickersManager.cpp @@ -13,6 +13,7 @@ #include "td/telegram/DialogId.h" #include "td/telegram/Document.h" #include "td/telegram/DocumentsManager.h" +#include "td/telegram/EmojiGroup.hpp" #include "td/telegram/FileReferenceManager.h" #include "td/telegram/files/FileLocation.h" #include "td/telegram/files/FileManager.h" @@ -5704,7 +5705,7 @@ void StickersManager::do_reload_sticker_set(StickerSetId sticker_set_id, } void StickersManager::on_reload_sticker_set(StickerSetId sticker_set_id, Result &&result) { - if (G()->close_flag()) { + if (G()->close_flag() && result.is_ok()) { result = Global::request_aborted_error(); } LOG(INFO) << "Reloaded " << sticker_set_id; @@ -8229,7 +8230,7 @@ void StickersManager::on_uploaded_sticker_file(FileId file_id, tl_object_ptr result) { - if (G()->close_flag()) { + if (G()->close_flag() && result.is_ok()) { result = Global::request_aborted_error(); } @@ -8340,7 +8341,7 @@ void StickersManager::do_add_sticker_to_set(UserId user_id, string short_name, } void StickersManager::on_added_sticker_uploaded(int64 random_id, Result result) { - if (G()->close_flag()) { + if (G()->close_flag() && result.is_ok()) { result = Global::request_aborted_error(); } @@ -8441,7 +8442,7 @@ void StickersManager::do_set_sticker_set_thumbnail(UserId user_id, string short_ } void StickersManager::on_sticker_set_thumbnail_uploaded(int64 random_id, Result result) { - if (G()->close_flag()) { + if (G()->close_flag() && result.is_ok()) { result = Global::request_aborted_error(); } @@ -9774,7 +9775,7 @@ void StickersManager::load_emoji_keywords_difference(const string &language_code void StickersManager::on_get_emoji_keywords_difference( const string &language_code, int32 from_version, Result> &&result) { - if (G()->close_flag()) { + if (G()->close_flag() && result.is_ok()) { result = Global::request_aborted_error(); } if (result.is_error()) { @@ -9967,6 +9968,10 @@ td_api::object_ptr StickersManager::get_emoji_suggestions_url_r return result; } +string StickersManager::get_emoji_groups_database_key(EmojiGroupType group_type) { + return PSTRING() << "emojigroup" << static_cast(group_type); +} + void StickersManager::get_emoji_groups(EmojiGroupType group_type, Promise> &&promise) { auto type = static_cast(group_type); @@ -9986,6 +9991,55 @@ void StickersManager::get_emoji_groups(EmojiGroupType group_type, return; } + if (G()->parameters().use_file_db /* have SQLite PMC */) { + G()->td_db()->get_sqlite_pmc()->get( + get_emoji_groups_database_key(group_type), + PromiseCreator::lambda( + [group_type, used_language_codes = std::move(used_language_codes)](string value) mutable { + send_closure(G()->stickers_manager(), &StickersManager::on_load_emoji_groups_from_database, group_type, + std::move(used_language_codes), std::move(value)); + })); + } else { + reload_emoji_groups(group_type, std::move(used_language_codes)); + } +} + +void StickersManager::on_load_emoji_groups_from_database(EmojiGroupType group_type, string used_language_codes, + string value) { + if (G()->close_flag()) { + return on_get_emoji_groups(group_type, std::move(used_language_codes), G()->close_status()); + } + if (value.empty()) { + LOG(INFO) << "Emoji groups of type " << group_type << " aren't found in database"; + return reload_emoji_groups(group_type, std::move(used_language_codes)); + } + + LOG(INFO) << "Successfully loaded emoji groups of type " << group_type << " from database"; + + auto type = static_cast(group_type); + auto status = log_event_parse(emoji_group_list_[type], value); + if (status.is_error()) { + LOG(ERROR) << "Can't load emoji groups: " << status; + emoji_group_list_[type] = {}; + return reload_emoji_groups(group_type, std::move(used_language_codes)); + } + + if (emoji_group_list_[type].get_used_language_codes() != used_language_codes) { + return reload_emoji_groups(group_type, std::move(used_language_codes)); + } + + auto promises = std::move(emoji_group_load_queries_[type]); + reset_to_empty(emoji_group_load_queries_[type]); + for (auto &promise : promises) { + promise.set_value(emoji_group_list_[type].get_emoji_categories_object()); + } +} + +void StickersManager::reload_emoji_groups(EmojiGroupType group_type, string used_language_codes) { + auto type = static_cast(group_type); + if (used_language_codes.empty()) { + used_language_codes = implode(get_used_language_codes({}, Slice()), '$'); + } auto query_promise = PromiseCreator::lambda( [actor_id = actor_id(this), group_type, used_language_codes = std::move(used_language_codes)]( Result> r_emoji_groups) { @@ -10000,7 +10054,7 @@ void StickersManager::on_get_emoji_groups( EmojiGroupType group_type, string used_language_codes, Result> r_emoji_groups) { auto type = static_cast(group_type); - if (G()->close_flag()) { + if (G()->close_flag() && r_emoji_groups.is_ok()) { r_emoji_groups = Global::request_aborted_error(); } if (r_emoji_groups.is_error()) { @@ -10018,18 +10072,25 @@ void StickersManager::on_get_emoji_groups( auto emoji_groups = r_emoji_groups.move_as_ok(); switch (emoji_groups->get_id()) { case telegram_api::messages_emojiGroupsNotModified::ID: - emoji_group_list_[type].update_next_reload_time(); + if (!used_language_codes.empty()) { + emoji_group_list_[type].update_next_reload_time(); + } break; case telegram_api::messages_emojiGroups::ID: { auto groups = telegram_api::move_object_as(emoji_groups); emoji_group_list_[type] = - EmojiGroupList(std::move(used_language_codes), groups->hash_, std::move(groups->groups_)); + EmojiGroupList(used_language_codes, groups->hash_, std::move(groups->groups_)); break; } default: UNREACHABLE(); } + if (!used_language_codes.empty() && G()->parameters().use_file_db /* have SQLite PMC */) { + G()->td_db()->get_sqlite_pmc()->set(get_emoji_groups_database_key(group_type), + log_event_store(emoji_group_list_[type]).as_slice().str(), Auto()); + } + auto promises = std::move(emoji_group_load_queries_[type]); reset_to_empty(emoji_group_load_queries_[type]); for (auto &promise : promises) { diff --git a/td/telegram/StickersManager.h b/td/telegram/StickersManager.h index 430bc3c4d..cddb28f1a 100644 --- a/td/telegram/StickersManager.h +++ b/td/telegram/StickersManager.h @@ -971,6 +971,8 @@ class StickersManager final : public Actor { static string get_default_dialog_photo_custom_emoji_ids_database_key(bool for_user); + static string get_emoji_groups_database_key(EmojiGroupType group_type); + int32 get_emoji_language_code_version(const string &language_code); double get_emoji_language_code_last_difference_time(const string &language_code); @@ -1002,6 +1004,10 @@ class StickersManager final : public Actor { void on_get_emoji_suggestions_url(int64 random_id, Promise &&promise, Result> &&r_emoji_url); + void on_load_emoji_groups_from_database(EmojiGroupType group_type, string used_language_codes, string value); + + void reload_emoji_groups(EmojiGroupType group_type, string used_language_codes); + void on_get_emoji_groups(EmojiGroupType group_type, string used_language_codes, Result> r_emoji_groups);