Add disableAllSupergroupUsernames.

This commit is contained in:
levlam 2022-10-17 14:58:47 +03:00
parent 85b0c0060e
commit e42e224154
8 changed files with 93 additions and 0 deletions

View File

@ -6293,6 +6293,9 @@ setSupergroupUsername supergroup_id:int53 username:string = Ok;
//@description Changes active state for a username of a supergroup or channel, requires owner privileges in the supergroup or channel. The editable username can't be disabled. May return an error with a message "USERNAMES_ACTIVE_TOO_MUCH" if the maximum number of active usernames has been reached @supergroup_id Identifier of the supergroup or channel @username The username to change @is_active True, if the username must be activated, or false, if it must be disabled
toggleSupergroupUsernameIsActive supergroup_id:int53 username:string is_active:Bool = Ok;
//@description Disables all active non-editable usernames of a supergroup or channel, requires owner privileges in the supergroup or channel @supergroup_id Identifier of the supergroup or channel
disableAllSupergroupUsernames supergroup_id:int53 = Ok;
//@description Changes order of active usernames of a supergroup or channel, requires owner privileges in the supergroup or channel @supergroup_id Identifier of the supergroup or channel @usernames The new order of active usernames. All currently active usernames must be specified
reorderSupergroupActiveUsernames supergroup_id:int53 usernames:vector<string> = Ok;

View File

