Add separate banChatMember method.

This commit is contained in:
levlam 2021-01-20 21:12:48 +03:00
parent 7a45faff24
commit da4d2b7636
9 changed files with 62 additions and 8 deletions

View File

@ -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<int32> = 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;

Binary file not shown.

View File

@ -1952,8 +1952,11 @@ class DeleteChatUserQuery : public Td::ResultHandler {
explicit DeleteChatUserQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(ChatId chat_id, tl_object_ptr<telegram_api::InputUser> &&input_user) {
void send(ChatId chat_id, tl_object_ptr<telegram_api::InputUser> &&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<Unit> &&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<ImportDialogInviteLinkQuery>(std::move(promise))->send(invite_link);
}
void ContactsManager::delete_chat_participant(ChatId chat_id, UserId user_id, Promise<Unit> &&promise) {
void ContactsManager::delete_chat_participant(ChatId chat_id, UserId user_id, bool revoke_messages,
Promise<Unit> &&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<DeleteChatUserQuery>(std::move(promise))->send(chat_id, std::move(input_user));
td_->create_handler<DeleteChatUserQuery>(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,

View File

@ -385,6 +385,8 @@ class ContactsManager : public Actor {
void change_channel_participant_status(ChannelId channel_id, UserId user_id, DialogParticipantStatus status,
Promise<Unit> &&promise);
void delete_chat_participant(ChatId chat_id, UserId user_id, bool revoke_messages, Promise<Unit> &&promise);
void can_transfer_ownership(Promise<CanTransferOwnershipResult> &&promise);
static td_api::object_ptr<td_api::CanTransferOwnershipResult> get_can_transfer_ownership_result_object(

View File

@ -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<Unit> &&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<UserId> &user_ids,
Promise<Unit> &&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<td_api::ChatMemberStatus> &chat_member_status,
Promise<Unit> &&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<Unit> &&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<Unit> &&promise) {
LOG(INFO) << "Receive GetChatMember request to get " << user_id << " in " << dialog_id << " with random_id "

View File

@ -477,6 +477,9 @@ class MessagesManager : public Actor {
const tl_object_ptr<td_api::ChatMemberStatus> &chat_member_status,
Promise<Unit> &&promise);
void ban_dialog_participant(DialogId dialog_id, UserId user_id, int32 banned_until_date, bool revoke_messages,
Promise<Unit> &&promise);
DialogParticipant get_dialog_participant(DialogId dialog_id, UserId user_id, int64 &random_id, bool force,
Promise<Unit> &&promise);

View File

@ -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();

View File

@ -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);

View File

@ -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<td_api::addChatMembers>(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<td_api::banChatMember>(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;