Support revoking permanent chat invite links.

This commit is contained in:
levlam 2021-02-05 18:56:44 +03:00
parent 313a58b952
commit 4cb8af0584
5 changed files with 100 additions and 16 deletions

View File

@ -4469,10 +4469,11 @@ getChatInviteLinks chat_id:int53 administrator_user_id:int32 is_revoked:Bool off
//@offset_member A chat member from which to return next chat members; use null to get results from the beginning @limit Maximum number of chat members to return //@offset_member A chat member from which to return next chat members; use null to get results from the beginning @limit Maximum number of chat members to return
getChatInviteLinkMembers chat_id:int53 invite_link:string offset_member:chatInviteLinkMember limit:int32 = ChatInviteLinkMembers; getChatInviteLinkMembers chat_id:int53 invite_link:string offset_member:chatInviteLinkMember limit:int32 = ChatInviteLinkMembers;
//@description Revokes a non-permanent 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 //@description Revokes 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.
//-If a permanent link is revoked, then additionally to the revoked link returns new permanent link
//@chat_id Chat identifier //@chat_id Chat identifier
//@invite_link Invite link to be revoked //@invite_link Invite link to be revoked
revokeChatInviteLink chat_id:int53 invite_link:string = ChatInviteLink; revokeChatInviteLink chat_id:int53 invite_link:string = ChatInviteLinks;
//@description Deletes revoked chat invite links. 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 revoke //@description Deletes revoked chat invite links. 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 revoke
deleteRevokedChatInviteLink chat_id:int53 invite_link:string = Ok; deleteRevokedChatInviteLink chat_id:int53 invite_link:string = Ok;

Binary file not shown.

View File

