Drop sponsored message cache when Premium state is changed.

This commit is contained in:
levlam 2022-10-21 15:51:32 +03:00
parent ec1310a0e7
commit 0497c09047

View File

@ -117,6 +117,7 @@ struct SponsoredMessageManager::DialogSponsoredMessages {
vector<SponsoredMessage> messages; vector<SponsoredMessage> messages;
FlatHashMap<int64, string> message_random_ids; FlatHashMap<int64, string> message_random_ids;
int32 messages_between = 0; int32 messages_between = 0;
bool is_premium = false;
}; };
SponsoredMessageManager::SponsoredMessageManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { SponsoredMessageManager::SponsoredMessageManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
@ -147,10 +148,10 @@ void SponsoredMessageManager::delete_cached_sponsored_messages(DialogId dialog_i
} }
auto it = dialog_sponsored_messages_.find(dialog_id); auto it = dialog_sponsored_messages_.find(dialog_id);
CHECK(it != dialog_sponsored_messages_.end()); if (it != dialog_sponsored_messages_.end() && it->second->promises.empty()) {
CHECK(it->second->promises.empty());
dialog_sponsored_messages_.erase(it); dialog_sponsored_messages_.erase(it);
} }
}
td_api::object_ptr<td_api::sponsoredMessage> SponsoredMessageManager::get_sponsored_message_object( td_api::object_ptr<td_api::sponsoredMessage> SponsoredMessageManager::get_sponsored_message_object(
DialogId dialog_id, const SponsoredMessage &sponsored_message) const { DialogId dialog_id, const SponsoredMessage &sponsored_message) const {
@ -216,7 +217,14 @@ void SponsoredMessageManager::get_dialog_sponsored_messages(
auto &messages = dialog_sponsored_messages_[dialog_id]; auto &messages = dialog_sponsored_messages_[dialog_id];
if (messages != nullptr && messages->promises.empty()) { if (messages != nullptr && messages->promises.empty()) {
if (messages->is_premium == td_->option_manager_->get_option_boolean("is_premium", false)) {
// use cached value
return promise.set_value(get_sponsored_messages_object(dialog_id, *messages)); return promise.set_value(get_sponsored_messages_object(dialog_id, *messages));
} else {
// drop cache
messages = nullptr;
delete_cached_sponsored_messages_timeout_.cancel_timeout(dialog_id.get());
}
} }
if (messages == nullptr) { if (messages == nullptr) {
@ -336,6 +344,7 @@ void SponsoredMessageManager::on_get_dialog_sponsored_messages(
default: default:
UNREACHABLE(); UNREACHABLE();
} }
messages->is_premium = td_->option_manager_->get_option_boolean("is_premium", false);
for (auto &promise : promises) { for (auto &promise : promises) {
promise.set_value(get_sponsored_messages_object(dialog_id, *messages)); promise.set_value(get_sponsored_messages_object(dialog_id, *messages));