tdesktop/Telegram/SourceFiles/info/profile/info_profile_values.cpp

507 lines
14 KiB
C++
Raw Normal View History

/*
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 "countries/countries_instance.h"
2019-07-24 11:45:24 +00:00
#include "main/main_session.h"
#include "ui/wrap/slide_wrap.h"
2021-07-26 06:32:16 +00:00
#include "ui/text/format_values.h" // Ui::FormatPhone
2019-06-18 12:16:43 +00:00
#include "ui/text/text_utilities.h"
#include "lang/lang_keys.h"
#include "data/notify/data_notify_settings.h"
#include "data/data_peer_values.h"
#include "data/data_shared_media.h"
#include "data/data_message_reactions.h"
2019-04-15 11:54:03 +00:00
#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"
#include "boxes/premium_limits_box.h"
2022-03-15 11:12:49 +00:00
#include "base/unixtime.h"
namespace Info {
namespace Profile {
namespace {
using UpdateFlag = Data::PeerUpdate::Flag;
2020-10-29 16:53:07 +00:00
auto PlainAboutValue(not_null<PeerData*> peer) {
return peer->session().changes().peerFlagsValue(
peer,
UpdateFlag::About
) | rpl::map([=] {
2020-10-29 16:53:07 +00:00
return peer->about();
});
}
auto PlainUsernameValue(not_null<PeerData*> peer) {
return peer->session().changes().peerFlagsValue(
peer,
UpdateFlag::Username
) | rpl::map([=] {
return peer->userName();
});
}
2020-10-29 16:53:07 +00:00
void StripExternalLinks(TextWithEntities &text) {
const auto local = [](const QString &url) {
return !UrlRequiresConfirmation(QUrl::fromUserInput(url));
2020-10-29 16:53:07 +00:00
};
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<TextWithEntities> NameValue(not_null<PeerData*> peer) {
return peer->session().changes().peerFlagsValue(
peer,
UpdateFlag::Name
) | rpl::map([=] {
return peer->name;
}) | Ui::Text::ToWithEntities();
}
rpl::producer<TextWithEntities> PhoneValue(not_null<UserData*> user) {
return rpl::merge(
Countries::Instance().updated(),
user->session().changes().peerFlagsValue(
user,
UpdateFlag::PhoneNumber) | rpl::to_empty
) | rpl::map([=] {
2021-07-26 06:32:16 +00:00
return Ui::FormatPhone(user->phone());
2019-06-18 12:16:43 +00:00
}) | Ui::Text::ToWithEntities();
}
rpl::producer<TextWithEntities> PhoneOrHiddenValue(not_null<UserData*> user) {
return rpl::combine(
PhoneValue(user),
PlainUsernameValue(user),
2020-10-29 16:53:07 +00:00
PlainAboutValue(user),
tr::lng_info_mobile_hidden()
) | rpl::map([](
const TextWithEntities &phone,
const QString &username,
2020-10-29 16:53:07 +00:00
const QString &about,
const QString &hidden) {
2020-10-29 16:53:07 +00:00
return (phone.text.isEmpty() && username.isEmpty() && about.isEmpty())
? Ui::Text::WithEntities(hidden)
: phone;
});
}
rpl::producer<TextWithEntities> UsernameValue(not_null<UserData*> user) {
return PlainUsernameValue(
user
) | rpl::map([](QString &&username) {
return username.isEmpty()
? QString()
: ('@' + username);
2019-06-18 12:16:43 +00:00
}) | Ui::Text::ToWithEntities();
}
TextWithEntities AboutWithEntities(
not_null<PeerData*> peer,
const QString &value) {
2020-10-29 16:53:07 +00:00
auto flags = TextParseLinks | TextParseMentions;
const auto user = peer->asUser();
const auto isBot = user && user->isBot();
const auto isPremium = user && user->isPremium();
2020-10-29 16:53:07 +00:00
if (!user) {
flags |= TextParseHashtags;
} else if (isBot) {
flags |= TextParseHashtags | TextParseBotCommands;
}
2020-10-29 16:53:07 +00:00
const auto stripExternal = peer->isChat()
|| peer->isMegagroup()
|| (user && !isBot && !isPremium);
const auto limit = AppConfigLimit(
&peer->session(),
"about_length_limit_default",
70);
const auto used = (!user || isPremium || value.size() <= limit)
? value
: value.mid(0, limit) + "...";
auto result = TextWithEntities{ value };
TextUtilities::ParseEntities(result, flags);
if (stripExternal) {
StripExternalLinks(result);
}
return result;
}
rpl::producer<TextWithEntities> AboutValue(not_null<PeerData*> peer) {
return PlainAboutValue(
peer
) | rpl::map([peer](const QString &value) {
return AboutWithEntities(peer, value);
});
}
rpl::producer<QString> LinkValue(not_null<PeerData*> peer) {
return PlainUsernameValue(
peer
) | rpl::map([=](QString &&username) {
return username.isEmpty()
? QString()
: peer->session().createInternalLinkFull(username);
});
}
2019-06-21 12:27:46 +00:00
rpl::producer<const ChannelLocation*> LocationValue(
not_null<ChannelData*> channel) {
return channel->session().changes().peerFlagsValue(
2019-06-21 12:27:46 +00:00
channel,
UpdateFlag::ChannelLocation
2019-06-21 12:27:46 +00:00
) | rpl::map([=] {
return channel->getLocation();
});
}
rpl::producer<bool> NotificationsEnabledValue(not_null<PeerData*> peer) {
return rpl::merge(
peer->session().changes().peerFlagsValue(
peer,
UpdateFlag::Notifications
) | rpl::to_empty,
peer->owner().notifySettings().defaultUpdates(peer)
2020-06-08 09:06:50 +00:00
) | rpl::map([=] {
return !peer->owner().notifySettings().isMuted(peer);
}) | rpl::distinct_until_changed();
}
rpl::producer<bool> IsContactValue(not_null<UserData*> user) {
return user->session().changes().peerFlagsValue(
user,
UpdateFlag::IsContact
) | rpl::map([=] {
return user->isContact();
});
}
[[nodiscard]] rpl::producer<QString> InviteToChatButton(
not_null<UserData*> user) {
if (!user->isBot() || user->isRepliesChat() || user->isSupport()) {
return rpl::single(QString());
}
using Flag = Data::PeerUpdate::Flag;
return user->session().changes().peerFlagsValue(
user,
Flag::BotCanBeInvited | Flag::Rights
) | rpl::map([=] {
const auto info = user->botInfo.get();
return info->cantJoinGroups
? (info->channelAdminRights
? tr::lng_profile_invite_to_channel(tr::now)
: QString())
: (info->channelAdminRights
? tr::lng_profile_add_bot_as_admin(tr::now)
: tr::lng_profile_invite_to_group(tr::now));
});
}
[[nodiscard]] rpl::producer<QString> InviteToChatAbout(
not_null<UserData*> user) {
if (!user->isBot() || user->isRepliesChat() || user->isSupport()) {
return rpl::single(QString());
2017-11-07 11:53:05 +00:00
}
using Flag = Data::PeerUpdate::Flag;
return user->session().changes().peerFlagsValue(
user,
Flag::BotCanBeInvited | Flag::Rights
) | rpl::map([=] {
const auto info = user->botInfo.get();
return (info->cantJoinGroups || !info->groupAdminRights)
? (info->channelAdminRights
? tr::lng_profile_invite_to_channel_about(tr::now)
: QString())
: (info->channelAdminRights
? tr::lng_profile_add_bot_as_admin_about(tr::now)
: tr::lng_profile_invite_to_group_about(tr::now));
});
2017-11-07 11:53:05 +00:00
}
rpl::producer<bool> CanShareContactValue(not_null<UserData*> user) {
return user->session().changes().peerFlagsValue(
user,
UpdateFlag::CanShareContact
) | rpl::map([=] {
return user->canShareThisContact();
});
}
rpl::producer<bool> CanAddContactValue(not_null<UserData*> user) {
using namespace rpl::mappers;
if (user->isBot() || user->isSelf()) {
return rpl::single(false);
}
return IsContactValue(
user
) | rpl::map(!_1);
}
rpl::producer<bool> AmInChannelValue(not_null<ChannelData*> channel) {
return channel->session().changes().peerFlagsValue(
2017-11-07 11:53:05 +00:00
channel,
UpdateFlag::ChannelAmIn
) | rpl::map([=] {
return channel->amIn();
});
2017-11-07 11:53:05 +00:00
}
rpl::producer<int> MembersCountValue(not_null<PeerData*> peer) {
if (const auto chat = peer->asChat()) {
return peer->session().changes().peerFlagsValue(
2017-11-07 11:53:05 +00:00
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<int> PendingRequestsCountValue(not_null<PeerData*> 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<int> AdminsCountValue(not_null<PeerData*> peer) {
if (const auto chat = peer->asChat()) {
return peer->session().changes().peerFlagsValue(
peer,
UpdateFlag::Admins | UpdateFlag::Rights
) | rpl::map([=] {
2019-01-10 12:38:57 +00:00
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().");
2017-11-08 16:45:30 +00:00
}
rpl::producer<int> RestrictionsCountValue(not_null<PeerData*> 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().");
}
2019-07-16 12:54:38 +00:00
rpl::producer<not_null<PeerData*>> MigratedOrMeValue(
not_null<PeerData*> peer) {
if (const auto chat = peer->asChat()) {
return peer->session().changes().peerFlagsValue(
peer,
UpdateFlag::Migration
2019-07-16 12:54:38 +00:00
) | rpl::map([=] {
return chat->migrateToOrMe();
});
} else {
return rpl::single(peer);
}
}
rpl::producer<int> RestrictedCountValue(not_null<ChannelData*> channel) {
return channel->session().changes().peerFlagsValue(
2017-11-08 16:45:30 +00:00
channel,
UpdateFlag::BannedUsers | UpdateFlag::Rights
) | rpl::map([=] {
return channel->canViewBanned()
? channel->restrictedCount()
: 0;
});
2017-11-08 16:45:30 +00:00
}
rpl::producer<int> KickedCountValue(not_null<ChannelData*> channel) {
return channel->session().changes().peerFlagsValue(
2017-11-08 16:45:30 +00:00
channel,
UpdateFlag::BannedUsers | UpdateFlag::Rights
) | rpl::map([=] {
return channel->canViewBanned()
? channel->kickedCount()
: 0;
});
2017-11-08 16:45:30 +00:00
}
rpl::producer<int> SharedMediaCountValue(
not_null<PeerData*> peer,
PeerData *migrated,
Storage::SharedMediaType type) {
auto aroundId = 0;
auto limit = 0;
auto updated = SharedMediaMergedViewer(
2020-06-08 09:06:50 +00:00
&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<int> CommonGroupsCountValue(not_null<UserData*> user) {
return user->session().changes().peerFlagsValue(
user,
UpdateFlag::CommonChats
) | rpl::map([=] {
return user->commonChatsCount();
});
}
rpl::producer<bool> CanAddMemberValue(not_null<PeerData*> peer) {
2019-06-23 12:18:33 +00:00
if (const auto chat = peer->asChat()) {
return peer->session().changes().peerFlagsValue(
peer,
UpdateFlag::Rights
) | rpl::map([=] {
return chat->canAddMembers();
});
2019-06-23 12:18:33 +00:00
} else if (const auto channel = peer->asChannel()) {
return peer->session().changes().peerFlagsValue(
peer,
UpdateFlag::Rights
) | rpl::map([=] {
return channel->canAddMembers();
});
2017-09-25 16:06:53 +00:00
}
return rpl::single(false);
}
rpl::producer<int> FullReactionsCountValue(
not_null<Main::Session*> 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<int> AllowedReactionsCountValue(not_null<PeerData*> 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.");
});
}
2021-01-21 12:39:40 +00:00
template <typename Flag, typename Peer>
rpl::producer<Badge> BadgeValueFromFlags(Peer peer) {
2022-05-20 15:28:50 +00:00
return rpl::combine(
Data::PeerFlagsValue(
peer,
Flag::Verified | Flag::Scam | Flag::Fake),
Data::PeerPremiumValue(peer)
) | rpl::map([=](base::flags<Flag> value, bool premium) {
2021-07-08 13:11:09 +00:00
return (value & Flag::Verified)
2021-01-21 12:39:40 +00:00
? Badge::Verified
2022-05-20 15:28:50 +00:00
: premium
? Badge::Premium
2021-07-08 13:11:09 +00:00
: (value & Flag::Scam)
2021-01-21 12:39:40 +00:00
? Badge::Scam
2021-07-08 13:11:09 +00:00
: (value & Flag::Fake)
2021-01-21 12:39:40 +00:00
? Badge::Fake
: Badge::None;
});
2017-11-06 14:13:56 +00:00
}
2019-06-23 12:18:33 +00:00
2021-01-21 12:39:40 +00:00
rpl::producer<Badge> BadgeValue(not_null<PeerData*> peer) {
2019-06-23 12:18:33 +00:00
if (const auto user = peer->asUser()) {
2021-07-08 13:11:09 +00:00
return BadgeValueFromFlags<UserDataFlag>(user);
2019-06-23 12:18:33 +00:00
} else if (const auto channel = peer->asChannel()) {
2021-07-08 13:11:09 +00:00
return BadgeValueFromFlags<ChannelDataFlag>(channel);
2019-06-23 12:18:33 +00:00
}
2021-01-21 12:39:40 +00:00
return rpl::single(Badge::None);
2019-06-23 12:18:33 +00:00
}
2021-01-21 12:39:40 +00:00
} // namespace Profile
} // namespace Info