@ -1635,7 +1635,7 @@ class EditChatInviteLinkQuery : public Td::ResultHandler {
: promise_(std::move(promise)) { : promise_(std::move(promise)) {
} }
void send(DialogId dialog_id, const string &invite_link, int32 expire_date, int32 usage_limit, bool is_revoked) { void send(DialogId dialog_id, const string &invite_link, int32 expire_date, int32 usage_limit) {
dialog_id_ = dialog_id; dialog_id_ = dialog_id;
auto input_peer = td->messages_manager_->get_input_peer(dialog_id, AccessRights::Write); auto input_peer = td->messages_manager_->get_input_peer(dialog_id, AccessRights::Write);
if (input_peer == nullptr) { if (input_peer == nullptr) {
@ -1644,9 +1644,6 @@ class EditChatInviteLinkQuery : public Td::ResultHandler {
int32 flags = telegram_api::messages_editExportedChatInvite::EXPIRE_DATE_MASK | int32 flags = telegram_api::messages_editExportedChatInvite::EXPIRE_DATE_MASK |
telegram_api::messages_editExportedChatInvite::USAGE_LIMIT_MASK; telegram_api::messages_editExportedChatInvite::USAGE_LIMIT_MASK;
if (is_revoked) {
flags |= telegram_api::messages_editExportedChatInvite::REVOKED_MASK;
}
send_query(G()->net_query_creator().create(telegram_api::messages_editExportedChatInvite( send_query(G()->net_query_creator().create(telegram_api::messages_editExportedChatInvite(
flags, false /*ignored*/, std::move(input_peer), invite_link, expire_date, usage_limit))); flags, false /*ignored*/, std::move(input_peer), invite_link, expire_date, usage_limit)));
} }
@ -1657,12 +1654,18 @@ class EditChatInviteLinkQuery : public Td::ResultHandler {
return on_error(id, result_ptr.move_as_error()); return on_error(id, result_ptr.move_as_error());
} }
auto result = move_tl_object_as<telegram_api::messages_exportedChatInvite>(result_ptr.move_as_ok()); auto result = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for ExportChatInviteQuery: " << to_string(result); LOG(INFO) << "Receive result for EditChatInviteLinkQuery: " << to_string(result);
td->contacts_manager_->on_get_users(std::move(result->users_), "EditChatInviteLinkQuery"); if (result->get_id() != telegram_api::messages_exportedChatInvite::ID) {
return on_error(id, Status::Error(500, "Receive unexpected response from server"));
}
DialogInviteLink invite_link(std::move(result->invite_)); auto invite = move_tl_object_as<telegram_api::messages_exportedChatInvite>(result);
td->contacts_manager_->on_get_users(std::move(invite->users_), "EditChatInviteLinkQuery");
DialogInviteLink invite_link(std::move(invite->invite_));
if (!invite_link.is_valid()) { if (!invite_link.is_valid()) {
return on_error(id, Status::Error(500, "Receive invalid invite link")); return on_error(id, Status::Error(500, "Receive invalid invite link"));
} }
@ -1853,6 +1856,77 @@ class GetChatInviteImportersQuery : public Td::ResultHandler {
} }
}; };
class RevokeChatInviteLinkQuery : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::chatInviteLinks>> promise_;
DialogId dialog_id_;
public:
explicit RevokeChatInviteLinkQuery(Promise<td_api::object_ptr<td_api::chatInviteLinks>> &&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"));
}
int32 flags = telegram_api::messages_editExportedChatInvite::REVOKED_MASK;
send_query(G()->net_query_creator().create(telegram_api::messages_editExportedChatInvite(
flags, false /*ignored*/, std::move(input_peer), invite_link, 0, 0)));
}
void on_result(uint64 id, BufferSlice packet) override {
auto result_ptr = fetch_result<telegram_api::messages_editExportedChatInvite>(packet);
if (result_ptr.is_error()) {
return on_error(id, result_ptr.move_as_error());
}
auto result = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for ExportChatInviteQuery: " << to_string(result);
vector<td_api::object_ptr<td_api::chatInviteLink>> links;
switch (result->get_id()) {
case telegram_api::messages_exportedChatInvite::ID: {
auto invite = move_tl_object_as<telegram_api::messages_exportedChatInvite>(result);
td->contacts_manager_->on_get_users(std::move(invite->users_), "RevokeChatInviteLinkQuery");
DialogInviteLink invite_link(std::move(invite->invite_));
if (!invite_link.is_valid()) {
return on_error(id, Status::Error(500, "Receive invalid invite link"));
}
links.push_back(invite_link.get_chat_invite_link_object(td->contacts_manager_.get()));
break;
}
case telegram_api::messages_exportedChatInviteReplaced::ID: {
auto invite = move_tl_object_as<telegram_api::messages_exportedChatInviteReplaced>(result);
td->contacts_manager_->on_get_users(std::move(invite->users_), "RevokeChatInviteLinkQuery");
DialogInviteLink invite_link(std::move(invite->invite_));
DialogInviteLink new_invite_link(std::move(invite->new_invite_));
if (!invite_link.is_valid() || !new_invite_link.is_valid()) {
return on_error(id, Status::Error(500, "Receive invalid invite link"));
}
links.push_back(invite_link.get_chat_invite_link_object(td->contacts_manager_.get()));
links.push_back(new_invite_link.get_chat_invite_link_object(td->contacts_manager_.get()));
break;
}
default:
UNREACHABLE();
}
auto total_count = static_cast<int32>(links.size());
promise_.set_value(td_api::make_object<td_api::chatInviteLinks>(total_count, std::move(links)));
}
void on_error(uint64 id, Status status) override {
td->messages_manager_->on_get_dialog_error(dialog_id_, status, "RevokeChatInviteLinkQuery");
promise_.set_error(std::move(status));
}
};
class DeleteExportedChatInviteQuery : public Td::ResultHandler { class DeleteExportedChatInviteQuery : public Td::ResultHandler {
Promise<Unit> promise_; Promise<Unit> promise_;
DialogId dialog_id_; DialogId dialog_id_;
@ -6933,7 +7007,7 @@ void ContactsManager::export_dialog_invite_link_impl(DialogId dialog_id, int32 e
} }
void ContactsManager::edit_dialog_invite_link(DialogId dialog_id, const string &invite_link, int32 expire_date, void ContactsManager::edit_dialog_invite_link(DialogId dialog_id, const string &invite_link, int32 expire_date,
int32 usage_limit, bool is_revoked, int32 usage_limit,
Promise<td_api::object_ptr<td_api::chatInviteLink>> &&promise) { Promise<td_api::object_ptr<td_api::chatInviteLink>> &&promise) {
TRY_STATUS_PROMISE(promise, can_manage_dialog_invite_links(dialog_id)); TRY_STATUS_PROMISE(promise, can_manage_dialog_invite_links(dialog_id));
@ -6942,7 +7016,7 @@ void ContactsManager::edit_dialog_invite_link(DialogId dialog_id, const string &
// } // }
td_->create_handler<EditChatInviteLinkQuery>(std::move(promise)) td_->create_handler<EditChatInviteLinkQuery>(std::move(promise))
->send(dialog_id, invite_link, expire_date, usage_limit, is_revoked); ->send(dialog_id, invite_link, expire_date, usage_limit);
} }
void ContactsManager::get_dialog_invite_link_counts( void ContactsManager::get_dialog_invite_link_counts(
@ -6989,6 +7063,13 @@ void ContactsManager::get_dialog_invite_link_users(
->send(dialog_id, invite_link, offset_date, offset_user_id, limit); ->send(dialog_id, invite_link, offset_date, offset_user_id, limit);
} }
void ContactsManager::revoke_dialog_invite_link(DialogId dialog_id, const string &invite_link,
Promise<td_api::object_ptr<td_api::chatInviteLinks>> &&promise) {
TRY_STATUS_PROMISE(promise, can_manage_dialog_invite_links(dialog_id));
td_->create_handler<RevokeChatInviteLinkQuery>(std::move(promise))->send(dialog_id, invite_link);
}
void ContactsManager::delete_revoked_dialog_invite_link(DialogId dialog_id, const string &invite_link, void ContactsManager::delete_revoked_dialog_invite_link(DialogId dialog_id, const string &invite_link,
Promise<Unit> &&promise) { Promise<Unit> &&promise) {
TRY_STATUS_PROMISE(promise, can_manage_dialog_invite_links(dialog_id)); TRY_STATUS_PROMISE(promise, can_manage_dialog_invite_links(dialog_id));

View File

@ -387,7 +387,7 @@ class ContactsManager : public Actor {
Promise<td_api::object_ptr<td_api::chatInviteLink>> &&promise); Promise<td_api::object_ptr<td_api::chatInviteLink>> &&promise);
void edit_dialog_invite_link(DialogId dialog_id, const string &link, int32 expire_date, int32 usage_limit, void edit_dialog_invite_link(DialogId dialog_id, const string &link, int32 expire_date, int32 usage_limit,
bool is_revoked, Promise<td_api::object_ptr<td_api::chatInviteLink>> &&promise); Promise<td_api::object_ptr<td_api::chatInviteLink>> &&promise);
void get_dialog_invite_link_counts(DialogId dialog_id, void get_dialog_invite_link_counts(DialogId dialog_id,
Promise<td_api::object_ptr<td_api::chatInviteLinkCounts>> &&promise); Promise<td_api::object_ptr<td_api::chatInviteLinkCounts>> &&promise);
@ -400,6 +400,9 @@ class ContactsManager : public Actor {
td_api::object_ptr<td_api::chatInviteLinkMember> offset_member, int32 limit, td_api::object_ptr<td_api::chatInviteLinkMember> offset_member, int32 limit,
Promise<td_api::object_ptr<td_api::chatInviteLinkMembers>> &&promise); Promise<td_api::object_ptr<td_api::chatInviteLinkMembers>> &&promise);
void revoke_dialog_invite_link(DialogId dialog_id, const string &link,
Promise<td_api::object_ptr<td_api::chatInviteLinks>> &&promise);
void delete_revoked_dialog_invite_link(DialogId dialog_id, const string &invite_link, Promise<Unit> &&promise); void delete_revoked_dialog_invite_link(DialogId dialog_id, const string &invite_link, Promise<Unit> &&promise);
void delete_all_revoked_dialog_invite_links(DialogId dialog_id, UserId administrator_user_id, void delete_all_revoked_dialog_invite_links(DialogId dialog_id, UserId administrator_user_id,

View File

@ -6328,7 +6328,7 @@ void Td::on_request(uint64 id, td_api::editChatInviteLink &request) {
CLEAN_INPUT_STRING(request.invite_link_); CLEAN_INPUT_STRING(request.invite_link_);
CREATE_REQUEST_PROMISE(); CREATE_REQUEST_PROMISE();
contacts_manager_->edit_dialog_invite_link(DialogId(request.chat_id_), request.invite_link_, request.expire_date_, contacts_manager_->edit_dialog_invite_link(DialogId(request.chat_id_), request.invite_link_, request.expire_date_,
request.member_limit_, false, std::move(promise)); request.member_limit_, std::move(promise));
} }
void Td::on_request(uint64 id, const td_api::getChatInviteLinkCounts &request) { void Td::on_request(uint64 id, const td_api::getChatInviteLinkCounts &request) {
@ -6358,8 +6358,7 @@ void Td::on_request(uint64 id, td_api::getChatInviteLinkMembers &request) {
void Td::on_request(uint64 id, td_api::revokeChatInviteLink &request) { void Td::on_request(uint64 id, td_api::revokeChatInviteLink &request) {
CLEAN_INPUT_STRING(request.invite_link_); CLEAN_INPUT_STRING(request.invite_link_);
CREATE_REQUEST_PROMISE(); CREATE_REQUEST_PROMISE();
contacts_manager_->edit_dialog_invite_link(DialogId(request.chat_id_), request.invite_link_, 0, 0, true, contacts_manager_->revoke_dialog_invite_link(DialogId(request.chat_id_), request.invite_link_, std::move(promise));
std::move(promise));
} }
void Td::on_request(uint64 id, td_api::deleteRevokedChatInviteLink &request) { void Td::on_request(uint64 id, td_api::deleteRevokedChatInviteLink &request) {