From ebff6c637000e8b3a24c1ef4019bc8d032085226 Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 4 Nov 2024 11:20:29 +0400 Subject: [PATCH] Show exact reactions count in channels. --- .../history/history_inner_widget.cpp | 12 +++++--- .../history/view/history_view_list_widget.cpp | 6 ++-- .../view/reactions/history_view_reactions.cpp | 28 ++++++++++++++++++- .../view/reactions/history_view_reactions.h | 12 +++++++- Telegram/SourceFiles/lang/lang_tag.cpp | 1 + Telegram/SourceFiles/lang/lang_tag.h | 1 + 6 files changed, 50 insertions(+), 10 deletions(-) diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp index 238faaccb..73b5cbb17 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.cpp +++ b/Telegram/SourceFiles/history/history_inner_widget.cpp @@ -16,6 +16,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/moderate_messages_box.h" #include "history/view/media/history_view_sticker.h" #include "history/view/media/history_view_web_page.h" +#include "history/view/reactions/history_view_reactions.h" #include "history/view/reactions/history_view_reactions_button.h" #include "history/view/reactions/history_view_reactions_selector.h" #include "history/view/history_view_about_view.h" @@ -2197,10 +2198,8 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { const auto leaderOrSelf = groupLeaderOrSelf(_dragStateItem); const auto hasWhoReactedItem = leaderOrSelf && Api::WhoReactedExists(leaderOrSelf, Api::WhoReactedList::All); - const auto clickedReaction = link - ? link->property( - kReactionsCountEmojiProperty).value() - : Data::ReactionId(); + using namespace HistoryView::Reactions; + const auto clickedReaction = ReactionIdOfLink(link); _whoReactedMenuLifetime.destroy(); if (!clickedReaction.empty() && leaderOrSelf @@ -4655,6 +4654,11 @@ QString HistoryInner::tooltipText() const { } } } else if (const auto lnk = ClickHandler::getActive()) { + using namespace HistoryView::Reactions; + const auto count = ReactionCountOfLink(_dragStateItem, lnk); + if (count.count && count.shortened) { + return Lang::FormatCountDecimal(count.count); + } return lnk->tooltip(); } else if (const auto view = Element::Moused()) { StateRequest request; diff --git a/Telegram/SourceFiles/history/view/history_view_list_widget.cpp b/Telegram/SourceFiles/history/view/history_view_list_widget.cpp index b4e6fd86e..ca1e16327 100644 --- a/Telegram/SourceFiles/history/view/history_view_list_widget.cpp +++ b/Telegram/SourceFiles/history/view/history_view_list_widget.cpp @@ -16,6 +16,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/history_item_text.h" #include "history/view/media/history_view_media.h" #include "history/view/media/history_view_sticker.h" +#include "history/view/reactions/history_view_reactions.h" #include "history/view/reactions/history_view_reactions_button.h" #include "history/view/reactions/history_view_reactions_selector.h" #include "history/view/history_view_context_menu.h" @@ -2788,10 +2789,7 @@ void ListWidget::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { : _overElement ? _overElement->data().get() : nullptr; - const auto clickedReaction = link - ? link->property( - kReactionsCountEmojiProperty).value() - : Data::ReactionId(); + const auto clickedReaction = Reactions::ReactionIdOfLink(link); _whoReactedMenuLifetime.destroy(); if (!clickedReaction.empty() && overItem diff --git a/Telegram/SourceFiles/history/view/reactions/history_view_reactions.cpp b/Telegram/SourceFiles/history/view/reactions/history_view_reactions.cpp index c1be784a2..41390373b 100644 --- a/Telegram/SourceFiles/history/view/reactions/history_view_reactions.cpp +++ b/Telegram/SourceFiles/history/view/reactions/history_view_reactions.cpp @@ -844,4 +844,30 @@ InlineListData InlineListDataFromMessage(not_null message) { return result; } -} // namespace HistoryView +ReactionId ReactionIdOfLink(const ClickHandlerPtr &link) { + return link + ? link->property(kReactionsCountEmojiProperty).value() + : ReactionId(); +} + +ReactionCount ReactionCountOfLink( + HistoryItem *item, + const ClickHandlerPtr &link) { + const auto id = ReactionIdOfLink(link); + if (!item || !id) { + return {}; + } + const auto groups = &item->history()->owner().groups(); + if (const auto group = groups->find(item)) { + item = group->items.front(); + } + const auto &list = item->reactions(); + const auto i = ranges::find(list, id, &Data::MessageReaction::id); + if (i == end(list) || !i->count) { + return {}; + } + const auto formatted = Lang::FormatCountToShort(i->count); + return { .count = i->count, .shortened = formatted.shortened }; +} + +} // namespace HistoryView::Reactions diff --git a/Telegram/SourceFiles/history/view/reactions/history_view_reactions.h b/Telegram/SourceFiles/history/view/reactions/history_view_reactions.h index be7aa4e64..31982fd1c 100644 --- a/Telegram/SourceFiles/history/view/reactions/history_view_reactions.h +++ b/Telegram/SourceFiles/history/view/reactions/history_view_reactions.h @@ -149,4 +149,14 @@ private: [[nodiscard]] InlineListData InlineListDataFromMessage( not_null message); -} // namespace HistoryView +[[nodiscard]] ReactionId ReactionIdOfLink(const ClickHandlerPtr &link); + +struct ReactionCount { + int count = 0; + bool shortened = false; +}; +[[nodiscard]] ReactionCount ReactionCountOfLink( + HistoryItem *item, + const ClickHandlerPtr &link); + +} // namespace HistoryView::Reactions diff --git a/Telegram/SourceFiles/lang/lang_tag.cpp b/Telegram/SourceFiles/lang/lang_tag.cpp index 7b1a10acb..2435a77eb 100644 --- a/Telegram/SourceFiles/lang/lang_tag.cpp +++ b/Telegram/SourceFiles/lang/lang_tag.cpp @@ -929,6 +929,7 @@ ShortenedCount FormatCountToShort(int64 number) { // Update given number. // E.g. 12345 will be 12000. result.number = rounded * divider; + result.shortened = true; }; if (abs >= 1'000'000) { shorten(1'000'000, 'M'); diff --git a/Telegram/SourceFiles/lang/lang_tag.h b/Telegram/SourceFiles/lang/lang_tag.h index 297ae1ff2..b1163b06e 100644 --- a/Telegram/SourceFiles/lang/lang_tag.h +++ b/Telegram/SourceFiles/lang/lang_tag.h @@ -22,6 +22,7 @@ constexpr auto kTagReplacementSize = 4; struct ShortenedCount { int64 number = 0; QString string; + bool shortened = false; }; [[nodiscard]] ShortenedCount FormatCountToShort(int64 number); [[nodiscard]] QString FormatCountDecimal(int64 number);