Add td_api::reorderActiveBotUsernames.

This commit is contained in:
levlam 2023-03-30 16:41:03 +03:00
parent 93c3b5cc07
commit ea56e6865a
6 changed files with 104 additions and 25 deletions

View File

@ -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<string> = 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

View File

@ -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<Unit> promise_;
UserId bot_user_id_;
vector<string> usernames_;
public:
explicit ReorderBotUsernamesQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(UserId bot_user_id, vector<string> &&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<telegram_api::bots_reorderUsernames>(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<string> &&usernames, Promise
td_->create_handler<ReorderUsernamesQuery>(std::move(promise))->send(std::move(usernames));
}
void ContactsManager::on_update_username_is_active(string &&username, bool is_active, Promise<Unit> &&promise) {
auto user_id = get_my_id();
void ContactsManager::on_update_username_is_active(UserId user_id, string &&username, bool is_active,
Promise<Unit> &&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<string> &&usernames, Promise<Unit> &&promise) {
auto user_id = get_my_id();
void ContactsManager::on_update_active_usernames_order(UserId user_id, vector<string> &&usernames,
Promise<Unit> &&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<ToggleBotUsernameQuery>(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<Unit> &&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<string> &&usernames, Promise<Unit> &&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<ReorderBotUsernamesQuery>(std::move(promise))->send(bot_user_id, std::move(usernames));
}
void ContactsManager::set_emoji_status(EmojiStatus emoji_status, Promise<Unit> &&promise) {

View File

@ -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<Unit> &&promise);
void on_update_username_is_active(UserId user_id, string &&username, bool is_active, Promise<Unit> &&promise);
void on_update_active_usernames_order(vector<string> &&usernames, Promise<Unit> &&promise);
void on_update_bot_username_is_active(UserId bot_user_id, string &&username, bool is_active, Promise<Unit> &&promise);
void on_update_active_usernames_order(UserId user_id, vector<string> &&usernames, Promise<Unit> &&promise);
void on_update_channel_username_is_active(ChannelId channel_id, string &&username, bool is_active,
Promise<Unit> &&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<Unit> &&promise);
void reorder_bot_usernames(UserId bot_user_id, vector<string> &&usernames, Promise<Unit> &&promise);
void set_emoji_status(EmojiStatus emoji_status, Promise<Unit> &&promise);
void set_chat_description(ChatId chat_id, const string &description, Promise<Unit> &&promise);

View File

@ -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();

View File

@ -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);

View File

@ -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<td_api::setBotProfilePhoto>(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<td_api::toggleBotUsernameIsActive>(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<td_api::reorderActiveBotUsernames>(bot_user_id, autosplit_str(usernames)));
} else if (op == "sbid") {
UserId bot_user_id;
string language_code;