Add td_api::removeBackground.

GitOrigin-RevId: f4a2e293f003207a7f224de7fc8dd3de04a9f942
This commit is contained in:
levlam 2019-05-10 23:55:26 +03:00
parent db2ac0ed98
commit a156f3dee6
7 changed files with 98 additions and 1 deletions

View File

@ -3624,9 +3624,13 @@ getBackgroundUrl name:string type:BackgroundType = HttpUrl;
//@description Searches for a background by its name @name The name of the background
searchBackground name:string = Background;
//@description Sets background as a chosen by the user background @background The input background to use, null for solid backgrounds @type Background type
//@description Sets background as a selected by the user background; adds background to the list of installed backgrounds
//@background The input background to use, null for solid backgrounds @type Background type
setBackground background:InputBackground type:BackgroundType = Background;
//@description Removes background from the list of installed backgrounds @background_id The background indentifier
removeBackground background_id:int64 = Ok;
//@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;

Binary file not shown.

View File

@ -167,6 +167,38 @@ class UploadBackgroundQuery : public Td::ResultHandler {
}
};
class SaveBackgroundQuery : public Td::ResultHandler {
Promise<Unit> promise_;
public:
explicit SaveBackgroundQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(BackgroundId background_id, int64 access_hash, const BackgroundType &type, bool unsave) {
send_query(G()->net_query_creator().create(create_storer(telegram_api::account_saveWallPaper(
telegram_api::make_object<telegram_api::inputWallPaper>(background_id.get(), access_hash), unsave,
get_input_wallpaper_settings(type)))));
}
void on_result(uint64 id, BufferSlice packet) override {
auto result_ptr = fetch_result<telegram_api::account_saveWallPaper>(packet);
if (result_ptr.is_error()) {
return on_error(id, result_ptr.move_as_error());
}
bool result = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for fave background: " << result;
promise_.set_value(Unit());
}
void on_error(uint64 id, Status status) override {
if (!G()->close_flag()) {
LOG(ERROR) << "Receive error for save background: " << status;
}
promise_.set_error(std::move(status));
}
};
class BackgroundManager::UploadBackgroundFileCallback : public FileManager::UploadCallback {
public:
void on_upload_ok(FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file) override {
@ -607,6 +639,41 @@ void BackgroundManager::on_uploaded_background_file(FileId file_id, const Backgr
promise.set_value(Unit());
}
void BackgroundManager::remove_background(BackgroundId background_id, Promise<Unit> &&promise) {
auto background = get_background(background_id);
if (background == nullptr) {
return promise.set_error(Status::Error(400, "Background not found"));
}
auto query_promise = PromiseCreator::lambda(
[actor_id = actor_id(this), background_id, promise = std::move(promise)](Result<Unit> &&result) mutable {
send_closure(actor_id, &BackgroundManager::on_removed_background, background_id, std::move(result),
std::move(promise));
});
if (background->type.type == BackgroundType::Type::Solid) {
return query_promise.set_value(Unit());
}
td_->create_handler<SaveBackgroundQuery>(std::move(query_promise))
->send(background_id, background->access_hash, background->type, true);
}
void BackgroundManager::on_removed_background(BackgroundId background_id, Result<Unit> &&result,
Promise<Unit> &&promise) {
if (result.is_error()) {
return promise.set_error(result.move_as_error());
}
auto it = std::find(installed_background_ids_.begin(), installed_background_ids_.end(), background_id);
if (it != installed_background_ids_.end()) {
installed_background_ids_.erase(it);
}
if (background_id == set_background_id_) {
set_background_id(BackgroundId(), BackgroundType());
}
promise.set_value(Unit());
}
BackgroundManager::Background *BackgroundManager::add_background(BackgroundId background_id) {
CHECK(background_id.is_valid());
auto *result = &backgrounds_[background_id];

View File

@ -45,6 +45,8 @@ class BackgroundManager : public Actor {
void set_background_id(BackgroundId background_id, const BackgroundType &type);
void remove_background(BackgroundId background_id, Promise<Unit> &&promise);
td_api::object_ptr<td_api::background> get_background_object(BackgroundId background_id) const;
td_api::object_ptr<td_api::backgrounds> get_backgrounds_object() const;
@ -107,6 +109,8 @@ class BackgroundManager : public Actor {
BackgroundId set_background(BackgroundId background_id, const BackgroundType &type, Promise<Unit> &&promise);
void on_removed_background(BackgroundId background_id, Result<Unit> &&result, Promise<Unit> &&promise);
void upload_background_file(FileId file_id, const BackgroundType &type, Promise<Unit> &&promise);
void on_upload_background_file(FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file);

View File

@ -2943,6 +2943,19 @@ class SetBackgroundRequest : public RequestActor<> {
}
};
class RemoveBackgroundRequest : public RequestOnceActor {
BackgroundId background_id_;
void do_run(Promise<Unit> &&promise) override {
td->background_manager_->remove_background(background_id_, std::move(promise));
}
public:
RemoveBackgroundRequest(ActorShared<Td> td, uint64 request_id, int64 background_id)
: RequestOnceActor(std::move(td), request_id), background_id_(background_id) {
}
};
class GetRecentlyVisitedTMeUrlsRequest : public RequestActor<tl_object_ptr<td_api::tMeUrls>> {
string referrer_;
@ -7012,6 +7025,11 @@ void Td::on_request(uint64 id, td_api::setBackground &request) {
CREATE_REQUEST(SetBackgroundRequest, std::move(request.background_), std::move(request.type_));
}
void Td::on_request(uint64 id, td_api::removeBackground &request) {
CHECK_IS_USER();
CREATE_REQUEST(RemoveBackgroundRequest, request.background_id_);
}
void Td::on_request(uint64 id, td_api::getRecentlyVisitedTMeUrls &request) {
CHECK_IS_USER();
CLEAN_INPUT_STRING(request.referrer_);

View File

@ -930,6 +930,8 @@ class Td final : public NetQueryCallback {
void on_request(uint64 id, td_api::setBackground &request);
void on_request(uint64 id, td_api::removeBackground &request);
void on_request(uint64 id, td_api::getRecentlyVisitedTMeUrls &request);
void on_request(uint64 id, td_api::setBotUpdatesStatus &request);

View File

@ -2001,6 +2001,8 @@ class CliClient final : public Actor {
send_request(td_api::make_object<td_api::setBackground>(
td_api::make_object<td_api::inputBackgroundRemote>(to_integer<int64>(args)),
td_api::make_object<td_api::backgroundTypePattern>(true, 0xabcdef, 49)));
} else if (op == "rbg") {
send_request(td_api::make_object<td_api::removeBackground>(to_integer<int64>(args)));
} else if (op == "gccode") {
send_request(td_api::make_object<td_api::getCountryCode>());
} else if (op == "git") {