From c35b6e12096b21f7dcbedf5b68ed9786bc3ea3fc Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 29 Oct 2020 19:53:07 +0300 Subject: [PATCH] Highlight internal links in Bio. --- .../info/profile/info_profile_actions.cpp | 9 ++- .../info/profile/info_profile_values.cpp | 71 ++++++++++--------- .../info/profile/info_profile_values.h | 1 - .../settings/settings_information.cpp | 2 +- 4 files changed, 44 insertions(+), 39 deletions(-) diff --git a/Telegram/SourceFiles/info/profile/info_profile_actions.cpp b/Telegram/SourceFiles/info/profile/info_profile_actions.cpp index 1206ce486..1a0e78b85 100644 --- a/Telegram/SourceFiles/info/profile/info_profile_actions.cpp +++ b/Telegram/SourceFiles/info/profile/info_profile_actions.cpp @@ -267,11 +267,10 @@ object_ptr DetailsFiller::setupInfo() { tr::lng_info_mobile_label(), PhoneOrHiddenValue(user), tr::lng_profile_copy_phone(tr::now)); - if (user->isBot()) { - addInfoLine(tr::lng_info_about_label(), AboutValue(user)); - } else { - addInfoLine(tr::lng_info_bio_label(), BioValue(user)); - } + auto label = user->isBot() + ? tr::lng_info_about_label() + : tr::lng_info_bio_label(); + addInfoLine(std::move(label), AboutValue(user)); addInfoOneLine( tr::lng_info_username_label(), UsernameValue(user), diff --git a/Telegram/SourceFiles/info/profile/info_profile_values.cpp b/Telegram/SourceFiles/info/profile/info_profile_values.cpp index a30b8a81d..7c74ebf8d 100644 --- a/Telegram/SourceFiles/info/profile/info_profile_values.cpp +++ b/Telegram/SourceFiles/info/profile/info_profile_values.cpp @@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "info/profile/info_profile_values.h" #include "core/application.h" +#include "core/local_url_handlers.h" #include "main/main_session.h" #include "ui/wrap/slide_wrap.h" #include "ui/text/text_utilities.h" @@ -29,12 +30,12 @@ namespace { using UpdateFlag = Data::PeerUpdate::Flag; -auto PlainBioValue(not_null user) { - return user->session().changes().peerFlagsValue( - user, +auto PlainAboutValue(not_null peer) { + return peer->session().changes().peerFlagsValue( + peer, UpdateFlag::About ) | rpl::map([=] { - return user->about(); + return peer->about(); }); } @@ -47,6 +48,24 @@ auto PlainUsernameValue(not_null peer) { }); } +void StripExternalLinks(TextWithEntities &text) { + const auto local = [](const QString &url) { + return Core::TryConvertUrlToLocal(url).startsWith(qstr("tg://")); + }; + const auto notLocal = [&](const EntityInText &entity) { + if (entity.type() == EntityType::CustomUrl) { + return !local(entity.data()); + } else if (entity.type() == EntityType::Url) { + return !local(text.text.mid(entity.offset(), entity.length())); + } else { + return false; + } + }; + text.entities.erase( + ranges::remove_if(text.entities, notLocal), + text.entities.end()); +} + } // namespace rpl::producer NameValue(not_null peer) { @@ -71,25 +90,19 @@ rpl::producer PhoneOrHiddenValue(not_null user) { return rpl::combine( PhoneValue(user), PlainUsernameValue(user), - PlainBioValue(user), + PlainAboutValue(user), tr::lng_info_mobile_hidden() ) | rpl::map([]( const TextWithEntities &phone, const QString &username, - const QString &bio, + const QString &about, const QString &hidden) { - return (phone.text.isEmpty() && username.isEmpty() && bio.isEmpty()) + return (phone.text.isEmpty() && username.isEmpty() && about.isEmpty()) ? Ui::Text::WithEntities(hidden) : phone; }); } -rpl::producer BioValue(not_null user) { - return PlainBioValue(user) - | ToSingleLine() - | Ui::Text::ToWithEntities(); -} - rpl::producer UsernameValue(not_null user) { return PlainUsernameValue( user @@ -100,32 +113,26 @@ rpl::producer UsernameValue(not_null user) { }) | Ui::Text::ToWithEntities(); } -rpl::producer PlainAboutValue(not_null peer) { - if (const auto user = peer->asUser()) { - if (!user->isBot()) { - return rpl::single(QString()); - } - } - return peer->session().changes().peerFlagsValue( - peer, - UpdateFlag::About - ) | rpl::map([=] { - return peer->about(); - }); -} - rpl::producer AboutValue(not_null peer) { - auto flags = TextParseLinks - | TextParseMentions - | TextParseHashtags; - if (peer->isUser()) { - flags |= TextParseBotCommands; + auto flags = TextParseLinks | TextParseMentions; + const auto user = peer->asUser(); + const auto isBot = user && user->isBot(); + if (!user) { + flags |= TextParseHashtags; + } else if (isBot) { + flags |= TextParseHashtags | TextParseBotCommands; } + const auto stripExternal = peer->isChat() + || peer->isMegagroup() + || (user && !isBot); return PlainAboutValue( peer ) | Ui::Text::ToWithEntities( ) | rpl::map([=](TextWithEntities &&text) { TextUtilities::ParseEntities(text, flags); + if (stripExternal) { + StripExternalLinks(text); + } return std::move(text); }); } diff --git a/Telegram/SourceFiles/info/profile/info_profile_values.h b/Telegram/SourceFiles/info/profile/info_profile_values.h index 2601d690d..da87867f6 100644 --- a/Telegram/SourceFiles/info/profile/info_profile_values.h +++ b/Telegram/SourceFiles/info/profile/info_profile_values.h @@ -37,7 +37,6 @@ rpl::producer> MigratedOrMeValue( rpl::producer NameValue(not_null peer); rpl::producer PhoneValue(not_null user); rpl::producer PhoneOrHiddenValue(not_null user); -rpl::producer BioValue(not_null user); rpl::producer UsernameValue(not_null user); rpl::producer AboutValue(not_null peer); rpl::producer LinkValue(not_null peer); diff --git a/Telegram/SourceFiles/settings/settings_information.cpp b/Telegram/SourceFiles/settings/settings_information.cpp index da2fd6461..a35832df8 100644 --- a/Telegram/SourceFiles/settings/settings_information.cpp +++ b/Telegram/SourceFiles/settings/settings_information.cpp @@ -346,7 +346,7 @@ BioManager SetupBio( std::move(done)); }; - Info::Profile::BioValue( + Info::Profile::AboutValue( self ) | rpl::start_with_next([=](const TextWithEntities &text) { const auto wasChanged = (*current != bio->getLastText());