diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index cd1ec4447..b04afeecd 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -3817,6 +3817,9 @@ void register_message_content(Td *td, const MessageContent *content, FullMessage auto dice = static_cast(content); return td->stickers_manager_->register_dice(dice->emoji, dice->dice_value, full_message_id, source); } + case MessageContentType::GiftPremium: + return td->stickers_manager_->register_gift_premium(static_cast(content)->months, + full_message_id, source); default: return; } @@ -3858,6 +3861,12 @@ void reregister_message_content(Td *td, const MessageContent *old_content, const return; } break; + case MessageContentType::GiftPremium: + if (static_cast(old_content)->months == + static_cast(new_content)->months) { + return; + } + break; default: return; } @@ -3888,6 +3897,9 @@ void unregister_message_content(Td *td, const MessageContent *content, FullMessa auto dice = static_cast(content); return td->stickers_manager_->unregister_dice(dice->emoji, dice->dice_value, full_message_id, source); } + case MessageContentType::GiftPremium: + return td->stickers_manager_->unregister_gift_premium(static_cast(content)->months, + full_message_id, source); default: return; } diff --git a/td/telegram/StickersManager.cpp b/td/telegram/StickersManager.cpp index faef4c68e..2604a009f 100644 --- a/td/telegram/StickersManager.cpp +++ b/td/telegram/StickersManager.cpp @@ -1367,10 +1367,11 @@ StickersManager::StickersManager(Td *td, ActorShared<> parent) : td_(td), parent } StickersManager::~StickersManager() { - Scheduler::instance()->destroy_on_scheduler( - G()->get_gc_scheduler_id(), stickers_, sticker_sets_, short_name_to_sticker_set_id_, attached_sticker_sets_, - found_stickers_, found_sticker_sets_, emoji_language_codes_, emoji_language_code_versions_, - emoji_language_code_last_difference_times_, reloaded_emoji_keywords_, dice_messages_, emoji_messages_); + Scheduler::instance()->destroy_on_scheduler(G()->get_gc_scheduler_id(), stickers_, sticker_sets_, + short_name_to_sticker_set_id_, attached_sticker_sets_, found_stickers_, + found_sticker_sets_, emoji_language_codes_, emoji_language_code_versions_, + emoji_language_code_last_difference_times_, reloaded_emoji_keywords_, + gift_premium_messages_, dice_messages_, emoji_messages_); } void StickersManager::start_up() { @@ -4799,6 +4800,50 @@ void StickersManager::try_update_animated_emoji_messages() { } } +void StickersManager::register_gift_premium(int32 months, FullMessageId full_message_id, const char *source) { + if (td_->auth_manager_->is_bot() || months == 0) { + return; + } + + LOG(INFO) << "Register premium gift for " << months << " months from " << full_message_id << " from " << source; + bool is_inserted = gift_premium_messages_[months].insert(full_message_id).second; + LOG_CHECK(is_inserted) << source << " " << months << " " << full_message_id; + + auto &special_sticker_set = add_special_sticker_set(SpecialStickerSetType::premium_gifts()); + bool need_load = false; + StickerSet *sticker_set = nullptr; + if (!special_sticker_set.id_.is_valid()) { + need_load = true; + } else { + sticker_set = get_sticker_set(special_sticker_set.id_); + CHECK(sticker_set != nullptr); + need_load = !sticker_set->was_loaded; + } + + if (need_load) { + LOG(INFO) << "Waiting for a premium gifts sticker set needed in " << full_message_id; + load_special_sticker_set(special_sticker_set); + } else { + // TODO reload once in a while + // reload_special_sticker_set(special_sticker_set, sticker_set->is_loaded ? sticker_set->hash : 0); + } +} + +void StickersManager::unregister_gift_premium(int32 months, FullMessageId full_message_id, const char *source) { + if (td_->auth_manager_->is_bot() || months == 0) { + return; + } + + LOG(INFO) << "Unregister premium gift for " << months << " months from " << full_message_id << " from " << source; + auto &message_ids = gift_premium_messages_[months]; + auto is_deleted = message_ids.erase(full_message_id) > 0; + LOG_CHECK(is_deleted) << source << " " << months << " " << full_message_id; + + if (message_ids.empty()) { + gift_premium_messages_.erase(months); + } +} + void StickersManager::register_dice(const string &emoji, int32 value, FullMessageId full_message_id, const char *source) { CHECK(!emoji.empty()); diff --git a/td/telegram/StickersManager.h b/td/telegram/StickersManager.h index 2dfe7774f..ada72e7c2 100644 --- a/td/telegram/StickersManager.h +++ b/td/telegram/StickersManager.h @@ -80,6 +80,10 @@ class StickersManager final : public Actor { tl_object_ptr get_input_sticker_set(StickerSetId sticker_set_id) const; + void register_gift_premium(int32 months, FullMessageId full_message_id, const char *source); + + void unregister_gift_premium(int32 months, FullMessageId full_message_id, const char *source); + void register_dice(const string &emoji, int32 value, FullMessageId full_message_id, const char *source); void unregister_dice(const string &emoji, int32 value, FullMessageId full_message_id, const char *source); @@ -937,6 +941,8 @@ class StickersManager final : public Actor { FlatHashMap>> load_language_codes_queries_; FlatHashMap emoji_suggestions_urls_; + FlatHashMap> gift_premium_messages_; + FlatHashMap> dice_messages_; struct EmojiMessages {