diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 7caf19b7b..87e56998a 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -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 = Ok; diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 159c54918..86a9ebcb8 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -908,6 +908,44 @@ class ToggleChannelUsernameQuery final : public Td::ResultHandler { } }; +class DeactivateAllChannelUsernamesQuery final : public Td::ResultHandler { + Promise promise_; + ChannelId channel_id_; + + public: + explicit DeactivateAllChannelUsernamesQuery(Promise &&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(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 promise_; ChannelId channel_id_; @@ -6965,6 +7003,17 @@ void ContactsManager::toggle_channel_username_is_active(ChannelId channel_id, st td_->create_handler(std::move(promise))->send(channel_id, std::move(username), is_active); } +void ContactsManager::disable_all_channel_usernames(ChannelId channel_id, Promise &&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(std::move(promise))->send(channel_id); +} + void ContactsManager::reorder_channel_usernames(ChannelId channel_id, vector &&usernames, Promise &&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 &&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 &&usernames, Promise &&promise) { auto *c = get_channel(channel_id); diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 5ddda1c18..be244311e 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -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 &&promise); + void on_deactivate_channel_usernames(ChannelId channel_id, Promise &&promise); + void on_update_channel_active_usernames_order(ChannelId channel_id, vector &&usernames, Promise &&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 &&promise); + void disable_all_channel_usernames(ChannelId channel_id, Promise &&promise); + void reorder_channel_usernames(ChannelId channel_id, vector &&usernames, Promise &&promise); void set_channel_sticker_set(ChannelId channel_id, StickerSetId sticker_set_id, Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 1f3fc3d9d..b3271ec46 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -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_) { diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 4fded664c..c8a0cc125 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -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); diff --git a/td/telegram/Usernames.cpp b/td/telegram/Usernames.cpp index c38e1b4bf..cb859733b 100644 --- a/td/telegram/Usernames.cpp +++ b/td/telegram/Usernames.cpp @@ -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(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 &new_username_order) const { if (new_username_order.size() != active_usernames_.size()) { return false; diff --git a/td/telegram/Usernames.h b/td/telegram/Usernames.h index 3cf3e3f10..7bcd8e2d4 100644 --- a/td/telegram/Usernames.h +++ b/td/telegram/Usernames.h @@ -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 &new_username_order) const; Usernames reorder_to(vector &&new_username_order) const; diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index a106073c8..60ac2203c 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -4554,6 +4554,10 @@ class CliClient final : public Actor { get_args(args, supergroup_id, username, is_active); send_request(td_api::make_object(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(as_supergroup_id(supergroup_id))); } else if (op == "rsgaun" || op == "rchaun") { string supergroup_id; get_args(args, supergroup_id);