Use request promise in setBackground.
This commit is contained in:
parent
63b528f213
commit
f085e7eea3
@ -126,13 +126,14 @@ class InstallBackgroundQuery final : public Td::ResultHandler {
|
|||||||
};
|
};
|
||||||
|
|
||||||
class UploadBackgroundQuery final : public Td::ResultHandler {
|
class UploadBackgroundQuery final : public Td::ResultHandler {
|
||||||
Promise<Unit> promise_;
|
Promise<td_api::object_ptr<td_api::background>> promise_;
|
||||||
FileId file_id_;
|
FileId file_id_;
|
||||||
BackgroundType type_;
|
BackgroundType type_;
|
||||||
bool for_dark_theme_;
|
bool for_dark_theme_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit UploadBackgroundQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
explicit UploadBackgroundQuery(Promise<td_api::object_ptr<td_api::background>> &&promise)
|
||||||
|
: promise_(std::move(promise)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void send(FileId file_id, tl_object_ptr<telegram_api::InputFile> &&input_file, const BackgroundType &type,
|
void send(FileId file_id, tl_object_ptr<telegram_api::InputFile> &&input_file, const BackgroundType &type,
|
||||||
@ -605,15 +606,14 @@ BackgroundId BackgroundManager::add_local_background(const BackgroundType &type)
|
|||||||
return background.id;
|
return background.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
BackgroundId BackgroundManager::set_background(const td_api::InputBackground *input_background,
|
void BackgroundManager::set_background(const td_api::InputBackground *input_background,
|
||||||
const td_api::BackgroundType *background_type, bool for_dark_theme,
|
const td_api::BackgroundType *background_type, bool for_dark_theme,
|
||||||
Promise<Unit> &&promise) {
|
Promise<td_api::object_ptr<td_api::background>> &&promise) {
|
||||||
BackgroundType type;
|
BackgroundType type;
|
||||||
if (background_type != nullptr) {
|
if (background_type != nullptr) {
|
||||||
auto r_type = BackgroundType::get_background_type(background_type);
|
auto r_type = BackgroundType::get_background_type(background_type);
|
||||||
if (r_type.is_error()) {
|
if (r_type.is_error()) {
|
||||||
promise.set_error(r_type.move_as_error());
|
return promise.set_error(r_type.move_as_error());
|
||||||
return BackgroundId();
|
|
||||||
}
|
}
|
||||||
type = r_type.move_as_ok();
|
type = r_type.move_as_ok();
|
||||||
} else {
|
} else {
|
||||||
@ -623,12 +623,10 @@ BackgroundId BackgroundManager::set_background(const td_api::InputBackground *in
|
|||||||
if (input_background == nullptr) {
|
if (input_background == nullptr) {
|
||||||
if (background_type == nullptr) {
|
if (background_type == nullptr) {
|
||||||
set_background_id(BackgroundId(), BackgroundType(), for_dark_theme);
|
set_background_id(BackgroundId(), BackgroundType(), for_dark_theme);
|
||||||
promise.set_value(Unit());
|
return promise.set_value(nullptr);
|
||||||
return BackgroundId();
|
|
||||||
}
|
}
|
||||||
if (type.has_file()) {
|
if (type.has_file()) {
|
||||||
promise.set_error(Status::Error(400, "Input background must be non-empty for the background type"));
|
return promise.set_error(Status::Error(400, "Input background must be non-empty for the background type"));
|
||||||
return BackgroundId();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto background_id = add_local_background(type);
|
auto background_id = add_local_background(type);
|
||||||
@ -637,23 +635,20 @@ BackgroundId BackgroundManager::set_background(const td_api::InputBackground *in
|
|||||||
local_background_ids_[for_dark_theme].insert(local_background_ids_[for_dark_theme].begin(), background_id);
|
local_background_ids_[for_dark_theme].insert(local_background_ids_[for_dark_theme].begin(), background_id);
|
||||||
save_local_backgrounds(for_dark_theme);
|
save_local_backgrounds(for_dark_theme);
|
||||||
|
|
||||||
promise.set_value(Unit());
|
return promise.set_value(get_background_object(background_id, for_dark_theme, nullptr));
|
||||||
return background_id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (input_background->get_id()) {
|
switch (input_background->get_id()) {
|
||||||
case td_api::inputBackgroundLocal::ID: {
|
case td_api::inputBackgroundLocal::ID: {
|
||||||
if (!type.has_file()) {
|
if (!type.has_file()) {
|
||||||
promise.set_error(Status::Error(400, "Can't specify local file for the background type"));
|
return promise.set_error(Status::Error(400, "Can't specify local file for the background type"));
|
||||||
return BackgroundId();
|
|
||||||
}
|
}
|
||||||
CHECK(background_type != nullptr);
|
CHECK(background_type != nullptr);
|
||||||
|
|
||||||
auto background_local = static_cast<const td_api::inputBackgroundLocal *>(input_background);
|
auto background_local = static_cast<const td_api::inputBackgroundLocal *>(input_background);
|
||||||
auto r_file_id = prepare_input_file(background_local->background_);
|
auto r_file_id = prepare_input_file(background_local->background_);
|
||||||
if (r_file_id.is_error()) {
|
if (r_file_id.is_error()) {
|
||||||
promise.set_error(r_file_id.move_as_error());
|
return promise.set_error(r_file_id.move_as_error());
|
||||||
return BackgroundId();
|
|
||||||
}
|
}
|
||||||
auto file_id = r_file_id.move_as_ok();
|
auto file_id = r_file_id.move_as_ok();
|
||||||
LOG(INFO) << "Receive file " << file_id << " for input background";
|
LOG(INFO) << "Receive file " << file_id << " for input background";
|
||||||
@ -675,34 +670,29 @@ BackgroundId BackgroundManager::set_background(const td_api::InputBackground *in
|
|||||||
default:
|
default:
|
||||||
UNREACHABLE();
|
UNREACHABLE();
|
||||||
}
|
}
|
||||||
return BackgroundId();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BackgroundId BackgroundManager::set_background(BackgroundId background_id, BackgroundType type, bool for_dark_theme,
|
void BackgroundManager::set_background(BackgroundId background_id, BackgroundType type, bool for_dark_theme,
|
||||||
Promise<Unit> &&promise) {
|
Promise<td_api::object_ptr<td_api::background>> &&promise) {
|
||||||
LOG(INFO) << "Set " << background_id << " with " << type;
|
LOG(INFO) << "Set " << background_id << " with " << type;
|
||||||
const auto *background = get_background(background_id);
|
const auto *background = get_background(background_id);
|
||||||
if (background == nullptr) {
|
if (background == nullptr) {
|
||||||
promise.set_error(Status::Error(400, "Background to set not found"));
|
return promise.set_error(Status::Error(400, "Background to set not found"));
|
||||||
return BackgroundId();
|
|
||||||
}
|
}
|
||||||
if (!type.has_file()) {
|
if (!type.has_file()) {
|
||||||
type = background->type;
|
type = background->type;
|
||||||
} else if (!background->type.has_equal_type(type)) {
|
} else if (!background->type.has_equal_type(type)) {
|
||||||
promise.set_error(Status::Error(400, "Background type mismatch"));
|
return promise.set_error(Status::Error(400, "Background type mismatch"));
|
||||||
return BackgroundId();
|
|
||||||
}
|
}
|
||||||
if (set_background_id_[for_dark_theme] == background_id && set_background_type_[for_dark_theme] == type) {
|
if (set_background_id_[for_dark_theme] == background_id && set_background_type_[for_dark_theme] == type) {
|
||||||
promise.set_value(Unit());
|
return promise.set_value(get_background_object(background_id, for_dark_theme, nullptr));
|
||||||
return background_id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG(INFO) << "Install " << background_id << " with " << type;
|
LOG(INFO) << "Install " << background_id << " with " << type;
|
||||||
|
|
||||||
if (!type.has_file()) {
|
if (!type.has_file()) {
|
||||||
set_background_id(background_id, type, for_dark_theme);
|
set_background_id(background_id, type, for_dark_theme);
|
||||||
promise.set_value(Unit());
|
return promise.set_value(get_background_object(background_id, for_dark_theme, nullptr));
|
||||||
return background_id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto query_promise = PromiseCreator::lambda([actor_id = actor_id(this), background_id, type, for_dark_theme,
|
auto query_promise = PromiseCreator::lambda([actor_id = actor_id(this), background_id, type, for_dark_theme,
|
||||||
@ -713,11 +703,11 @@ BackgroundId BackgroundManager::set_background(BackgroundId background_id, Backg
|
|||||||
td_->create_handler<InstallBackgroundQuery>(std::move(query_promise))
|
td_->create_handler<InstallBackgroundQuery>(std::move(query_promise))
|
||||||
->send(telegram_api::make_object<telegram_api::inputWallPaper>(background_id.get(), background->access_hash),
|
->send(telegram_api::make_object<telegram_api::inputWallPaper>(background_id.get(), background->access_hash),
|
||||||
type);
|
type);
|
||||||
return BackgroundId();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BackgroundManager::on_installed_background(BackgroundId background_id, BackgroundType type, bool for_dark_theme,
|
void BackgroundManager::on_installed_background(BackgroundId background_id, BackgroundType type, bool for_dark_theme,
|
||||||
Result<Unit> &&result, Promise<Unit> &&promise) {
|
Result<Unit> &&result,
|
||||||
|
Promise<td_api::object_ptr<td_api::background>> &&promise) {
|
||||||
if (result.is_error()) {
|
if (result.is_error()) {
|
||||||
return promise.set_error(result.move_as_error());
|
return promise.set_error(result.move_as_error());
|
||||||
}
|
}
|
||||||
@ -733,7 +723,7 @@ void BackgroundManager::on_installed_background(BackgroundId background_id, Back
|
|||||||
installed_backgrounds_.insert(installed_backgrounds_.begin(), {background_id, type});
|
installed_backgrounds_.insert(installed_backgrounds_.begin(), {background_id, type});
|
||||||
}
|
}
|
||||||
set_background_id(background_id, type, for_dark_theme);
|
set_background_id(background_id, type, for_dark_theme);
|
||||||
promise.set_value(Unit());
|
promise.set_value(get_background_object(background_id, for_dark_theme, nullptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
string BackgroundManager::get_background_database_key(bool for_dark_theme) {
|
string BackgroundManager::get_background_database_key(bool for_dark_theme) {
|
||||||
@ -790,7 +780,7 @@ void BackgroundManager::save_local_backgrounds(bool for_dark_theme) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void BackgroundManager::upload_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
|
void BackgroundManager::upload_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
|
||||||
Promise<Unit> &&promise) {
|
Promise<td_api::object_ptr<td_api::background>> &&promise) {
|
||||||
auto upload_file_id = td_->file_manager_->dup_file_id(file_id);
|
auto upload_file_id = td_->file_manager_->dup_file_id(file_id);
|
||||||
bool is_inserted =
|
bool is_inserted =
|
||||||
being_uploaded_files_.emplace(upload_file_id, UploadedFileInfo(type, for_dark_theme, std::move(promise))).second;
|
being_uploaded_files_.emplace(upload_file_id, UploadedFileInfo(type, for_dark_theme, std::move(promise))).second;
|
||||||
@ -836,14 +826,13 @@ void BackgroundManager::on_upload_background_file_error(FileId file_id, Status s
|
|||||||
|
|
||||||
void BackgroundManager::do_upload_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
|
void BackgroundManager::do_upload_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
|
||||||
tl_object_ptr<telegram_api::InputFile> &&input_file,
|
tl_object_ptr<telegram_api::InputFile> &&input_file,
|
||||||
Promise<Unit> &&promise) {
|
Promise<td_api::object_ptr<td_api::background>> &&promise) {
|
||||||
if (input_file == nullptr) {
|
if (input_file == nullptr) {
|
||||||
FileView file_view = td_->file_manager_->get_file_view(file_id);
|
FileView file_view = td_->file_manager_->get_file_view(file_id);
|
||||||
file_id = file_view.get_main_file_id();
|
file_id = file_view.get_main_file_id();
|
||||||
auto it = file_id_to_background_id_.find(file_id);
|
auto it = file_id_to_background_id_.find(file_id);
|
||||||
if (it != file_id_to_background_id_.end()) {
|
if (it != file_id_to_background_id_.end()) {
|
||||||
set_background(it->second, type, for_dark_theme, std::move(promise));
|
return set_background(it->second, type, for_dark_theme, std::move(promise));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
return promise.set_error(Status::Error(500, "Failed to reupload background"));
|
return promise.set_error(Status::Error(500, "Failed to reupload background"));
|
||||||
}
|
}
|
||||||
@ -854,7 +843,7 @@ void BackgroundManager::do_upload_background_file(FileId file_id, const Backgrou
|
|||||||
|
|
||||||
void BackgroundManager::on_uploaded_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
|
void BackgroundManager::on_uploaded_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
|
||||||
telegram_api::object_ptr<telegram_api::WallPaper> wallpaper,
|
telegram_api::object_ptr<telegram_api::WallPaper> wallpaper,
|
||||||
Promise<Unit> &&promise) {
|
Promise<td_api::object_ptr<td_api::background>> &&promise) {
|
||||||
CHECK(wallpaper != nullptr);
|
CHECK(wallpaper != nullptr);
|
||||||
|
|
||||||
auto added_background = on_get_background(BackgroundId(), string(), std::move(wallpaper), true);
|
auto added_background = on_get_background(BackgroundId(), string(), std::move(wallpaper), true);
|
||||||
@ -874,7 +863,7 @@ void BackgroundManager::on_uploaded_background_file(FileId file_id, const Backgr
|
|||||||
}
|
}
|
||||||
LOG_STATUS(td_->file_manager_->merge(background->file_id, file_id));
|
LOG_STATUS(td_->file_manager_->merge(background->file_id, file_id));
|
||||||
set_background_id(background_id, type, for_dark_theme);
|
set_background_id(background_id, type, for_dark_theme);
|
||||||
promise.set_value(Unit());
|
promise.set_value(get_background_object(background_id, for_dark_theme, nullptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BackgroundManager::remove_background(BackgroundId background_id, Promise<Unit> &&promise) {
|
void BackgroundManager::remove_background(BackgroundId background_id, Promise<Unit> &&promise) {
|
||||||
|
@ -39,9 +39,8 @@ class BackgroundManager final : public Actor {
|
|||||||
|
|
||||||
std::pair<BackgroundId, BackgroundType> search_background(const string &name, Promise<Unit> &&promise);
|
std::pair<BackgroundId, BackgroundType> search_background(const string &name, Promise<Unit> &&promise);
|
||||||
|
|
||||||
BackgroundId set_background(const td_api::InputBackground *input_background,
|
void set_background(const td_api::InputBackground *input_background, const td_api::BackgroundType *background_type,
|
||||||
const td_api::BackgroundType *background_type, bool for_dark_theme,
|
bool for_dark_theme, Promise<td_api::object_ptr<td_api::background>> &&promise);
|
||||||
Promise<Unit> &&promise);
|
|
||||||
|
|
||||||
void remove_background(BackgroundId background_id, Promise<Unit> &&promise);
|
void remove_background(BackgroundId background_id, Promise<Unit> &&promise);
|
||||||
|
|
||||||
@ -58,7 +57,7 @@ class BackgroundManager final : public Actor {
|
|||||||
|
|
||||||
void on_uploaded_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
|
void on_uploaded_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
|
||||||
telegram_api::object_ptr<telegram_api::WallPaper> wallpaper,
|
telegram_api::object_ptr<telegram_api::WallPaper> wallpaper,
|
||||||
Promise<Unit> &&promise);
|
Promise<td_api::object_ptr<td_api::background>> &&promise);
|
||||||
|
|
||||||
void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const;
|
void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const;
|
||||||
|
|
||||||
@ -135,11 +134,11 @@ class BackgroundManager final : public Actor {
|
|||||||
|
|
||||||
Result<FileId> prepare_input_file(const tl_object_ptr<td_api::InputFile> &input_file);
|
Result<FileId> prepare_input_file(const tl_object_ptr<td_api::InputFile> &input_file);
|
||||||
|
|
||||||
BackgroundId set_background(BackgroundId background_id, BackgroundType type, bool for_dark_theme,
|
void set_background(BackgroundId background_id, BackgroundType type, bool for_dark_theme,
|
||||||
Promise<Unit> &&promise);
|
Promise<td_api::object_ptr<td_api::background>> &&promise);
|
||||||
|
|
||||||
void on_installed_background(BackgroundId background_id, BackgroundType type, bool for_dark_theme,
|
void on_installed_background(BackgroundId background_id, BackgroundType type, bool for_dark_theme,
|
||||||
Result<Unit> &&result, Promise<Unit> &&promise);
|
Result<Unit> &&result, Promise<td_api::object_ptr<td_api::background>> &&promise);
|
||||||
|
|
||||||
void set_background_id(BackgroundId background_id, const BackgroundType &type, bool for_dark_theme);
|
void set_background_id(BackgroundId background_id, const BackgroundType &type, bool for_dark_theme);
|
||||||
|
|
||||||
@ -147,14 +146,16 @@ class BackgroundManager final : public Actor {
|
|||||||
|
|
||||||
void on_reset_background(Result<Unit> &&result, Promise<Unit> &&promise);
|
void on_reset_background(Result<Unit> &&result, Promise<Unit> &&promise);
|
||||||
|
|
||||||
void upload_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme, Promise<Unit> &&promise);
|
void upload_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
|
||||||
|
Promise<td_api::object_ptr<td_api::background>> &&promise);
|
||||||
|
|
||||||
void on_upload_background_file(FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file);
|
void on_upload_background_file(FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file);
|
||||||
|
|
||||||
void on_upload_background_file_error(FileId file_id, Status status);
|
void on_upload_background_file_error(FileId file_id, Status status);
|
||||||
|
|
||||||
void do_upload_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
|
void do_upload_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
|
||||||
tl_object_ptr<telegram_api::InputFile> &&input_file, Promise<Unit> &&promise);
|
tl_object_ptr<telegram_api::InputFile> &&input_file,
|
||||||
|
Promise<td_api::object_ptr<td_api::background>> &&promise);
|
||||||
|
|
||||||
FlatHashMap<BackgroundId, unique_ptr<Background>, BackgroundIdHash> backgrounds_;
|
FlatHashMap<BackgroundId, unique_ptr<Background>, BackgroundIdHash> backgrounds_;
|
||||||
|
|
||||||
@ -180,9 +181,10 @@ class BackgroundManager final : public Actor {
|
|||||||
struct UploadedFileInfo {
|
struct UploadedFileInfo {
|
||||||
BackgroundType type_;
|
BackgroundType type_;
|
||||||
bool for_dark_theme_;
|
bool for_dark_theme_;
|
||||||
Promise<Unit> promise_;
|
Promise<td_api::object_ptr<td_api::background>> promise_;
|
||||||
|
|
||||||
UploadedFileInfo(BackgroundType type, bool for_dark_theme, Promise<Unit> &&promise)
|
UploadedFileInfo(BackgroundType type, bool for_dark_theme,
|
||||||
|
Promise<td_api::object_ptr<td_api::background>> &&promise)
|
||||||
: type_(type), for_dark_theme_(for_dark_theme), promise_(std::move(promise)) {
|
: type_(type), for_dark_theme_(for_dark_theme), promise_(std::move(promise)) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -2550,33 +2550,6 @@ class SearchBackgroundRequest final : public RequestActor<> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class SetBackgroundRequest final : public RequestActor<> {
|
|
||||||
td_api::object_ptr<td_api::InputBackground> input_background_;
|
|
||||||
td_api::object_ptr<td_api::BackgroundType> background_type_;
|
|
||||||
bool for_dark_theme_ = false;
|
|
||||||
|
|
||||||
BackgroundId background_id_;
|
|
||||||
|
|
||||||
void do_run(Promise<Unit> &&promise) final {
|
|
||||||
background_id_ = td_->background_manager_->set_background(input_background_.get(), background_type_.get(),
|
|
||||||
for_dark_theme_, std::move(promise));
|
|
||||||
}
|
|
||||||
|
|
||||||
void do_send_result() final {
|
|
||||||
send_result(td_->background_manager_->get_background_object(background_id_, for_dark_theme_, nullptr));
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
SetBackgroundRequest(ActorShared<Td> td, uint64 request_id,
|
|
||||||
td_api::object_ptr<td_api::InputBackground> &&input_background,
|
|
||||||
td_api::object_ptr<td_api::BackgroundType> background_type, bool for_dark_theme)
|
|
||||||
: RequestActor(std::move(td), request_id)
|
|
||||||
, input_background_(std::move(input_background))
|
|
||||||
, background_type_(std::move(background_type))
|
|
||||||
, for_dark_theme_(for_dark_theme) {
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Td::Td(unique_ptr<TdCallback> callback, Options options)
|
Td::Td(unique_ptr<TdCallback> callback, Options options)
|
||||||
: callback_(std::move(callback)), td_options_(std::move(options)) {
|
: callback_(std::move(callback)), td_options_(std::move(options)) {
|
||||||
CHECK(callback_ != nullptr);
|
CHECK(callback_ != nullptr);
|
||||||
@ -7898,8 +7871,9 @@ void Td::on_request(uint64 id, td_api::searchBackground &request) {
|
|||||||
|
|
||||||
void Td::on_request(uint64 id, td_api::setBackground &request) {
|
void Td::on_request(uint64 id, td_api::setBackground &request) {
|
||||||
CHECK_IS_USER();
|
CHECK_IS_USER();
|
||||||
CREATE_REQUEST(SetBackgroundRequest, std::move(request.background_), std::move(request.type_),
|
CREATE_REQUEST_PROMISE();
|
||||||
request.for_dark_theme_);
|
background_manager_->set_background(request.background_.get(), request.type_.get(), request.for_dark_theme_,
|
||||||
|
std::move(promise));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Td::on_request(uint64 id, const td_api::removeBackground &request) {
|
void Td::on_request(uint64 id, const td_api::removeBackground &request) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user