Use help.getPeerColors to update accent colors.

This commit is contained in:
levlam 2023-11-27 16:35:27 +03:00
parent d08796fade
commit fbcd4c7198
5 changed files with 118 additions and 76 deletions

View File

@ -1486,9 +1486,6 @@ void ConfigManager::process_app_config(tl_object_ptr<telegram_api::JSONValue> &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<AccentColorId, vector<int32>, AccentColorIdHash> light_colors;
FlatHashMap<AccentColorId, vector<int32>, AccentColorIdHash> dark_colors;
vector<AccentColorId> 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<telegram_api::JSONValue> &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<telegram_api::jsonObject *>(value)->value_);
for (auto &peer_color_id : peer_color_ids) {
CHECK(peer_color_id != nullptr);
auto r_accent_color_id = to_integer_safe<int32>(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<telegram_api::jsonArray *>(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<uint32>(color_str);
if (r_color.is_ok() && r_color.ok() <= 0xFFFFFF) {
colors.push_back(static_cast<int32>(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<telegram_api::jsonArray *>(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<telegram_api::JSONValue> &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);

View File

@ -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<telegram_api::JSONValue> config_;

View File

@ -52,6 +52,32 @@ class GetChatThemesQuery final : public Td::ResultHandler {
}
};
class GetPeerColorsQuery final : public Td::ResultHandler {
Promise<telegram_api::object_ptr<telegram_api::help_PeerColors>> promise_;
public:
explicit GetPeerColorsQuery(Promise<telegram_api::object_ptr<telegram_api::help_PeerColors>> &&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<telegram_api::help_getPeerColors>(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 <class StorerT>
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<int32>(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 <class ParserT>
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<telegram_api::theme>
promise.set_value(Unit());
}
void ThemeManager::on_update_accent_colors(FlatHashMap<AccentColorId, vector<int32>, AccentColorIdHash> light_colors,
bool ThemeManager::on_update_accent_colors(FlatHashMap<AccentColorId, vector<int32>, AccentColorIdHash> light_colors,
FlatHashMap<AccentColorId, vector<int32>, AccentColorIdHash> dark_colors,
vector<AccentColorId> accent_color_ids) {
auto are_equal = [](const FlatHashMap<AccentColorId, vector<int32>, AccentColorIdHash> &lhs,
@ -301,7 +344,7 @@ void ThemeManager::on_update_accent_colors(FlatHashMap<AccentColorId, vector<int
};
if (accent_color_ids == accent_colors_.accent_color_ids_ && are_equal(light_colors, accent_colors_.light_colors_) &&
are_equal(dark_colors, accent_colors_.dark_colors_)) {
return;
return false;
}
for (auto &it : light_colors) {
accent_colors_.light_colors_[it.first] = std::move(it.second);
@ -313,6 +356,7 @@ void ThemeManager::on_update_accent_colors(FlatHashMap<AccentColorId, vector<int
save_accent_colors();
send_update_accent_colors();
return true;
}
namespace {
@ -538,6 +582,67 @@ void ThemeManager::on_get_chat_themes(Result<telegram_api::object_ptr<telegram_a
send_update_chat_themes();
}
void ThemeManager::reload_accent_colors() {
auto request_promise = PromiseCreator::lambda(
[actor_id = actor_id(this)](Result<telegram_api::object_ptr<telegram_api::help_PeerColors>> result) {
send_closure(actor_id, &ThemeManager::on_get_accent_colors, std::move(result));
});
td_->create_handler<GetPeerColorsQuery>(std::move(request_promise))->send(accent_colors_.hash_);
}
void ThemeManager::on_get_accent_colors(Result<telegram_api::object_ptr<telegram_api::help_PeerColors>> 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<telegram_api::help_peerColors>(peer_colors_ptr);
FlatHashMap<AccentColorId, vector<int32>, AccentColorIdHash> light_colors;
FlatHashMap<AccentColorId, vector<int32>, AccentColorIdHash> dark_colors;
vector<AccentColorId> 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<telegram_api::help_peerColorSet>(option->colors_);
light_colors[accent_color_id] = std::move(colors->colors_);
}
if (option->dark_colors_ != nullptr) {
auto colors = telegram_api::move_object_as<telegram_api::help_peerColorSet>(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<telegram_api::BaseTheme> &base_theme) {
CHECK(base_theme != nullptr);

View File

@ -32,9 +32,7 @@ class ThemeManager final : public Actor {
void reload_chat_themes();
void on_update_accent_colors(FlatHashMap<AccentColorId, vector<int32>, AccentColorIdHash> light_colors,
FlatHashMap<AccentColorId, vector<int32>, AccentColorIdHash> dark_colors,
vector<AccentColorId> accent_color_ids);
void reload_accent_colors();
static string get_theme_parameters_json_string(const td_api::object_ptr<td_api::themeParameters> &theme,
bool for_web_view);
@ -95,6 +93,7 @@ class ThemeManager final : public Actor {
FlatHashMap<AccentColorId, vector<int32>, AccentColorIdHash> light_colors_;
FlatHashMap<AccentColorId, vector<int32>, AccentColorIdHash> dark_colors_;
vector<AccentColorId> accent_color_ids_;
int32 hash_ = 0;
td_api::object_ptr<td_api::updateAccentColors> get_update_accent_colors_object() const;
@ -117,6 +116,12 @@ class ThemeManager final : public Actor {
void on_get_chat_themes(Result<telegram_api::object_ptr<telegram_api::account_Themes>> result);
bool on_update_accent_colors(FlatHashMap<AccentColorId, vector<int32>, AccentColorIdHash> light_colors,
FlatHashMap<AccentColorId, vector<int32>, AccentColorIdHash> dark_colors,
vector<AccentColorId> accent_color_ids);
void on_get_accent_colors(Result<telegram_api::object_ptr<telegram_api::help_PeerColors>> result);
td_api::object_ptr<td_api::themeSettings> get_theme_settings_object(const ThemeSettings &settings) const;
td_api::object_ptr<td_api::chatTheme> get_chat_theme_object(const ChatTheme &theme) const;

View File

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