Add td_api::setChatPermissions.

GitOrigin-RevId: ce072b20283e5a1887bd94fea33ad4d0cb4ec5b9
This commit is contained in:
levlam 2019-03-20 20:58:13 +03:00
parent e71b749a79
commit 9b8a5438be
10 changed files with 146 additions and 5 deletions

View File

@ -3171,6 +3171,10 @@ setChatTitle chat_id:int53 title:string = Ok;
//@chat_id Chat identifier @photo New chat photo. You can use a zero InputFileId to delete the chat photo. Files that are accessible only by HTTP URL are not acceptable //@chat_id Chat identifier @photo New chat photo. You can use a zero InputFileId to delete the chat photo. Files that are accessible only by HTTP URL are not acceptable
setChatPhoto chat_id:int53 photo:InputFile = Ok; setChatPhoto chat_id:int53 photo:InputFile = Ok;
//@description Changes the chat members permissions. Supported only for basic groups and supergroups. Requires can_restrict_members administrator right
//@chat_id Chat identifier @permissions New members permissions in the chat
setChatPermissions chat_id:int53 permissions:chatPermissions = Ok;
//@description Changes the draft message in a chat @chat_id Chat identifier @draft_message New draft message; may be null //@description Changes the draft message in a chat @chat_id Chat identifier @draft_message New draft message; may be null
setChatDraftMessage chat_id:int53 draft_message:draftMessage = Ok; setChatDraftMessage chat_id:int53 draft_message:draftMessage = Ok;

Binary file not shown.

View File

