Return BackgroundType from on_get_background.

This commit is contained in:
levlam 2021-08-24 17:13:51 +03:00
parent 8e7eba4f8c
commit bba085318e
4 changed files with 59 additions and 34 deletions

View File

@ -53,7 +53,7 @@ class GetBackgroundQuery final : public Td::ResultHandler {
telegram_api::object_ptr<telegram_api::InputWallPaper> &&input_wallpaper) {
background_id_ = background_id;
background_name_ = background_name;
LOG(INFO) << "Load " << background_id_ << "/" << background_name_ << " from server: " << to_string(input_wallpaper);
LOG(INFO) << "Load " << background_id_ << "/" << background_name_ << " from server";
send_query(G()->net_query_creator().create(telegram_api::account_getWallPaper(std::move(input_wallpaper))));
}
@ -145,7 +145,7 @@ class UploadBackgroundQuery final : public Td::ResultHandler {
type_ = type;
for_dark_theme_ = for_dark_theme;
send_query(G()->net_query_creator().create(telegram_api::account_uploadWallPaper(
std::move(input_file), type_.get_mime_type(), type.get_input_wallpaper_settings())));
std::move(input_file), type_.get_mime_type(), type_.get_input_wallpaper_settings())));
}
void on_result(uint64 id, BufferSlice packet) final {
@ -549,7 +549,8 @@ void BackgroundManager::on_load_background_from_database(string name, string val
td_api::object_ptr<td_api::updateSelectedBackground> BackgroundManager::get_update_selected_background_object(
bool for_dark_theme) const {
return td_api::make_object<td_api::updateSelectedBackground>(
for_dark_theme, get_background_object(set_background_id_[for_dark_theme], for_dark_theme));
for_dark_theme,
get_background_object(set_background_id_[for_dark_theme], for_dark_theme, &set_background_type_[for_dark_theme]));
}
void BackgroundManager::send_update_selected_background(bool for_dark_theme) const {
@ -714,8 +715,15 @@ void BackgroundManager::on_installed_background(BackgroundId background_id, Back
return promise.set_error(result.move_as_error());
}
if (!td::contains(installed_background_ids_, background_id)) {
installed_background_ids_.insert(installed_background_ids_.begin(), background_id);
size_t i;
for (i = 0; i < installed_backgrounds_.size(); i++) {
if (installed_backgrounds_[i].first == background_id) {
installed_backgrounds_[i].second = type;
break;
}
}
if (i == installed_backgrounds_.size()) {
installed_backgrounds_.insert(installed_backgrounds_.begin(), {background_id, type});
}
set_background_id(background_id, type, for_dark_theme);
promise.set_value(Unit());
@ -841,11 +849,14 @@ void BackgroundManager::on_uploaded_background_file(FileId file_id, const Backgr
Promise<Unit> &&promise) {
CHECK(wallpaper != nullptr);
BackgroundId background_id = on_get_background(BackgroundId(), string(), std::move(wallpaper));
auto added_background = on_get_background(BackgroundId(), string(), std::move(wallpaper));
auto background_id = added_background.first;
if (!background_id.is_valid()) {
td_->file_manager_->cancel_upload(file_id);
return promise.set_error(Status::Error(500, "Receive wrong uploaded background"));
}
LOG_IF(ERROR, added_background.second != type)
<< "Type of uploaded background has changed from " << type << " to " << added_background.second;
const auto *background = get_background(background_id);
CHECK(background != nullptr);
@ -888,7 +899,8 @@ void BackgroundManager::on_removed_background(BackgroundId background_id, Result
if (result.is_error()) {
return promise.set_error(result.move_as_error());
}
td::remove(installed_background_ids_, background_id);
td::remove_if(installed_backgrounds_,
[background_id](const auto &background) { return background.first == background_id; });
if (background_id == set_background_id_[0]) {
set_background_id(BackgroundId(), BackgroundType(), false);
}
@ -919,7 +931,7 @@ void BackgroundManager::on_reset_background(Result<Unit> &&result, Promise<Unit>
if (result.is_error()) {
return promise.set_error(result.move_as_error());
}
installed_background_ids_.clear();
installed_backgrounds_.clear();
set_background_id(BackgroundId(), BackgroundType(), false);
set_background_id(BackgroundId(), BackgroundType(), true);
if (!local_background_ids_[0].empty()) {
@ -1031,9 +1043,9 @@ string BackgroundManager::get_background_name_database_key(const string &name) {
return PSTRING() << "bgn" << name;
}
BackgroundId BackgroundManager::on_get_background(BackgroundId expected_background_id,
const string &expected_background_name,
telegram_api::object_ptr<telegram_api::WallPaper> wallpaper_ptr) {
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) {
CHECK(wallpaper_ptr != nullptr);
if (wallpaper_ptr->get_id() == telegram_api::wallPaperNoFile::ID) {
@ -1041,13 +1053,13 @@ BackgroundId BackgroundManager::on_get_background(BackgroundId expected_backgrou
if (wallpaper->settings_ == nullptr) {
LOG(ERROR) << "Receive wallPaperNoFile without settings: " << to_string(wallpaper);
return BackgroundId();
return {};
}
auto background_id = BackgroundId(wallpaper->id_);
if (!background_id.is_valid() || background_id.is_local()) {
LOG(ERROR) << "Receive " << to_string(wallpaper);
return BackgroundId();
return {};
}
Background background;
@ -1059,14 +1071,14 @@ BackgroundId BackgroundManager::on_get_background(BackgroundId expected_backgrou
background.name = background.type.get_link();
add_background(background);
return background_id;
return {background_id, background.type};
}
auto wallpaper = move_tl_object_as<telegram_api::wallPaper>(wallpaper_ptr);
auto background_id = BackgroundId(wallpaper->id_);
if (!background_id.is_valid() || background_id.is_local() || is_background_name_local(wallpaper->slug_)) {
LOG(ERROR) << "Receive " << to_string(wallpaper);
return BackgroundId();
return {};
}
if (expected_background_id.is_valid() && background_id != expected_background_id) {
LOG(ERROR) << "Expected " << expected_background_id << ", but receive " << to_string(wallpaper);
@ -1075,7 +1087,7 @@ BackgroundId BackgroundManager::on_get_background(BackgroundId expected_backgrou
int32 document_id = wallpaper->document_->get_id();
if (document_id == telegram_api::documentEmpty::ID) {
LOG(ERROR) << "Receive " << to_string(wallpaper);
return BackgroundId();
return {};
}
CHECK(document_id == telegram_api::document::ID);
@ -1087,7 +1099,7 @@ BackgroundId BackgroundManager::on_get_background(BackgroundId expected_backgrou
Document::Type::General, true, is_pattern);
if (!document.file_id.is_valid()) {
LOG(ERROR) << "Receive wrong document in " << to_string(wallpaper);
return BackgroundId();
return {};
}
CHECK(document.type == Document::Type::General); // guaranteed by is_background parameter to on_get_document
@ -1114,7 +1126,7 @@ BackgroundId BackgroundManager::on_get_background(BackgroundId expected_backgrou
log_event_store(background).as_slice().str(), Auto());
}
return background_id;
return {background_id, background.type};
}
void BackgroundManager::on_get_backgrounds(Result<telegram_api::object_ptr<telegram_api::account_WallPapers>> result) {
@ -1123,7 +1135,7 @@ void BackgroundManager::on_get_backgrounds(Result<telegram_api::object_ptr<teleg
reset_to_empty(pending_get_backgrounds_queries_);
if (result.is_error()) {
// do not clear installed_background_ids_
// do not clear installed_backgrounds_
auto error = result.move_as_error();
for (auto &promise : promises) {
@ -1141,12 +1153,12 @@ void BackgroundManager::on_get_backgrounds(Result<telegram_api::object_ptr<teleg
return;
}
installed_background_ids_.clear();
installed_backgrounds_.clear();
auto wallpapers = telegram_api::move_object_as<telegram_api::account_wallPapers>(wallpapers_ptr);
for (auto &wallpaper : wallpapers->wallpapers_) {
auto background_id = on_get_background(BackgroundId(), string(), std::move(wallpaper));
if (background_id.is_valid()) {
installed_background_ids_.push_back(background_id);
auto background = on_get_background(BackgroundId(), string(), std::move(wallpaper));
if (background.first.is_valid()) {
installed_backgrounds_.push_back(std::move(background));
}
}
@ -1179,16 +1191,24 @@ td_api::object_ptr<td_api::background> BackgroundManager::get_background_object(
}
td_api::object_ptr<td_api::backgrounds> BackgroundManager::get_backgrounds_object(bool for_dark_theme) const {
auto backgrounds = transform(installed_background_ids_, [this, for_dark_theme](BackgroundId background_id) {
return get_background_object(background_id, for_dark_theme);
});
auto backgrounds = transform(installed_backgrounds_,
[this, for_dark_theme](const std::pair<BackgroundId, BackgroundType> &background) {
return get_background_object(background.first, for_dark_theme, &background.second);
});
auto background_id = set_background_id_[for_dark_theme];
if (background_id.is_valid() && !td::contains(installed_background_ids_, background_id)) {
backgrounds.push_back(get_background_object(background_id, for_dark_theme));
bool have_background = false;
for (const auto &background : installed_backgrounds_) {
if (background_id == background.first) {
have_background = true;
break;
}
}
if (background_id.is_valid() && !have_background) {
backgrounds.push_back(get_background_object(background_id, for_dark_theme, nullptr));
}
for (auto local_background_id : local_background_ids_[for_dark_theme]) {
if (local_background_id != background_id) {
backgrounds.push_back(get_background_object(local_background_id, for_dark_theme));
backgrounds.push_back(get_background_object(local_background_id, for_dark_theme, nullptr));
}
}
std::stable_sort(backgrounds.begin(), backgrounds.end(),

View File

@ -51,12 +51,13 @@ class BackgroundManager final : public Actor {
void reset_backgrounds(Promise<Unit> &&promise);
td_api::object_ptr<td_api::background> get_background_object(BackgroundId background_id, bool for_dark_theme,
const BackgroundType *type = nullptr) const;
const BackgroundType *type) const;
td_api::object_ptr<td_api::backgrounds> get_backgrounds_object(bool for_dark_theme) const;
BackgroundId on_get_background(BackgroundId expected_background_id, const string &expected_background_name,
telegram_api::object_ptr<telegram_api::WallPaper> wallpaper_ptr);
std::pair<BackgroundId, BackgroundType> on_get_background(
BackgroundId expected_background_id, const string &expected_background_name,
telegram_api::object_ptr<telegram_api::WallPaper> wallpaper_ptr);
FileSourceId get_background_file_source_id(BackgroundId background_id, int64 access_hash);
@ -167,7 +168,7 @@ class BackgroundManager final : public Actor {
BackgroundId set_background_id_[2];
BackgroundType set_background_type_[2];
vector<BackgroundId> installed_background_ids_;
vector<std::pair<BackgroundId, BackgroundType>> installed_backgrounds_;
vector<Promise<Unit>> pending_get_backgrounds_queries_;

View File

@ -125,6 +125,10 @@ class BackgroundType {
bool operator==(const BackgroundType &lhs, const BackgroundType &rhs);
inline bool operator!=(const BackgroundType &lhs, const BackgroundType &rhs) {
return !(lhs == rhs);
}
StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType &type);
} // namespace td

View File

@ -2901,7 +2901,7 @@ class SetBackgroundRequest final : public RequestActor<> {
}
void do_send_result() final {
send_result(td->background_manager_->get_background_object(background_id_, for_dark_theme_));
send_result(td->background_manager_->get_background_object(background_id_, for_dark_theme_, nullptr));
}
public: