Save accent colors to binlog.

This commit is contained in:
levlam 2023-11-02 21:52:59 +03:00
parent 4489ef54aa
commit 3f8362a4bb
2 changed files with 75 additions and 0 deletions

View File

@ -138,6 +138,49 @@ void ThemeManager::ChatThemes::parse(ParserT &parser) {
td::parse(themes, parser);
}
template <class StorerT>
void ThemeManager::AccentColors::store(StorerT &storer) const {
BEGIN_STORE_FLAGS();
END_STORE_FLAGS();
td::store(static_cast<int32>(light_colors_.size()), storer);
for (auto &it : light_colors_) {
td::store(it.first, storer);
td::store(it.second, storer);
}
td::store(static_cast<int32>(dark_colors_.size()), storer);
for (auto &it : dark_colors_) {
td::store(it.first, storer);
td::store(it.second, storer);
}
td::store(accent_color_ids_, storer);
}
template <class ParserT>
void ThemeManager::AccentColors::parse(ParserT &parser) {
BEGIN_PARSE_FLAGS();
END_PARSE_FLAGS();
int32 size;
td::parse(size, parser);
for (int32 i = 0; i < size; i++) {
AccentColorId accent_color_id;
vector<int32> colors;
td::parse(accent_color_id, parser);
td::parse(colors, parser);
CHECK(accent_color_id.is_valid());
light_colors_.emplace(accent_color_id, std::move(colors));
}
td::parse(size, parser);
for (int32 i = 0; i < size; i++) {
AccentColorId accent_color_id;
vector<int32> colors;
td::parse(accent_color_id, parser);
td::parse(colors, parser);
CHECK(accent_color_id.is_valid());
dark_colors_.emplace(accent_color_id, std::move(colors));
}
td::parse(accent_color_ids_, parser);
}
ThemeManager::ThemeManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
}
@ -161,6 +204,18 @@ void ThemeManager::init() {
}
}
chat_themes_.next_reload_time = Time::now();
log_event_string = G()->td_db()->get_binlog_pmc()->get(get_accent_colors_database_key());
if (!log_event_string.empty()) {
auto status = log_event_parse(accent_colors_, log_event_string);
if (status.is_ok()) {
send_update_accent_colors();
} else {
LOG(ERROR) << "Failed to parse accent colors from binlog: " << status;
accent_colors_ = AccentColors();
}
}
loop();
}
@ -264,6 +319,7 @@ void ThemeManager::on_update_accent_colors(FlatHashMap<AccentColorId, vector<int
}
accent_colors_.accent_color_ids_ = std::move(accent_color_ids);
save_accent_colors();
send_update_accent_colors();
}
@ -371,10 +427,19 @@ string ThemeManager::get_chat_themes_database_key() {
return "chat_themes";
}
string ThemeManager::get_accent_colors_database_key() {
return "accent_colors";
}
void ThemeManager::save_chat_themes() {
G()->td_db()->get_binlog_pmc()->set(get_chat_themes_database_key(), log_event_store(chat_themes_).as_slice().str());
}
void ThemeManager::save_accent_colors() {
G()->td_db()->get_binlog_pmc()->set(get_accent_colors_database_key(),
log_event_store(accent_colors_).as_slice().str());
}
void ThemeManager::send_update_chat_themes() const {
send_closure(G()->td(), &Td::send_update, get_update_chat_themes_object());
}

View File

@ -95,6 +95,12 @@ class ThemeManager final : public Actor {
vector<AccentColorId> accent_color_ids_;
td_api::object_ptr<td_api::updateAccentColors> get_update_accent_colors_object() const;
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
void parse(ParserT &parser);
};
void start_up() final;
@ -115,8 +121,12 @@ class ThemeManager final : public Actor {
static string get_chat_themes_database_key();
string get_accent_colors_database_key();
void save_chat_themes();
void save_accent_colors();
void send_update_chat_themes() const;
static BaseTheme get_base_theme(const telegram_api::object_ptr<telegram_api::BaseTheme> &base_theme);