/* This file is part of Telegram Desktop, the official desktop application for the Telegram messaging service. For license and copyright information please follow this link: https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "info/profile/info_profile_values.h" #include "core/application.h" #include "core/click_handler_types.h" #include "main/main_session.h" #include "ui/wrap/slide_wrap.h" #include "ui/text/format_values.h" // Ui::FormatPhone #include "ui/text/text_utilities.h" #include "lang/lang_keys.h" #include "data/data_peer_values.h" #include "data/data_shared_media.h" #include "data/data_message_reactions.h" #include "data/data_folder.h" #include "data/data_changes.h" #include "data/data_channel.h" #include "data/data_chat.h" #include "data/data_user.h" #include "data/data_session.h" #include "boxes/peers/edit_peer_permissions_box.h" namespace Info { namespace Profile { namespace { using UpdateFlag = Data::PeerUpdate::Flag; auto PlainAboutValue(not_null peer) { return peer->session().changes().peerFlagsValue( peer, UpdateFlag::About ) | rpl::map([=] { return peer->about(); }); } auto PlainUsernameValue(not_null peer) { return peer->session().changes().peerFlagsValue( peer, UpdateFlag::Username ) | rpl::map([=] { return peer->userName(); }); } void StripExternalLinks(TextWithEntities &text) { const auto local = [](const QString &url) { return !UrlRequiresConfirmation(QUrl::fromUserInput(url)); }; 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) { return peer->session().changes().peerFlagsValue( peer, UpdateFlag::Name ) | rpl::map([=] { return peer->name; }) | Ui::Text::ToWithEntities(); } rpl::producer PhoneValue(not_null user) { return user->session().changes().peerFlagsValue( user, UpdateFlag::PhoneNumber ) | rpl::map([=] { return Ui::FormatPhone(user->phone()); }) | Ui::Text::ToWithEntities(); } rpl::producer PhoneOrHiddenValue(not_null user) { return rpl::combine( PhoneValue(user), PlainUsernameValue(user), PlainAboutValue(user), tr::lng_info_mobile_hidden() ) | rpl::map([]( const TextWithEntities &phone, const QString &username, const QString &about, const QString &hidden) { return (phone.text.isEmpty() && username.isEmpty() && about.isEmpty()) ? Ui::Text::WithEntities(hidden) : phone; }); } rpl::producer UsernameValue(not_null user) { return PlainUsernameValue( user ) | rpl::map([](QString &&username) { return username.isEmpty() ? QString() : ('@' + username); }) | Ui::Text::ToWithEntities(); } TextWithEntities AboutWithEntities( not_null peer, const QString &value) { 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); auto result = TextWithEntities{ value }; TextUtilities::ParseEntities(result, flags); if (stripExternal) { StripExternalLinks(result); } return result; } rpl::producer AboutValue(not_null peer) { return PlainAboutValue( peer ) | rpl::map([peer](const QString &value) { return AboutWithEntities(peer, value); }); } rpl::producer LinkValue(not_null peer) { return PlainUsernameValue( peer ) | rpl::map([=](QString &&username) { return username.isEmpty() ? QString() : peer->session().createInternalLinkFull(username); }); } rpl::producer LocationValue( not_null channel) { return channel->session().changes().peerFlagsValue( channel, UpdateFlag::ChannelLocation ) | rpl::map([=] { return channel->getLocation(); }); } rpl::producer NotificationsEnabledValue(not_null peer) { return rpl::merge( peer->session().changes().peerFlagsValue( peer, UpdateFlag::Notifications ) | rpl::to_empty, peer->owner().defaultNotifyUpdates(peer) ) | rpl::map([=] { return !peer->owner().notifyIsMuted(peer); }) | rpl::distinct_until_changed(); } rpl::producer IsContactValue(not_null user) { return user->session().changes().peerFlagsValue( user, UpdateFlag::IsContact ) | rpl::map([=] { return user->isContact(); }); } rpl::producer CanInviteBotToGroupValue(not_null user) { if (!user->isBot() || user->isSupport()) { return rpl::single(false); } return user->session().changes().peerFlagsValue( user, UpdateFlag::BotCanBeInvited ) | rpl::map([=] { return !user->botInfo->cantJoinGroups; }); } rpl::producer CanShareContactValue(not_null user) { return user->session().changes().peerFlagsValue( user, UpdateFlag::CanShareContact ) | rpl::map([=] { return user->canShareThisContact(); }); } rpl::producer CanAddContactValue(not_null user) { using namespace rpl::mappers; if (user->isBot() || user->isSelf()) { return rpl::single(false); } return IsContactValue( user ) | rpl::map(!_1); } rpl::producer AmInChannelValue(not_null channel) { return channel->session().changes().peerFlagsValue( channel, UpdateFlag::ChannelAmIn ) | rpl::map([=] { return channel->amIn(); }); } rpl::producer MembersCountValue(not_null peer) { if (const auto chat = peer->asChat()) { return peer->session().changes().peerFlagsValue( peer, UpdateFlag::Members ) | rpl::map([=] { return chat->amIn() ? std::max(chat->count, int(chat->participants.size())) : 0; }); } else if (const auto channel = peer->asChannel()) { return peer->session().changes().peerFlagsValue( peer, UpdateFlag::Members ) | rpl::map([=] { return channel->membersCount(); }); } Unexpected("User in MembersCountViewer()."); } rpl::producer PendingRequestsCountValue(not_null peer) { if (const auto chat = peer->asChat()) { return peer->session().changes().peerFlagsValue( peer, UpdateFlag::PendingRequests ) | rpl::map([=] { return chat->pendingRequestsCount(); }); } else if (const auto channel = peer->asChannel()) { return peer->session().changes().peerFlagsValue( peer, UpdateFlag::PendingRequests ) | rpl::map([=] { return channel->pendingRequestsCount(); }); } Unexpected("User in MembersCountViewer()."); } rpl::producer AdminsCountValue(not_null peer) { if (const auto chat = peer->asChat()) { return peer->session().changes().peerFlagsValue( peer, UpdateFlag::Admins | UpdateFlag::Rights ) | rpl::map([=] { return chat->participants.empty() ? 0 : int(chat->admins.size() + (chat->creator ? 1 : 0)); }); } else if (const auto channel = peer->asChannel()) { return peer->session().changes().peerFlagsValue( peer, UpdateFlag::Admins | UpdateFlag::Rights ) | rpl::map([=] { return channel->canViewAdmins() ? channel->adminsCount() : 0; }); } Unexpected("User in AdminsCountValue()."); } rpl::producer RestrictionsCountValue(not_null peer) { const auto countOfRestrictions = [](ChatRestrictions restrictions) { auto count = 0; for (const auto &f : Data::ListOfRestrictions()) { if (restrictions & f) count++; } return int(Data::ListOfRestrictions().size()) - count; }; if (const auto chat = peer->asChat()) { return peer->session().changes().peerFlagsValue( peer, UpdateFlag::Rights ) | rpl::map([=] { return countOfRestrictions(chat->defaultRestrictions()); }); } else if (const auto channel = peer->asChannel()) { return peer->session().changes().peerFlagsValue( peer, UpdateFlag::Rights ) | rpl::map([=] { return countOfRestrictions(channel->defaultRestrictions()); }); } Unexpected("User in RestrictionsCountValue()."); } rpl::producer> MigratedOrMeValue( not_null peer) { if (const auto chat = peer->asChat()) { return peer->session().changes().peerFlagsValue( peer, UpdateFlag::Migration ) | rpl::map([=] { return chat->migrateToOrMe(); }); } else { return rpl::single(peer); } } rpl::producer RestrictedCountValue(not_null channel) { return channel->session().changes().peerFlagsValue( channel, UpdateFlag::BannedUsers | UpdateFlag::Rights ) | rpl::map([=] { return channel->canViewBanned() ? channel->restrictedCount() : 0; }); } rpl::producer KickedCountValue(not_null channel) { return channel->session().changes().peerFlagsValue( channel, UpdateFlag::BannedUsers | UpdateFlag::Rights ) | rpl::map([=] { return channel->canViewBanned() ? channel->kickedCount() : 0; }); } rpl::producer SharedMediaCountValue( not_null peer, PeerData *migrated, Storage::SharedMediaType type) { auto aroundId = 0; auto limit = 0; auto updated = SharedMediaMergedViewer( &peer->session(), SharedMediaMergedKey( SparseIdsMergedSlice::Key( peer->id, migrated ? migrated->id : 0, aroundId), type), limit, limit ) | rpl::map([](const SparseIdsMergedSlice &slice) { return slice.fullCount(); }) | rpl::filter_optional(); return rpl::single(0) | rpl::then(std::move(updated)); } rpl::producer CommonGroupsCountValue(not_null user) { return user->session().changes().peerFlagsValue( user, UpdateFlag::CommonChats ) | rpl::map([=] { return user->commonChatsCount(); }); } rpl::producer CanAddMemberValue(not_null peer) { if (const auto chat = peer->asChat()) { return peer->session().changes().peerFlagsValue( peer, UpdateFlag::Rights ) | rpl::map([=] { return chat->canAddMembers(); }); } else if (const auto channel = peer->asChannel()) { return peer->session().changes().peerFlagsValue( peer, UpdateFlag::Rights ) | rpl::map([=] { return channel->canAddMembers(); }); } return rpl::single(false); } rpl::producer FullReactionsCountValue( not_null session) { const auto reactions = &session->data().reactions(); return rpl::single(rpl::empty) | rpl::then( reactions->updates() ) | rpl::map([=] { return int(reactions->list(Data::Reactions::Type::Active).size()); }) | rpl::distinct_until_changed(); } rpl::producer AllowedReactionsCountValue(not_null peer) { if (peer->isUser()) { return FullReactionsCountValue(&peer->session()); } return peer->session().changes().peerFlagsValue( peer, UpdateFlag::Reactions ) | rpl::map([=] { if (const auto chat = peer->asChat()) { return int(chat->allowedReactions().size()); } else if (const auto channel = peer->asChannel()) { return int(channel->allowedReactions().size()); } Unexpected("Peer type in AllowedReactionsCountValue."); }); } template rpl::producer BadgeValueFromFlags(Peer peer) { return Data::PeerFlagsValue( peer, Flag::Verified | Flag::Scam | Flag::Fake ) | rpl::map([=](base::flags value) { return (value & Flag::Verified) ? Badge::Verified : (value & Flag::Scam) ? Badge::Scam : (value & Flag::Fake) ? Badge::Fake : Badge::None; }); } rpl::producer BadgeValue(not_null peer) { if (const auto user = peer->asUser()) { return BadgeValueFromFlags(user); } else if (const auto channel = peer->asChannel()) { return BadgeValueFromFlags(channel); } return rpl::single(Badge::None); } } // namespace Profile } // namespace Info