Add messageGiftedPremium.sticker.

This commit is contained in:
levlam 2022-07-22 15:01:44 +03:00
parent 61fe5d0df5
commit a81740892d
7 changed files with 95 additions and 46 deletions

View File

@ -2009,7 +2009,8 @@ messagePaymentSuccessful invoice_chat_id:int53 invoice_message_id:int53 currency
messagePaymentSuccessfulBot currency:string total_amount:int53 is_recurring:Bool is_first_recurring:Bool invoice_payload:bytes shipping_option_id:string order_info:orderInfo telegram_payment_charge_id:string provider_payment_charge_id:string = MessageContent;
//@description Telegram Premium was gifted to the user @currency Currency for the paid amount @amount The paid amount, in the smallest units of the currency @month_count Number of month the Telegram Premium subscription will be active
messageGiftedPremium currency:string amount:int53 month_count:int32 = MessageContent;
//@sticker A sticker to be shown in the message; may be null if unknown
messageGiftedPremium currency:string amount:int53 month_count:int32 sticker:sticker = MessageContent;
//@description A contact has registered with Telegram
messageContactRegistered = MessageContent;
@ -6446,9 +6447,6 @@ getPremiumFeatures source:PremiumSource = PremiumFeatures;
//@description Returns examples of premium stickers for demonstration purposes
getPremiumStickers = Stickers;
//@description Returns a Premium gift option sticker by its position among Premium options @month_count Number of month the Telegram Premium subscription will be active
getPremiumGiftOptionSticker month_count:int32 = Sticker;
//@description Informs TDLib that the user viewed detailed information about a Premium feature on the Premium features screen @feature The viewed premium feature
viewPremiumFeature feature:PremiumFeature = Ok;

View File

