Added usernames support to info profile.

This commit is contained in:
23rd 2022-10-13 01:24:03 +03:00 committed by John Preston
parent ad7bc6326d
commit 79f592a84f
9 changed files with 155 additions and 43 deletions

View file

@ -1144,6 +1144,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_info_mobile_label" = "Mobile";
"lng_info_mobile_hidden" = "Hidden";
"lng_info_username_label" = "Username";
"lng_info_usernames_label" = "also";
"lng_info_bio_label" = "Bio";
"lng_info_link_label" = "Link";
"lng_info_location_label" = "Location";

View file

@ -67,6 +67,7 @@ struct PeerUpdate {
IsBlocked = (1ULL << 8),
MessagesTTL = (1ULL << 9),
FullInfo = (1ULL << 10),
Usernames = (1ULL << 11),
// For users
CanShareContact = (1ULL << 11),

View file

@ -109,13 +109,18 @@ void ChannelData::setUsername(const QString &username) {
}
void ChannelData::setUsernames(const Data::Usernames &usernames) {
_usernames = ranges::views::all(
auto newUsernames = ranges::views::all(
usernames
) | ranges::views::filter([&](const Data::Username &username) {
return username.active;
}) | ranges::views::transform([&](const Data::Username &username) {
return username.username;
}) | ranges::to_vector;
if (!ranges::equal(_usernames, newUsernames)) {
_usernames = std::move(newUsernames);
session().changes().peerUpdated(this, UpdateFlag::Usernames);
}
}
QString ChannelData::username() const {

View file

@ -121,13 +121,18 @@ void UserData::setName(const QString &newFirstName, const QString &newLastName,
}
void UserData::setUsernames(const Data::Usernames &usernames) {
_usernames = ranges::views::all(
auto newUsernames = ranges::views::all(
usernames
) | ranges::views::filter([&](const Data::Username &username) {
return username.active;
}) | ranges::views::transform([&](const Data::Username &username) {
return username.username;
}) | ranges::to_vector;
if (!ranges::equal(_usernames, newUsernames)) {
_usernames = std::move(newUsernames);
session().changes().peerUpdated(this, UpdateFlag::Usernames);
}
}
void UserData::setUsername(const QString &username) {

View file

@ -27,6 +27,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/layers/generic_box.h"
#include "ui/toast/toast.h"
#include "ui/text/text_utilities.h" // Ui::Text::ToUpper
#include "ui/text/text_variant.h"
#include "history/history_location_manager.h" // LocationClickHandler.
#include "history/view/history_view_context_menu.h" // HistoryView::ShowReportPeerBox
#include "boxes/abstract_box.h"
@ -67,12 +68,59 @@ namespace Info {
namespace Profile {
namespace {
object_ptr<Ui::RpWidget> CreateSkipWidget(
[[nodiscard]] rpl::producer<TextWithEntities> UsernamesSubtext(
not_null<PeerData*> peer,
rpl::producer<QString> fallback) {
return rpl::combine(
UsernamesValue(peer),
std::move(fallback)
) | rpl::map([](std::vector<TextWithEntities> usernames, QString text) {
if (usernames.size() < 2) {
return TextWithEntities{ .text = text };
} else {
auto result = TextWithEntities();
result.append(tr::lng_info_usernames_label(tr::now));
result.append(' ');
auto &&subrange = ranges::make_subrange(
begin(usernames) + 1,
end(usernames));
for (auto &username : std::move(subrange)) {
const auto isLast = (usernames.back() == username);
result.append(Ui::Text::Link(
'@' + base::take(username.text),
username.entities.front().data()));
if (!isLast) {
result.append(u", "_q);
}
}
return result;
}
});
}
[[nodiscard]] Fn<void(QString)> UsernamesLinkCallback(
not_null<PeerData*> peer,
Window::Show show,
const QString &addToLink) {
return [=](QString link) {
if (!link.startsWith(u"https://"_q)) {
link = peer->session().createInternalLinkFull(peer->userName());
}
if (!link.isEmpty()) {
QGuiApplication::clipboard()->setText(link + addToLink);
Ui::Toast::Show(
show.toastParent(),
tr::lng_username_copied(tr::now));
}
};
}
[[nodiscard]] object_ptr<Ui::RpWidget> CreateSkipWidget(
not_null<Ui::RpWidget*> parent) {
return Ui::CreateSkipWidget(parent, st::infoProfileSkip);
}
object_ptr<Ui::SlideWrap<>> CreateSlideSkipWidget(
[[nodiscard]] object_ptr<Ui::SlideWrap<>> CreateSlideSkipWidget(
not_null<Ui::RpWidget*> parent) {
auto result = Ui::CreateSlideSkipWidget(
parent,
@ -113,7 +161,7 @@ auto AddActionButton(
};
template <typename Text, typename ToggleOn, typename Callback>
auto AddMainButton(
[[nodiscard]] auto AddMainButton(
not_null<Ui::VerticalLayout*> parent,
Text &&text,
ToggleOn &&toggleOn,
@ -263,23 +311,23 @@ object_ptr<Ui::RpWidget> DetailsFiller::setupInfo() {
};
const auto addInfoLineGeneric = [&](
rpl::producer<QString> &&label,
v::text::data &&label,
rpl::producer<TextWithEntities> &&text,
const style::FlatLabel &textSt = st::infoLabeled,
const style::margins &padding = st::infoProfileLabeledPadding) {
auto line = CreateTextWithLabel(
result,
std::move(label) | Ui::Text::ToWithEntities(),
v::text::take_marked(std::move(label)),
std::move(text),
textSt,
padding);
tracker.track(result->add(std::move(line.wrap)));
line.text->setClickHandlerFilter(infoClickFilter);
return line.text;
return line;
};
const auto addInfoLine = [&](
rpl::producer<QString> &&label,
v::text::data &&label,
rpl::producer<TextWithEntities> &&text,
const style::FlatLabel &textSt = st::infoLabeled,
const style::margins &padding = st::infoProfileLabeledPadding) {
@ -290,17 +338,17 @@ object_ptr<Ui::RpWidget> DetailsFiller::setupInfo() {
padding);
};
const auto addInfoOneLine = [&](
rpl::producer<QString> &&label,
v::text::data &&label,
rpl::producer<TextWithEntities> &&text,
const QString &contextCopyText,
const style::margins &padding = st::infoProfileLabeledPadding) {
const auto result = addInfoLine(
auto result = addInfoLine(
std::move(label),
std::move(text),
st::infoLabeledOneLine,
padding);
result->setDoubleClickSelectsParagraph(true);
result->setContextCopyText(contextCopyText);
result.text->setDoubleClickSelectsParagraph(true);
result.text->setContextCopyText(contextCopyText);
return result;
};
if (const auto user = _peer->asUser()) {
@ -320,11 +368,16 @@ object_ptr<Ui::RpWidget> DetailsFiller::setupInfo() {
: tr::lng_info_bio_label();
addInfoLine(std::move(label), AboutValue(user));
const auto usernameLabel = addInfoOneLine(
tr::lng_info_username_label(),
UsernameValue(user),
const auto usernameLine = addInfoOneLine(
UsernamesSubtext(_peer, tr::lng_info_username_label()),
UsernameValue(user, true),
tr::lng_context_copy_mention(tr::now),
st::infoProfileLabeledUsernamePadding);
usernameLine.subtext->overrideLinkClickHandler(UsernamesLinkCallback(
_peer,
Window::Show(controller),
QString()));
const auto usernameLabel = usernameLine.text;
if (user->isBot()) {
const auto copyUsername = Ui::CreateChild<Ui::IconButton>(
usernameLabel->parentWidget(),
@ -361,7 +414,8 @@ object_ptr<Ui::RpWidget> DetailsFiller::setupInfo() {
? "?topic=" + QString::number(topicRootId.bare)
: QString();
auto linkText = LinkValue(
_peer
_peer,
true
) | rpl::map([=](const QString &link) {
return link.isEmpty()
? TextWithEntities()
@ -371,21 +425,17 @@ object_ptr<Ui::RpWidget> DetailsFiller::setupInfo() {
: link) + addToLink,
link + addToLink);
});
auto link = addInfoOneLine(
tr::lng_info_link_label(),
auto linkLine = addInfoOneLine(
UsernamesSubtext(_peer, tr::lng_info_link_label()),
std::move(linkText),
QString());
const auto controller = _controller->parentController();
link->overrideLinkClickHandler([=, peer = _peer] {
const auto link = peer->session().createInternalLinkFull(
peer->userName());
if (!link.isEmpty()) {
QGuiApplication::clipboard()->setText(link + addToLink);
Ui::Toast::Show(
Window::Show(controller).toastParent(),
tr::lng_username_copied(tr::now));
}
});
const auto linkCallback = UsernamesLinkCallback(
_peer,
Window::Show(controller),
addToLink);
linkLine.text->overrideLinkClickHandler(linkCallback);
linkLine.subtext->overrideLinkClickHandler(linkCallback);
if (const auto channel = _topic ? nullptr : _peer->asChannel()) {
auto locationText = LocationValue(
@ -401,7 +451,7 @@ object_ptr<Ui::RpWidget> DetailsFiller::setupInfo() {
tr::lng_info_location_label(),
std::move(locationText),
QString()
)->setLinksTrusted();
).text->setLinksTrusted();
}
addInfoLine(

View file

@ -51,7 +51,7 @@ TextWithLabel CreateTextWithLabel(
textSt));
labeled->setSelectable(true);
layout->add(Ui::CreateSkipWidget(layout, st::infoLabelSkip));
layout->add(object_ptr<Ui::FlatLabel>(
const auto subtext = layout->add(object_ptr<Ui::FlatLabel>(
layout,
std::move(
label
@ -60,7 +60,7 @@ TextWithLabel CreateTextWithLabel(
}),
st::infoLabel));
result->finishAnimating();
return { std::move(result), labeled };
return { std::move(result), labeled, subtext };
}
} // namespace Profile

View file

@ -28,6 +28,7 @@ namespace Profile {
struct TextWithLabel {
object_ptr<Ui::SlideWrap<Ui::VerticalLayout>> wrap;
not_null<Ui::FlatLabel*> text;
not_null<Ui::FlatLabel*> subtext;
};
TextWithLabel CreateTextWithLabel(

View file

@ -47,14 +47,26 @@ auto PlainAboutValue(not_null<PeerData*> peer) {
}
auto PlainUsernameValue(not_null<PeerData*> peer) {
return peer->session().changes().peerFlagsValue(
peer,
UpdateFlag::Username
return rpl::merge(
peer->session().changes().peerFlagsValue(peer, UpdateFlag::Username),
peer->session().changes().peerFlagsValue(peer, UpdateFlag::Usernames)
) | rpl::map([=] {
return peer->userName();
});
}
auto PlainPrimaryUsernameValue(not_null<PeerData*> peer) {
return UsernamesValue(
peer
) | rpl::map([=](std::vector<TextWithEntities> usernames) {
if (!usernames.empty()) {
return rpl::single(usernames.front().text);
} else {
return PlainUsernameValue(peer);
}
}) | rpl::flatten_latest();
}
void StripExternalLinks(TextWithEntities &text) {
const auto local = [](const QString &url) {
return !UrlRequiresConfirmation(QUrl::fromUserInput(url));
@ -124,9 +136,12 @@ rpl::producer<TextWithEntities> PhoneOrHiddenValue(not_null<UserData*> user) {
});
}
rpl::producer<TextWithEntities> UsernameValue(not_null<UserData*> user) {
return PlainUsernameValue(
user
rpl::producer<TextWithEntities> UsernameValue(
not_null<UserData*> user,
bool primary) {
return (primary
? PlainPrimaryUsernameValue(user)
: PlainUsernameValue(user)
) | rpl::map([](QString &&username) {
return username.isEmpty()
? QString()
@ -134,6 +149,34 @@ rpl::producer<TextWithEntities> UsernameValue(not_null<UserData*> user) {
}) | Ui::Text::ToWithEntities();
}
rpl::producer<std::vector<TextWithEntities>> UsernamesValue(
not_null<PeerData*> peer) {
const auto map = [=](const std::vector<QString> &usernames) {
return ranges::views::all(
usernames
) | ranges::views::transform([&](const QString &u) {
return Ui::Text::Link(
u,
peer->session().createInternalLinkFull(u));
}) | ranges::to_vector;
};
auto value = rpl::merge(
peer->session().changes().peerFlagsValue(peer, UpdateFlag::Username),
peer->session().changes().peerFlagsValue(peer, UpdateFlag::Usernames)
);
if (const auto user = peer->asUser()) {
return std::move(value) | rpl::map([=] {
return map(user->usernames());
});
} else if (const auto channel = peer->asChannel()) {
return std::move(value) | rpl::map([=] {
return map(channel->usernames());
});
} else {
return rpl::never<std::vector<TextWithEntities>>();
}
}
TextWithEntities AboutWithEntities(
not_null<PeerData*> peer,
const QString &value) {
@ -170,9 +213,10 @@ rpl::producer<TextWithEntities> AboutValue(not_null<PeerData*> peer) {
});
}
rpl::producer<QString> LinkValue(not_null<PeerData*> peer) {
return PlainUsernameValue(
peer
rpl::producer<QString> LinkValue(not_null<PeerData*> peer, bool primary) {
return (primary
? PlainPrimaryUsernameValue(peer)
: PlainUsernameValue(peer)
) | rpl::map([=](QString &&username) {
return username.isEmpty()
? QString()

View file

@ -54,13 +54,18 @@ rpl::producer<not_null<PeerData*>> MigratedOrMeValue(
[[nodiscard]] rpl::producer<TextWithEntities> PhoneOrHiddenValue(
not_null<UserData*> user);
[[nodiscard]] rpl::producer<TextWithEntities> UsernameValue(
not_null<UserData*> user);
not_null<UserData*> user,
bool primary = false);
[[nodiscard]] rpl::producer<std::vector<TextWithEntities>> UsernamesValue(
not_null<PeerData*> peer);
[[nodiscard]] TextWithEntities AboutWithEntities(
not_null<PeerData*> peer,
const QString &value);
[[nodiscard]] rpl::producer<TextWithEntities> AboutValue(
not_null<PeerData*> peer);
[[nodiscard]] rpl::producer<QString> LinkValue(not_null<PeerData*> peer);
[[nodiscard]] rpl::producer<QString> LinkValue(
not_null<PeerData*> peer,
bool primary = false);
[[nodiscard]] rpl::producer<const ChannelLocation*> LocationValue(
not_null<ChannelData*> channel);
[[nodiscard]] rpl::producer<bool> NotificationsEnabledValue(