From 64d572bcbeecd53544dee056be877e80291ab342 Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 9 Oct 2021 14:20:48 +0300 Subject: [PATCH] Allow creation and edit of invite links, requiring approval. --- td/generate/scheme/td_api.tl | 6 ++++-- td/telegram/ContactsManager.cpp | 32 +++++++++++++++++++------------- td/telegram/ContactsManager.h | 10 +++++----- td/telegram/Td.cpp | 6 +++--- td/telegram/cli.cpp | 16 ++++++++++------ 5 files changed, 41 insertions(+), 29 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 1f76ba480..e3340d32c 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -4872,14 +4872,16 @@ replacePrimaryChatInviteLink chat_id:int53 = ChatInviteLink; //@chat_id Chat identifier //@expire_date Point in time (Unix timestamp) when the link will expire; pass 0 if never //@member_limit The maximum number of chat members that can join the chat by the link simultaneously; 0-99999; pass 0 if not limited -createChatInviteLink chat_id:int53 expire_date:int32 member_limit:int32 = ChatInviteLink; +//@requires_approval True, if users joining the chat by the link need to be approved by chat administrators +createChatInviteLink chat_id:int53 expire_date:int32 member_limit:int32 requires_approval:Bool = ChatInviteLink; //@description Edits a non-primary invite link for a chat. Available for basic groups, supergroups, and channels. Requires administrator privileges and can_invite_users right in the chat for own links and owner privileges for other links //@chat_id Chat identifier //@invite_link Invite link to be edited //@expire_date Point in time (Unix timestamp) when the link will expire; pass 0 if never //@member_limit The 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; +//@requires_approval True, if users joining the chat by the link need to be approved by chat administrators +editChatInviteLink chat_id:int53 invite_link:string expire_date:int32 member_limit:int32 requires_approval:Bool = 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 diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index fb8c35de4..a16ef65b7 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -1610,7 +1610,7 @@ class ExportChatInviteQuery final : public Td::ResultHandler { : promise_(std::move(promise)) { } - void send(DialogId dialog_id, int32 expire_date, int32 usage_limit, bool is_permanent) { + void send(DialogId dialog_id, int32 expire_date, int32 usage_limit, bool requires_approval, bool is_permanent) { dialog_id_ = dialog_id; auto input_peer = td->messages_manager_->get_input_peer(dialog_id, AccessRights::Write); if (input_peer == nullptr) { @@ -1624,6 +1624,9 @@ class ExportChatInviteQuery final : public Td::ResultHandler { if (usage_limit > 0) { flags |= telegram_api::messages_exportChatInvite::USAGE_LIMIT_MASK; } + if (requires_approval) { + flags |= telegram_api::messages_exportChatInvite::REQUEST_NEEDED_MASK; + } if (is_permanent) { flags |= telegram_api::messages_exportChatInvite::LEGACY_REVOKE_PERMANENT_MASK; } @@ -1669,7 +1672,8 @@ class EditChatInviteLinkQuery final : public Td::ResultHandler { : promise_(std::move(promise)) { } - void send(DialogId dialog_id, const string &invite_link, int32 expire_date, int32 usage_limit) { + void send(DialogId dialog_id, const string &invite_link, int32 expire_date, int32 usage_limit, + bool requires_approval) { dialog_id_ = dialog_id; auto input_peer = td->messages_manager_->get_input_peer(dialog_id, AccessRights::Write); if (input_peer == nullptr) { @@ -1677,9 +1681,11 @@ class EditChatInviteLinkQuery final : public Td::ResultHandler { } int32 flags = telegram_api::messages_editExportedChatInvite::EXPIRE_DATE_MASK | - telegram_api::messages_editExportedChatInvite::USAGE_LIMIT_MASK; - send_query(G()->net_query_creator().create(telegram_api::messages_editExportedChatInvite( - flags, false /*ignored*/, std::move(input_peer), invite_link, expire_date, usage_limit, false, string()))); + telegram_api::messages_editExportedChatInvite::USAGE_LIMIT_MASK | + telegram_api::messages_editExportedChatInvite::REQUEST_NEEDED_MASK; + send_query(G()->net_query_creator().create( + telegram_api::messages_editExportedChatInvite(flags, false /*ignored*/, std::move(input_peer), invite_link, + expire_date, usage_limit, requires_approval, string()))); } void on_result(uint64 id, BufferSlice packet) final { @@ -7268,31 +7274,31 @@ Status ContactsManager::can_manage_dialog_invite_links(DialogId dialog_id, bool } void ContactsManager::export_dialog_invite_link(DialogId dialog_id, int32 expire_date, int32 usage_limit, - bool is_permanent, + bool requires_approval, bool is_permanent, Promise> &&promise) { - get_me(PromiseCreator::lambda([actor_id = actor_id(this), dialog_id, expire_date, usage_limit, is_permanent, - promise = std::move(promise)](Result &&result) mutable { + get_me(PromiseCreator::lambda([actor_id = actor_id(this), dialog_id, expire_date, usage_limit, requires_approval, + is_permanent, promise = std::move(promise)](Result &&result) mutable { if (result.is_error()) { promise.set_error(result.move_as_error()); } else { send_closure(actor_id, &ContactsManager::export_dialog_invite_link_impl, dialog_id, expire_date, usage_limit, - is_permanent, std::move(promise)); + requires_approval, is_permanent, std::move(promise)); } })); } void ContactsManager::export_dialog_invite_link_impl(DialogId dialog_id, int32 expire_date, int32 usage_limit, - bool is_permanent, + bool requires_approval, bool is_permanent, Promise> &&promise) { TRY_STATUS_PROMISE(promise, G()->close_status()); TRY_STATUS_PROMISE(promise, can_manage_dialog_invite_links(dialog_id)); td_->create_handler(std::move(promise)) - ->send(dialog_id, expire_date, usage_limit, is_permanent); + ->send(dialog_id, expire_date, usage_limit, requires_approval, is_permanent); } void ContactsManager::edit_dialog_invite_link(DialogId dialog_id, const string &invite_link, int32 expire_date, - int32 usage_limit, + int32 usage_limit, bool requires_approval, Promise> &&promise) { TRY_STATUS_PROMISE(promise, can_manage_dialog_invite_links(dialog_id)); @@ -7301,7 +7307,7 @@ void ContactsManager::edit_dialog_invite_link(DialogId dialog_id, const string & } td_->create_handler(std::move(promise)) - ->send(dialog_id, invite_link, expire_date, usage_limit); + ->send(dialog_id, invite_link, expire_date, requires_approval, usage_limit); } void ContactsManager::get_dialog_invite_link(DialogId dialog_id, const string &invite_link, diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index ce1a2138c..7b97656ba 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -382,11 +382,11 @@ class ContactsManager final : public Actor { void transfer_dialog_ownership(DialogId dialog_id, UserId user_id, const string &password, Promise &&promise); - void export_dialog_invite_link(DialogId dialog_id, int32 expire_date, int32 usage_limit, bool is_permanent, - Promise> &&promise); + void export_dialog_invite_link(DialogId dialog_id, int32 expire_date, int32 usage_limit, bool requires_approval, + bool is_permanent, Promise> &&promise); void edit_dialog_invite_link(DialogId dialog_id, const string &link, int32 expire_date, int32 usage_limit, - Promise> &&promise); + bool requires_approval, Promise> &&promise); void get_dialog_invite_link(DialogId dialog_id, const string &invite_link, Promise> &&promise); @@ -1387,8 +1387,8 @@ class ContactsManager final : public Actor { static bool is_channel_public(const Channel *c); - void export_dialog_invite_link_impl(DialogId dialog_id, int32 expire_date, int32 usage_limit, bool is_permanent, - Promise> &&promise); + void export_dialog_invite_link_impl(DialogId dialog_id, int32 expire_date, int32 usage_limit, bool requires_approval, + bool is_permanent, Promise> &&promise); void remove_dialog_access_by_invite_link(DialogId dialog_id); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 6bf50bd1a..8e0fad362 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -6285,20 +6285,20 @@ void Td::on_request(uint64 id, td_api::getChatAdministrators &request) { void Td::on_request(uint64 id, const td_api::replacePrimaryChatInviteLink &request) { CREATE_REQUEST_PROMISE(); - contacts_manager_->export_dialog_invite_link(DialogId(request.chat_id_), 0, 0, true, std::move(promise)); + contacts_manager_->export_dialog_invite_link(DialogId(request.chat_id_), 0, 0, false, true, std::move(promise)); } void Td::on_request(uint64 id, const td_api::createChatInviteLink &request) { CREATE_REQUEST_PROMISE(); contacts_manager_->export_dialog_invite_link(DialogId(request.chat_id_), request.expire_date_, request.member_limit_, - false, std::move(promise)); + request.requires_approval_, false, std::move(promise)); } void Td::on_request(uint64 id, td_api::editChatInviteLink &request) { CLEAN_INPUT_STRING(request.invite_link_); CREATE_REQUEST_PROMISE(); contacts_manager_->edit_dialog_invite_link(DialogId(request.chat_id_), request.invite_link_, request.expire_date_, - request.member_limit_, std::move(promise)); + request.member_limit_, request.requires_approval_, std::move(promise)); } void Td::on_request(uint64 id, td_api::getChatInviteLink &request) { diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 092f977bc..854e460be 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -2939,16 +2939,19 @@ class CliClient final : public Actor { string chat_id; int32 expire_date; int32 member_limit; - get_args(args, chat_id, expire_date, member_limit); - send_request(td_api::make_object(as_chat_id(chat_id), expire_date, member_limit)); + bool requires_approval; + get_args(args, chat_id, expire_date, member_limit, requires_approval); + send_request(td_api::make_object(as_chat_id(chat_id), expire_date, member_limit, + requires_approval)); } else if (op == "ecil") { string chat_id; string invite_link; int32 expire_date; int32 member_limit; - get_args(args, chat_id, invite_link, expire_date, member_limit); - send_request( - td_api::make_object(as_chat_id(chat_id), invite_link, expire_date, member_limit)); + bool requires_approval; + get_args(args, chat_id, invite_link, expire_date, member_limit, requires_approval); + send_request(td_api::make_object(as_chat_id(chat_id), invite_link, expire_date, + member_limit, requires_approval)); } else if (op == "rcil") { string chat_id; string invite_link; @@ -2981,7 +2984,8 @@ class CliClient final : public Actor { get_args(args, chat_id, invite_link, offset_user_id, offset_date, limit); send_request(td_api::make_object( as_chat_id(chat_id), invite_link, - td_api::make_object(as_user_id(offset_user_id), offset_date), as_limit(limit))); + td_api::make_object(as_user_id(offset_user_id), offset_date, 0), + as_limit(limit))); } else if (op == "drcil") { string chat_id; string invite_link;