Show exact reactions count in channels.

This commit is contained in:
John Preston 2024-11-04 11:20:29 +04:00
parent 233eb6d916
commit ebff6c6370
6 changed files with 50 additions and 10 deletions

View file

@ -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>()
: 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;

View file

@ -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>()
: Data::ReactionId();
const auto clickedReaction = Reactions::ReactionIdOfLink(link);
_whoReactedMenuLifetime.destroy();
if (!clickedReaction.empty()
&& overItem

View file

@ -844,4 +844,30 @@ InlineListData InlineListDataFromMessage(not_null<Message*> message) {
return result;
}
} // namespace HistoryView
ReactionId ReactionIdOfLink(const ClickHandlerPtr &link) {
return link
? link->property(kReactionsCountEmojiProperty).value<ReactionId>()
: 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

View file

@ -149,4 +149,14 @@ private:
[[nodiscard]] InlineListData InlineListDataFromMessage(
not_null<Message*> 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

View file

@ -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');

View file

@ -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);