@ -3818,7 +3818,7 @@ void register_message_content(Td *td, const MessageContent *content, FullMessage
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<const MessageGiftPremium *>(content)->months,
return td->stickers_manager_->register_premium_gift(static_cast<const MessageGiftPremium *>(content)->months,
full_message_id, source);
default:
return;
@ -3898,7 +3898,7 @@ void unregister_message_content(Td *td, const MessageContent *content, FullMessa
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<const MessageGiftPremium *>(content)->months,
return td->stickers_manager_->unregister_premium_gift(static_cast<const MessageGiftPremium *>(content)->months,
full_message_id, source);
default:
return;
@ -5284,7 +5284,8 @@ tl_object_ptr<td_api::MessageContent> get_message_content_object(const MessageCo
}
case MessageContentType::GiftPremium: {
const auto *m = static_cast<const MessageGiftPremium *>(content);
return make_tl_object<td_api::messageGiftedPremium>(m->currency, m->amount, m->months);
return make_tl_object<td_api::messageGiftedPremium>(
m->currency, m->amount, m->months, td->stickers_manager_->get_premium_gift_sticker_object(m->months));
}
default:
UNREACHABLE();

View File

@ -1371,7 +1371,7 @@ StickersManager::~StickersManager() {
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_);
premium_gift_messages_, dice_messages_, emoji_messages_);
}
void StickersManager::start_up() {
@ -2217,6 +2217,35 @@ tl_object_ptr<td_api::stickerSetInfo> StickersManager::get_sticker_set_info_obje
std::move(stickers));
}
td_api::object_ptr<td_api::sticker> StickersManager::get_premium_gift_sticker_object(int32 month_count) {
auto it = premium_gift_messages_.find(month_count);
if (it == premium_gift_messages_.end()) {
return get_sticker_object(get_premium_gift_option_sticker_id(month_count));
} else {
return get_sticker_object(it->second->sticker_id);
}
}
const StickersManager::StickerSet *StickersManager::get_premium_gift_sticker_set() {
if (td_->auth_manager_->is_bot()) {
return nullptr;
}
auto &special_sticker_set = add_special_sticker_set(SpecialStickerSetType::premium_gifts());
if (!special_sticker_set.id_.is_valid()) {
load_special_sticker_set(special_sticker_set);
return nullptr;
}
auto sticker_set = get_sticker_set(special_sticker_set.id_);
CHECK(sticker_set != nullptr);
if (!sticker_set->was_loaded) {
load_special_sticker_set(special_sticker_set);
return nullptr;
}
return sticker_set;
}
FileId StickersManager::get_premium_gift_option_sticker_id(const StickerSet *sticker_set, int32 month_count) {
if (sticker_set == nullptr || sticker_set->sticker_ids.empty() || month_count <= 0) {
return {};
@ -2254,6 +2283,10 @@ FileId StickersManager::get_premium_gift_option_sticker_id(const StickerSet *sti
return sticker_set->sticker_ids[0];
}
FileId StickersManager::get_premium_gift_option_sticker_id(int32 month_count) {
return get_premium_gift_option_sticker_id(get_premium_gift_sticker_set(), month_count);
}
const StickersManager::StickerSet *StickersManager::get_animated_emoji_sticker_set() {
if (td_->auth_manager_->is_bot() || disable_animated_emojis_) {
return nullptr;
@ -3395,6 +3428,9 @@ StickerSetId StickersManager::on_get_messages_sticker_set(StickerSetId sticker_s
if (set_id == add_special_sticker_set(SpecialStickerSetType::animated_emoji()).id_) {
try_update_animated_emoji_messages();
}
if (set_id == add_special_sticker_set(SpecialStickerSetType::premium_gifts()).id_) {
try_update_premium_gift_messages();
}
return set_id;
}
@ -3471,6 +3507,8 @@ void StickersManager::on_get_special_sticker_set(const SpecialStickerSetType &ty
<< ' ' << sticker_set.short_name_);
if (type == SpecialStickerSetType::animated_emoji()) {
try_update_animated_emoji_messages();
} else if (type == SpecialStickerSetType::premium_gifts()) {
try_update_premium_gift_messages();
} else if (!type.get_dice_emoji().empty()) {
sticker_set.is_being_loaded_ = true;
}
@ -4800,47 +4838,57 @@ void StickersManager::try_update_animated_emoji_messages() {
}
}
void StickersManager::register_gift_premium(int32 months, FullMessageId full_message_id, const char *source) {
void StickersManager::try_update_premium_gift_messages() {
auto sticker_set = get_premium_gift_sticker_set();
vector<FullMessageId> full_message_ids;
for (auto &it : premium_gift_messages_) {
auto new_sticker_id = get_premium_gift_option_sticker_id(sticker_set, it.first);
if (new_sticker_id != it.second->sticker_id) {
it.second->sticker_id = new_sticker_id;
for (const auto &full_message_id : it.second->full_message_ids) {
full_message_ids.push_back(full_message_id);
}
}
}
for (const auto &full_message_id : full_message_ids) {
td_->messages_manager_->on_external_update_message_content(full_message_id);
}
}
void StickersManager::register_premium_gift(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;
auto &premium_gift_messages_ptr = premium_gift_messages_[months];
if (premium_gift_messages_ptr == nullptr) {
premium_gift_messages_ptr = make_unique<GiftPremiumMessages>();
}
auto &premium_gift_messages = *premium_gift_messages_ptr;
if (premium_gift_messages.full_message_ids.empty()) {
premium_gift_messages.sticker_id = get_premium_gift_option_sticker_id(months);
}
bool is_inserted = premium_gift_messages.full_message_ids.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) {
void StickersManager::unregister_premium_gift(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 it = premium_gift_messages_.find(months);
CHECK(it != premium_gift_messages_.end());
auto &message_ids = it->second->full_message_ids;
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);
premium_gift_messages_.erase(it);
}
}

View File

@ -76,13 +76,15 @@ class StickersManager final : public Actor {
const vector<StickerSetId> &sticker_set_ids,
size_t covers_limit) const;
td_api::object_ptr<td_api::sticker> get_premium_gift_sticker_object(int32 month_count);
td_api::object_ptr<td_api::animatedEmoji> get_animated_emoji_object(const string &emoji);
tl_object_ptr<telegram_api::InputStickerSet> get_input_sticker_set(StickerSetId sticker_set_id) const;
void register_gift_premium(int32 months, FullMessageId full_message_id, const char *source);
void register_premium_gift(int32 months, FullMessageId full_message_id, const char *source);
void unregister_gift_premium(int32 months, FullMessageId full_message_id, const char *source);
void unregister_premium_gift(int32 months, FullMessageId full_message_id, const char *source);
void register_dice(const string &emoji, int32 value, FullMessageId full_message_id, const char *source);
@ -708,7 +710,13 @@ class StickersManager final : public Actor {
bool update_sticker_set_cache(const StickerSet *sticker_set, Promise<Unit> &promise);
static FileId get_premium_gift_option_sticker_id(const StickerSet *sticker_set, int32 position);
const StickerSet *get_premium_gift_sticker_set();
static FileId get_premium_gift_option_sticker_id(const StickerSet *sticker_set, int32 month_count);
FileId get_premium_gift_option_sticker_id(int32 month_count);
void try_update_premium_gift_messages();
const StickerSet *get_animated_emoji_sticker_set();
@ -941,7 +949,11 @@ class StickersManager final : public Actor {
FlatHashMap<string, vector<Promise<Unit>>> load_language_codes_queries_;
FlatHashMap<int64, string> emoji_suggestions_urls_;
FlatHashMap<int32, FlatHashSet<FullMessageId, FullMessageIdHash>> gift_premium_messages_;
struct GiftPremiumMessages {
FlatHashSet<FullMessageId, FullMessageIdHash> full_message_ids;
FileId sticker_id;
};
FlatHashMap<int32, unique_ptr<GiftPremiumMessages>> premium_gift_messages_;
FlatHashMap<string, FlatHashSet<FullMessageId, FullMessageIdHash>> dice_messages_;

View File

@ -7906,12 +7906,6 @@ void Td::on_request(uint64 id, const td_api::getPremiumStickers &request) {
CREATE_REQUEST(SearchStickersRequest, "⭐️⭐️", 100);
}
void Td::on_request(uint64 id, const td_api::getPremiumGiftOptionSticker &request) {
CHECK_IS_USER();
CREATE_REQUEST_PROMISE();
stickers_manager_->get_premium_gift_option_sticker(request.month_count_, false, std::move(promise));
}
void Td::on_request(uint64 id, const td_api::viewPremiumFeature &request) {
CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE();

View File

@ -1301,8 +1301,6 @@ class Td final : public Actor {
void on_request(uint64 id, const td_api::getPremiumStickers &request);
void on_request(uint64 id, const td_api::getPremiumGiftOptionSticker &request);
void on_request(uint64 id, const td_api::viewPremiumFeature &request);
void on_request(uint64 id, const td_api::clickPremiumSubscriptionButton &request);

View File

@ -2793,8 +2793,6 @@ class CliClient final : public Actor {
send_request(td_api::make_object<td_api::getEmojiSuggestionsUrl>(args));
} else if (op == "gces") {
send_request(td_api::make_object<td_api::getCustomEmojiStickers>(to_integers<int64>(args)));
} else if (op == "gpgos") {
send_request(td_api::make_object<td_api::getPremiumGiftOptionSticker>(to_integer<int32>(args)));
} else if (op == "gsan") {
send_request(td_api::make_object<td_api::getSavedAnimations>());
} else if (op == "asan") {