diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 93e381497..d70be69c6 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -411,6 +411,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_usernames_active" = "active"; "lng_usernames_non_active" = "non active"; "lng_usernames_subtitle" = "Usernames order"; +"lng_usernames_activate_error#one" = "Sorry, you can't activate more than **{count}** usernames."; +"lng_usernames_activate_error#other" = "Sorry, you can't activate more than **{count}** usernames."; "lng_usernames_activate_description" = "Do you want to show this username on your info page?"; "lng_usernames_activate_confirm" = "Show"; "lng_channel_usernames_subtitle" = "Links order"; diff --git a/Telegram/SourceFiles/api/api_user_names.cpp b/Telegram/SourceFiles/api/api_user_names.cpp index 67f5a31e9..3e74dd65c 100644 --- a/Telegram/SourceFiles/api/api_user_names.cpp +++ b/Telegram/SourceFiles/api/api_user_names.cpp @@ -98,7 +98,7 @@ rpl::producer Usernames::loadUsernames( }; } -rpl::producer<> Usernames::toggle( +rpl::producer Usernames::toggle( not_null peer, const QString &username, bool active) { @@ -118,31 +118,47 @@ rpl::producer<> Usernames::toggle( entry.usernames.push_back(username); } - const auto finish = [=] { + const auto pop = [=](Error error) { const auto it = _toggleRequests.find(peerId); if (it != end(_toggleRequests)) { auto &list = it->second.usernames; list.erase(ranges::remove(list, username), end(list)); if (list.empty()) { - it->second.done.fire_done(); + if (error == Error::Unknown) { + it->second.done.fire_done(); + } else if (error == Error::TooMuch) { + it->second.done.fire_error_copy(error); + } _toggleRequests.remove(peerId); } } }; + const auto done = [=] { + pop(Error::Unknown); + }; + const auto fail = [=](const MTP::Error &error) { + const auto type = error.type(); + if (type == u"USERNAMES_ACTIVE_TOO_MUCH"_q) { + pop(Error::TooMuch); + } else { + pop(Error::Unknown); + } + }; + if (peer->isSelf()) { _api.request(MTPaccount_ToggleUsername( MTP_string(username), MTP_bool(active) - )).done(finish).fail(finish).send(); + )).done(done).fail(fail).send(); } else if (const auto channel = peer->asChannel()) { _api.request(MTPchannels_ToggleUsername( channel->inputChannel, MTP_string(username), MTP_bool(active) - )).done(finish).fail(finish).send(); + )).done(done).fail(fail).send(); } else { - return rpl::never<>(); + return rpl::never(); } return entry.done.events(); } diff --git a/Telegram/SourceFiles/api/api_user_names.h b/Telegram/SourceFiles/api/api_user_names.h index d94ecdc95..54b59ac29 100644 --- a/Telegram/SourceFiles/api/api_user_names.h +++ b/Telegram/SourceFiles/api/api_user_names.h @@ -21,11 +21,16 @@ namespace Api { class Usernames final { public: + enum class Error { + TooMuch, + Unknown, + }; + explicit Usernames(not_null api); [[nodiscard]] rpl::producer loadUsernames( not_null peer) const; - [[nodiscard]] rpl::producer<> toggle( + [[nodiscard]] rpl::producer toggle( not_null peer, const QString &username, bool active); @@ -45,7 +50,7 @@ private: using Key = PeerId; struct Entry final { - rpl::event_stream<> done; + rpl::event_stream done; std::vector usernames; }; base::flat_map _toggleRequests; diff --git a/Telegram/SourceFiles/boxes/peers/edit_peer_usernames_list.cpp b/Telegram/SourceFiles/boxes/peers/edit_peer_usernames_list.cpp index e775e7bc0..2c785024d 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_peer_usernames_list.cpp +++ b/Telegram/SourceFiles/boxes/peers/edit_peer_usernames_list.cpp @@ -17,6 +17,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/boxes/confirm_box.h" #include "ui/layers/show.h" #include "ui/painter.h" +#include "ui/text/text_utilities.h" // Ui::Text::RichLangValue. #include "ui/toast/toast.h" #include "ui/widgets/buttons.h" #include "ui/widgets/popup_menu.h" @@ -283,8 +284,23 @@ void UsernamesList::rebuild(const Data::Usernames &usernames) { _peer, username.username, !username.active - ) | rpl::start_with_done([=] { + ) | rpl::start_with_error_done([=]( + Api::Usernames::Error error) { + if (error == Api::Usernames::Error::TooMuch) { + constexpr auto kMaxUsernames = 10.; + _show->showBox( + Ui::MakeInformBox( + tr::lng_usernames_activate_error( + lt_count, + rpl::single(kMaxUsernames), + Ui::Text::RichLangValue)), + Ui::LayerOption::KeepOther); + } load(); + _toggleLifetime.destroy(); + }, [=] { + load(); + _toggleLifetime.destroy(); }); }); close();