Reload chat themes at most once an hour.

This commit is contained in:
levlam 2021-09-07 13:30:51 +03:00
parent 546898e199
commit 192915c0bf
2 changed files with 34 additions and 22 deletions

View File

@ -44,6 +44,7 @@ class GetChatThemesQuery final : public Td::ResultHandler {
}; };
ThemeManager::ThemeManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { ThemeManager::ThemeManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
chat_themes_.next_reload_time = Time::now();
} }
void ThemeManager::tear_down() { void ThemeManager::tear_down() {
@ -51,7 +52,16 @@ void ThemeManager::tear_down() {
} }
void ThemeManager::get_chat_themes(Promise<td_api::object_ptr<td_api::chatThemes>> &&promise) { void ThemeManager::get_chat_themes(Promise<td_api::object_ptr<td_api::chatThemes>> &&promise) {
if (Time::now() < chat_themes_.next_reload_time) {
return promise.set_value(get_chat_themes_object());
}
if (!chat_themes_.themes.empty()) {
promise.set_value(get_chat_themes_object());
pending_get_chat_themes_queries_.push_back(Promise<td_api::object_ptr<td_api::chatThemes>>());
} else {
pending_get_chat_themes_queries_.push_back(std::move(promise)); pending_get_chat_themes_queries_.push_back(std::move(promise));
}
if (pending_get_chat_themes_queries_.size() == 1) { if (pending_get_chat_themes_queries_.size() == 1) {
auto request_promise = PromiseCreator::lambda( auto request_promise = PromiseCreator::lambda(
[actor_id = actor_id(this)](Result<telegram_api::object_ptr<telegram_api::account_ChatThemes>> result) { [actor_id = actor_id(this)](Result<telegram_api::object_ptr<telegram_api::account_ChatThemes>> result) {
@ -106,17 +116,13 @@ void ThemeManager::on_get_chat_themes(Result<telegram_api::object_ptr<telegram_a
return; return;
} }
chat_themes_.next_reload_time = Time::now() + THEME_CACHE_TIME;
auto chat_themes_ptr = result.move_as_ok(); auto chat_themes_ptr = result.move_as_ok();
LOG(DEBUG) << "Receive " << to_string(chat_themes_ptr); LOG(DEBUG) << "Receive " << to_string(chat_themes_ptr);
if (chat_themes_ptr->get_id() == telegram_api::account_chatThemesNotModified::ID) { if (chat_themes_ptr->get_id() != telegram_api::account_chatThemesNotModified::ID) {
for (auto &promise : promises) { CHECK(chat_themes_ptr->get_id() == telegram_api::account_chatThemes::ID);
promise.set_value(get_chat_themes_object());
}
return;
}
auto chat_themes = telegram_api::move_object_as<telegram_api::account_chatThemes>(chat_themes_ptr); auto chat_themes = telegram_api::move_object_as<telegram_api::account_chatThemes>(chat_themes_ptr);
LOG(INFO) << "Receive " << to_string(chat_themes);
chat_themes_.hash = chat_themes->hash_; chat_themes_.hash = chat_themes->hash_;
chat_themes_.themes.clear(); chat_themes_.themes.clear();
for (auto &chat_theme : chat_themes->themes_) { for (auto &chat_theme : chat_themes->themes_) {
@ -131,11 +137,14 @@ void ThemeManager::on_get_chat_themes(Result<telegram_api::object_ptr<telegram_a
theme.dark_theme = get_chat_theme_settings(std::move(chat_theme->dark_theme_->settings_)); theme.dark_theme = get_chat_theme_settings(std::move(chat_theme->dark_theme_->settings_));
chat_themes_.themes.push_back(std::move(theme)); chat_themes_.themes.push_back(std::move(theme));
} }
}
for (auto &promise : promises) { for (auto &promise : promises) {
if (promise) {
promise.set_value(get_chat_themes_object()); promise.set_value(get_chat_themes_object());
} }
} }
}
ThemeManager::BaseTheme ThemeManager::get_base_theme( ThemeManager::BaseTheme ThemeManager::get_base_theme(
const telegram_api::object_ptr<telegram_api::BaseTheme> &base_theme) { const telegram_api::object_ptr<telegram_api::BaseTheme> &base_theme) {

View File

@ -30,6 +30,8 @@ class ThemeManager final : public Actor {
private: private:
enum class BaseTheme : int32 { Classic, Day, Night, Tinted, Arctic }; enum class BaseTheme : int32 { Classic, Day, Night, Tinted, Arctic };
static constexpr int32 THEME_CACHE_TIME = 3600;
struct ThemeSettings { struct ThemeSettings {
int32 accent_color = 0; int32 accent_color = 0;
BackgroundId background_id; BackgroundId background_id;
@ -47,6 +49,7 @@ class ThemeManager final : public Actor {
struct ChatThemes { struct ChatThemes {
int32 hash = 0; int32 hash = 0;
double next_reload_time = 0;
vector<ChatTheme> themes; vector<ChatTheme> themes;
}; };