From c11f4efc5cb611c8a420829aff34969a17908cb2 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 11 Apr 2024 16:32:58 +0400 Subject: [PATCH] Support disabling top peers completely. --- Telegram/Resources/langs/lang.strings | 3 ++ .../SourceFiles/data/components/top_peers.cpp | 23 +++++++++- .../SourceFiles/data/components/top_peers.h | 1 + .../dialogs/ui/dialogs_suggestions.cpp | 44 ++++++++++++++----- .../dialogs/ui/top_peers_strip.cpp | 12 +++++ .../SourceFiles/dialogs/ui/top_peers_strip.h | 3 +- .../admin_log/history_admin_log_item.cpp | 20 +++------ Telegram/SourceFiles/history/history.cpp | 20 ++++++--- Telegram/SourceFiles/history/history.h | 3 +- .../settings/settings_privacy_security.cpp | 31 +++++++++++++ 10 files changed, 125 insertions(+), 35 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index f6c4e47a5..36c2c9393 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -730,6 +730,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_settings_angle_backend_d3d11on12" = "D3D11on12"; "lng_settings_angle_backend_opengl" = "OpenGL"; "lng_settings_angle_backend_disabled" = "Disabled"; +"lng_settings_top_peers_title" = "Frequent contacts"; +"lng_settings_top_peers_suggest" = "Suggest frequent contacts"; +"lng_settings_top_peers_about" = "Display people you message frequently at the top of the search section for quick access."; "lng_settings_sensitive_title" = "Sensitive content"; "lng_settings_sensitive_disable_filtering" = "Disable filtering"; "lng_settings_sensitive_about" = "Display sensitive media in public channels on all your Telegram devices."; diff --git a/Telegram/SourceFiles/data/components/top_peers.cpp b/Telegram/SourceFiles/data/components/top_peers.cpp index 7d41f2399..8e6149259 100644 --- a/Telegram/SourceFiles/data/components/top_peers.cpp +++ b/Telegram/SourceFiles/data/components/top_peers.cpp @@ -70,7 +70,7 @@ void TopPeers::remove(not_null peer) { } void TopPeers::increment(not_null peer, TimeId date) { - if (date <= _lastReceivedDate) { + if (_disabled || date <= _lastReceivedDate) { return; } if (const auto user = peer->asUser(); user && !user->isBot()) { @@ -107,6 +107,27 @@ void TopPeers::reload() { request(); } +void TopPeers::toggleDisabled(bool disabled) { + if (disabled) { + if (!_disabled || !_list.empty()) { + _disabled = true; + _list.clear(); + _updates.fire({}); + } + } else if (_disabled) { + _disabled = false; + _updates.fire({}); + } + + _session->api().request(MTPcontacts_ToggleTopPeers( + MTP_bool(!disabled) + )).done([=] { + if (!_disabled) { + request(); + } + }).send(); +} + void TopPeers::request() { if (_requestId) { return; diff --git a/Telegram/SourceFiles/data/components/top_peers.h b/Telegram/SourceFiles/data/components/top_peers.h index 67e69261a..da44a0c6c 100644 --- a/Telegram/SourceFiles/data/components/top_peers.h +++ b/Telegram/SourceFiles/data/components/top_peers.h @@ -25,6 +25,7 @@ public: void remove(not_null peer); void increment(not_null peer, TimeId date); void reload(); + void toggleDisabled(bool disabled); private: struct TopPeer { diff --git a/Telegram/SourceFiles/dialogs/ui/dialogs_suggestions.cpp b/Telegram/SourceFiles/dialogs/ui/dialogs_suggestions.cpp index b62c662c4..ad5bce8d0 100644 --- a/Telegram/SourceFiles/dialogs/ui/dialogs_suggestions.cpp +++ b/Telegram/SourceFiles/dialogs/ui/dialogs_suggestions.cpp @@ -16,6 +16,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/history.h" #include "lang/lang_keys.h" #include "main/main_session.h" +#include "ui/boxes/confirm_box.h" #include "ui/widgets/buttons.h" #include "ui/widgets/elastic_scroll.h" #include "ui/widgets/labels.h" @@ -23,6 +24,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/wrap/slide_wrap.h" #include "ui/dynamic_thumbnails.h" #include "window/window_session_controller.h" +#include "styles/style_chat.h" #include "styles/style_dialogs.h" #include "styles/style_layers.h" #include "styles/style_menu_icons.h" @@ -33,13 +35,23 @@ namespace { void FillTopPeerMenu( not_null controller, const ShowTopPeerMenuRequest &request, - Fn)> remove) { + Fn)> remove, + Fn hideAll) { const auto owner = &controller->session().data(); const auto peer = owner->peer(PeerId(request.id)); const auto &add = request.callback; const auto group = peer->isMegagroup(); const auto channel = peer->isChannel(); + add({ + .text = tr::lng_recent_remove(tr::now), + .handler = [=] { remove(peer); }, + .icon = &st::menuIconDeleteAttention, + .isAttention = true, + }); + + add({ .separatorSt = &st::expandedMenuSeparator }); + const auto showHistoryText = group ? tr::lng_context_open_group(tr::now) : channel @@ -58,12 +70,14 @@ void FillTopPeerMenu( controller->showPeerInfo(peer); }, channel ? &st::menuIconInfo : &st::menuIconProfile); - add({ - .text = tr::lng_recent_remove(tr::now), - .handler = [=] { remove(peer); }, - .icon = &st::menuIconDeleteAttention, - .isAttention = true, - }); + add({ .separatorSt = &st::expandedMenuSeparator }); + + add(tr::lng_recent_hide_top(tr::now), [=] { + controller->show(Ui::MakeConfirmBox({ + .text = tr::lng_recent_hide_sure(), + .confirmed = [=](Fn close) { hideAll(); close(); } + })); + }, &st::menuIconCancel); } } // namespace @@ -90,11 +104,21 @@ Suggestions::Suggestions( _topPeers->showMenuRequests( ) | rpl::start_with_next([=](const ShowTopPeerMenuRequest &request) { - const auto remove = crl::guard(this, [=](not_null peer) { + const auto weak = Ui::MakeWeak(this); + const auto remove = [=](not_null peer) { peer->session().topPeers().remove(peer); - _topPeers->removeLocally(peer->id.value); + if (weak) { + _topPeers->removeLocally(peer->id.value); + } + }; + const auto session = &controller->session(); + const auto hideAll = crl::guard(session, [=] { + session->topPeers().toggleDisabled(true); + if (weak) { + _topPeers->removeLocally(); + } }); - FillTopPeerMenu(controller, request, remove); + FillTopPeerMenu(controller, request, remove, hideAll); }, _topPeers->lifetime()); } diff --git a/Telegram/SourceFiles/dialogs/ui/top_peers_strip.cpp b/Telegram/SourceFiles/dialogs/ui/top_peers_strip.cpp index 74756f53c..01771f9e0 100644 --- a/Telegram/SourceFiles/dialogs/ui/top_peers_strip.cpp +++ b/Telegram/SourceFiles/dialogs/ui/top_peers_strip.cpp @@ -246,6 +246,15 @@ auto TopPeersStrip::showMenuRequests() const } void TopPeersStrip::removeLocally(uint64 id) { + if (!id) { + unsubscribeUserpics(true); + setSelected(-1); + _pressed = -1; + _entries.clear(); + _hiddenLocally = true; + _empty = true; + return; + } _removed.emplace(id); const auto i = ranges::find(_entries, id, &Entry::id); if (i == end(_entries)) { @@ -321,6 +330,9 @@ bool TopPeersStrip::chooseRow() { } void TopPeersStrip::apply(const TopPeersList &list) { + if (_hiddenLocally) { + return; + } auto now = std::vector(); auto selectedId = (_selected >= 0) ? _entries[_selected].id : 0; diff --git a/Telegram/SourceFiles/dialogs/ui/top_peers_strip.h b/Telegram/SourceFiles/dialogs/ui/top_peers_strip.h index 5a4c9c2b1..84584e56d 100644 --- a/Telegram/SourceFiles/dialogs/ui/top_peers_strip.h +++ b/Telegram/SourceFiles/dialogs/ui/top_peers_strip.h @@ -50,7 +50,7 @@ public: [[nodiscard]] auto showMenuRequests() const -> rpl::producer; - void removeLocally(uint64 id); + void removeLocally(uint64 id = 0); [[nodiscard]] bool selectedByKeyboard() const; void selectByKeyboard(int delta); @@ -105,6 +105,7 @@ private: int _selected = -1; int _pressed = -1; bool _selectionByKeyboard = false; + bool _hiddenLocally = false; Ui::RoundRect _selection; base::unique_qptr _menu; diff --git a/Telegram/SourceFiles/history/admin_log/history_admin_log_item.cpp b/Telegram/SourceFiles/history/admin_log/history_admin_log_item.cpp index 53e0926e0..72d797460 100644 --- a/Telegram/SourceFiles/history/admin_log/history_admin_log_item.cpp +++ b/Telegram/SourceFiles/history/admin_log/history_admin_log_item.cpp @@ -953,13 +953,11 @@ void GenerateItems( Ui::Text::WithEntities); addSimpleServiceMessage(text, realId); - const auto detachExistingItem = false; addPart( history->createItem( history->nextNonHistoryEntryId(), PrepareLogMessage(action.vmessage(), date), - MessageFlag::AdminLogEntry, - detachExistingItem), + MessageFlag::AdminLogEntry), ExtractSentDate(action.vmessage()), realId); }, [&](const auto &) { @@ -1008,12 +1006,10 @@ void GenerateItems( Ui::Text::WithEntities); addSimpleServiceMessage(text, realId); - const auto detachExistingItem = false; const auto body = history->createItem( history->nextNonHistoryEntryId(), PrepareLogMessage(action.vnew_message(), date), - MessageFlag::AdminLogEntry, - detachExistingItem); + MessageFlag::AdminLogEntry); if (oldValue.text.isEmpty()) { oldValue = PrepareText( QString(), @@ -1038,13 +1034,11 @@ void GenerateItems( Ui::Text::WithEntities); addSimpleServiceMessage(text, realId); - const auto detachExistingItem = false; addPart( history->createItem( history->nextNonHistoryEntryId(), PrepareLogMessage(action.vmessage(), date), - MessageFlag::AdminLogEntry, - detachExistingItem), + MessageFlag::AdminLogEntry), ExtractSentDate(action.vmessage()), realId); }; @@ -1218,13 +1212,11 @@ void GenerateItems( Ui::Text::WithEntities); addSimpleServiceMessage(text, realId); - const auto detachExistingItem = false; addPart( history->createItem( history->nextNonHistoryEntryId(), PrepareLogMessage(action.vmessage(), date), - MessageFlag::AdminLogEntry, - detachExistingItem), + MessageFlag::AdminLogEntry), ExtractSentDate(action.vmessage()), realId); }; @@ -1604,13 +1596,11 @@ void GenerateItems( Ui::Text::WithEntities); addSimpleServiceMessage(text, realId); - const auto detachExistingItem = false; addPart( history->createItem( history->nextNonHistoryEntryId(), PrepareLogMessage(data.vmessage(), date), - MessageFlag::AdminLogEntry, - detachExistingItem), + MessageFlag::AdminLogEntry), ExtractSentDate(data.vmessage()), realId); }; diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index 94454a586..dca53737d 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -423,7 +423,8 @@ not_null History::createItem( MsgId id, const MTPMessage &message, MessageFlags localFlags, - bool detachExistingItem) { + bool detachExistingItem, + bool newMessage) { if (const auto result = owner().message(peer, id)) { if (detachExistingItem) { result->removeMainView(); @@ -433,7 +434,7 @@ not_null History::createItem( const auto result = message.match([&](const auto &data) { return makeMessage(id, data, localFlags); }); - if (result->out() && result->isRegular()) { + if (newMessage && result->out() && result->isRegular()) { session().topPeers().increment(peer, result->date()); } return result; @@ -461,16 +462,21 @@ not_null History::addNewMessage( const MTPMessage &message, MessageFlags localFlags, NewMessageType type) { - const auto detachExisting = (type == NewMessageType::Unread); - const auto item = createItem(id, message, localFlags, detachExisting); + const auto newMessage = (type == NewMessageType::Unread); + const auto detachExisting = newMessage; + const auto item = createItem( + id, + message, + localFlags, + detachExisting, + newMessage); if (type == NewMessageType::Existing || item->mainView()) { return item; } - const auto unread = (type == NewMessageType::Unread); - if (unread && item->isHistoryEntry()) { + if (newMessage && item->isHistoryEntry()) { applyMessageChanges(item, message); } - return addNewItem(item, unread); + return addNewItem(item, newMessage); } not_null History::insertItem( diff --git a/Telegram/SourceFiles/history/history.h b/Telegram/SourceFiles/history/history.h index 4a4ad8ddc..e00c23b8d 100644 --- a/Telegram/SourceFiles/history/history.h +++ b/Telegram/SourceFiles/history/history.h @@ -185,7 +185,8 @@ public: MsgId id, const MTPMessage &message, MessageFlags localFlags, - bool detachExistingItem); + bool detachExistingItem = false, + bool newMessage = false); std::vector> createItems( const QVector &data); diff --git a/Telegram/SourceFiles/settings/settings_privacy_security.cpp b/Telegram/SourceFiles/settings/settings_privacy_security.cpp index 4849719ff..66a26632b 100644 --- a/Telegram/SourceFiles/settings/settings_privacy_security.cpp +++ b/Telegram/SourceFiles/settings/settings_privacy_security.cpp @@ -44,6 +44,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "calls/calls_instance.h" #include "core/update_checker.h" #include "lang/lang_keys.h" +#include "data/components/top_peers.h" #include "data/data_session.h" #include "data/data_chat.h" #include "data/data_channel.h" @@ -546,6 +547,35 @@ void SetupCloudPassword( session->api().cloudPassword().reload(); } +void SetupTopPeers( + not_null controller, + not_null container) { + Ui::AddSkip(container); + Ui::AddSubsectionTitle(container, tr::lng_settings_top_peers_title()); + + const auto session = &controller->session(); + + container->add(object_ptr