diff --git a/td/telegram/BotCommand.cpp b/td/telegram/BotCommand.cpp index 859c69db2..6ef5699bd 100644 --- a/td/telegram/BotCommand.cpp +++ b/td/telegram/BotCommand.cpp @@ -25,11 +25,10 @@ class SetBotCommandsQuery : public Td::ResultHandler { explicit SetBotCommandsQuery(Promise &&promise) : promise_(std::move(promise)) { } - void send(BotCommandScope scope, const string &language_code, vector> &&commands) { + void send(BotCommandScope scope, const string &language_code, vector &&commands) { send_query(G()->net_query_creator().create(telegram_api::bots_setBotCommands( - scope.get_input_bot_command_scope(td), language_code, transform(commands, [](const auto &command) { - return make_tl_object(command.first, command.second); - })))); + scope.get_input_bot_command_scope(td), language_code, + transform(commands, [](const BotCommand &command) { return command.get_input_bot_command(); })))); } void on_result(uint64 id, BufferSlice packet) override { @@ -113,6 +112,10 @@ td_api::object_ptr BotCommand::get_bot_command_object() cons return td_api::make_object(command_, description_); } +telegram_api::object_ptr BotCommand::get_input_bot_command() const { + return telegram_api::make_object(command_, description_); +} + bool operator==(const BotCommand &lhs, const BotCommand &rhs) { return lhs.command_ == rhs.command_ && lhs.description_ == rhs.description_; } @@ -134,19 +137,25 @@ bool operator==(const BotCommands &lhs, const BotCommands &rhs) { return lhs.bot_user_id_ == rhs.bot_user_id_ && lhs.commands_ == rhs.commands_; } +static bool is_valid_language_code(const string &language_code) { + if (language_code.empty()) { + return true; + } + if (language_code.size() != 2) { + return false; + } + return 'a' <= language_code[0] && language_code[0] <= 'z' && 'a' <= language_code[1] && language_code[1] <= 'z'; +} + void set_commands(Td *td, td_api::object_ptr &&scope_ptr, string &&language_code, vector> &&commands, Promise &&promise) { TRY_RESULT_PROMISE(promise, scope, BotCommandScope::get_bot_command_scope(td, std::move(scope_ptr))); - if (!language_code.empty() && (language_code.size() != 2 || language_code[0] < 'a' || language_code[0] > 'z' || - language_code[1] < 'a' || language_code[1] > 'z')) { + if (!is_valid_language_code(language_code)) { return promise.set_error(Status::Error(400, "Invalid language code specified")); } - if (!scope.have_input_bot_command_scope(td)) { - return promise.set_error(Status::Error(400, "Invalid scope specified")); - } - vector> new_commands; + vector new_commands; for (auto &command : commands) { if (command == nullptr) { return promise.set_error(Status::Error(400, "Command must be non-empty")); @@ -194,13 +203,9 @@ void delete_commands(Td *td, td_api::object_ptr &&scope Promise &&promise) { TRY_RESULT_PROMISE(promise, scope, BotCommandScope::get_bot_command_scope(td, std::move(scope_ptr))); - if (!language_code.empty() && (language_code.size() != 2 || language_code[0] < 'a' || language_code[0] > 'z' || - language_code[1] < 'a' || language_code[1] > 'z')) { + if (!is_valid_language_code(language_code)) { return promise.set_error(Status::Error(400, "Invalid language code specified")); } - if (!scope.have_input_bot_command_scope(td)) { - return promise.set_error(Status::Error(400, "Invalid scope specified")); - } td->create_handler(std::move(promise))->send(scope, language_code); } @@ -209,13 +214,9 @@ void get_commands(Td *td, td_api::object_ptr &&scope_pt Promise> &&promise) { TRY_RESULT_PROMISE(promise, scope, BotCommandScope::get_bot_command_scope(td, std::move(scope_ptr))); - if (!language_code.empty() && (language_code.size() != 2 || language_code[0] < 'a' || language_code[0] > 'z' || - language_code[1] < 'a' || language_code[1] > 'z')) { + if (!is_valid_language_code(language_code)) { return promise.set_error(Status::Error(400, "Invalid language code specified")); } - if (!scope.have_input_bot_command_scope(td)) { - return promise.set_error(Status::Error(400, "Invalid scope specified")); - } td->create_handler(std::move(promise))->send(scope, language_code); } diff --git a/td/telegram/BotCommand.h b/td/telegram/BotCommand.h index 4a54a32a4..e4e954bd3 100644 --- a/td/telegram/BotCommand.h +++ b/td/telegram/BotCommand.h @@ -29,10 +29,12 @@ class BotCommand { BotCommand() = default; BotCommand(string command, string description) : command_(std::move(command)), description_(std::move(description)) { } - BotCommand(telegram_api::object_ptr &&bot_command); + explicit BotCommand(telegram_api::object_ptr &&bot_command); td_api::object_ptr get_bot_command_object() const; + telegram_api::object_ptr get_input_bot_command() const; + template void store(StorerT &storer) const { td::store(command_, storer); diff --git a/td/telegram/BotCommandScope.cpp b/td/telegram/BotCommandScope.cpp index 7a50c2008..9fb951b0c 100644 --- a/td/telegram/BotCommandScope.cpp +++ b/td/telegram/BotCommandScope.cpp @@ -56,6 +56,9 @@ Result BotCommandScope::get_bot_command_scope(Td *td, if (!user_id.is_valid()) { return Status::Error(400, "User not found"); } + if (!td->contacts_manager_->have_input_user(user_id)) { + return Status::Error(400, "Can't access the user"); + } break; } default: @@ -92,16 +95,6 @@ Result BotCommandScope::get_bot_command_scope(Td *td, return BotCommandScope(type, dialog_id, user_id); } -bool BotCommandScope::have_input_bot_command_scope(const Td *td) const { - if (dialog_id_.is_valid() && !td->messages_manager_->have_input_peer(dialog_id_, AccessRights::Read)) { - return false; - } - if (user_id_.is_valid() && !td->contacts_manager_->have_input_user(user_id_)) { - return false; - } - return true; -} - telegram_api::object_ptr BotCommandScope::get_input_bot_command_scope( const Td *td) const { auto input_peer = diff --git a/td/telegram/BotCommandScope.h b/td/telegram/BotCommandScope.h index d16750835..1a1ad7f87 100644 --- a/td/telegram/BotCommandScope.h +++ b/td/telegram/BotCommandScope.h @@ -37,8 +37,6 @@ class BotCommandScope { public: static Result get_bot_command_scope(Td *td, td_api::object_ptr scope_ptr); - bool have_input_bot_command_scope(const Td *td) const; - telegram_api::object_ptr get_input_bot_command_scope(const Td *td) const; };