From 14f31d81c5fce285299c40bddb0005f65d9eda8b Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 23 Apr 2024 22:48:47 +0300 Subject: [PATCH] Add td_api::toggleHasSponsoredMessagesEnabled. --- td/generate/scheme/td_api.tl | 4 +++ td/telegram/Td.cpp | 6 +++++ td/telegram/Td.h | 2 ++ td/telegram/UserManager.cpp | 50 ++++++++++++++++++++++++++++++++++++ td/telegram/UserManager.h | 4 +++ td/telegram/cli.cpp | 2 ++ 6 files changed, 68 insertions(+) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 2ec0e268b..b63fa75f6 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -9788,6 +9788,10 @@ setEmojiStatus emoji_status:emojiStatus = Ok; //@description Changes the location of the current user. Needs to be called if getOption("is_location_visible") is true and location changes for more than 1 kilometer. Must not be called if the user has a business location @location The new location of the user setLocation location:location = Ok; +//@description Toggles whether the current user has sponsored messages enabled. The setting has no effect for users without Telegram Premium for which sponsored messages are always enabled +//@has_sponsored_messages_enabled Pass true to enable sponsored messages for the current user; false to disable them +toggleHasSponsoredMessagesEnabled has_sponsored_messages_enabled:Bool = Ok; + //@description Changes the business location of the current user. Requires Telegram Business subscription @location The new location of the business; pass null to remove the location setBusinessLocation location:businessLocation = Ok; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index b2fadb4dc..3e4786bbe 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -7775,6 +7775,12 @@ void Td::on_request(uint64 id, const td_api::setEmojiStatus &request) { user_manager_->set_emoji_status(EmojiStatus(request.emoji_status_), std::move(promise)); } +void Td::on_request(uint64 id, const td_api::toggleHasSponsoredMessagesEnabled &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + user_manager_->toggle_sponsored_messages(request.has_sponsored_messages_enabled_, std::move(promise)); +} + void Td::on_request(uint64 id, const td_api::getThemedEmojiStatuses &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 43a1cf047..21333185a 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1375,6 +1375,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::setEmojiStatus &request); + void on_request(uint64 id, const td_api::toggleHasSponsoredMessagesEnabled &request); + void on_request(uint64 id, const td_api::getThemedEmojiStatuses &request); void on_request(uint64 id, const td_api::getThemedChatEmojiStatuses &request); diff --git a/td/telegram/UserManager.cpp b/td/telegram/UserManager.cpp index 5595554b9..fd39a9ad6 100644 --- a/td/telegram/UserManager.cpp +++ b/td/telegram/UserManager.cpp @@ -1119,6 +1119,33 @@ class UpdateEmojiStatusQuery final : public Td::ResultHandler { } }; +class ToggleSponsoredMessagesQuery final : public Td::ResultHandler { + Promise promise_; + + public: + explicit ToggleSponsoredMessagesQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(bool sponsored_enabled) { + send_query( + G()->net_query_creator().create(telegram_api::account_toggleSponsoredMessages(sponsored_enabled), {{"me"}})); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + LOG(DEBUG) << "Receive result for ToggleSponsoredMessagesQuery: " << result_ptr.ok(); + promise_.set_value(Unit()); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + class GetUsersQuery final : public Td::ResultHandler { Promise promise_; @@ -5185,6 +5212,29 @@ void UserManager::on_set_emoji_status(EmojiStatus emoji_status, Promise && promise.set_value(Unit()); } +void UserManager::toggle_sponsored_messages(bool sponsored_enabled, Promise &&promise) { + auto query_promise = PromiseCreator::lambda( + [actor_id = actor_id(this), sponsored_enabled, promise = std::move(promise)](Result result) mutable { + if (result.is_ok()) { + send_closure(actor_id, &UserManager::on_toggle_sponsored_messages, sponsored_enabled, std::move(promise)); + } else { + promise.set_error(result.move_as_error()); + } + }); + td_->create_handler(std::move(query_promise))->send(sponsored_enabled); +} + +void UserManager::on_toggle_sponsored_messages(bool sponsored_enabled, Promise &&promise) { + auto my_user_id = get_my_id(); + UserFull *user_full = get_user_full_force(my_user_id, "on_toggle_sponsored_messages"); + if (user_full != nullptr && user_full->sponsored_enabled != sponsored_enabled) { + user_full->sponsored_enabled = sponsored_enabled; + user_full->is_changed = true; + update_user_full(user_full, my_user_id, "on_toggle_sponsored_messages"); + } + promise.set_value(Unit()); +} + void UserManager::get_support_user(Promise> &&promise) { if (support_user_id_.is_valid()) { return promise.set_value(get_user_object(support_user_id_)); diff --git a/td/telegram/UserManager.h b/td/telegram/UserManager.h index 4284a129d..82936bc15 100644 --- a/td/telegram/UserManager.h +++ b/td/telegram/UserManager.h @@ -355,6 +355,8 @@ class UserManager final : public Actor { void set_emoji_status(const EmojiStatus &emoji_status, Promise &&promise); + void toggle_sponsored_messages(bool sponsored_enabled, Promise &&promise); + void get_support_user(Promise> &&promise); void get_user_profile_photos(UserId user_id, int32 offset, int32 limit, @@ -866,6 +868,8 @@ class UserManager final : public Actor { void on_set_emoji_status(EmojiStatus emoji_status, Promise &&promise); + void on_toggle_sponsored_messages(bool sponsored_enabled, Promise &&promise); + void on_get_support_user(UserId user_id, Promise> &&promise); void send_get_user_photos_query(UserId user_id, const UserPhotos *user_photos); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 243ab75f0..69a8587c4 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -5933,6 +5933,8 @@ class CliClient final : public Actor { get_args(args, custom_emoji_id, expiration_date); send_request(td_api::make_object( td_api::make_object(custom_emoji_id, expiration_date))); + } else if (op == "thsme") { + send_request(td_api::make_object(as_bool(args))); } else if (op == "gtes") { send_request(td_api::make_object()); } else if (op == "gdes") {