From a156f3dee6544543f3e6a57d245fd7f4e35b36e3 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 10 May 2019 23:55:26 +0300 Subject: [PATCH] Add td_api::removeBackground. GitOrigin-RevId: f4a2e293f003207a7f224de7fc8dd3de04a9f942 --- td/generate/scheme/td_api.tl | 6 ++- td/generate/scheme/td_api.tlo | Bin 154216 -> 154316 bytes td/telegram/BackgroundManager.cpp | 67 ++++++++++++++++++++++++++++++ td/telegram/BackgroundManager.h | 4 ++ td/telegram/Td.cpp | 18 ++++++++ td/telegram/Td.h | 2 + td/telegram/cli.cpp | 2 + 7 files changed, 98 insertions(+), 1 deletion(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 1bd57b99..613f05e4 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -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; diff --git a/td/generate/scheme/td_api.tlo b/td/generate/scheme/td_api.tlo index edafa4149931b2b850b024d1916900f131c44067..81bb5af94a55126fddb303a3bbe913eb7d9758e6 100644 GIT binary patch delta 61 zcmV-D0K)(1vp@XLoa%F9Ac4b0gV{2z}Z*^{Dm%-Kn T6q5mwE0=(_0T8$N7y@uXE2SAk delta 25 hcmX@JmGi|G&W0_F0^1k^whL}!e4eyDMv|$(5de`e3L^jj diff --git a/td/telegram/BackgroundManager.cpp b/td/telegram/BackgroundManager.cpp index fe06a5a3..608b77e7 100644 --- a/td/telegram/BackgroundManager.cpp +++ b/td/telegram/BackgroundManager.cpp @@ -167,6 +167,38 @@ class UploadBackgroundQuery : public Td::ResultHandler { } }; +class SaveBackgroundQuery : public Td::ResultHandler { + Promise promise_; + + public: + explicit SaveBackgroundQuery(Promise &&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(background_id.get(), access_hash), unsave, + get_input_wallpaper_settings(type))))); + } + + void on_result(uint64 id, BufferSlice packet) override { + auto result_ptr = fetch_result(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 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 &&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 &&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(std::move(query_promise)) + ->send(background_id, background->access_hash, background->type, true); +} + +void BackgroundManager::on_removed_background(BackgroundId background_id, Result &&result, + Promise &&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]; diff --git a/td/telegram/BackgroundManager.h b/td/telegram/BackgroundManager.h index edca3161..79060c3a 100644 --- a/td/telegram/BackgroundManager.h +++ b/td/telegram/BackgroundManager.h @@ -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 &&promise); + td_api::object_ptr get_background_object(BackgroundId background_id) const; td_api::object_ptr get_backgrounds_object() const; @@ -107,6 +109,8 @@ class BackgroundManager : public Actor { BackgroundId set_background(BackgroundId background_id, const BackgroundType &type, Promise &&promise); + void on_removed_background(BackgroundId background_id, Result &&result, Promise &&promise); + void upload_background_file(FileId file_id, const BackgroundType &type, Promise &&promise); void on_upload_background_file(FileId file_id, tl_object_ptr input_file); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 90b75432..d7af1eb4 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -2943,6 +2943,19 @@ class SetBackgroundRequest : public RequestActor<> { } }; +class RemoveBackgroundRequest : public RequestOnceActor { + BackgroundId background_id_; + + void do_run(Promise &&promise) override { + td->background_manager_->remove_background(background_id_, std::move(promise)); + } + + public: + RemoveBackgroundRequest(ActorShared td, uint64 request_id, int64 background_id) + : RequestOnceActor(std::move(td), request_id), background_id_(background_id) { + } +}; + class GetRecentlyVisitedTMeUrlsRequest : public RequestActor> { 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_); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index cbd3f677..474a3917 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -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); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 5fef1703..0d1f7172 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -2001,6 +2001,8 @@ class CliClient final : public Actor { send_request(td_api::make_object( td_api::make_object(to_integer(args)), td_api::make_object(true, 0xabcdef, 49))); + } else if (op == "rbg") { + send_request(td_api::make_object(to_integer(args))); } else if (op == "gccode") { send_request(td_api::make_object()); } else if (op == "git") {