From 075d004b9f06b3d99162505624547dd9b0e2e89a Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 13 Sep 2021 15:55:01 +0300 Subject: [PATCH] Replace getChatThemes with updateChatThemes. --- td/generate/scheme/td_api.tl | 10 +-- td/mtproto/SessionConnection.cpp | 1 + td/telegram/AuthManager.cpp | 2 + td/telegram/BackgroundManager.cpp | 9 ++- td/telegram/BackgroundType.cpp | 24 +++--- td/telegram/Td.cpp | 7 +- td/telegram/Td.h | 2 - td/telegram/ThemeManager.cpp | 124 +++++++++++++++++------------- td/telegram/ThemeManager.h | 14 +++- td/telegram/cli.cpp | 2 - 10 files changed, 109 insertions(+), 86 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 6211d640f..a2eb991dd 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -2733,9 +2733,6 @@ themeSettings accent_color:int32 background:background message_fill:BackgroundFi //@dark_settings Theme settings for a dark chat theme chatTheme name:string light_settings:themeSettings dark_settings:themeSettings = ChatTheme; -//@description Contains a list of chat themes @chat_themes A list of chat themes -chatThemes chat_themes:vector = ChatThemes; - //@description Contains a list of hashtags @hashtags A list of hashtags hashtags hashtags:vector = Hashtags; @@ -3817,6 +3814,9 @@ updateSavedAnimations animation_ids:vector = Update; //@description The selected background has changed @for_dark_theme True, if background for dark theme has changed @background The new selected background; may be null updateSelectedBackground for_dark_theme:Bool background:background = Update; +//@description The list of available chat themes has changed @chat_themes The new list of chat themes +updateChatThemes chat_themes:vector = Update; + //@description Some language pack strings have been updated @localization_target Localization target to which the language pack belongs @language_pack_id Identifier of the updated language pack @strings List of changed language pack strings updateLanguagePackStrings localization_target:string language_pack_id:string strings:vector = Update; @@ -5263,10 +5263,6 @@ removeBackground background_id:int64 = Ok; resetBackgrounds = Ok; -//@description Returns the list of available chat themes -getChatThemes = ChatThemes; - - //@description Returns information about the current localization target. This is an offline request if only_local is true. Can be called before authorization @only_local If true, returns only locally available information without sending network requests getLocalizationTargetInfo only_local:Bool = LocalizationTargetInfo; diff --git a/td/mtproto/SessionConnection.cpp b/td/mtproto/SessionConnection.cpp index b9a40ea40..2c4b48687 100644 --- a/td/mtproto/SessionConnection.cpp +++ b/td/mtproto/SessionConnection.cpp @@ -32,6 +32,7 @@ #include #include +#include namespace td { diff --git a/td/telegram/AuthManager.cpp b/td/telegram/AuthManager.cpp index cc7fd58ab..978235dfc 100644 --- a/td/telegram/AuthManager.cpp +++ b/td/telegram/AuthManager.cpp @@ -26,6 +26,7 @@ #include "td/telegram/StickersManager.h" #include "td/telegram/Td.h" #include "td/telegram/TdDb.h" +#include "td/telegram/ThemeManager.h" #include "td/telegram/TopDialogManager.h" #include "td/telegram/UpdatesManager.h" @@ -782,6 +783,7 @@ void AuthManager::on_get_authorization(tl_object_ptrmessages_manager_->on_authorization_success(); td->notification_manager_->init(); td->stickers_manager_->init(); + td->theme_manager_->init(); send_closure(td->top_dialog_manager_, &TopDialogManager::do_start_up); td->updates_manager_->get_difference("on_get_authorization"); td->on_online_updated(false, true); diff --git a/td/telegram/BackgroundManager.cpp b/td/telegram/BackgroundManager.cpp index e5346d78f..3d5fdc3cc 100644 --- a/td/telegram/BackgroundManager.cpp +++ b/td/telegram/BackgroundManager.cpp @@ -1050,7 +1050,9 @@ string BackgroundManager::get_background_name_database_key(const string &name) { std::pair BackgroundManager::on_get_background( BackgroundId expected_background_id, const string &expected_background_name, telegram_api::object_ptr wallpaper_ptr, bool replace_type) { - CHECK(wallpaper_ptr != nullptr); + if (wallpaper_ptr == nullptr) { + return {}; + } if (wallpaper_ptr->get_id() == telegram_api::wallPaperNoFile::ID) { auto wallpaper = move_tl_object_as(wallpaper_ptr); @@ -1061,10 +1063,13 @@ std::pair BackgroundManager::on_get_background( } auto background_id = BackgroundId(wallpaper->id_); - if (!background_id.is_valid() || background_id.is_local()) { + if (background_id.is_local()) { LOG(ERROR) << "Receive " << to_string(wallpaper); return {}; } + if (!background_id.is_valid()) { + background_id = get_next_local_background_id(); + } Background background; background.id = background_id; diff --git a/td/telegram/BackgroundType.cpp b/td/telegram/BackgroundType.cpp index de2aa77a3..eed0161a1 100644 --- a/td/telegram/BackgroundType.cpp +++ b/td/telegram/BackgroundType.cpp @@ -26,6 +26,15 @@ static bool is_valid_color(int32 color) { return 0 <= color && color <= 0xFFFFFF; } +static bool validate_alpha_color(int32 &color) { + if (-0x1000000 <= color && color <= 0xFFFFFF) { + color &= 0xFFFFFF; + return true; + } + color = 0; + return false; +} + static bool is_valid_rotation_angle(int32 rotation_angle) { return 0 <= rotation_angle && rotation_angle < 360 && rotation_angle % 45 == 0; } @@ -38,35 +47,30 @@ BackgroundFill::BackgroundFill(const telegram_api::wallPaperSettings *settings) auto flags = settings->flags_; if ((flags & telegram_api::wallPaperSettings::BACKGROUND_COLOR_MASK) != 0) { top_color_ = settings->background_color_; - if (!is_valid_color(top_color_)) { + if (!validate_alpha_color(top_color_)) { LOG(ERROR) << "Receive " << to_string(*settings); - top_color_ = 0; } } if ((flags & telegram_api::wallPaperSettings::FOURTH_BACKGROUND_COLOR_MASK) != 0 || (flags & telegram_api::wallPaperSettings::THIRD_BACKGROUND_COLOR_MASK) != 0) { bottom_color_ = settings->second_background_color_; - if (!is_valid_color(bottom_color_)) { + if (!validate_alpha_color(bottom_color_)) { LOG(ERROR) << "Receive " << to_string(*settings); - bottom_color_ = 0; } third_color_ = settings->third_background_color_; - if (!is_valid_color(third_color_)) { + if (!validate_alpha_color(third_color_)) { LOG(ERROR) << "Receive " << to_string(*settings); - third_color_ = 0; } if ((flags & telegram_api::wallPaperSettings::FOURTH_BACKGROUND_COLOR_MASK) != 0) { fourth_color_ = settings->fourth_background_color_; - if (!is_valid_color(fourth_color_)) { + if (!validate_alpha_color(fourth_color_)) { LOG(ERROR) << "Receive " << to_string(*settings); - fourth_color_ = 0; } } } else if ((flags & telegram_api::wallPaperSettings::SECOND_BACKGROUND_COLOR_MASK) != 0) { bottom_color_ = settings->second_background_color_; - if (!is_valid_color(bottom_color_)) { + if (!validate_alpha_color(bottom_color_)) { LOG(ERROR) << "Receive " << to_string(*settings); - bottom_color_ = 0; } rotation_angle_ = settings->rotation_; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index e917b01bf..63e73ba20 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -4334,6 +4334,7 @@ void Td::send_update(tl_object_ptr &&object) { } switch (object_id) { + case td_api::updateChatThemes::ID: case td_api::updateFavoriteStickers::ID: case td_api::updateInstalledStickerSets::ID: case td_api::updateRecentStickers::ID: @@ -8002,12 +8003,6 @@ void Td::on_request(uint64 id, const td_api::resetBackgrounds &request) { background_manager_->reset_backgrounds(std::move(promise)); } -void Td::on_request(uint64 id, const td_api::getChatThemes &request) { - CHECK_IS_USER(); - CREATE_REQUEST_PROMISE(); - theme_manager_->get_chat_themes(std::move(promise)); -} - void Td::on_request(uint64 id, td_api::getRecentlyVisitedTMeUrls &request) { CHECK_IS_USER(); CLEAN_INPUT_STRING(request.referrer_); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 72f8d687a..9e6cebd54 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1177,8 +1177,6 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::resetBackgrounds &request); - void on_request(uint64 id, const td_api::getChatThemes &request); - void on_request(uint64 id, td_api::getRecentlyVisitedTMeUrls &request); void on_request(uint64 id, td_api::setBotUpdatesStatus &request); diff --git a/td/telegram/ThemeManager.cpp b/td/telegram/ThemeManager.cpp index 15e974aea..1f7ef974f 100644 --- a/td/telegram/ThemeManager.cpp +++ b/td/telegram/ThemeManager.cpp @@ -6,6 +6,7 @@ // #include "td/telegram/ThemeManager.h" +#include "td/telegram/AuthManager.h" #include "td/telegram/BackgroundManager.h" #include "td/telegram/Global.h" #include "td/telegram/net/NetQueryCreator.h" @@ -14,6 +15,7 @@ #include "td/utils/algorithm.h" #include "td/utils/buffer.h" #include "td/utils/logging.h" +#include "td/utils/Random.h" #include "td/utils/Time.h" namespace td { @@ -55,49 +57,61 @@ bool operator!=(const ThemeManager::ThemeSettings &lhs, const ThemeManager::Them } ThemeManager::ThemeManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { - chat_themes_.next_reload_time = Time::now(); +} + +void ThemeManager::start_up() { + init(); +} + +void ThemeManager::init() { + if (!td_->auth_manager_->is_authorized() || td_->auth_manager_->is_bot()) { + return; + } + + chat_themes_.next_reload_time = Time::now(); // TODO load chat themes from binlog + loop(); } void ThemeManager::tear_down() { parent_.reset(); } -void ThemeManager::get_chat_themes(Promise> &&promise) { +void ThemeManager::loop() { + if (!td_->auth_manager_->is_authorized() || td_->auth_manager_->is_bot()) { + return; + } + if (Time::now() < chat_themes_.next_reload_time) { - return promise.set_value(get_chat_themes_object()); + return set_timeout_at(chat_themes_.next_reload_time); } - if (!chat_themes_.themes.empty()) { - promise.set_value(get_chat_themes_object()); - pending_get_chat_themes_queries_.push_back(Promise>()); - } else { - pending_get_chat_themes_queries_.push_back(std::move(promise)); - } - if (pending_get_chat_themes_queries_.size() == 1) { - auto request_promise = PromiseCreator::lambda( - [actor_id = actor_id(this)](Result> result) { - send_closure(actor_id, &ThemeManager::on_get_chat_themes, std::move(result)); - }); + auto request_promise = PromiseCreator::lambda( + [actor_id = actor_id(this)](Result> result) { + send_closure(actor_id, &ThemeManager::on_get_chat_themes, std::move(result)); + }); - td_->create_handler(std::move(request_promise))->send(chat_themes_.hash); - } + td_->create_handler(std::move(request_promise))->send(chat_themes_.hash); } void ThemeManager::on_update_theme(telegram_api::object_ptr &&theme, Promise &&promise) { CHECK(theme != nullptr); + bool is_changed = false; for (auto &chat_theme : chat_themes_.themes) { if (chat_theme.light_id == theme->id_ || chat_theme.dark_id == theme->id_) { - chat_themes_.hash = 0; - chat_themes_.next_reload_time = Time::now(); auto theme_settings = get_chat_theme_settings(std::move(theme->settings_)); if (chat_theme.light_id == theme->id_ && chat_theme.light_theme != theme_settings) { chat_theme.light_theme = theme_settings; + is_changed = true; } if (chat_theme.dark_id == theme->id_ && chat_theme.dark_theme != theme_settings) { chat_theme.dark_theme = theme_settings; + is_changed = true; } } } + if (is_changed) { + send_update_chat_themes(); + } promise.set_value(Unit()); } @@ -125,56 +139,52 @@ td_api::object_ptr ThemeManager::get_chat_theme_object(const get_theme_settings_object(theme.dark_theme)); } -td_api::object_ptr ThemeManager::get_chat_themes_object() const { - return td_api::make_object( +td_api::object_ptr ThemeManager::get_update_chat_themes_object() const { + return td_api::make_object( transform(chat_themes_.themes, [this](const ChatTheme &theme) { return get_chat_theme_object(theme); })); } +void ThemeManager::send_update_chat_themes() const { + send_closure(G()->td(), &Td::send_update, get_update_chat_themes_object()); +} + void ThemeManager::on_get_chat_themes(Result> result) { - auto promises = std::move(pending_get_chat_themes_queries_); - CHECK(!promises.empty()); - reset_to_empty(pending_get_chat_themes_queries_); - if (result.is_error()) { - // do not clear chat_themes_ - - auto error = result.move_as_error(); - for (auto &promise : promises) { - promise.set_error(error.clone()); - } + set_timeout_in(Random::fast(40, 60)); return; } chat_themes_.next_reload_time = Time::now() + THEME_CACHE_TIME; + set_timeout_at(chat_themes_.next_reload_time); auto chat_themes_ptr = result.move_as_ok(); LOG(DEBUG) << "Receive " << to_string(chat_themes_ptr); - if (chat_themes_ptr->get_id() != telegram_api::account_chatThemesNotModified::ID) { - CHECK(chat_themes_ptr->get_id() == telegram_api::account_chatThemes::ID); - auto chat_themes = telegram_api::move_object_as(chat_themes_ptr); - chat_themes_.hash = chat_themes->hash_; - chat_themes_.themes.clear(); - for (auto &chat_theme : chat_themes->themes_) { - if (chat_theme->emoticon_.empty()) { - LOG(ERROR) << "Receive " << to_string(chat_theme); - continue; - } - - ChatTheme theme; - theme.emoji = std::move(chat_theme->emoticon_); - theme.light_id = chat_theme->theme_->id_; - theme.dark_id = chat_theme->dark_theme_->id_; - theme.light_theme = get_chat_theme_settings(std::move(chat_theme->theme_->settings_)); - theme.dark_theme = get_chat_theme_settings(std::move(chat_theme->dark_theme_->settings_)); - chat_themes_.themes.push_back(std::move(theme)); + if (chat_themes_ptr->get_id() == telegram_api::account_chatThemesNotModified::ID) { + return; + } + CHECK(chat_themes_ptr->get_id() == telegram_api::account_chatThemes::ID); + auto chat_themes = telegram_api::move_object_as(chat_themes_ptr); + chat_themes_.hash = chat_themes->hash_; + chat_themes_.themes.clear(); + for (auto &chat_theme : chat_themes->themes_) { + if (chat_theme->emoticon_.empty()) { + LOG(ERROR) << "Receive " << to_string(chat_theme); + continue; } + + ChatTheme theme; + theme.emoji = std::move(chat_theme->emoticon_); + theme.light_id = chat_theme->theme_->id_; + theme.dark_id = chat_theme->dark_theme_->id_; + theme.light_theme = get_chat_theme_settings(std::move(chat_theme->theme_->settings_)); + theme.dark_theme = get_chat_theme_settings(std::move(chat_theme->dark_theme_->settings_)); + if (theme.light_theme.message_colors.empty() || theme.dark_theme.message_colors.empty()) { + continue; + } + chat_themes_.themes.push_back(std::move(theme)); } - for (auto &promise : promises) { - if (promise) { - promise.set_value(get_chat_themes_object()); - } - } + send_update_chat_themes(); } ThemeManager::BaseTheme ThemeManager::get_base_theme( @@ -214,4 +224,12 @@ ThemeManager::ThemeSettings ThemeManager::get_chat_theme_settings( return result; } +void ThemeManager::get_current_state(vector> &updates) const { + if (!td_->auth_manager_->is_authorized() || td_->auth_manager_->is_bot() || chat_themes_.themes.empty()) { + return; + } + + updates.push_back(get_update_chat_themes_object()); +} + } // namespace td diff --git a/td/telegram/ThemeManager.h b/td/telegram/ThemeManager.h index d45bcd5e5..dc8fcb93d 100644 --- a/td/telegram/ThemeManager.h +++ b/td/telegram/ThemeManager.h @@ -25,10 +25,12 @@ class ThemeManager final : public Actor { public: ThemeManager(Td *td, ActorShared<> parent); - void get_chat_themes(Promise> &&promise); + void init(); void on_update_theme(telegram_api::object_ptr &&theme, Promise &&promise); + void get_current_state(vector> &updates) const; + private: enum class BaseTheme : int32 { Classic, Day, Night, Tinted, Arctic }; @@ -61,6 +63,10 @@ class ThemeManager final : public Actor { vector themes; }; + void start_up() final; + + void loop() final; + void tear_down() final; void on_get_chat_themes(Result> result); @@ -69,14 +75,14 @@ class ThemeManager final : public Actor { td_api::object_ptr get_chat_theme_object(const ChatTheme &theme) const; - td_api::object_ptr get_chat_themes_object() const; + td_api::object_ptr get_update_chat_themes_object() const; + + void send_update_chat_themes() const; static BaseTheme get_base_theme(const telegram_api::object_ptr &base_theme); ThemeSettings get_chat_theme_settings(telegram_api::object_ptr settings); - vector>> pending_get_chat_themes_queries_; - ChatThemes chat_themes_; Td *td_; diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 8b17cd0aa..9ca736122 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -2295,8 +2295,6 @@ class CliClient final : public Actor { send_request(td_api::make_object(to_integer(args))); } else if (op == "rbgs") { send_request(td_api::make_object()); - } else if (op == "gcts") { - send_request(td_api::make_object()); } else if (op == "gcos") { send_request(td_api::make_object()); } else if (op == "gcoc") {