Added handling of error for too much number of activated usernames.

This commit is contained in:
23rd 2022-10-17 19:10:56 +03:00 committed by John Preston
parent c9390dc02a
commit a4856e4436
4 changed files with 48 additions and 9 deletions

View file

@ -411,6 +411,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_usernames_active" = "active"; "lng_usernames_active" = "active";
"lng_usernames_non_active" = "non active"; "lng_usernames_non_active" = "non active";
"lng_usernames_subtitle" = "Usernames order"; "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_description" = "Do you want to show this username on your info page?";
"lng_usernames_activate_confirm" = "Show"; "lng_usernames_activate_confirm" = "Show";
"lng_channel_usernames_subtitle" = "Links order"; "lng_channel_usernames_subtitle" = "Links order";

View file

@ -98,7 +98,7 @@ rpl::producer<Data::Usernames> Usernames::loadUsernames(
}; };
} }
rpl::producer<> Usernames::toggle( rpl::producer<rpl::no_value, Usernames::Error> Usernames::toggle(
not_null<PeerData*> peer, not_null<PeerData*> peer,
const QString &username, const QString &username,
bool active) { bool active) {
@ -118,31 +118,47 @@ rpl::producer<> Usernames::toggle(
entry.usernames.push_back(username); entry.usernames.push_back(username);
} }
const auto finish = [=] { const auto pop = [=](Error error) {
const auto it = _toggleRequests.find(peerId); const auto it = _toggleRequests.find(peerId);
if (it != end(_toggleRequests)) { if (it != end(_toggleRequests)) {
auto &list = it->second.usernames; auto &list = it->second.usernames;
list.erase(ranges::remove(list, username), end(list)); list.erase(ranges::remove(list, username), end(list));
if (list.empty()) { 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); _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()) { if (peer->isSelf()) {
_api.request(MTPaccount_ToggleUsername( _api.request(MTPaccount_ToggleUsername(
MTP_string(username), MTP_string(username),
MTP_bool(active) MTP_bool(active)
)).done(finish).fail(finish).send(); )).done(done).fail(fail).send();
} else if (const auto channel = peer->asChannel()) { } else if (const auto channel = peer->asChannel()) {
_api.request(MTPchannels_ToggleUsername( _api.request(MTPchannels_ToggleUsername(
channel->inputChannel, channel->inputChannel,
MTP_string(username), MTP_string(username),
MTP_bool(active) MTP_bool(active)
)).done(finish).fail(finish).send(); )).done(done).fail(fail).send();
} else { } else {
return rpl::never<>(); return rpl::never<rpl::no_value, Error>();
} }
return entry.done.events(); return entry.done.events();
} }

View file

@ -21,11 +21,16 @@ namespace Api {
class Usernames final { class Usernames final {
public: public:
enum class Error {
TooMuch,
Unknown,
};
explicit Usernames(not_null<ApiWrap*> api); explicit Usernames(not_null<ApiWrap*> api);
[[nodiscard]] rpl::producer<Data::Usernames> loadUsernames( [[nodiscard]] rpl::producer<Data::Usernames> loadUsernames(
not_null<PeerData*> peer) const; not_null<PeerData*> peer) const;
[[nodiscard]] rpl::producer<> toggle( [[nodiscard]] rpl::producer<rpl::no_value, Error> toggle(
not_null<PeerData*> peer, not_null<PeerData*> peer,
const QString &username, const QString &username,
bool active); bool active);
@ -45,7 +50,7 @@ private:
using Key = PeerId; using Key = PeerId;
struct Entry final { struct Entry final {
rpl::event_stream<> done; rpl::event_stream<rpl::no_value, Error> done;
std::vector<QString> usernames; std::vector<QString> usernames;
}; };
base::flat_map<Key, Entry> _toggleRequests; base::flat_map<Key, Entry> _toggleRequests;

View file

@ -17,6 +17,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/boxes/confirm_box.h" #include "ui/boxes/confirm_box.h"
#include "ui/layers/show.h" #include "ui/layers/show.h"
#include "ui/painter.h" #include "ui/painter.h"
#include "ui/text/text_utilities.h" // Ui::Text::RichLangValue.
#include "ui/toast/toast.h" #include "ui/toast/toast.h"
#include "ui/widgets/buttons.h" #include "ui/widgets/buttons.h"
#include "ui/widgets/popup_menu.h" #include "ui/widgets/popup_menu.h"
@ -283,8 +284,23 @@ void UsernamesList::rebuild(const Data::Usernames &usernames) {
_peer, _peer,
username.username, username.username,
!username.active !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(); load();
_toggleLifetime.destroy();
}, [=] {
load();
_toggleLifetime.destroy();
}); });
}); });
close(); close();