From 74396f65a193e56967ee65347d4c50f663ffead4 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 19 Oct 2023 15:12:26 +0300 Subject: [PATCH] Add class StickerListType. --- CMakeLists.txt | 2 + td/telegram/StickerListType.cpp | 35 +++++++++ td/telegram/StickerListType.h | 22 ++++++ td/telegram/StickersManager.cpp | 133 ++++++++++++++++---------------- td/telegram/StickersManager.h | 30 +++---- td/telegram/Td.cpp | 5 +- td/telegram/UpdatesManager.cpp | 5 +- 7 files changed, 147 insertions(+), 85 deletions(-) create mode 100644 td/telegram/StickerListType.cpp create mode 100644 td/telegram/StickerListType.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a7fc32a72..5f6c065d4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -472,6 +472,7 @@ set(TDLIB_SOURCE td/telegram/StateManager.cpp td/telegram/StatisticsManager.cpp td/telegram/StickerFormat.cpp + td/telegram/StickerListType.cpp td/telegram/StickerMaskPosition.cpp td/telegram/StickerPhotoSize.cpp td/telegram/StickerSetId.cpp @@ -783,6 +784,7 @@ set(TDLIB_SOURCE td/telegram/StateManager.h td/telegram/StatisticsManager.h td/telegram/StickerFormat.h + td/telegram/StickerListType.h td/telegram/StickerMaskPosition.h td/telegram/StickerPhotoSize.h td/telegram/StickerSetId.h diff --git a/td/telegram/StickerListType.cpp b/td/telegram/StickerListType.cpp new file mode 100644 index 000000000..e6385de1f --- /dev/null +++ b/td/telegram/StickerListType.cpp @@ -0,0 +1,35 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 +// +// 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) +// +#include "td/telegram/StickerListType.h" + +namespace td { + +string get_sticker_list_type_database_key(StickerListType sticker_list_type) { + switch (sticker_list_type) { + case StickerListType::DialogPhoto: + return "default_dialog_photo_custom_emoji_ids"; + case StickerListType::UserProfilePhoto: + return "default_profile_photo_custom_emoji_ids"; + default: + UNREACHABLE(); + return string(); + } +} + +StringBuilder &operator<<(StringBuilder &string_builder, StickerListType sticker_list_type) { + switch (sticker_list_type) { + case StickerListType::DialogPhoto: + return string_builder << "default chat photo custom emoji identifiers"; + case StickerListType::UserProfilePhoto: + return string_builder << "default user profile photo custom emoji identifiers"; + default: + UNREACHABLE(); + return string_builder; + } +} + +} // namespace td diff --git a/td/telegram/StickerListType.h b/td/telegram/StickerListType.h new file mode 100644 index 000000000..fb7dff851 --- /dev/null +++ b/td/telegram/StickerListType.h @@ -0,0 +1,22 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 +// +// 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 + +#include "td/utils/common.h" +#include "td/utils/StringBuilder.h" + +namespace td { + +enum class StickerListType : int32 { DialogPhoto, UserProfilePhoto }; + +static constexpr int32 MAX_STICKER_LIST_TYPE = 2; + +string get_sticker_list_type_database_key(StickerListType sticker_list_type); + +StringBuilder &operator<<(StringBuilder &string_builder, StickerListType sticker_list_type); + +} // namespace td diff --git a/td/telegram/StickersManager.cpp b/td/telegram/StickersManager.cpp index cb4c58abd..8dff29998 100644 --- a/td/telegram/StickersManager.cpp +++ b/td/telegram/StickersManager.cpp @@ -1446,11 +1446,17 @@ class GetDefaultDialogPhotoEmojisQuery final : public Td::ResultHandler { : promise_(std::move(promise)) { } - void send(bool for_user, int64 hash) { - if (for_user) { - send_query(G()->net_query_creator().create(telegram_api::account_getDefaultProfilePhotoEmojis(hash))); - } else { - send_query(G()->net_query_creator().create(telegram_api::account_getDefaultGroupPhotoEmojis(hash))); + void send(StickerListType sticker_list_type, int64 hash) { + switch (sticker_list_type) { + case StickerListType::DialogPhoto: + send_query(G()->net_query_creator().create(telegram_api::account_getDefaultGroupPhotoEmojis(hash))); + break; + case StickerListType::UserProfilePhoto: + send_query(G()->net_query_creator().create(telegram_api::account_getDefaultProfilePhotoEmojis(hash))); + break; + default: + UNREACHABLE(); + break; } } @@ -6223,36 +6229,31 @@ void StickersManager::on_get_custom_emoji_documents( promise.set_value(get_custom_emoji_stickers_object(custom_emoji_ids)); } -string StickersManager::get_default_dialog_photo_custom_emoji_ids_database_key(bool for_user) { - return for_user ? "default_profile_photo_custom_emoji_ids" : "default_dialog_photo_custom_emoji_ids"; -} - -void StickersManager::get_default_dialog_photo_custom_emoji_stickers( - bool for_user, bool force_reload, Promise> &&promise) { - if (are_default_dialog_photo_custom_emoji_ids_loaded_[for_user] && !force_reload) { - return get_custom_emoji_stickers_unlimited(default_dialog_photo_custom_emoji_ids_[for_user], std::move(promise)); +void StickersManager::get_default_custom_emoji_stickers(StickerListType sticker_list_type, bool force_reload, + Promise> &&promise) { + auto index = static_cast(sticker_list_type); + if (are_default_custom_emoji_ids_loaded_[index] && !force_reload) { + return get_custom_emoji_stickers_unlimited(default_custom_emoji_ids_[index], std::move(promise)); } - auto &queries = default_dialog_photo_custom_emoji_ids_load_queries_[for_user]; + auto &queries = default_custom_emoji_ids_load_queries_[index]; queries.push_back(std::move(promise)); if (queries.size() != 1) { // query has already been sent, just wait for the result return; } - if (G()->use_sqlite_pmc() && !are_default_dialog_photo_custom_emoji_ids_loaded_[for_user]) { - LOG(INFO) << "Trying to load " << (for_user ? "profile" : "chat") - << " photo custom emoji identifiers from database"; + if (G()->use_sqlite_pmc() && !are_default_custom_emoji_ids_loaded_[index]) { + LOG(INFO) << "Trying to load " << sticker_list_type << " from database"; return G()->td_db()->get_sqlite_pmc()->get( - get_default_dialog_photo_custom_emoji_ids_database_key(for_user), - PromiseCreator::lambda([for_user, force_reload](string value) { - send_closure(G()->stickers_manager(), - &StickersManager::on_load_default_dialog_photo_custom_emoji_ids_from_database, for_user, - force_reload, std::move(value)); + get_sticker_list_type_database_key(sticker_list_type), + PromiseCreator::lambda([sticker_list_type, force_reload](string value) { + send_closure(G()->stickers_manager(), &StickersManager::on_load_default_custom_emoji_ids_from_database, + sticker_list_type, force_reload, std::move(value)); })); } - reload_default_dialog_photo_custom_emoji_ids(for_user); + reload_default_custom_emoji_ids(sticker_list_type); } class StickersManager::CustomEmojiIdsLogEvent { @@ -6279,76 +6280,77 @@ class StickersManager::CustomEmojiIdsLogEvent { } }; -void StickersManager::on_load_default_dialog_photo_custom_emoji_ids_from_database(bool for_user, bool force_reload, - string value) { +void StickersManager::on_load_default_custom_emoji_ids_from_database(StickerListType sticker_list_type, + bool force_reload, string value) { if (G()->close_flag()) { - fail_promises(default_dialog_photo_custom_emoji_ids_load_queries_[for_user], Global::request_aborted_error()); + auto index = static_cast(sticker_list_type); + fail_promises(default_custom_emoji_ids_load_queries_[index], Global::request_aborted_error()); return; } if (value.empty()) { - return reload_default_dialog_photo_custom_emoji_ids(for_user); + return reload_default_custom_emoji_ids(sticker_list_type); } - LOG(INFO) << "Successfully loaded default " << (for_user ? "profile" : "chat") - << " photo custom emoji identifiers of size " << value.size() << " from database"; + LOG(INFO) << "Successfully loaded " << sticker_list_type << " of size " << value.size() << " from database"; CustomEmojiIdsLogEvent log_event; if (log_event_parse(log_event, value).is_error()) { - LOG(ERROR) << "Delete invalid default " << (for_user ? "profile" : "chat") - << " photo custom emoji identifiers from database"; - G()->td_db()->get_sqlite_pmc()->erase(get_default_dialog_photo_custom_emoji_ids_database_key(for_user), Auto()); - return reload_default_dialog_photo_custom_emoji_ids(for_user); + LOG(ERROR) << "Delete invalid " << sticker_list_type << " from database"; + G()->td_db()->get_sqlite_pmc()->erase(get_sticker_list_type_database_key(sticker_list_type), Auto()); + return reload_default_custom_emoji_ids(sticker_list_type); } - on_get_default_dialog_photo_custom_emoji_ids_success(for_user, std::move(log_event.custom_emoji_ids_), - log_event.hash_); + on_get_default_custom_emoji_ids_success(sticker_list_type, std::move(log_event.custom_emoji_ids_), log_event.hash_); if (force_reload) { - reload_default_dialog_photo_custom_emoji_ids(for_user); + reload_default_custom_emoji_ids(sticker_list_type); } } -void StickersManager::reload_default_dialog_photo_custom_emoji_ids(bool for_user) { +void StickersManager::reload_default_custom_emoji_ids(StickerListType sticker_list_type) { if (G()->close_flag()) { - fail_promises(default_dialog_photo_custom_emoji_ids_load_queries_[for_user], Global::request_aborted_error()); + auto index = static_cast(sticker_list_type); + fail_promises(default_custom_emoji_ids_load_queries_[index], Global::request_aborted_error()); return; } CHECK(!td_->auth_manager_->is_bot()); - if (are_default_dialog_photo_custom_emoji_ids_being_loaded_[for_user]) { + auto index = static_cast(sticker_list_type); + if (are_default_custom_emoji_ids_being_loaded_[index]) { return; } - are_default_dialog_photo_custom_emoji_ids_being_loaded_[for_user] = true; + are_default_custom_emoji_ids_being_loaded_[index] = true; auto query_promise = - PromiseCreator::lambda([actor_id = actor_id(this), for_user]( + PromiseCreator::lambda([actor_id = actor_id(this), sticker_list_type]( Result> r_emoji_list) mutable { - send_closure(actor_id, &StickersManager::on_get_default_dialog_photo_custom_emoji_ids, for_user, + send_closure(actor_id, &StickersManager::on_get_default_custom_emoji_ids, sticker_list_type, std::move(r_emoji_list)); }); td_->create_handler(std::move(query_promise)) - ->send(for_user, default_dialog_photo_custom_emoji_ids_hash_[for_user]); + ->send(sticker_list_type, default_custom_emoji_ids_hash_[index]); } -void StickersManager::on_get_default_dialog_photo_custom_emoji_ids( - bool for_user, Result> r_emoji_list) { +void StickersManager::on_get_default_custom_emoji_ids( + StickerListType sticker_list_type, Result> r_emoji_list) { G()->ignore_result_if_closing(r_emoji_list); - CHECK(are_default_dialog_photo_custom_emoji_ids_being_loaded_[for_user]); - are_default_dialog_photo_custom_emoji_ids_being_loaded_[for_user] = false; + auto index = static_cast(sticker_list_type); + CHECK(are_default_custom_emoji_ids_being_loaded_[index]); + are_default_custom_emoji_ids_being_loaded_[index] = false; if (r_emoji_list.is_error()) { - fail_promises(default_dialog_photo_custom_emoji_ids_load_queries_[for_user], r_emoji_list.move_as_error()); + fail_promises(default_custom_emoji_ids_load_queries_[index], r_emoji_list.move_as_error()); return; } auto emoji_list_ptr = r_emoji_list.move_as_ok(); int32 constructor_id = emoji_list_ptr->get_id(); if (constructor_id == telegram_api::emojiListNotModified::ID) { - LOG(INFO) << "Default " << (for_user ? "profile" : "chat") << " photo custom emoji identifiers aren't modified"; - if (!are_default_dialog_photo_custom_emoji_ids_loaded_[for_user]) { - on_get_default_dialog_photo_custom_emoji_ids_success(for_user, {}, 0); + LOG(INFO) << "The " << sticker_list_type << " isn't modified"; + if (!are_default_custom_emoji_ids_loaded_[index]) { + on_get_default_custom_emoji_ids_success(sticker_list_type, {}, 0); } - auto promises = std::move(default_dialog_photo_custom_emoji_ids_load_queries_[for_user]); - reset_to_empty(default_dialog_photo_custom_emoji_ids_load_queries_[for_user]); + auto promises = std::move(default_custom_emoji_ids_load_queries_[index]); + reset_to_empty(default_custom_emoji_ids_load_queries_[index]); for (auto &promise : promises) { CHECK(!promise); } @@ -6361,26 +6363,25 @@ void StickersManager::on_get_default_dialog_photo_custom_emoji_ids( if (G()->use_sqlite_pmc()) { CustomEmojiIdsLogEvent log_event(custom_emoji_ids, hash); - G()->td_db()->get_sqlite_pmc()->set(get_default_dialog_photo_custom_emoji_ids_database_key(for_user), + G()->td_db()->get_sqlite_pmc()->set(get_sticker_list_type_database_key(sticker_list_type), log_event_store(log_event).as_slice().str(), Auto()); } - on_get_default_dialog_photo_custom_emoji_ids_success(for_user, std::move(custom_emoji_ids), hash); + on_get_default_custom_emoji_ids_success(sticker_list_type, std::move(custom_emoji_ids), hash); } -void StickersManager::on_get_default_dialog_photo_custom_emoji_ids_success(bool for_user, - vector custom_emoji_ids, - int64 hash) { - LOG(INFO) << "Load " << custom_emoji_ids.size() << " default " << (for_user ? "profile" : "chat") - << " photo custom emoji identifiers"; - default_dialog_photo_custom_emoji_ids_[for_user] = std::move(custom_emoji_ids); - default_dialog_photo_custom_emoji_ids_hash_[for_user] = hash; - are_default_dialog_photo_custom_emoji_ids_loaded_[for_user] = true; +void StickersManager::on_get_default_custom_emoji_ids_success(StickerListType sticker_list_type, + vector custom_emoji_ids, int64 hash) { + auto index = static_cast(sticker_list_type); + LOG(INFO) << "Load " << custom_emoji_ids.size() << ' ' << sticker_list_type; + default_custom_emoji_ids_[index] = std::move(custom_emoji_ids); + default_custom_emoji_ids_hash_[index] = hash; + are_default_custom_emoji_ids_loaded_[index] = true; - auto promises = std::move(default_dialog_photo_custom_emoji_ids_load_queries_[for_user]); - reset_to_empty(default_dialog_photo_custom_emoji_ids_load_queries_[for_user]); + auto promises = std::move(default_custom_emoji_ids_load_queries_[index]); + reset_to_empty(default_custom_emoji_ids_load_queries_[index]); for (auto &promise : promises) { - get_custom_emoji_stickers_unlimited(default_dialog_photo_custom_emoji_ids_[for_user], std::move(promise)); + get_custom_emoji_stickers_unlimited(default_custom_emoji_ids_[index], std::move(promise)); } } diff --git a/td/telegram/StickersManager.h b/td/telegram/StickersManager.h index 8ace863b6..e0f597196 100644 --- a/td/telegram/StickersManager.h +++ b/td/telegram/StickersManager.h @@ -19,6 +19,7 @@ #include "td/telegram/SecretInputMedia.h" #include "td/telegram/SpecialStickerSetType.h" #include "td/telegram/StickerFormat.h" +#include "td/telegram/StickerListType.h" #include "td/telegram/StickerMaskPosition.h" #include "td/telegram/StickerSetId.h" #include "td/telegram/StickerType.h" @@ -134,8 +135,8 @@ class StickersManager final : public Actor { void get_custom_emoji_stickers(vector custom_emoji_ids, bool use_database, Promise> &&promise); - void get_default_dialog_photo_custom_emoji_stickers(bool for_user, bool force_reload, - Promise> &&promise); + void get_default_custom_emoji_stickers(StickerListType sticker_list_type, bool force_reload, + Promise> &&promise); void get_premium_gift_option_sticker(int32 month_count, bool is_recursive, Promise> &&promise); @@ -617,15 +618,16 @@ class StickersManager final : public Actor { void on_load_custom_emoji_from_database(CustomEmojiId custom_emoji_id, string value); - void on_load_default_dialog_photo_custom_emoji_ids_from_database(bool for_user, bool force_reload, string value); + void on_load_default_custom_emoji_ids_from_database(StickerListType sticker_list_type, bool force_reload, + string value); - void reload_default_dialog_photo_custom_emoji_ids(bool for_user); + void reload_default_custom_emoji_ids(StickerListType sticker_list_type); - void on_get_default_dialog_photo_custom_emoji_ids( - bool for_user, Result> r_emoji_list); + void on_get_default_custom_emoji_ids(StickerListType sticker_list_type, + Result> r_emoji_list); - void on_get_default_dialog_photo_custom_emoji_ids_success(bool for_user, vector custom_emoji_ids, - int64 hash); + void on_get_default_custom_emoji_ids_success(StickerListType sticker_list_type, + vector custom_emoji_ids, int64 hash); FileId on_get_sticker(unique_ptr new_sticker, bool replace); @@ -908,8 +910,6 @@ class StickersManager final : public Actor { static string get_emoji_language_codes_database_key(const vector &language_codes); - 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); @@ -1122,11 +1122,11 @@ class StickersManager final : public Actor { EmojiGroupList emoji_group_list_[MAX_EMOJI_GROUP_TYPE]; vector>> emoji_group_load_queries_[MAX_EMOJI_GROUP_TYPE]; - vector default_dialog_photo_custom_emoji_ids_[2]; - int64 default_dialog_photo_custom_emoji_ids_hash_[2] = {0, 0}; - vector>> default_dialog_photo_custom_emoji_ids_load_queries_[2]; - bool are_default_dialog_photo_custom_emoji_ids_loaded_[2] = {false, false}; - bool are_default_dialog_photo_custom_emoji_ids_being_loaded_[2] = {false, false}; + vector default_custom_emoji_ids_[MAX_STICKER_LIST_TYPE]; + int64 default_custom_emoji_ids_hash_[MAX_STICKER_LIST_TYPE] = {0, 0}; + vector>> default_custom_emoji_ids_load_queries_[MAX_STICKER_LIST_TYPE]; + bool are_default_custom_emoji_ids_loaded_[MAX_STICKER_LIST_TYPE] = {false, false}; + bool are_default_custom_emoji_ids_being_loaded_[MAX_STICKER_LIST_TYPE] = {false, false}; WaitFreeHashMap custom_emoji_to_sticker_id_; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 562bd7bd1..2d1afd486 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -123,6 +123,7 @@ #include "td/telegram/StateManager.h" #include "td/telegram/StatisticsManager.h" #include "td/telegram/StickerFormat.h" +#include "td/telegram/StickerListType.h" #include "td/telegram/StickerSetId.h" #include "td/telegram/StickersManager.h" #include "td/telegram/StickerType.h" @@ -7977,13 +7978,13 @@ void Td::on_request(uint64 id, const td_api::getCustomEmojiStickers &request) { void Td::on_request(uint64 id, const td_api::getDefaultChatPhotoCustomEmojiStickers &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); - stickers_manager_->get_default_dialog_photo_custom_emoji_stickers(false, false, std::move(promise)); + stickers_manager_->get_default_custom_emoji_stickers(StickerListType::DialogPhoto, false, std::move(promise)); } void Td::on_request(uint64 id, const td_api::getDefaultProfilePhotoCustomEmojiStickers &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); - stickers_manager_->get_default_dialog_photo_custom_emoji_stickers(true, false, std::move(promise)); + stickers_manager_->get_default_custom_emoji_stickers(StickerListType::UserProfilePhoto, false, std::move(promise)); } void Td::on_request(uint64 id, const td_api::getSavedAnimations &request) { diff --git a/td/telegram/UpdatesManager.cpp b/td/telegram/UpdatesManager.cpp index 26b5596a2..98269e9f7 100644 --- a/td/telegram/UpdatesManager.cpp +++ b/td/telegram/UpdatesManager.cpp @@ -54,6 +54,7 @@ #include "td/telegram/ServerMessageId.h" #include "td/telegram/SpecialStickerSetType.h" #include "td/telegram/StateManager.h" +#include "td/telegram/StickerListType.h" #include "td/telegram/StickerSetId.h" #include "td/telegram/StickersManager.h" #include "td/telegram/StickerType.h" @@ -2265,8 +2266,8 @@ void UpdatesManager::try_reload_data() { td_->stickers_manager_->reload_special_sticker_set_by_type(SpecialStickerSetType::generic_animations()); td_->stickers_manager_->reload_special_sticker_set_by_type(SpecialStickerSetType::default_statuses()); td_->stickers_manager_->reload_special_sticker_set_by_type(SpecialStickerSetType::default_topic_icons()); - td_->stickers_manager_->get_default_dialog_photo_custom_emoji_stickers(false, true, Auto()); - td_->stickers_manager_->get_default_dialog_photo_custom_emoji_stickers(true, true, Auto()); + td_->stickers_manager_->get_default_custom_emoji_stickers(StickerListType::DialogPhoto, true, Auto()); + td_->stickers_manager_->get_default_custom_emoji_stickers(StickerListType::UserProfilePhoto, true, Auto()); td_->story_manager_->reload_active_stories(); td_->story_manager_->reload_all_read_stories();