diff --git a/td/telegram/SpecialStickerSetType.cpp b/td/telegram/SpecialStickerSetType.cpp index 42ffbc50a..2804127ea 100644 --- a/td/telegram/SpecialStickerSetType.cpp +++ b/td/telegram/SpecialStickerSetType.cpp @@ -12,17 +12,17 @@ namespace td { -string SpecialStickerSetType::animated_emoji() { - return "animated_emoji_sticker_set"; +SpecialStickerSetType SpecialStickerSetType::animated_emoji() { + return SpecialStickerSetType("animated_emoji_sticker_set"); } -string SpecialStickerSetType::animated_emoji_click() { - return "animated_emoji_click_sticker_set"; +SpecialStickerSetType SpecialStickerSetType::animated_emoji_click() { + return SpecialStickerSetType("animated_emoji_click_sticker_set"); } -string SpecialStickerSetType::animated_dice(const string &emoji) { +SpecialStickerSetType SpecialStickerSetType::animated_dice(const string &emoji) { CHECK(!emoji.empty()); - return PSTRING() << "animated_dice_sticker_set#" << emoji; + return SpecialStickerSetType(PSTRING() << "animated_dice_sticker_set#" << emoji); } SpecialStickerSetType::SpecialStickerSetType( @@ -30,13 +30,13 @@ SpecialStickerSetType::SpecialStickerSetType( CHECK(input_sticker_set != nullptr); switch (input_sticker_set->get_id()) { case telegram_api::inputStickerSetAnimatedEmoji::ID: - type_ = animated_emoji(); + *this = animated_emoji(); break; case telegram_api::inputStickerSetAnimatedEmojiAnimations::ID: - type_ = animated_emoji_click(); + *this = animated_emoji_click(); break; case telegram_api::inputStickerSetDice::ID: - type_ = animated_dice(static_cast(input_sticker_set.get())->emoticon_); + *this = animated_dice(static_cast(input_sticker_set.get())->emoticon_); break; default: UNREACHABLE(); @@ -45,17 +45,18 @@ SpecialStickerSetType::SpecialStickerSetType( } string SpecialStickerSetType::get_dice_emoji() const { - if (begins_with(type_, "animated_dice_sticker_set#")) { - return type_.substr(Slice("animated_dice_sticker_set#").size()); + auto prefix = Slice("animated_dice_sticker_set#"); + if (begins_with(type_, prefix)) { + return type_.substr(prefix.size()); } return string(); } telegram_api::object_ptr SpecialStickerSetType::get_input_sticker_set() const { - if (type_ == animated_emoji()) { + if (*this == animated_emoji()) { return telegram_api::make_object(); } - if (type_ == animated_emoji_click()) { + if (*this == animated_emoji_click()) { return telegram_api::make_object(); } auto emoji = get_dice_emoji(); diff --git a/td/telegram/SpecialStickerSetType.h b/td/telegram/SpecialStickerSetType.h index 5a25c0188..fd0d2a53d 100644 --- a/td/telegram/SpecialStickerSetType.h +++ b/td/telegram/SpecialStickerSetType.h @@ -10,19 +10,31 @@ #include "td/utils/common.h" +#include + namespace td { -struct SpecialStickerSetType { +class SpecialStickerSetType { + explicit SpecialStickerSetType(string type) : type_(type) { + } + + friend struct SpecialStickerSetTypeHash; + + public: string type_; - static string animated_emoji(); + static SpecialStickerSetType animated_emoji(); - static string animated_emoji_click(); + static SpecialStickerSetType animated_emoji_click(); - static string animated_dice(const string &emoji); + static SpecialStickerSetType animated_dice(const string &emoji); string get_dice_emoji() const; + bool is_empty() const { + return type_.empty(); + } + SpecialStickerSetType() = default; explicit SpecialStickerSetType(const telegram_api::object_ptr &input_sticker_set); @@ -30,4 +42,18 @@ struct SpecialStickerSetType { telegram_api::object_ptr get_input_sticker_set() const; }; +inline bool operator==(const SpecialStickerSetType &lhs, const SpecialStickerSetType &rhs) { + return lhs.type_ == rhs.type_; +} + +inline bool operator!=(const SpecialStickerSetType &lhs, const SpecialStickerSetType &rhs) { + return !(lhs == rhs); +} + +struct SpecialStickerSetTypeHash { + std::size_t operator()(SpecialStickerSetType type) const { + return std::hash()(type.type_); + } +}; + } // namespace td diff --git a/td/telegram/StickersManager.cpp b/td/telegram/StickersManager.cpp index 94884c77f..d9d0c8e03 100644 --- a/td/telegram/StickersManager.cpp +++ b/td/telegram/StickersManager.cpp @@ -1313,12 +1313,12 @@ void StickersManager::init() { G()->shared_config().set_option_empty("animated_dice_sticker_set_name"); // legacy } -StickersManager::SpecialStickerSet &StickersManager::add_special_sticker_set(const string &type) { +StickersManager::SpecialStickerSet &StickersManager::add_special_sticker_set(const SpecialStickerSetType &type) { auto &result = special_sticker_sets_[type]; - if (result.type_.type_.empty()) { - result.type_.type_ = type; + if (result.type_.is_empty()) { + result.type_ = type; } else { - CHECK(result.type_.type_ == type); + CHECK(result.type_ == type); } return result; } @@ -1367,7 +1367,7 @@ void StickersManager::load_special_sticker_set_by_type(const SpecialStickerSetTy return; } - auto &sticker_set = add_special_sticker_set(type.type_); + auto &sticker_set = add_special_sticker_set(type); CHECK(sticker_set.is_being_loaded_); sticker_set.is_being_loaded_ = false; load_special_sticker_set(sticker_set); @@ -1407,7 +1407,7 @@ void StickersManager::on_load_special_sticker_set(const SpecialStickerSetType &t return; } - auto &special_sticker_set = add_special_sticker_set(type.type_); + auto &special_sticker_set = add_special_sticker_set(type); if (!special_sticker_set.is_being_loaded_) { return; } @@ -1424,7 +1424,7 @@ void StickersManager::on_load_special_sticker_set(const SpecialStickerSetType &t special_sticker_set.is_being_loaded_ = false; - if (type.type_ == SpecialStickerSetType::animated_emoji()) { + if (type == SpecialStickerSetType::animated_emoji()) { auto promises = std::move(pending_get_animated_emoji_queries_); reset_to_empty(pending_get_animated_emoji_queries_); for (auto &promise : promises) { @@ -1438,7 +1438,7 @@ void StickersManager::on_load_special_sticker_set(const SpecialStickerSetType &t CHECK(sticker_set != nullptr); CHECK(sticker_set->was_loaded); - if (type.type_ == SpecialStickerSetType::animated_emoji_click()) { + if (type == SpecialStickerSetType::animated_emoji_click()) { auto pending_get_requests = std::move(pending_get_animated_emoji_click_stickers_); reset_to_empty(pending_get_animated_emoji_click_stickers_); for (auto &pending_request : pending_get_requests) { @@ -2222,7 +2222,7 @@ StickerSetId StickersManager::get_sticker_set_id(const tl_object_ptraccess_hash << ' ' << s->short_name; - auto &sticker_set = add_special_sticker_set(type.type_); + auto &sticker_set = add_special_sticker_set(type); if (sticker_set_id == sticker_set.id_ && s->access_hash == sticker_set.access_hash_ && s->short_name == sticker_set.short_name_ && !s->short_name.empty()) { on_load_special_sticker_set(type, Status::OK()); @@ -3031,7 +3031,7 @@ void StickersManager::on_get_special_sticker_set(const SpecialStickerSetType &ty G()->td_db()->get_binlog_pmc()->set(type.type_, PSTRING() << sticker_set.id_.get() << ' ' << sticker_set.access_hash_ << ' ' << sticker_set.short_name_); - if (type.type_ == SpecialStickerSetType::animated_emoji()) { + if (type == SpecialStickerSetType::animated_emoji()) { G()->shared_config().set_option_string(PSLICE() << type.type_ << "_name", sticker_set.short_name_); try_update_animated_emoji_messages(); } else if (!type.get_dice_emoji().empty()) { diff --git a/td/telegram/StickersManager.h b/td/telegram/StickersManager.h index 737d5b647..e9ee7012a 100644 --- a/td/telegram/StickersManager.h +++ b/td/telegram/StickersManager.h @@ -654,7 +654,7 @@ class StickersManager final : public Actor { void tear_down() final; - SpecialStickerSet &add_special_sticker_set(const string &type); + SpecialStickerSet &add_special_sticker_set(const SpecialStickerSetType &type); static void init_special_sticker_set(SpecialStickerSet &sticker_set, int64 sticker_set_id, int64 access_hash, string name); @@ -782,7 +782,7 @@ class StickersManager final : public Actor { int32 recent_stickers_limit_ = 200; int32 favorite_stickers_limit_ = 5; - std::unordered_map special_sticker_sets_; + std::unordered_map special_sticker_sets_; struct StickerSetLoadRequest { Promise promise;