From 743c3c54a7820f9ae718aa016d0bccc18c3ecfd3 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Tue, 26 Nov 2024 06:08:17 +0300 Subject: [PATCH] Added message counter to each peer in moderate box. --- .../boxes/moderate_messages_box.cpp | 87 ++++++++++++++----- .../ui/widgets/expandable_peer_list.cpp | 39 ++++++--- .../ui/widgets/expandable_peer_list.h | 1 + 3 files changed, 96 insertions(+), 31 deletions(-) diff --git a/Telegram/SourceFiles/boxes/moderate_messages_box.cpp b/Telegram/SourceFiles/boxes/moderate_messages_box.cpp index c7e696e9e3..49df5b0ea6 100644 --- a/Telegram/SourceFiles/boxes/moderate_messages_box.cpp +++ b/Telegram/SourceFiles/boxes/moderate_messages_box.cpp @@ -87,19 +87,35 @@ ModerateOptions CalculateModerateOptions(const HistoryItemsList &items) { return result; } -[[nodiscard]] rpl::producer MessagesCountValue( +[[nodiscard]] rpl::producer> MessagesCountValue( not_null history, - not_null from) { + std::vector> from) { return [=](auto consumer) { auto lifetime = rpl::lifetime(); - auto search = lifetime.make_state(history); - consumer.put_next(0); - - search->messagesFounds( - ) | rpl::start_with_next([=](const Api::FoundMessages &found) { - consumer.put_next_copy(found.total); - }, lifetime); - search->searchMessages({ .from = from }); + struct State final { + base::flat_map messagesCounts; + int index = 0; + rpl::lifetime apiLifetime; + }; + const auto search = lifetime.make_state(history); + const auto state = lifetime.make_state(); + const auto send = [=](auto repeat) -> void { + if (state->index >= from.size()) { + consumer.put_next_copy(state->messagesCounts); + return; + } + const auto peer = from[state->index]; + const auto peerId = peer->id; + state->apiLifetime = search->messagesFounds( + ) | rpl::start_with_next([=](const Api::FoundMessages &found) { + state->messagesCounts[peerId] = found.total; + state->index++; + repeat(repeat); + }); + search->searchMessages({ .from = peer }); + }; + consumer.put_next({}); + send(send); return lifetime; }; @@ -274,15 +290,50 @@ void CreateModerateMessagesBox( false, st::defaultBoxCheckbox), st::boxRowPadding + buttonPadding); - if (isSingle) { - const auto history = items.front()->history(); + const auto history = items.front()->history(); + auto messagesCounts = MessagesCountValue(history, participants); + + const auto controller = box->lifetime().make_state( + Controller::Data{ + .messagesCounts = rpl::duplicate(messagesCounts), + .participants = participants, + }); + Ui::AddExpandablePeerList(deleteAll, controller, inner); + { tr::lng_selected_delete_sure( lt_count, rpl::combine( - MessagesCountValue(history, participants.front()), - deleteAll->checkedValue() - ) | rpl::map([s = items.size()](int all, bool checked) { - return float64((checked && all) ? all : s); + std::move(messagesCounts), + isSingle + ? deleteAll->checkedValue() + : rpl::merge( + controller->toggleRequestsFromInner.events(), + controller->checkAllRequests.events()) + ) | rpl::map([=, s = items.size()](const auto &map, bool c) { + const auto checked = (isSingle && !c) + ? Participants() + : controller->collectRequests + ? controller->collectRequests() + : Participants(); + auto result = 0; + for (const auto &[peerId, count] : map) { + for (const auto &peer : checked) { + if (peer->id == peerId) { + result += count; + break; + } + } + } + for (const auto &item : items) { + for (const auto &peer : checked) { + if (peer->id == item->from()->id) { + result--; + break; + } + } + result++; + } + return float64(result); }) ) | rpl::start_with_next([=](const QString &text) { title->setText(text); @@ -290,10 +341,6 @@ void CreateModerateMessagesBox( - rect::m::sum::h(st::boxRowPadding)); }, title->lifetime()); } - - const auto controller = box->lifetime().make_state( - Controller::Data{ .participants = participants }); - Ui::AddExpandablePeerList(deleteAll, controller, inner); handleSubmition(deleteAll); handleConfirmation(deleteAll, controller, [=]( diff --git a/Telegram/SourceFiles/ui/widgets/expandable_peer_list.cpp b/Telegram/SourceFiles/ui/widgets/expandable_peer_list.cpp index db09645b60..343d623aa8 100644 --- a/Telegram/SourceFiles/ui/widgets/expandable_peer_list.cpp +++ b/Telegram/SourceFiles/ui/widgets/expandable_peer_list.cpp @@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/widgets/expandable_peer_list.h" #include "data/data_peer.h" +#include "info/profile/info_profile_values.h" #include "ui/controls/userpic_button.h" #include "ui/rect.h" #include "ui/text/text_utilities.h" @@ -148,19 +149,35 @@ void AddExpandablePeerList( const auto &st = st::moderateBoxUserpic; line->resize(line->width(), st.size.height()); - const auto userpic = Ui::CreateChild( - line, - peer, - st); + using namespace Info::Profile; + auto name = controller->data.bold + ? NameValue(peer) | rpl::map(Ui::Text::Bold) + : NameValue(peer) | rpl::map(Ui::Text::WithEntities); + const auto userpic + = Ui::CreateChild(line, peer, st); const auto checkbox = Ui::CreateChild( line, - controller->data.bold - ? Ui::Text::Bold(peer->name()) - : TextWithEntities{ .text = peer->name() }, - ranges::contains(controller->data.checked, peer->id), - st::defaultBoxCheckbox); - line->widthValue( - ) | rpl::start_with_next([=](int width) { + controller->data.messagesCounts + ? rpl::combine( + std::move(name), + rpl::duplicate(controller->data.messagesCounts) + ) | rpl::map([=](const auto &richName, const auto &map) { + const auto it = map.find(peer->id); + return (it == map.end() || !it->second) + ? richName + : TextWithEntities( + (u"(%1) "_q).arg(it->second)).append(richName); + }) + : std::move(name), + st::defaultBoxCheckbox, + std::make_unique( + st::defaultCheck, + ranges::contains(controller->data.checked, peer->id))); + checkbox->setCheckAlignment(style::al_left); + rpl::combine( + line->widthValue(), + checkbox->widthValue() + ) | rpl::start_with_next([=](int width, int) { userpic->moveToLeft( st::boxRowPadding.left() + checkbox->checkRect().width() diff --git a/Telegram/SourceFiles/ui/widgets/expandable_peer_list.h b/Telegram/SourceFiles/ui/widgets/expandable_peer_list.h index 3ff7e4ee19..983701f538 100644 --- a/Telegram/SourceFiles/ui/widgets/expandable_peer_list.h +++ b/Telegram/SourceFiles/ui/widgets/expandable_peer_list.h @@ -18,6 +18,7 @@ class VerticalLayout; struct ExpandablePeerListController final { struct Data final { + rpl::producer> messagesCounts = nullptr; Participants participants; std::vector checked; bool skipSingle = false;