/* 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 "dialogs/dialogs_top_bar_suggestion.h" #include "api/api_authorizations.h" #include "api/api_credits.h" #include "api/api_peer_photo.h" #include "api/api_premium.h" #include "apiwrap.h" #include "base/call_delayed.h" #include "boxes/star_gift_box.h" // ShowStarGiftBox. #include "boxes/star_gift_auction_box.h" #include "core/application.h" #include "core/click_handler_types.h" #include "core/ui_integration.h" #include "data/components/gift_auctions.h" #include "data/components/promo_suggestions.h" #include "data/data_birthday.h" #include "data/data_changes.h" #include "data/data_peer_values.h" // Data::AmPremiumValue. #include "data/data_session.h" #include "data/data_user.h" #include "dialogs/ui/dialogs_top_bar_suggestion_content.h" #include "history/view/history_view_group_call_bar.h" #include "info/profile/info_profile_values.h" #include "lang/lang_keys.h" #include "main/main_session.h" #include "settings/settings_active_sessions.h" #include "settings/settings_credits_graphics.h" #include "settings/settings_premium.h" #include "ui/boxes/confirm_box.h" #include "ui/controls/userpic_button.h" #include "ui/effects/credits_graphics.h" #include "ui/layers/generic_box.h" #include "ui/rect.h" #include "ui/text/format_values.h" #include "ui/text/text_utilities.h" #include "ui/toast/toast.h" #include "ui/ui_utility.h" #include "ui/vertical_list.h" #include "ui/wrap/fade_wrap.h" #include "ui/wrap/slide_wrap.h" #include "window/window_controller.h" #include "window/window_session_controller.h" #include "styles/style_boxes.h" #include "styles/style_chat.h" #include "styles/style_chat_helpers.h" #include "styles/style_dialogs.h" #include "styles/style_layers.h" namespace Dialogs { namespace { [[nodiscard]] not_null FindSessionController( not_null widget) { const auto window = Core::App().findWindow(widget); Assert(window != nullptr); return window->sessionController(); } [[nodiscard]] QString FormatAuthInfo(const Data::UnreviewedAuth &auth) { const auto location = auth.location.isEmpty() ? QString() : "\U0001F30D " + auth.location; const auto device = auth.device.isEmpty() ? QString() : "\U0001F4F1 " + auth.device; if (!location.isEmpty() && !device.isEmpty()) { return location + " (" + device + ")"; } else if (!location.isEmpty()) { return location; } else if (!device.isEmpty()) { return device; } return QString(); } void ShowAuthToast( not_null parent, not_null session, const std::vector &list, bool confirmed) { if (confirmed) { auto text = tr::lng_unconfirmed_auth_confirmed_message( tr::now, lt_link, tr::link(tr::lng_settings_sessions_title(tr::now)), tr::rich); auto filter = [=]( ClickHandlerPtr handler, Qt::MouseButton button) { if (const auto controller = FindSessionController(parent)) { session->api().authorizations().reload(); controller->showSettings(Settings::Sessions::Id()); return false; } return true; }; Ui::Toast::Show(parent->window(), Ui::Toast::Config{ .title = tr::lng_unconfirmed_auth_confirmed(tr::now), .text = std::move(text), .filter = std::move(filter), .duration = crl::time(5000), }); } else { auto messageText = QString(); if (list.size() == 1) { messageText = tr::lng_unconfirmed_auth_denied_single( tr::now, lt_country, FormatAuthInfo(list.front())); } else { auto authList = QString('\n'); for (auto i = 0; i < std::min(int(list.size()), 10); ++i) { const auto info = FormatAuthInfo(list[i]); if (!info.isEmpty()) { authList += "• " + info + "\n"; } } messageText = tr::lng_unconfirmed_auth_denied_multiple( tr::now, lt_country, authList); } if (const auto controller = FindSessionController(parent)) { const auto count = float64(list.size()); controller->show(Box(ShowAuthDeniedBox, count, messageText)); } } } constexpr auto kSugSetBirthday = "BIRTHDAY_SETUP"_cs; constexpr auto kSugBirthdayContacts = "BIRTHDAY_CONTACTS_TODAY"_cs; constexpr auto kSugPremiumAnnual = "PREMIUM_ANNUAL"_cs; constexpr auto kSugPremiumUpgrade = "PREMIUM_UPGRADE"_cs; constexpr auto kSugPremiumRestore = "PREMIUM_RESTORE"_cs; constexpr auto kSugPremiumGrace = "PREMIUM_GRACE"_cs; constexpr auto kSugSetUserpic = "USERPIC_SETUP"_cs; constexpr auto kSugLowCreditsSubs = "STARS_SUBSCRIPTION_LOW_BALANCE"_cs; } // namespace rpl::producer*> TopBarSuggestionValue( not_null parent, not_null session, rpl::producer outerWrapToggleValue) { return [=, outerWrapToggleValue = rpl::duplicate(outerWrapToggleValue)]( auto consumer) { auto lifetime = rpl::lifetime(); struct Toggle { bool value = false; anim::type type; }; struct State { TopBarSuggestionContent *content = nullptr; Ui::SlideWrap *unconfirmedWarning = nullptr; base::unique_qptr> wrap; rpl::variable leftPadding; rpl::variable desiredWrapToggle; rpl::variable outerWrapToggle; rpl::lifetime birthdayLifetime; rpl::lifetime premiumLifetime; rpl::lifetime userpicLifetime; rpl::lifetime giftsLifetime; rpl::lifetime creditsLifetime; rpl::lifetime auctionsLifetime; std::unique_ptr creditsHistory; }; const auto state = lifetime.make_state(); state->outerWrapToggle = rpl::duplicate(outerWrapToggleValue); state->leftPadding = rpl::variable( rpl::single(st::dialogsTopBarLeftPadding)); const auto ensureContent = [=] { if (!state->content) { const auto window = FindSessionController(parent); state->content = Ui::CreateChild( parent, [=] { return window->isGifPausedAtLeastFor( Window::GifPauseReason::Layer); }); rpl::combine( parent->widthValue(), state->content->desiredHeightValue() ) | rpl::on_next([=](int width, int height) { state->content->resize(width, height); }, state->content->lifetime()); } }; const auto ensureWrap = [=](not_null child) { if (!state->wrap) { state->wrap = base::make_unique_q>( parent, object_ptr::fromRaw(child)); state->desiredWrapToggle.force_assign( Toggle{ false, anim::type::instant }); } }; const auto setLeftPaddingRelativeTo = [=]( not_null content, not_null relativeTo) { content->setLeftPadding(state->leftPadding.value( ) | rpl::map([w = relativeTo->width()](int padding) { return w + padding * 2; })); }; const auto processCurrentSuggestion = [=](auto repeat) -> void { state->birthdayLifetime.destroy(); state->premiumLifetime.destroy(); state->userpicLifetime.destroy(); state->giftsLifetime.destroy(); state->creditsLifetime.destroy(); state->auctionsLifetime.destroy(); if (!session->api().authorizations().unreviewed().empty()) { state->content = nullptr; state->wrap = nullptr; const auto &list = session->api().authorizations().unreviewed(); const auto hashes = ranges::views::all( list ) | ranges::views::transform([](const auto &auth) { return auth.hash; }) | ranges::to_vector; const auto content = CreateUnconfirmedAuthContent( parent, list, [=](bool confirmed) { ShowAuthToast(parent, session, list, confirmed); session->api().authorizations().review( hashes, confirmed); }); ensureWrap(content); const auto wasUnconfirmedWarning = state->unconfirmedWarning; state->unconfirmedWarning = content; state->desiredWrapToggle.force_assign(Toggle{ true, (state->unconfirmedWarning != wasUnconfirmedWarning) ? anim::type::instant : anim::type::normal, }); return; } else { if (state->unconfirmedWarning) { state->unconfirmedWarning = nullptr; state->wrap = nullptr; } } ensureContent(); ensureWrap(state->content); const auto content = state->content; const auto wrap = state->wrap.get(); using RightIcon = TopBarSuggestionContent::RightIcon; const auto promo = &session->promoSuggestions(); const auto auctions = &session->giftAuctions(); if (auctions->hasActive()) { using namespace Data; struct Button { rpl::variable text; Fn callback; base::has_weak_ptr guard; }; auto &lifetime = state->auctionsLifetime; const auto button = lifetime.template make_state