@ -908,6 +908,44 @@ class ToggleChannelUsernameQuery final : public Td::ResultHandler {
}
};
class DeactivateAllChannelUsernamesQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
ChannelId channel_id_;
public:
explicit DeactivateAllChannelUsernamesQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(ChannelId channel_id) {
channel_id_ = channel_id;
auto input_channel = td_->contacts_manager_->get_input_channel(channel_id);
CHECK(input_channel != nullptr);
send_query(
G()->net_query_creator().create(telegram_api::channels_deactivateAllUsernames(std::move(input_channel))));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::channels_deactivateAllUsernames>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
bool result = result_ptr.ok();
LOG(DEBUG) << "Receive result for DeactivateAllChannelUsernamesQuery: " << result;
td_->contacts_manager_->on_deactivate_channel_usernames(channel_id_, std::move(promise_));
}
void on_error(Status status) final {
if (status.message() == "USERNAME_NOT_MODIFIED" || status.message() == "CHAT_NOT_MODIFIED") {
td_->contacts_manager_->on_deactivate_channel_usernames(channel_id_, std::move(promise_));
return;
} else {
td_->contacts_manager_->on_get_channel_error(channel_id_, status, "DeactivateAllChannelUsernamesQuery");
}
promise_.set_error(std::move(status));
}
};
class ReorderChannelUsernamesQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
ChannelId channel_id_;
@ -6965,6 +7003,17 @@ void ContactsManager::toggle_channel_username_is_active(ChannelId channel_id, st
td_->create_handler<ToggleChannelUsernameQuery>(std::move(promise))->send(channel_id, std::move(username), is_active);
}
void ContactsManager::disable_all_channel_usernames(ChannelId channel_id, Promise<Unit> &&promise) {
const auto *c = get_channel(channel_id);
if (c == nullptr) {
return promise.set_error(Status::Error(400, "Supergroup not found"));
}
if (!get_channel_status(c).is_creator()) {
return promise.set_error(Status::Error(400, "Not enough rights to disable usernames"));
}
td_->create_handler<DeactivateAllChannelUsernamesQuery>(std::move(promise))->send(channel_id);
}
void ContactsManager::reorder_channel_usernames(ChannelId channel_id, vector<string> &&usernames,
Promise<Unit> &&promise) {
const auto *c = get_channel(channel_id);
@ -6992,6 +7041,14 @@ void ContactsManager::on_update_channel_username_is_active(ChannelId channel_id,
promise.set_value(Unit());
}
void ContactsManager::on_deactivate_channel_usernames(ChannelId channel_id, Promise<Unit> &&promise) {
auto *c = get_channel(channel_id);
CHECK(c != nullptr);
on_update_channel_usernames(c, channel_id, c->usernames.deactivate_all());
update_channel(c, channel_id);
promise.set_value(Unit());
}
void ContactsManager::on_update_channel_active_usernames_order(ChannelId channel_id, vector<string> &&usernames,
Promise<Unit> &&promise) {
auto *c = get_channel(channel_id);

View File

@ -292,6 +292,8 @@ class ContactsManager final : public Actor {
void on_update_channel_username_is_active(ChannelId channel_id, string &&username, bool is_active,
Promise<Unit> &&promise);
void on_deactivate_channel_usernames(ChannelId channel_id, Promise<Unit> &&promise);
void on_update_channel_active_usernames_order(ChannelId channel_id, vector<string> &&usernames,
Promise<Unit> &&promise);
@ -370,6 +372,8 @@ class ContactsManager final : public Actor {
void toggle_channel_username_is_active(ChannelId channel_id, string &&username, bool is_active,
Promise<Unit> &&promise);
void disable_all_channel_usernames(ChannelId channel_id, Promise<Unit> &&promise);
void reorder_channel_usernames(ChannelId channel_id, vector<string> &&usernames, Promise<Unit> &&promise);
void set_channel_sticker_set(ChannelId channel_id, StickerSetId sticker_set_id, Promise<Unit> &&promise);

View File

@ -6867,6 +6867,12 @@ void Td::on_request(uint64 id, td_api::toggleSupergroupUsernameIsActive &request
request.is_active_, std::move(promise));
}
void Td::on_request(uint64 id, const td_api::disableAllSupergroupUsernames &request) {
CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE();
contacts_manager_->disable_all_channel_usernames(ChannelId(request.supergroup_id_), std::move(promise));
}
void Td::on_request(uint64 id, td_api::reorderSupergroupActiveUsernames &request) {
CHECK_IS_USER();
for (auto &username : request.usernames_) {

View File

@ -1068,6 +1068,8 @@ class Td final : public Actor {
void on_request(uint64 id, td_api::toggleSupergroupUsernameIsActive &request);
void on_request(uint64 id, const td_api::disableAllSupergroupUsernames &request);
void on_request(uint64 id, td_api::reorderSupergroupActiveUsernames &request);
void on_request(uint64 id, const td_api::setSupergroupStickerSet &request);

View File

@ -123,6 +123,21 @@ Usernames Usernames::toggle(const string &username, bool is_active) const {
return result;
}
Usernames Usernames::deactivate_all() const {
Usernames result;
for (size_t i = 0; i < active_usernames_.size(); i++) {
if (i == static_cast<size_t>(editable_username_pos_)) {
result.active_usernames_.push_back(active_usernames_[i]);
result.editable_username_pos_ = 0;
} else {
result.disabled_usernames_.push_back(active_usernames_[i]);
}
}
td::append(result.disabled_usernames_, disabled_usernames_);
CHECK(result.has_editable_username() == has_editable_username());
return result;
}
bool Usernames::can_reorder_to(const vector<string> &new_username_order) const {
if (new_username_order.size() != active_usernames_.size()) {
return false;

View File

@ -69,6 +69,8 @@ class Usernames {
Usernames toggle(const string &username, bool is_active) const;
Usernames deactivate_all() const;
bool can_reorder_to(const vector<string> &new_username_order) const;
Usernames reorder_to(vector<string> &&new_username_order) const;

View File

@ -4554,6 +4554,10 @@ class CliClient final : public Actor {
get_args(args, supergroup_id, username, is_active);
send_request(td_api::make_object<td_api::toggleSupergroupUsernameIsActive>(as_supergroup_id(supergroup_id),
username, is_active));
} else if (op == "dasgun" || op == "dachun") {
string supergroup_id;
get_args(args, supergroup_id);
send_request(td_api::make_object<td_api::disableAllSupergroupUsernames>(as_supergroup_id(supergroup_id)));
} else if (op == "rsgaun" || op == "rchaun") {
string supergroup_id;
get_args(args, supergroup_id);