From 1e7795354d7e601cb601143c28710bc6323b9266 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 24 Feb 2021 22:25:10 +0300 Subject: [PATCH] Add td_api::getChatInviteLink. --- td/generate/scheme/td_api.tl | 5 +++ td/generate/scheme/td_api.tlo | Bin 198036 -> 198164 bytes td/telegram/ContactsManager.cpp | 57 ++++++++++++++++++++++++++++++++ td/telegram/ContactsManager.h | 3 ++ td/telegram/Td.cpp | 7 ++++ td/telegram/Td.h | 2 ++ td/telegram/cli.cpp | 7 +++- 7 files changed, 80 insertions(+), 1 deletion(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 3725372b7..6e1e571d7 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -4482,6 +4482,11 @@ createChatInviteLink chat_id:int53 expire_date:int32 member_limit:int32 = ChatIn //@member_limit Maximum number of chat members that can join the chat by the link simultaneously; 0-99999; pass 0 if not limited editChatInviteLink chat_id:int53 invite_link:string expire_date:int32 member_limit:int32 = ChatInviteLink; +//@description Returns information about an invite link. Requires administrator privileges and can_invite_users right in the chat to get own links and owner privileges to get other links +//@chat_id Chat identifier +//@invite_link Invite link to get +getChatInviteLink chat_id:int53 invite_link:string = ChatInviteLink; + //@description Returns list of chat administrators with number of their invite links. Requires owner privileges in the chat @chat_id Chat identifier getChatInviteLinkCounts chat_id:int53 = ChatInviteLinkCounts; diff --git a/td/generate/scheme/td_api.tlo b/td/generate/scheme/td_api.tlo index b87be208b71e4e4c9e643aa5f7961bb47547e1fe..f5d348e2bb8d905b5c7370cfa60bcfc222ae9d04 100644 GIT binary patch delta 74 zcmV-Q0JZ;=iwu;E41k0Iv;vSa0iw5&G6EYy9Q{FR;}K_NbVF!ibV+V@X>?^wX>Mx( g0Q;8lgqLu>0xg$abpjufHVBs>x&jcltaJkCkSV> promise_; + DialogId dialog_id_; + + public: + explicit GetExportedChatInviteQuery(Promise> &&promise) + : promise_(std::move(promise)) { + } + + void send(DialogId dialog_id, const string &invite_link) { + dialog_id_ = dialog_id; + auto input_peer = td->messages_manager_->get_input_peer(dialog_id, AccessRights::Write); + if (input_peer == nullptr) { + return on_error(0, Status::Error(400, "Can't access the chat")); + } + + send_query(G()->net_query_creator().create( + telegram_api::messages_getExportedChatInvite(std::move(input_peer), invite_link))); + } + + void on_result(uint64 id, BufferSlice packet) override { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(id, result_ptr.move_as_error()); + } + + if (result_ptr.ok()->get_id() != telegram_api::messages_exportedChatInvite::ID) { + LOG(ERROR) << "Receive result for GetExportedChatInviteQuery: " << to_string(result_ptr.ok()); + return on_error(id, Status::Error(500, "Receive unexpected response")); + } + + auto result = move_tl_object_as(result_ptr.ok_ref()); + LOG(INFO) << "Receive result for GetExportedChatInviteQuery: " << to_string(result); + + td->contacts_manager_->on_get_users(std::move(result->users_), "GetExportedChatInviteQuery"); + + DialogInviteLink invite_link(std::move(result->invite_)); + if (!invite_link.is_valid()) { + LOG(ERROR) << "Receive invalid invite link in " << dialog_id_; + return on_error(id, Status::Error(500, "Receive invalid invite link")); + } + promise_.set_value(invite_link.get_chat_invite_link_object(td->contacts_manager_.get())); + } + + void on_error(uint64 id, Status status) override { + td->messages_manager_->on_get_dialog_error(dialog_id_, status, "GetExportedChatInviteQuery"); + promise_.set_error(std::move(status)); + } +}; + class GetExportedChatInvitesQuery : public Td::ResultHandler { Promise> promise_; DialogId dialog_id_; @@ -7115,6 +7165,13 @@ void ContactsManager::edit_dialog_invite_link(DialogId dialog_id, const string & ->send(dialog_id, invite_link, expire_date, usage_limit); } +void ContactsManager::get_dialog_invite_link(DialogId dialog_id, const string &invite_link, + Promise> &&promise) { + TRY_STATUS_PROMISE(promise, can_manage_dialog_invite_links(dialog_id, false)); + + td_->create_handler(std::move(promise))->send(dialog_id, invite_link); +} + void ContactsManager::get_dialog_invite_link_counts( DialogId dialog_id, Promise> &&promise) { TRY_STATUS_PROMISE(promise, can_manage_dialog_invite_links(dialog_id, true)); diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 8f893a820..3f411463a 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -397,6 +397,9 @@ class ContactsManager : public Actor { void edit_dialog_invite_link(DialogId dialog_id, const string &link, int32 expire_date, int32 usage_limit, Promise> &&promise); + void get_dialog_invite_link(DialogId dialog_id, const string &invite_link, + Promise> &&promise); + void get_dialog_invite_link_counts(DialogId dialog_id, Promise> &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 8dd423b82..fed8aa6f2 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -6331,6 +6331,13 @@ void Td::on_request(uint64 id, td_api::editChatInviteLink &request) { request.member_limit_, std::move(promise)); } +void Td::on_request(uint64 id, td_api::getChatInviteLink &request) { + CHECK_IS_USER(); + CLEAN_INPUT_STRING(request.invite_link_); + CREATE_REQUEST_PROMISE(); + contacts_manager_->get_dialog_invite_link(DialogId(request.chat_id_), request.invite_link_, std::move(promise)); +} + void Td::on_request(uint64 id, const td_api::getChatInviteLinkCounts &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index b04d746cc..16ed25973 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -800,6 +800,8 @@ class Td final : public NetQueryCallback { void on_request(uint64 id, td_api::editChatInviteLink &request); + void on_request(uint64 id, td_api::getChatInviteLink &request); + void on_request(uint64 id, const td_api::getChatInviteLinkCounts &request); void on_request(uint64 id, td_api::getChatInviteLinks &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index bd482c65f..09822f77e 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -2744,7 +2744,12 @@ class CliClient final : public Actor { } else if (op == "gcilc") { string chat_id = args; send_request(td_api::make_object(as_chat_id(chat_id))); - } else if (op == "gcil" || op == "gcilr") { + } else if (op == "gcil") { + string chat_id; + string invite_link; + get_args(args, chat_id, invite_link); + send_request(td_api::make_object(as_chat_id(chat_id), invite_link)); + } else if (op == "gcils" || op == "gcilr") { string chat_id; string administrator_user_id; int32 offset_date;