diff --git a/Telegram/SourceFiles/api/api_user_names.cpp b/Telegram/SourceFiles/api/api_user_names.cpp index 6edefbad7..67f5a31e9 100644 --- a/Telegram/SourceFiles/api/api_user_names.cpp +++ b/Telegram/SourceFiles/api/api_user_names.cpp @@ -199,5 +199,28 @@ Data::Usernames Usernames::FromTL(const MTPVector &usernames) { ) | ranges::views::transform(UsernameFromTL) | ranges::to_vector; } +void Usernames::requestToCache(not_null peer) { + _tinyCache = {}; + if (const auto user = peer->asUser()) { + if (user->usernames().empty()) { + return; + } + } else if (const auto channel = peer->asChannel()) { + if (channel->usernames().empty()) { + return; + } + } + const auto lifetime = std::make_shared(); + *lifetime = loadUsernames( + peer + ) | rpl::start_with_next([=, id = peer->id](Data::Usernames usernames) { + _tinyCache = std::make_pair(id, std::move(usernames)); + lifetime->destroy(); + }); +} + +Data::Usernames Usernames::cacheFor(PeerId id) { + return (_tinyCache.first == id) ? _tinyCache.second : Data::Usernames(); +} } // namespace Api diff --git a/Telegram/SourceFiles/api/api_user_names.h b/Telegram/SourceFiles/api/api_user_names.h index dc3edc346..d94ecdc95 100644 --- a/Telegram/SourceFiles/api/api_user_names.h +++ b/Telegram/SourceFiles/api/api_user_names.h @@ -33,6 +33,9 @@ public: not_null peer, const std::vector &usernames); + void requestToCache(not_null peer); + [[nodiscard]] Data::Usernames cacheFor(PeerId id); + static Data::Usernames FromTL(const MTPVector &usernames); private: @@ -47,6 +50,8 @@ private: }; base::flat_map _toggleRequests; base::flat_map _reorderRequests; + // Used for a seamless display of usernames list. + std::pair _tinyCache; }; diff --git a/Telegram/SourceFiles/boxes/peers/edit_peer_info_box.cpp b/Telegram/SourceFiles/boxes/peers/edit_peer_info_box.cpp index e2da7b543..ae8961968 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_peer_info_box.cpp +++ b/Telegram/SourceFiles/boxes/peers/edit_peer_info_box.cpp @@ -635,7 +635,7 @@ void Controller::showEditPeerTypeBox( }); _typeDataSavedValue->hasLinkedChat = (_linkedChatSavedValue.value_or(nullptr) != nullptr); - _navigation->parentController()->show( + const auto box = _navigation->parentController()->show( Box( _navigation, _peer, @@ -644,6 +644,10 @@ void Controller::showEditPeerTypeBox( _typeDataSavedValue, error), Ui::LayerOption::KeepOther); + box->boxClosing( + ) | rpl::start_with_next([peer = _peer] { + peer->session().api().usernames().requestToCache(peer); + }, box->lifetime()); } void Controller::showEditLinkedChatBox() { @@ -740,6 +744,9 @@ void Controller::fillPrivacyTypeButton() { : tr::lng_manage_peer_channel_type)(), _privacyTypeUpdates.events( ) | rpl::map([=](Privacy flag) { + if (flag == Privacy::HasUsername) { + _peer->session().api().usernames().requestToCache(_peer); + } return (flag == Privacy::HasUsername) ? (hasLocation ? tr::lng_manage_peer_link_permanent diff --git a/Telegram/SourceFiles/boxes/peers/edit_peer_usernames_list.cpp b/Telegram/SourceFiles/boxes/peers/edit_peer_usernames_list.cpp index e3a8f8c7b..139bba5f6 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_peer_usernames_list.cpp +++ b/Telegram/SourceFiles/boxes/peers/edit_peer_usernames_list.cpp @@ -113,6 +113,13 @@ UsernamesList::UsernamesList( : RpWidget(parent) , _show(show) , _peer(peer) { + { + auto &api = _peer->session().api(); + const auto usernames = api.usernames().cacheFor(_peer->id); + if (!usernames.empty()) { + rebuild(usernames); + } + } load(); } diff --git a/Telegram/SourceFiles/settings/settings_information.cpp b/Telegram/SourceFiles/settings/settings_information.cpp index e4e6cfa31..298fcec9b 100644 --- a/Telegram/SourceFiles/settings/settings_information.cpp +++ b/Telegram/SourceFiles/settings/settings_information.cpp @@ -48,6 +48,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "window/window_peer_menu.h" #include "apiwrap.h" #include "api/api_peer_photo.h" +#include "api/api_user_names.h" #include "core/file_utilities.h" #include "base/call_delayed.h" #include "base/unixtime.h" @@ -445,12 +446,19 @@ void SetupRows( "internal:edit_username" }); return result; }); + session->api().usernames().requestToCache(session->user()); AddRow( container, std::move(label), std::move(value), tr::lng_context_copy_mention(tr::now), - [=] { controller->show(Box(UsernamesBox, session)); }, + [=] { + const auto box = controller->show(Box(UsernamesBox, session)); + box->boxClosing( + ) | rpl::start_with_next([=] { + session->api().usernames().requestToCache(session->user()); + }, box->lifetime()); + }, { &st::settingsIconMention, kIconLightOrange }); AddSkip(container);