@ -10119,7 +10119,8 @@ void ContactsManager::on_chat_update(telegram_api::channelForbidden &channel, co
int32 unban_date = (channel.flags_ & CHANNEL_FLAG_HAS_UNBAN_DATE) != 0 ? channel.until_date_ : 0; int32 unban_date = (channel.flags_ & CHANNEL_FLAG_HAS_UNBAN_DATE) != 0 ? channel.until_date_ : 0;
on_update_channel_status(c, channel_id, DialogParticipantStatus::Banned(unban_date)); on_update_channel_status(c, channel_id, DialogParticipantStatus::Banned(unban_date));
on_update_channel_username(c, channel_id, ""); // don't know if channel username is empty, but update it anyway on_update_channel_username(c, channel_id, ""); // don't know if channel username is empty, but update it anyway
on_update_channel_default_permissions(c, channel_id, get_restricted_rights(nullptr)); tl_object_ptr<telegram_api::chatBannedRights> banned_rights; // == nullptr
on_update_channel_default_permissions(c, channel_id, get_restricted_rights(banned_rights));
bool sign_messages = false; bool sign_messages = false;
bool is_megagroup = (channel.flags_ & CHANNEL_FLAG_IS_MEGAGROUP) != 0; bool is_megagroup = (channel.flags_ & CHANNEL_FLAG_IS_MEGAGROUP) != 0;

View File

@ -545,6 +545,18 @@ RestrictedRights get_restricted_rights(const tl_object_ptr<telegram_api::chatBan
can_change_info_and_settings, can_invite_users, can_pin_messages); can_change_info_and_settings, can_invite_users, can_pin_messages);
} }
RestrictedRights get_restricted_rights(const td_api::object_ptr<td_api::chatPermissions> &permissions) {
bool can_send_polls = permissions->can_send_polls_;
bool can_send_media = permissions->can_send_media_messages_ || permissions->can_send_other_messages_ ||
permissions->can_add_web_page_previews_;
bool can_send_messages = permissions->can_send_messages_ || can_send_media || can_send_polls;
return RestrictedRights(can_send_messages, can_send_media, permissions->can_send_other_messages_,
permissions->can_send_other_messages_, permissions->can_send_other_messages_,
permissions->can_send_other_messages_, permissions->can_add_web_page_previews_,
permissions->can_send_polls_, permissions->can_change_info_, permissions->can_invite_users_,
permissions->can_pin_messages_);
}
StringBuilder &operator<<(StringBuilder &string_builder, const DialogParticipant &dialog_participant) { StringBuilder &operator<<(StringBuilder &string_builder, const DialogParticipant &dialog_participant) {
return string_builder << '[' << dialog_participant.user_id << " invited by " << dialog_participant.inviter_user_id return string_builder << '[' << dialog_participant.user_id << " invited by " << dialog_participant.inviter_user_id
<< " at " << dialog_participant.joined_date << " with status " << dialog_participant.status << " at " << dialog_participant.joined_date << " with status " << dialog_participant.status

View File

@ -406,4 +406,6 @@ DialogParticipantStatus get_dialog_participant_status(
RestrictedRights get_restricted_rights(const tl_object_ptr<telegram_api::chatBannedRights> &banned_rights); RestrictedRights get_restricted_rights(const tl_object_ptr<telegram_api::chatBannedRights> &banned_rights);
RestrictedRights get_restricted_rights(const td_api::object_ptr<td_api::chatPermissions> &permissions);
} // namespace td } // namespace td

View File

@ -756,6 +756,48 @@ class EditDialogTitleQuery : public Td::ResultHandler {
} }
}; };
class EditDialogDefaultBannedRightsQuery : public Td::ResultHandler {
Promise<Unit> promise_;
DialogId dialog_id_;
public:
explicit EditDialogDefaultBannedRightsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(DialogId dialog_id, RestrictedRights permissions) {
dialog_id_ = dialog_id;
auto input_peer = td->messages_manager_->get_input_peer(dialog_id, AccessRights::Write);
CHECK(input_peer != nullptr);
send_query(G()->net_query_creator().create(create_storer(telegram_api::messages_editChatDefaultBannedRights(
std::move(input_peer), permissions.get_chat_banned_rights()))));
}
void on_result(uint64 id, BufferSlice packet) override {
auto result_ptr = fetch_result<telegram_api::messages_editChatDefaultBannedRights>(packet);
if (result_ptr.is_error()) {
return on_error(id, result_ptr.move_as_error());
}
auto ptr = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for editDialogPermissions " << to_string(ptr);
td->updates_manager_->on_get_updates(std::move(ptr));
promise_.set_value(Unit());
}
void on_error(uint64 id, Status status) override {
if (status.message() == "CHAT_NOT_MODIFIED") {
if (!td->auth_manager_->is_bot()) {
promise_.set_value(Unit());
return;
}
} else {
td->messages_manager_->on_get_dialog_error(dialog_id_, status, "EditDialogDefaultBannedRightsQuery");
}
promise_.set_error(std::move(status));
}
};
class SaveDraftMessageQuery : public Td::ResultHandler { class SaveDraftMessageQuery : public Td::ResultHandler {
Promise<Unit> promise_; Promise<Unit> promise_;
DialogId dialog_id_; DialogId dialog_id_;
@ -21369,7 +21411,7 @@ SearchMessagesFilter MessagesManager::get_search_messages_filter(
void MessagesManager::set_dialog_photo(DialogId dialog_id, const tl_object_ptr<td_api::InputFile> &photo, void MessagesManager::set_dialog_photo(DialogId dialog_id, const tl_object_ptr<td_api::InputFile> &photo,
Promise<Unit> &&promise) { Promise<Unit> &&promise) {
LOG(INFO) << "Receive SetChatPhoto request to change photo of " << dialog_id; LOG(INFO) << "Receive setChatPhoto request to change photo of " << dialog_id;
if (!have_dialog_force(dialog_id)) { if (!have_dialog_force(dialog_id)) {
return promise.set_error(Status::Error(3, "Chat not found")); return promise.set_error(Status::Error(3, "Chat not found"));
@ -21443,7 +21485,7 @@ void MessagesManager::upload_dialog_photo(DialogId dialog_id, FileId file_id, Pr
} }
void MessagesManager::set_dialog_title(DialogId dialog_id, const string &title, Promise<Unit> &&promise) { void MessagesManager::set_dialog_title(DialogId dialog_id, const string &title, Promise<Unit> &&promise) {
LOG(INFO) << "Receive SetChatTitle request to change title of " << dialog_id << " to \"" << title << '"'; LOG(INFO) << "Receive setChatTitle request to change title of " << dialog_id << " to \"" << title << '"';
if (!have_dialog_force(dialog_id)) { if (!have_dialog_force(dialog_id)) {
return promise.set_error(Status::Error(3, "Chat not found")); return promise.set_error(Status::Error(3, "Chat not found"));
@ -21489,6 +21531,62 @@ void MessagesManager::set_dialog_title(DialogId dialog_id, const string &title,
td_->create_handler<EditDialogTitleQuery>(std::move(promise))->send(dialog_id, new_title); td_->create_handler<EditDialogTitleQuery>(std::move(promise))->send(dialog_id, new_title);
} }
void MessagesManager::set_dialog_permissions(DialogId dialog_id,
const td_api::object_ptr<td_api::chatPermissions> &permissions,
Promise<Unit> &&promise) {
LOG(INFO) << "Receive setChatPermissions request to change permissions of " << dialog_id << " to "
<< to_string(permissions);
if (!have_dialog_force(dialog_id)) {
return promise.set_error(Status::Error(3, "Chat not found"));
}
if (!have_input_peer(dialog_id, AccessRights::Write)) {
return promise.set_error(Status::Error(3, "Can't access the chat"));
}
if (permissions == nullptr) {
return promise.set_error(Status::Error(3, "New permissions must not be empty"));
}
switch (dialog_id.get_type()) {
case DialogType::User:
return promise.set_error(Status::Error(3, "Can't change private chat permissions"));
case DialogType::Chat: {
auto chat_id = dialog_id.get_chat_id();
auto status = td_->contacts_manager_->get_chat_status(chat_id);
if (!status.can_restrict_members()) {
return promise.set_error(Status::Error(3, "Not enough rights to change chat permissions"));
}
break;
}
case DialogType::Channel: {
if (is_broadcast_channel(dialog_id)) {
return promise.set_error(Status::Error(3, "Can't change channel chat permissions"));
}
auto status = td_->contacts_manager_->get_channel_status(dialog_id.get_channel_id());
if (!status.can_restrict_members()) {
return promise.set_error(Status::Error(3, "Not enough rights to change chat permissions"));
}
break;
}
case DialogType::SecretChat:
return promise.set_error(Status::Error(3, "Can't change secret chat permissions"));
case DialogType::None:
default:
UNREACHABLE();
}
auto new_permissions = get_restricted_rights(permissions);
// TODO this can be wrong if there was previous change title requests
if (get_dialog_permissions(dialog_id) == new_permissions) {
return promise.set_value(Unit());
}
// TODO invoke after
td_->create_handler<EditDialogDefaultBannedRightsQuery>(std::move(promise))->send(dialog_id, new_permissions);
}
void MessagesManager::set_dialog_description(DialogId dialog_id, const string &description, Promise<Unit> &&promise) { void MessagesManager::set_dialog_description(DialogId dialog_id, const string &description, Promise<Unit> &&promise) {
LOG(INFO) << "Receive setChatDescription request to set description of " << dialog_id << " to \"" << description LOG(INFO) << "Receive setChatDescription request to set description of " << dialog_id << " to \"" << description
<< '"'; << '"';
@ -21620,7 +21718,7 @@ void MessagesManager::set_dialog_participant_status(DialogId dialog_id, UserId u
const tl_object_ptr<td_api::ChatMemberStatus> &chat_member_status, const tl_object_ptr<td_api::ChatMemberStatus> &chat_member_status,
Promise<Unit> &&promise) { Promise<Unit> &&promise) {
auto status = get_dialog_participant_status(chat_member_status); auto status = get_dialog_participant_status(chat_member_status);
LOG(INFO) << "Receive SetChatMemberStatus request with " << user_id << " and " << dialog_id << " to " << status; LOG(INFO) << "Receive setChatMemberStatus request with " << user_id << " and " << dialog_id << " to " << status;
if (!have_dialog_force(dialog_id)) { if (!have_dialog_force(dialog_id)) {
return promise.set_error(Status::Error(3, "Chat not found")); return promise.set_error(Status::Error(3, "Chat not found"));
} }
@ -21728,7 +21826,7 @@ std::pair<int32, vector<DialogParticipant>> MessagesManager::search_private_chat
std::pair<int32, vector<DialogParticipant>> MessagesManager::search_dialog_participants( std::pair<int32, vector<DialogParticipant>> MessagesManager::search_dialog_participants(
DialogId dialog_id, const string &query, int32 limit, DialogParticipantsFilter filter, int64 &random_id, bool force, DialogId dialog_id, const string &query, int32 limit, DialogParticipantsFilter filter, int64 &random_id, bool force,
Promise<Unit> &&promise) { Promise<Unit> &&promise) {
LOG(INFO) << "Receive SearchChatMembers request to search for " << query << " in " << dialog_id; LOG(INFO) << "Receive searchChatMembers request to search for " << query << " in " << dialog_id;
if (!have_dialog_force(dialog_id)) { if (!have_dialog_force(dialog_id)) {
promise.set_error(Status::Error(3, "Chat not found")); promise.set_error(Status::Error(3, "Chat not found"));
return {}; return {};

View File

@ -420,6 +420,9 @@ class MessagesManager : public Actor {
void set_dialog_description(DialogId dialog_id, const string &description, Promise<Unit> &&promise); void set_dialog_description(DialogId dialog_id, const string &description, Promise<Unit> &&promise);
void set_dialog_permissions(DialogId dialog_id, const td_api::object_ptr<td_api::chatPermissions> &permissions,
Promise<Unit> &&promise);
void pin_dialog_message(DialogId dialog_id, MessageId message_id, bool disable_notification, bool is_unpin, void pin_dialog_message(DialogId dialog_id, MessageId message_id, bool disable_notification, bool is_unpin,
Promise<Unit> &&promise); Promise<Unit> &&promise);

View File

@ -5664,6 +5664,11 @@ void Td::on_request(uint64 id, const td_api::setChatPhoto &request) {
messages_manager_->set_dialog_photo(DialogId(request.chat_id_), request.photo_, std::move(promise)); messages_manager_->set_dialog_photo(DialogId(request.chat_id_), request.photo_, std::move(promise));
} }
void Td::on_request(uint64 id, const td_api::setChatPermissions &request) {
CREATE_OK_REQUEST_PROMISE();
messages_manager_->set_dialog_permissions(DialogId(request.chat_id_), request.permissions_, std::move(promise));
}
void Td::on_request(uint64 id, td_api::setChatDraftMessage &request) { void Td::on_request(uint64 id, td_api::setChatDraftMessage &request) {
CHECK_IS_USER(); CHECK_IS_USER();
answer_ok_query( answer_ok_query(

View File

@ -638,6 +638,8 @@ class Td final : public NetQueryCallback {
void on_request(uint64 id, const td_api::setChatPhoto &request); void on_request(uint64 id, const td_api::setChatPhoto &request);
void on_request(uint64 id, const td_api::setChatPermissions &request);
void on_request(uint64 id, td_api::setChatDraftMessage &request); void on_request(uint64 id, td_api::setChatDraftMessage &request);
void on_request(uint64 id, const td_api::toggleChatIsPinned &request); void on_request(uint64 id, const td_api::toggleChatIsPinned &request);

View File

@ -3160,6 +3160,20 @@ class CliClient final : public Actor {
std::tie(chat_id, photo_path) = split(args); std::tie(chat_id, photo_path) = split(args);
send_request(td_api::make_object<td_api::setChatPhoto>(as_chat_id(chat_id), as_input_file(photo_path))); send_request(td_api::make_object<td_api::setChatPhoto>(as_chat_id(chat_id), as_input_file(photo_path)));
} else if (op == "scperm") {
string chat_id;
string permissions;
std::tie(chat_id, permissions) = split(args);
if (permissions.size() == 8) {
auto &s = permissions;
send_request(td_api::make_object<td_api::setChatPermissions>(
as_chat_id(chat_id),
td_api::make_object<td_api::chatPermissions>(s[0] == '1', s[1] == '1', s[2] == '1', s[3] == '1',
s[4] == '1', s[5] == '1', s[6] == '1', s[7] == '1')));
} else {
LOG(ERROR) << "Wrong permissions size, expected 8";
}
} else if (op == "scpid") { } else if (op == "scpid") {
string chat_id; string chat_id;
string file_id; string file_id;