From ea56e6865a45784109b02640f53e1c2e1d9443bb Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 30 Mar 2023 16:41:03 +0300 Subject: [PATCH] Add td_api::reorderActiveBotUsernames. --- td/generate/scheme/td_api.tl | 5 +- td/telegram/ContactsManager.cpp | 93 ++++++++++++++++++++++++++------- td/telegram/ContactsManager.h | 8 +-- td/telegram/Td.cpp | 10 ++++ td/telegram/Td.h | 2 + td/telegram/cli.cpp | 11 ++++ 6 files changed, 104 insertions(+), 25 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index b58597701..7ba656c6e 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -7649,12 +7649,15 @@ getBotName bot_user_id:int53 language_code:string = Text; //@description Changes a profile photo for a bot @bot_user_id Identifier of the target bot @photo Profile photo to set; pass null to delete the chat photo setBotProfilePhoto bot_user_id:int53 photo:InputChatPhoto = Ok; -//@description Changes active state for a username of a bot. The editable username can't be disabled. May return an error with a message "USERNAMES_ACTIVE_TOO_MUCH" if the maximum number of active usernames has been reached +//@description Changes active state for a username of a bot. The editable username can't be disabled. May return an error with a message "USERNAMES_ACTIVE_TOO_MUCH" if the maximum number of active usernames has been reached. Can be called only if userTypeBot.can_be_edited == true //@bot_user_id Identifier of the target bot //@username The username to change //@is_active Pass true to activate the username; pass false to disable it toggleBotUsernameIsActive bot_user_id:int53 username:string is_active:Bool = Ok; +//@description Changes order of active usernames of a bot. Can be called only if userTypeBot.can_be_edited == true @bot_user_id Identifier of the target bot @usernames The new order of active usernames. All currently active usernames must be specified +reorderActiveBotUsernames bot_user_id:int53 usernames:vector = Ok; + //@description Sets the text shown in the chat with a bot if the chat is empty. Can be called only if userTypeBot.can_be_edited == true //@bot_user_id Identifier of the target bot //@language_code A two-letter ISO 639-1 language code. If empty, the description will be shown to all users, for which language there are no dedicated description diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index de7d0f653..04ff1a43b 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -858,12 +858,14 @@ class ToggleUsernameQuery final : public Td::ResultHandler { bool result = result_ptr.ok(); LOG(DEBUG) << "Receive result for ToggleUsernameQuery: " << result; - td_->contacts_manager_->on_update_username_is_active(std::move(username_), is_active_, std::move(promise_)); + td_->contacts_manager_->on_update_username_is_active(td_->contacts_manager_->get_my_id(), std::move(username_), + is_active_, std::move(promise_)); } void on_error(Status status) final { if (status.message() == "USERNAME_NOT_MODIFIED") { - td_->contacts_manager_->on_update_username_is_active(std::move(username_), is_active_, std::move(promise_)); + td_->contacts_manager_->on_update_username_is_active(td_->contacts_manager_->get_my_id(), std::move(username_), + is_active_, std::move(promise_)); return; } promise_.set_error(std::move(status)); @@ -895,12 +897,14 @@ class ReorderUsernamesQuery final : public Td::ResultHandler { return on_error(Status::Error(500, "Usernames weren't updated")); } - td_->contacts_manager_->on_update_active_usernames_order(std::move(usernames_), std::move(promise_)); + td_->contacts_manager_->on_update_active_usernames_order(td_->contacts_manager_->get_my_id(), std::move(usernames_), + std::move(promise_)); } void on_error(Status status) final { if (status.message() == "USERNAME_NOT_MODIFIED") { - td_->contacts_manager_->on_update_active_usernames_order(std::move(usernames_), std::move(promise_)); + td_->contacts_manager_->on_update_active_usernames_order(td_->contacts_manager_->get_my_id(), + std::move(usernames_), std::move(promise_)); return; } promise_.set_error(std::move(status)); @@ -937,13 +941,58 @@ class ToggleBotUsernameQuery final : public Td::ResultHandler { bool result = result_ptr.ok(); LOG(DEBUG) << "Receive result for ToggleBotUsernameQuery: " << result; - td_->contacts_manager_->on_update_bot_username_is_active(bot_user_id_, std::move(username_), is_active_, - std::move(promise_)); + td_->contacts_manager_->on_update_username_is_active(bot_user_id_, std::move(username_), is_active_, + std::move(promise_)); } void on_error(Status status) final { if (status.message() == "USERNAME_NOT_MODIFIED") { - td_->contacts_manager_->on_update_bot_username_is_active(bot_user_id_, std::move(username_), is_active_, + td_->contacts_manager_->on_update_username_is_active(bot_user_id_, std::move(username_), is_active_, + std::move(promise_)); + return; + } + promise_.set_error(std::move(status)); + } +}; + +class ReorderBotUsernamesQuery final : public Td::ResultHandler { + Promise promise_; + UserId bot_user_id_; + vector usernames_; + + public: + explicit ReorderBotUsernamesQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(UserId bot_user_id, vector &&usernames) { + bot_user_id_ = bot_user_id; + usernames_ = usernames; + auto r_input_user = td_->contacts_manager_->get_input_user(bot_user_id_); + if (r_input_user.is_error()) { + return on_error(r_input_user.move_as_error()); + } + send_query(G()->net_query_creator().create( + telegram_api::bots_reorderUsernames(r_input_user.move_as_ok(), std::move(usernames)), {{bot_user_id_}})); + } + + 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()); + } + + bool result = result_ptr.ok(); + LOG(DEBUG) << "Receive result for ReorderBotUsernamesQuery: " << result; + if (!result) { + return on_error(Status::Error(500, "Usernames weren't updated")); + } + + td_->contacts_manager_->on_update_active_usernames_order(bot_user_id_, std::move(usernames_), std::move(promise_)); + } + + void on_error(Status status) final { + if (status.message() == "USERNAME_NOT_MODIFIED") { + td_->contacts_manager_->on_update_active_usernames_order(bot_user_id_, std::move(usernames_), std::move(promise_)); return; } @@ -7566,8 +7615,8 @@ void ContactsManager::reorder_usernames_impl(vector &&usernames, Promise td_->create_handler(std::move(promise))->send(std::move(usernames)); } -void ContactsManager::on_update_username_is_active(string &&username, bool is_active, Promise &&promise) { - auto user_id = get_my_id(); +void ContactsManager::on_update_username_is_active(UserId user_id, string &&username, bool is_active, + Promise &&promise) { User *u = get_user(user_id); CHECK(u != nullptr); if (!u->usernames.can_toggle(username)) { @@ -7578,8 +7627,8 @@ void ContactsManager::on_update_username_is_active(string &&username, bool is_ac promise.set_value(Unit()); } -void ContactsManager::on_update_active_usernames_order(vector &&usernames, Promise &&promise) { - auto user_id = get_my_id(); +void ContactsManager::on_update_active_usernames_order(UserId user_id, vector &&usernames, + Promise &&promise) { User *u = get_user(user_id); CHECK(u != nullptr); if (!u->usernames.can_reorder_to(usernames)) { @@ -7604,16 +7653,20 @@ void ContactsManager::toggle_bot_username_is_active(UserId bot_user_id, string & td_->create_handler(std::move(promise))->send(bot_user_id, std::move(username), is_active); } -void ContactsManager::on_update_bot_username_is_active(UserId bot_user_id, string &&username, bool is_active, - Promise &&promise) { - User *u = get_user(bot_user_id); - CHECK(u != nullptr); - if (!u->usernames.can_toggle(username)) { - return reload_user(bot_user_id, std::move(promise)); +void ContactsManager::reorder_bot_usernames(UserId bot_user_id, vector &&usernames, Promise &&promise) { + TRY_RESULT_PROMISE(promise, bot_data, get_bot_data(bot_user_id)); + if (!bot_data.can_be_edited) { + return promise.set_error(Status::Error(400, "The bot can't be edited")); } - on_update_user_usernames(u, bot_user_id, u->usernames.toggle(username, is_active)); - update_user(u, bot_user_id); - promise.set_value(Unit()); + const User *u = get_user(bot_user_id); + CHECK(u != nullptr); + if (!u->usernames.can_reorder_to(usernames)) { + return promise.set_error(Status::Error(400, "Invalid username order specified")); + } + if (usernames.size() <= 1) { + return promise.set_value(Unit()); + } + td_->create_handler(std::move(promise))->send(bot_user_id, std::move(usernames)); } void ContactsManager::set_emoji_status(EmojiStatus emoji_status, Promise &&promise) { diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index df568a23f..e8f397e69 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -304,11 +304,9 @@ class ContactsManager final : public Actor { UserId add_channel_bot_user(); - void on_update_username_is_active(string &&username, bool is_active, Promise &&promise); + void on_update_username_is_active(UserId user_id, string &&username, bool is_active, Promise &&promise); - void on_update_active_usernames_order(vector &&usernames, Promise &&promise); - - void on_update_bot_username_is_active(UserId bot_user_id, string &&username, bool is_active, Promise &&promise); + void on_update_active_usernames_order(UserId user_id, vector &&usernames, Promise &&promise); void on_update_channel_username_is_active(ChannelId channel_id, string &&username, bool is_active, Promise &&promise); @@ -403,6 +401,8 @@ class ContactsManager final : public Actor { void toggle_bot_username_is_active(UserId bot_user_id, string &&username, bool is_active, Promise &&promise); + void reorder_bot_usernames(UserId bot_user_id, vector &&usernames, Promise &&promise); + void set_emoji_status(EmojiStatus emoji_status, Promise &&promise); void set_chat_description(ChatId chat_id, const string &description, Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 495be3bc7..7438baca3 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -7035,6 +7035,16 @@ void Td::on_request(uint64 id, td_api::toggleBotUsernameIsActive &request) { request.is_active_, std::move(promise)); } +void Td::on_request(uint64 id, td_api::reorderActiveBotUsernames &request) { + CHECK_IS_USER(); + for (auto &username : request.usernames_) { + CLEAN_INPUT_STRING(username); + } + CREATE_OK_REQUEST_PROMISE(); + contacts_manager_->reorder_bot_usernames(UserId(request.bot_user_id_), std::move(request.usernames_), + std::move(promise)); +} + void Td::on_request(uint64 id, td_api::setBotInfoDescription &request) { CLEAN_INPUT_STRING(request.description_); CREATE_OK_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index b09b2d9bc..0f38e5131 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1145,6 +1145,8 @@ class Td final : public Actor { void on_request(uint64 id, td_api::toggleBotUsernameIsActive &request); + void on_request(uint64 id, td_api::reorderActiveBotUsernames &request); + void on_request(uint64 id, td_api::setBotInfoDescription &request); void on_request(uint64 id, const td_api::getBotInfoDescription &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 0360a2da0..6993ef4a3 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -5032,6 +5032,17 @@ class CliClient final : public Actor { InputChatPhoto input_chat_photo; get_args(args, bot_user_id, input_chat_photo); send_request(td_api::make_object(bot_user_id, input_chat_photo)); + } else if (op == "tbunia") { + UserId bot_user_id; + string username; + bool is_active; + get_args(args, bot_user_id, username, is_active); + send_request(td_api::make_object(bot_user_id, username, is_active)); + } else if (op == "rabun") { + UserId bot_user_id; + string usernames; + get_args(args, bot_user_id, usernames); + send_request(td_api::make_object(bot_user_id, autosplit_str(usernames))); } else if (op == "sbid") { UserId bot_user_id; string language_code;