From da4d2b7636504d875e9f194ff79d2eb7aae297cc Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 20 Jan 2021 21:12:48 +0300 Subject: [PATCH] Add separate banChatMember method. --- td/generate/scheme/td_api.tl | 9 ++++++++- td/generate/scheme/td_api.tlo | Bin 194376 -> 194580 bytes td/telegram/ContactsManager.cpp | 13 +++++++++---- td/telegram/ContactsManager.h | 2 ++ td/telegram/MessagesManager.cpp | 26 +++++++++++++++++++++++--- td/telegram/MessagesManager.h | 3 +++ td/telegram/Td.cpp | 7 +++++++ td/telegram/Td.h | 2 ++ td/telegram/cli.cpp | 8 ++++++++ 9 files changed, 62 insertions(+), 8 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 6862dc462..4bbf2baf2 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -479,7 +479,7 @@ chatMemberStatusRestricted is_member:Bool restricted_until_date:int32 permission chatMemberStatusLeft = ChatMemberStatus; //@description The user was banned (and hence is not a member of the chat). Implies the user can't return to the chat or view messages -//@banned_until_date Point in time (Unix timestamp) when the user will be unbanned; 0 if never. If the user is banned for more than 366 days or for less than 30 seconds from the current time, the user is considered to be banned forever +//@banned_until_date Point in time (Unix timestamp) when the user will be unbanned; 0 if never. If the user is banned for more than 366 days or for less than 30 seconds from the current time, the user is considered to be banned forever. Always 0 in basic groups chatMemberStatusBanned banned_until_date:int32 = ChatMemberStatus; @@ -4282,6 +4282,13 @@ addChatMembers chat_id:int53 user_ids:vector = Ok; //@chat_id Chat identifier @user_id User identifier @status The new status of the member in the chat setChatMemberStatus chat_id:int53 user_id:int32 status:ChatMemberStatus = Ok; +//@description Bans a member in a chat. Members can't be banned in private or secret chats. In supergroups and channels, the user will not be able to return to the group on their own using invite links, etc., unless [unbanned](#unbanchatmember) first +//@chat_id Chat identifier +//@user_id Identifier of the user +//@banned_until_date Point in time (Unix timestamp) when the user will be unbanned; 0 if never. If the user is banned for more than 366 days or for less than 30 seconds from the current time, the user is considered to be banned forever. Ignored in basic groups +//@revoke_messages Pass true to delete all messages in the chat for the user. Always true for supergroups and channels +banChatMember chat_id:int53 user_id:int32 banned_until_date:int32 revoke_messages:Bool = Ok; + //@description Checks whether the current session can be used to transfer a chat ownership to another user canTransferOwnership = CanTransferOwnershipResult; diff --git a/td/generate/scheme/td_api.tlo b/td/generate/scheme/td_api.tlo index 9f84e287927038e49494ee19d6d6a7f6cbbdaa43..c46745ef72ec62a52f9404d667a4730cdd539dc3 100644 GIT binary patch delta 75 zcmV-R0JQ(e?hBOg3xI?Hv;zDn0h+h{C<22F2nzQ@dJSS>ZkO;W0u+~I^8qNA5K96a hldzW!laYfX1`l#&c5iEwu7Mtx5H|uCw}3SQ$ia0N9OVE2 delta 27 jcmbR8f&0We?uHh|Elhtj7-w$(tHIRFv)#vx>BK<*uuKd4 diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index f1b952324..1b1a358a9 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -1952,8 +1952,11 @@ class DeleteChatUserQuery : public Td::ResultHandler { explicit DeleteChatUserQuery(Promise &&promise) : promise_(std::move(promise)) { } - void send(ChatId chat_id, tl_object_ptr &&input_user) { + void send(ChatId chat_id, tl_object_ptr &&input_user, bool revoke_messages) { int32 flags = 0; + if (revoke_messages) { + flags |= telegram_api::messages_deleteChatUser::REVOKE_HISTORY_MASK; + } send_query(G()->net_query_creator().create( telegram_api::messages_deleteChatUser(flags, false /*ignored*/, chat_id.get(), std::move(input_user)))); } @@ -7001,7 +7004,7 @@ void ContactsManager::promote_channel_participant(ChannelId channel_id, UserId u void ContactsManager::change_chat_participant_status(ChatId chat_id, UserId user_id, DialogParticipantStatus status, Promise &&promise) { if (!status.is_member()) { - return delete_chat_participant(chat_id, user_id, std::move(promise)); + return delete_chat_participant(chat_id, user_id, false, std::move(promise)); } auto c = get_chat(chat_id); @@ -7288,7 +7291,8 @@ void ContactsManager::import_dialog_invite_link(const string &invite_link, Promi td_->create_handler(std::move(promise))->send(invite_link); } -void ContactsManager::delete_chat_participant(ChatId chat_id, UserId user_id, Promise &&promise) { +void ContactsManager::delete_chat_participant(ChatId chat_id, UserId user_id, bool revoke_messages, + Promise &&promise) { const Chat *c = get_chat(chat_id); if (c == nullptr) { return promise.set_error(Status::Error(3, "Chat info not found")); @@ -7299,6 +7303,7 @@ void ContactsManager::delete_chat_participant(ChatId chat_id, UserId user_id, Pr auto my_id = get_my_id(); if (c->status.is_left()) { if (user_id == my_id) { + td_->messages_manager_->delete_dialog(DialogId(chat_id)); return promise.set_value(Unit()); } else { return promise.set_error(Status::Error(3, "Not in the chat")); @@ -7337,7 +7342,7 @@ void ContactsManager::delete_chat_participant(ChatId chat_id, UserId user_id, Pr } // TODO invoke after - td_->create_handler(std::move(promise))->send(chat_id, std::move(input_user)); + td_->create_handler(std::move(promise))->send(chat_id, std::move(input_user), revoke_messages); } void ContactsManager::restrict_channel_participant(ChannelId channel_id, UserId user_id, DialogParticipantStatus status, diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 09f5aade7..67cf25121 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -385,6 +385,8 @@ class ContactsManager : public Actor { void change_channel_participant_status(ChannelId channel_id, UserId user_id, DialogParticipantStatus status, Promise &&promise); + void delete_chat_participant(ChatId chat_id, UserId user_id, bool revoke_messages, Promise &&promise); + void can_transfer_ownership(Promise &&promise); static td_api::object_ptr get_can_transfer_ownership_result_object( diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 03a307ede..bd70da913 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -30234,7 +30234,6 @@ void MessagesManager::unpin_all_dialog_messages_on_server(DialogId dialog_id, ui void MessagesManager::add_dialog_participant(DialogId dialog_id, UserId user_id, int32 forward_limit, Promise &&promise) { - LOG(INFO) << "Receive AddChatParticipant request to add " << user_id << " to " << dialog_id; if (!have_dialog_force(dialog_id)) { return promise.set_error(Status::Error(3, "Chat not found")); } @@ -30257,7 +30256,6 @@ void MessagesManager::add_dialog_participant(DialogId dialog_id, UserId user_id, void MessagesManager::add_dialog_participants(DialogId dialog_id, const vector &user_ids, Promise &&promise) { - LOG(INFO) << "Receive AddChatParticipants request to add " << format::as_array(user_ids) << " to " << dialog_id; if (td_->auth_manager_->is_bot()) { return promise.set_error(Status::Error(3, "Method is not available for bots")); } @@ -30285,7 +30283,6 @@ void MessagesManager::set_dialog_participant_status(DialogId dialog_id, UserId u const tl_object_ptr &chat_member_status, Promise &&promise) { auto status = get_dialog_participant_status(chat_member_status); - LOG(INFO) << "Receive setChatMemberStatus request with " << user_id << " and " << dialog_id << " to " << status; if (!have_dialog_force(dialog_id)) { return promise.set_error(Status::Error(3, "Chat not found")); } @@ -30307,6 +30304,29 @@ void MessagesManager::set_dialog_participant_status(DialogId dialog_id, UserId u } } +void MessagesManager::ban_dialog_participant(DialogId dialog_id, UserId user_id, int32 banned_until_date, + bool revoke_messages, Promise &&promise) { + if (!have_dialog_force(dialog_id)) { + return promise.set_error(Status::Error(3, "Chat not found")); + } + + switch (dialog_id.get_type()) { + case DialogType::User: + return promise.set_error(Status::Error(3, "Can't ban members in a private chat")); + case DialogType::Chat: + return td_->contacts_manager_->delete_chat_participant(dialog_id.get_chat_id(), user_id, revoke_messages, + std::move(promise)); + case DialogType::Channel: + return td_->contacts_manager_->change_channel_participant_status( + dialog_id.get_channel_id(), user_id, DialogParticipantStatus::Banned(banned_until_date), std::move(promise)); + case DialogType::SecretChat: + return promise.set_error(Status::Error(3, "Can't ban members in a secret chat")); + case DialogType::None: + default: + UNREACHABLE(); + } +} + DialogParticipant MessagesManager::get_dialog_participant(DialogId dialog_id, UserId user_id, int64 &random_id, bool force, Promise &&promise) { LOG(INFO) << "Receive GetChatMember request to get " << user_id << " in " << dialog_id << " with random_id " diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index b14409c58..9e5ca5aea 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -477,6 +477,9 @@ class MessagesManager : public Actor { const tl_object_ptr &chat_member_status, Promise &&promise); + void ban_dialog_participant(DialogId dialog_id, UserId user_id, int32 banned_until_date, bool revoke_messages, + Promise &&promise); + DialogParticipant get_dialog_participant(DialogId dialog_id, UserId user_id, int64 &random_id, bool force, Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 3be1c17c0..97a6e6244 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -6251,6 +6251,13 @@ void Td::on_request(uint64 id, td_api::setChatMemberStatus &request) { request.status_, std::move(promise)); } +void Td::on_request(uint64 id, const td_api::banChatMember &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + messages_manager_->ban_dialog_participant(DialogId(request.chat_id_), UserId(request.user_id_), + request.banned_until_date_, request.revoke_messages_, std::move(promise)); +} + void Td::on_request(uint64 id, const td_api::canTransferOwnership &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 7efb1f85c..7aadd9fc7 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -780,6 +780,8 @@ class Td final : public NetQueryCallback { void on_request(uint64 id, td_api::setChatMemberStatus &request); + void on_request(uint64 id, const td_api::banChatMember &request); + void on_request(uint64 id, const td_api::canTransferOwnership &request); void on_request(uint64 id, td_api::transferChatOwnership &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 9dc50a8c6..32204b328 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -3536,6 +3536,14 @@ class CliClient final : public Actor { string user_ids; get_args(args, chat_id, user_ids); send_request(td_api::make_object(as_chat_id(chat_id), as_user_ids(user_ids))); + } else if (op == "bcm") { + string chat_id; + string user_id; + int32 banned_until_date; + bool revoke_messages; + get_args(args, chat_id, user_id, banned_until_date, revoke_messages); + send_request(td_api::make_object(as_chat_id(chat_id), as_user_id(user_id), + banned_until_date, revoke_messages)); } else if (op == "spolla") { string chat_id; string message_id;