diff --git a/td/telegram/ConfigManager.cpp b/td/telegram/ConfigManager.cpp index 959d8d181..1b785f3fb 100644 --- a/td/telegram/ConfigManager.cpp +++ b/td/telegram/ConfigManager.cpp @@ -1486,9 +1486,6 @@ void ConfigManager::process_app_config(tl_object_ptr &c // bool archive_all_stories = false; int32 story_viewers_expire_period = 86400; int64 stories_changelog_user_id = ContactsManager::get_service_notifications_user_id().get(); - FlatHashMap, AccentColorIdHash> light_colors; - FlatHashMap, AccentColorIdHash> dark_colors; - vector accent_color_ids; int32 transcribe_audio_trial_weekly_number = 0; int32 transcribe_audio_trial_duration_max = 0; int32 transcribe_audio_trial_cooldown_until = 0; @@ -1958,62 +1955,6 @@ void ConfigManager::process_app_config(tl_object_ptr &c get_json_value_int(std::move(key_value->value_), key)); continue; } - if (key == "peer_colors" || key == "dark_peer_colors") { - auto &color_map = key == "peer_colors" ? light_colors : dark_colors; - if (value->get_id() == telegram_api::jsonObject::ID) { - auto peer_color_ids = std::move(static_cast(value)->value_); - for (auto &peer_color_id : peer_color_ids) { - CHECK(peer_color_id != nullptr); - auto r_accent_color_id = to_integer_safe(peer_color_id->key_); - if (r_accent_color_id.is_error()) { - LOG(ERROR) << "Receive " << to_string(peer_color_id); - continue; - } - auto accent_color_id = AccentColorId(r_accent_color_id.ok()); - if (!accent_color_id.is_valid() || accent_color_id.is_built_in() || - peer_color_id->value_->get_id() != telegram_api::jsonArray::ID) { - LOG(ERROR) << "Receive " << to_string(peer_color_id); - continue; - } - auto &colors = color_map[accent_color_id]; - if (!colors.empty()) { - LOG(ERROR) << "Receive duplicate " << accent_color_id; - continue; - } - auto colors_json = std::move(static_cast(peer_color_id->value_.get())->value_); - for (auto &color_json : colors_json) { - auto color_str = get_json_value_string(std::move(color_json), key); - auto r_color = hex_to_integer_safe(color_str); - if (r_color.is_ok() && r_color.ok() <= 0xFFFFFF) { - colors.push_back(static_cast(r_color.ok())); - } - } - if (colors.empty() || colors.size() != colors_json.size()) { - LOG(ERROR) << "Receive invalid colors for " << accent_color_id; - color_map.erase(accent_color_id); - } - } - } else { - LOG(ERROR) << "Receive unexpected " << key << ' ' << to_string(*value); - } - continue; - } - if (key == "peer_colors_available") { - if (value->get_id() == telegram_api::jsonArray::ID) { - auto colors = std::move(static_cast(value)->value_); - for (auto &color : colors) { - auto accent_color_id = AccentColorId(get_json_value_int(std::move(color), key)); - if (accent_color_id.is_valid() && !td::contains(accent_color_ids, accent_color_id)) { - accent_color_ids.push_back(accent_color_id); - } else { - LOG(ERROR) << "Receive an invalid accent color identifier"; - } - } - } else { - LOG(ERROR) << "Receive unexpected peer_colors_available " << to_string(*value); - } - continue; - } if (key == "transcribe_audio_trial_weekly_number") { transcribe_audio_trial_weekly_number = get_json_value_int(std::move(key_value->value_), key); continue; @@ -2037,16 +1978,6 @@ void ConfigManager::process_app_config(tl_object_ptr &c send_closure(G()->link_manager(), &LinkManager::update_autologin_domains, std::move(autologin_domains), std::move(url_auth_domains), std::move(whitelisted_domains)); - td::remove_if(accent_color_ids, [&light_colors](AccentColorId accent_color_id) { - if (!accent_color_id.is_built_in() && light_colors.count(accent_color_id) == 0) { - LOG(ERROR) << "Receive unknown " << accent_color_id; - return true; - } - return false; - }); - send_closure(G()->theme_manager(), &ThemeManager::on_update_accent_colors, std::move(light_colors), - std::move(dark_colors), std::move(accent_color_ids)); - send_closure(G()->transcription_manager(), &TranscriptionManager::on_update_trial_parameters, transcribe_audio_trial_weekly_number, transcribe_audio_trial_duration_max, transcribe_audio_trial_cooldown_until); diff --git a/td/telegram/ConfigManager.h b/td/telegram/ConfigManager.h index 6032a913b..a71797ece 100644 --- a/td/telegram/ConfigManager.h +++ b/td/telegram/ConfigManager.h @@ -102,7 +102,7 @@ class ConfigManager final : public NetQueryCallback { private: struct AppConfig { - static constexpr int32 CURRENT_VERSION = 23; + static constexpr int32 CURRENT_VERSION = 24; int32 version_ = 0; int32 hash_ = 0; telegram_api::object_ptr config_; diff --git a/td/telegram/ThemeManager.cpp b/td/telegram/ThemeManager.cpp index 35d4b4663..50bc7cc59 100644 --- a/td/telegram/ThemeManager.cpp +++ b/td/telegram/ThemeManager.cpp @@ -52,6 +52,32 @@ class GetChatThemesQuery final : public Td::ResultHandler { } }; +class GetPeerColorsQuery final : public Td::ResultHandler { + Promise> promise_; + + public: + explicit GetPeerColorsQuery(Promise> &&promise) + : promise_(std::move(promise)) { + } + + void send(int32 hash) { + send_query(G()->net_query_creator().create(telegram_api::help_getPeerColors(hash))); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + promise_.set_value(result_ptr.move_as_ok()); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + bool operator==(const ThemeManager::ThemeSettings &lhs, const ThemeManager::ThemeSettings &rhs) { return lhs.accent_color == rhs.accent_color && lhs.message_accent_color == rhs.message_accent_color && lhs.background_info == rhs.background_info && lhs.base_theme == rhs.base_theme && @@ -140,7 +166,9 @@ void ThemeManager::ChatThemes::parse(ParserT &parser) { template void ThemeManager::AccentColors::store(StorerT &storer) const { + bool has_hash = hash_ != 0; BEGIN_STORE_FLAGS(); + STORE_FLAG(has_hash); END_STORE_FLAGS(); td::store(static_cast(light_colors_.size()), storer); for (auto &it : light_colors_) { @@ -153,11 +181,16 @@ void ThemeManager::AccentColors::store(StorerT &storer) const { td::store(it.second, storer); } td::store(accent_color_ids_, storer); + if (has_hash) { + td::store(hash_, storer); + } } template void ThemeManager::AccentColors::parse(ParserT &parser) { + bool has_hash; BEGIN_PARSE_FLAGS(); + PARSE_FLAG(has_hash); END_PARSE_FLAGS(); int32 size; td::parse(size, parser); @@ -179,6 +212,9 @@ void ThemeManager::AccentColors::parse(ParserT &parser) { dark_colors_.emplace(accent_color_id, std::move(colors)); } td::parse(accent_color_ids_, parser); + if (has_hash) { + td::parse(hash_, parser); + } } ThemeManager::ThemeManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { @@ -225,7 +261,14 @@ void ThemeManager::load_accent_colors() { void ThemeManager::init() { load_chat_themes(); - loop(); + if (td_->auth_manager_->is_authorized() && !td_->auth_manager_->is_bot()) { + if (chat_themes_.hash == 0) { + reload_chat_themes(); + } + if (accent_colors_.hash_ == 0) { + reload_accent_colors(); + } + } } void ThemeManager::tear_down() { @@ -286,7 +329,7 @@ void ThemeManager::on_update_theme(telegram_api::object_ptr promise.set_value(Unit()); } -void ThemeManager::on_update_accent_colors(FlatHashMap, AccentColorIdHash> light_colors, +bool ThemeManager::on_update_accent_colors(FlatHashMap, AccentColorIdHash> light_colors, FlatHashMap, AccentColorIdHash> dark_colors, vector accent_color_ids) { auto are_equal = [](const FlatHashMap, AccentColorIdHash> &lhs, @@ -301,7 +344,7 @@ void ThemeManager::on_update_accent_colors(FlatHashMap> result) { + send_closure(actor_id, &ThemeManager::on_get_accent_colors, std::move(result)); + }); + + td_->create_handler(std::move(request_promise))->send(accent_colors_.hash_); +} + +void ThemeManager::on_get_accent_colors(Result> result) { + if (result.is_error()) { + return; + } + + auto peer_colors_ptr = result.move_as_ok(); + LOG(DEBUG) << "Receive " << to_string(peer_colors_ptr); + if (peer_colors_ptr->get_id() == telegram_api::help_peerColorsNotModified::ID) { + return; + } + CHECK(peer_colors_ptr->get_id() == telegram_api::help_peerColors::ID); + auto peer_colors = telegram_api::move_object_as(peer_colors_ptr); + FlatHashMap, AccentColorIdHash> light_colors; + FlatHashMap, AccentColorIdHash> dark_colors; + vector accent_color_ids; + for (auto &option : peer_colors->colors_) { + if ((option->colors_ != nullptr && option->colors_->get_id() != telegram_api::help_peerColorSet::ID) || + (option->dark_colors_ != nullptr && option->dark_colors_->get_id() != telegram_api::help_peerColorSet::ID)) { + LOG(ERROR) << "Receive " << to_string(option); + continue; + } + AccentColorId accent_color_id(option->color_id_); + if (!accent_color_id.is_valid() || td::contains(accent_color_ids, accent_color_id) || + (accent_color_id.is_built_in() && (option->colors_ != nullptr || option->dark_colors_ != nullptr)) || + (!accent_color_id.is_built_in() && option->colors_ == nullptr)) { + LOG(ERROR) << "Receive " << to_string(option); + continue; + } + if (!option->hidden_) { + accent_color_ids.push_back(accent_color_id); + } + if (option->colors_ != nullptr) { + auto colors = telegram_api::move_object_as(option->colors_); + light_colors[accent_color_id] = std::move(colors->colors_); + } + if (option->dark_colors_ != nullptr) { + auto colors = telegram_api::move_object_as(option->dark_colors_); + dark_colors[accent_color_id] = std::move(colors->colors_); + } + } + + bool is_changed = false; + if (accent_colors_.hash_ != peer_colors->hash_) { + accent_colors_.hash_ = peer_colors->hash_; + is_changed = true; + } + if (!on_update_accent_colors(std::move(light_colors), std::move(dark_colors), std::move(accent_color_ids)) && + is_changed) { + save_accent_colors(); + } +} + ThemeManager::BaseTheme ThemeManager::get_base_theme( const telegram_api::object_ptr &base_theme) { CHECK(base_theme != nullptr); diff --git a/td/telegram/ThemeManager.h b/td/telegram/ThemeManager.h index 14c9831a8..556bd4e57 100644 --- a/td/telegram/ThemeManager.h +++ b/td/telegram/ThemeManager.h @@ -32,9 +32,7 @@ class ThemeManager final : public Actor { void reload_chat_themes(); - void on_update_accent_colors(FlatHashMap, AccentColorIdHash> light_colors, - FlatHashMap, AccentColorIdHash> dark_colors, - vector accent_color_ids); + void reload_accent_colors(); static string get_theme_parameters_json_string(const td_api::object_ptr &theme, bool for_web_view); @@ -95,6 +93,7 @@ class ThemeManager final : public Actor { FlatHashMap, AccentColorIdHash> light_colors_; FlatHashMap, AccentColorIdHash> dark_colors_; vector accent_color_ids_; + int32 hash_ = 0; td_api::object_ptr get_update_accent_colors_object() const; @@ -117,6 +116,12 @@ class ThemeManager final : public Actor { void on_get_chat_themes(Result> result); + bool on_update_accent_colors(FlatHashMap, AccentColorIdHash> light_colors, + FlatHashMap, AccentColorIdHash> dark_colors, + vector accent_color_ids); + + void on_get_accent_colors(Result> result); + td_api::object_ptr get_theme_settings_object(const ThemeSettings &settings) const; td_api::object_ptr get_chat_theme_object(const ChatTheme &theme) const; diff --git a/td/telegram/UpdatesManager.cpp b/td/telegram/UpdatesManager.cpp index b8906f1fd..30aab9fba 100644 --- a/td/telegram/UpdatesManager.cpp +++ b/td/telegram/UpdatesManager.cpp @@ -2261,6 +2261,7 @@ void UpdatesManager::try_reload_data() { td_->stickers_manager_->get_default_custom_emoji_stickers(StickerListType::UserProfilePhoto, true, Auto()); td_->story_manager_->reload_active_stories(); td_->story_manager_->reload_all_read_stories(); + td_->theme_manager_->reload_accent_colors(); td_->theme_manager_->reload_chat_themes(); schedule_data_reload();