From 66c6e706ab88b411f7ec08e52ae4476e647e4520 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 22 Dec 2023 18:47:23 +0300 Subject: [PATCH] Replase td_api::shareUserWithBot with td_api::shareUsersWithBot. --- td/generate/scheme/td_api.tl | 8 ++--- td/telegram/MessagesManager.cpp | 46 ++++++++++++++++------------- td/telegram/MessagesManager.h | 4 +-- td/telegram/ReplyMarkup.cpp | 11 +++++++ td/telegram/ReplyMarkup.h | 2 ++ td/telegram/RequestedDialogType.cpp | 14 +++++++-- td/telegram/RequestedDialogType.h | 2 ++ td/telegram/Td.cpp | 16 +++++----- td/telegram/Td.h | 2 +- td/telegram/cli.cpp | 8 ++--- 10 files changed, 73 insertions(+), 40 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 5b1239994..6753bdb73 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -7574,13 +7574,13 @@ getLoginUrlInfo chat_id:int53 message_id:int53 button_id:int53 = LoginUrlInfo; getLoginUrl chat_id:int53 message_id:int53 button_id:int53 allow_write_access:Bool = HttpUrl; -//@description Shares a user after pressing a keyboardButtonTypeRequestUsers button with the bot +//@description Shares users after pressing a keyboardButtonTypeRequestUsers button with the bot //@chat_id Identifier of the chat with the bot //@message_id Identifier of the message with the button //@button_id Identifier of the button -//@shared_user_id Identifier of the shared user -//@only_check Pass true to check that the user can be shared by the button instead of actually sharing them -shareUserWithBot chat_id:int53 message_id:int53 button_id:int32 shared_user_id:int53 only_check:Bool = Ok; +//@shared_user_ids Identifiers of the shared users +//@only_check Pass true to check that the users can be shared by the button instead of actually sharing them +shareUsersWithBot chat_id:int53 message_id:int53 button_id:int32 shared_user_ids:vector only_check:Bool = Ok; //@description Shares a chat after pressing a keyboardButtonTypeRequestChat button with the bot //@chat_id Identifier of the chat with the bot diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index e9672865d..9c95ba59c 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -4028,18 +4028,20 @@ class SendBotRequestedPeerQuery final : public Td::ResultHandler { explicit SendBotRequestedPeerQuery(Promise &&promise) : promise_(std::move(promise)) { } - void send(MessageFullId message_full_id, int32 button_id, DialogId requested_dialog_id) { + void send(MessageFullId message_full_id, int32 button_id, vector &&requested_dialog_ids) { auto dialog_id = message_full_id.get_dialog_id(); auto input_peer = td_->messages_manager_->get_input_peer(dialog_id, AccessRights::Write); if (input_peer == nullptr) { return on_error(Status::Error(400, "Can't access the chat")); } - auto requested_peer = td_->messages_manager_->get_input_peer(requested_dialog_id, AccessRights::Read); - if (requested_peer == nullptr) { - return on_error(Status::Error(400, "Can't access the chosen chat")); - } vector> requested_peers; - requested_peers.push_back(std::move(requested_peer)); + for (auto requested_dialog_id : requested_dialog_ids) { + auto requested_peer = td_->messages_manager_->get_input_peer(requested_dialog_id, AccessRights::Read); + if (requested_peer == nullptr) { + return on_error(Status::Error(400, "Can't access the chosen chat")); + } + requested_peers.push_back(std::move(requested_peer)); + } send_query(G()->net_query_creator().create( telegram_api::messages_sendBotRequestedPeer(std::move(input_peer), @@ -28616,8 +28618,9 @@ void MessagesManager::do_send_screenshot_taken_notification_message(DialogId dia ->send(dialog_id, random_id); } -void MessagesManager::share_dialog_with_bot(MessageFullId message_full_id, int32 button_id, DialogId shared_dialog_id, - bool expect_user, bool only_check, Promise &&promise) { +void MessagesManager::share_dialogs_with_bot(MessageFullId message_full_id, int32 button_id, + vector shared_dialog_ids, bool expect_user, bool only_check, + Promise &&promise) { const Message *m = get_message_force(message_full_id, "share_dialog_with_bot"); if (m == nullptr) { return promise.set_error(Status::Error(400, "Message not found")); @@ -28626,26 +28629,29 @@ void MessagesManager::share_dialog_with_bot(MessageFullId message_full_id, int32 return promise.set_error(Status::Error(400, "Message has no buttons")); } CHECK(m->message_id.is_valid() && m->message_id.is_server()); - if (shared_dialog_id.get_type() != DialogType::User) { - if (!have_dialog_force(shared_dialog_id, "share_dialog_with_bot")) { - return promise.set_error(Status::Error(400, "Shared chat not found")); - } - } else { - if (!expect_user) { - return promise.set_error(Status::Error(400, "Wrong chat type")); - } - if (!td_->contacts_manager_->have_user(shared_dialog_id.get_user_id())) { - return promise.set_error(Status::Error(400, "Shared user not found")); + TRY_STATUS_PROMISE(promise, m->reply_markup->check_shared_dialog_count(button_id, shared_dialog_ids.size())); + for (auto shared_dialog_id : shared_dialog_ids) { + if (shared_dialog_id.get_type() != DialogType::User) { + if (!have_dialog_force(shared_dialog_id, "share_dialogs_with_bot")) { + return promise.set_error(Status::Error(400, "Shared chat not found")); + } + } else { + if (!expect_user) { + return promise.set_error(Status::Error(400, "Wrong chat type")); + } + if (!td_->contacts_manager_->have_user(shared_dialog_id.get_user_id())) { + return promise.set_error(Status::Error(400, "Shared user not found")); + } } + TRY_STATUS_PROMISE(promise, m->reply_markup->check_shared_dialog(td_, button_id, shared_dialog_id)); } - TRY_STATUS_PROMISE(promise, m->reply_markup->check_shared_dialog(td_, button_id, shared_dialog_id)); if (only_check) { return promise.set_value(Unit()); } td_->create_handler(std::move(promise)) - ->send(message_full_id, button_id, shared_dialog_id); + ->send(message_full_id, button_id, std::move(shared_dialog_ids)); } Result MessagesManager::add_local_message( diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 42c5dbbca..c03c682b4 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -479,8 +479,8 @@ class MessagesManager final : public Actor { void set_dialog_message_ttl(DialogId dialog_id, int32 ttl, Promise &&promise); - void share_dialog_with_bot(MessageFullId message_full_id, int32 button_id, DialogId shared_dialog_id, - bool expect_user, bool only_check, Promise &&promise); + void share_dialogs_with_bot(MessageFullId message_full_id, int32 button_id, vector shared_dialog_ids, + bool expect_user, bool only_check, Promise &&promise); Result add_local_message( DialogId dialog_id, td_api::object_ptr &&sender, diff --git a/td/telegram/ReplyMarkup.cpp b/td/telegram/ReplyMarkup.cpp index 620c3a30b..eb69faf17 100644 --- a/td/telegram/ReplyMarkup.cpp +++ b/td/telegram/ReplyMarkup.cpp @@ -1149,6 +1149,17 @@ Status ReplyMarkup::check_shared_dialog(Td *td, int32 button_id, DialogId dialog return Status::Error(400, "Button not found"); } +Status ReplyMarkup::check_shared_dialog_count(int32 button_id, size_t count) const { + for (auto &row : keyboard) { + for (auto &button : row) { + if (button.requested_dialog_type != nullptr && button.requested_dialog_type->get_button_id() == button_id) { + return button.requested_dialog_type->check_shared_dialog_count(count); + } + } + } + return Status::Error(400, "Button not found"); +} + tl_object_ptr get_input_reply_markup(ContactsManager *contacts_manager, const unique_ptr &reply_markup) { if (reply_markup == nullptr) { diff --git a/td/telegram/ReplyMarkup.h b/td/telegram/ReplyMarkup.h index 036210e16..85dac2dff 100644 --- a/td/telegram/ReplyMarkup.h +++ b/td/telegram/ReplyMarkup.h @@ -91,6 +91,8 @@ struct ReplyMarkup { tl_object_ptr get_reply_markup_object(ContactsManager *contacts_manager) const; Status check_shared_dialog(Td *td, int32 button_id, DialogId dialog_id) const; + + Status check_shared_dialog_count(int32 button_id, size_t count) const; }; bool operator==(const ReplyMarkup &lhs, const ReplyMarkup &rhs); diff --git a/td/telegram/RequestedDialogType.cpp b/td/telegram/RequestedDialogType.cpp index 358d95b1b..bc9c2d3bd 100644 --- a/td/telegram/RequestedDialogType.cpp +++ b/td/telegram/RequestedDialogType.cpp @@ -16,7 +16,7 @@ RequestedDialogType::RequestedDialogType(td_api::object_ptrid_; - max_quantity_ = request_users->max_quantity_; + max_quantity_ = max(request_users->max_quantity_, 1); restrict_is_bot_ = request_users->restrict_user_is_bot_; is_bot_ = request_users->user_is_bot_; restrict_is_premium_ = request_users->restrict_user_is_premium_; @@ -44,7 +44,7 @@ RequestedDialogType::RequestedDialogType(telegram_api::object_ptrget_id()) { case telegram_api::requestPeerTypeUser::ID: { auto type = telegram_api::move_object_as(peer_type); @@ -273,4 +273,14 @@ Status RequestedDialogType::check_shared_dialog(Td *td, DialogId dialog_id) cons return Status::OK(); } +Status RequestedDialogType::check_shared_dialog_count(size_t count) const { + if (count == 0) { + return Status::Error(400, "Too few chats are chosen"); + } + if (count > static_cast(max_quantity_)) { + return Status::Error(400, "Too many chats are chosen"); + } + return Status::OK(); +} + } // namespace td diff --git a/td/telegram/RequestedDialogType.h b/td/telegram/RequestedDialogType.h index 5ad1ca703..edb145d40 100644 --- a/td/telegram/RequestedDialogType.h +++ b/td/telegram/RequestedDialogType.h @@ -59,6 +59,8 @@ class RequestedDialogType { Status check_shared_dialog(Td *td, DialogId dialog_id) const; + Status check_shared_dialog_count(size_t count) const; + template void store(StorerT &storer) const; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 579b10e42..bf4ae6310 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -8388,20 +8388,22 @@ void Td::on_request(uint64 id, const td_api::getLoginUrl &request) { request.allow_write_access_, std::move(promise)); } -void Td::on_request(uint64 id, const td_api::shareUserWithBot &request) { +void Td::on_request(uint64 id, const td_api::shareUsersWithBot &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); - messages_manager_->share_dialog_with_bot({DialogId(request.chat_id_), MessageId(request.message_id_)}, - request.button_id_, DialogId(UserId(request.shared_user_id_)), true, - request.only_check_, std::move(promise)); + auto user_ids = UserId::get_user_ids(request.shared_user_ids_); + auto dialog_ids = transform(user_ids, [](UserId user_id) { return DialogId(user_id); }); + messages_manager_->share_dialogs_with_bot({DialogId(request.chat_id_), MessageId(request.message_id_)}, + request.button_id_, std::move(dialog_ids), true, request.only_check_, + std::move(promise)); } void Td::on_request(uint64 id, const td_api::shareChatWithBot &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); - messages_manager_->share_dialog_with_bot({DialogId(request.chat_id_), MessageId(request.message_id_)}, - request.button_id_, DialogId(request.shared_chat_id_), false, - request.only_check_, std::move(promise)); + messages_manager_->share_dialogs_with_bot({DialogId(request.chat_id_), MessageId(request.message_id_)}, + request.button_id_, {DialogId(request.shared_chat_id_)}, false, + request.only_check_, std::move(promise)); } void Td::on_request(uint64 id, td_api::getInlineQueryResults &request) { diff --git a/td/telegram/Td.h b/td/telegram/Td.h index e02fa7e5c..e899d4c0c 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1509,7 +1509,7 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::getLoginUrl &request); - void on_request(uint64 id, const td_api::shareUserWithBot &request); + void on_request(uint64 id, const td_api::shareUsersWithBot &request); void on_request(uint64 id, const td_api::shareChatWithBot &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 5f614a69d..83f71d33b 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -6095,10 +6095,10 @@ class CliClient final : public Actor { ChatId chat_id; MessageId message_id; int32 button_id; - UserId shared_user_id; - get_args(args, chat_id, message_id, button_id, shared_user_id); - send_request( - td_api::make_object(chat_id, message_id, button_id, shared_user_id, op == "suwbc")); + string shared_user_ids; + get_args(args, chat_id, message_id, button_id, shared_user_ids); + send_request(td_api::make_object(chat_id, message_id, button_id, + as_user_ids(shared_user_ids), op == "suwbc")); } else if (op == "scwb" || op == "scwbc") { ChatId chat_id; MessageId message_id;