Replace getChatThemes with updateChatThemes.

This commit is contained in:
levlam 2021-09-13 15:55:01 +03:00
parent a838fff28f
commit 075d004b9f
10 changed files with 109 additions and 86 deletions

View File

@ -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<chatTheme> = ChatThemes;
//@description Contains a list of hashtags @hashtags A list of hashtags
hashtags hashtags:vector<string> = Hashtags;
@ -3817,6 +3814,9 @@ updateSavedAnimations animation_ids:vector<int32> = 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<chatTheme> = 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<languagePackString> = 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;

View File

@ -32,6 +32,7 @@
#include <algorithm>
#include <iterator>
#include <type_traits>
namespace td {

View File

@ -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_ptr<telegram_api::auth_Authoriz
td->messages_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);

View File

@ -1050,7 +1050,9 @@ string BackgroundManager::get_background_name_database_key(const string &name) {
std::pair<BackgroundId, BackgroundType> BackgroundManager::on_get_background(
BackgroundId expected_background_id, const string &expected_background_name,
telegram_api::object_ptr<telegram_api::WallPaper> 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<telegram_api::wallPaperNoFile>(wallpaper_ptr);
@ -1061,10 +1063,13 @@ std::pair<BackgroundId, BackgroundType> 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;

View File

@ -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_;

View File

@ -4334,6 +4334,7 @@ void Td::send_update(tl_object_ptr<td_api::Update> &&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_);

View File

@ -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);

View File

@ -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<td_api::object_ptr<td_api::chatThemes>> &&promise) {
if (Time::now() < chat_themes_.next_reload_time) {
return promise.set_value(get_chat_themes_object());
void ThemeManager::loop() {
if (!td_->auth_manager_->is_authorized() || td_->auth_manager_->is_bot()) {
return;
}
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));
if (Time::now() < chat_themes_.next_reload_time) {
return set_timeout_at(chat_themes_.next_reload_time);
}
if (pending_get_chat_themes_queries_.size() == 1) {
auto request_promise = PromiseCreator::lambda(
[actor_id = actor_id(this)](Result<telegram_api::object_ptr<telegram_api::account_ChatThemes>> result) {
send_closure(actor_id, &ThemeManager::on_get_chat_themes, std::move(result));
});
td_->create_handler<GetChatThemesQuery>(std::move(request_promise))->send(chat_themes_.hash);
}
}
void ThemeManager::on_update_theme(telegram_api::object_ptr<telegram_api::theme> &&theme, Promise<Unit> &&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,31 +139,29 @@ td_api::object_ptr<td_api::chatTheme> ThemeManager::get_chat_theme_object(const
get_theme_settings_object(theme.dark_theme));
}
td_api::object_ptr<td_api::chatThemes> ThemeManager::get_chat_themes_object() const {
return td_api::make_object<td_api::chatThemes>(
td_api::object_ptr<td_api::updateChatThemes> ThemeManager::get_update_chat_themes_object() const {
return td_api::make_object<td_api::updateChatThemes>(
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<telegram_api::object_ptr<telegram_api::account_ChatThemes>> 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) {
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<telegram_api::account_chatThemes>(chat_themes_ptr);
chat_themes_.hash = chat_themes->hash_;
@ -166,15 +178,13 @@ void ThemeManager::on_get_chat_themes(Result<telegram_api::object_ptr<telegram_a
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<td_api::object_ptr<td_api::Update>> &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

View File

@ -25,10 +25,12 @@ class ThemeManager final : public Actor {
public:
ThemeManager(Td *td, ActorShared<> parent);
void get_chat_themes(Promise<td_api::object_ptr<td_api::chatThemes>> &&promise);
void init();
void on_update_theme(telegram_api::object_ptr<telegram_api::theme> &&theme, Promise<Unit> &&promise);
void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const;
private:
enum class BaseTheme : int32 { Classic, Day, Night, Tinted, Arctic };
@ -61,6 +63,10 @@ class ThemeManager final : public Actor {
vector<ChatTheme> themes;
};
void start_up() final;
void loop() final;
void tear_down() final;
void on_get_chat_themes(Result<telegram_api::object_ptr<telegram_api::account_ChatThemes>> result);
@ -69,14 +75,14 @@ class ThemeManager final : public Actor {
td_api::object_ptr<td_api::chatTheme> get_chat_theme_object(const ChatTheme &theme) const;
td_api::object_ptr<td_api::chatThemes> get_chat_themes_object() const;
td_api::object_ptr<td_api::updateChatThemes> get_update_chat_themes_object() const;
void send_update_chat_themes() const;
static BaseTheme get_base_theme(const telegram_api::object_ptr<telegram_api::BaseTheme> &base_theme);
ThemeSettings get_chat_theme_settings(telegram_api::object_ptr<telegram_api::themeSettings> settings);
vector<Promise<td_api::object_ptr<td_api::chatThemes>>> pending_get_chat_themes_queries_;
ChatThemes chat_themes_;
Td *td_;

View File

@ -2295,8 +2295,6 @@ class CliClient final : public Actor {
send_request(td_api::make_object<td_api::removeBackground>(to_integer<int64>(args)));
} else if (op == "rbgs") {
send_request(td_api::make_object<td_api::resetBackgrounds>());
} else if (op == "gcts") {
send_request(td_api::make_object<td_api::getChatThemes>());
} else if (op == "gcos") {
send_request(td_api::make_object<td_api::getCountries>());
} else if (op == "gcoc") {