From 60002555c3aa13b7dc3f5cfdfb4ef24f953b122c Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 3 Sep 2020 12:42:25 +0400 Subject: [PATCH] Track comments count correctly. --- .../history/history_inner_widget.cpp | 33 +++++---- .../SourceFiles/history/history_message.cpp | 72 ++++++++++--------- .../SourceFiles/history/history_message.h | 5 +- 3 files changed, 60 insertions(+), 50 deletions(-) diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp index b2aac1e50..66ab48275 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.cpp +++ b/Telegram/SourceFiles/history/history_inner_widget.cpp @@ -1540,19 +1540,26 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { _widget->replyToMessage(itemId); }); } - if (IsServerMsgId(item->id) && item->repliesCount() > 0) { - const auto &phrase = item->repliesAreComments() - ? tr::lng_comments_view - : tr::lng_replies_view; - _menu->addAction(phrase(tr::now, lt_count, item->repliesCount()), [=] { - controller->showRepliesForMessage(_history, itemId.msg); - }); - } else if (const auto replyToTop = item->replyToTop()) { - const auto &phrase = item->repliesAreComments() - ? tr::lng_comments_view_thread - : tr::lng_replies_view_thread; - _menu->addAction(phrase(tr::now), [=] { - controller->showRepliesForMessage(_history, replyToTop); + const auto withComments = item->repliesAreComments(); + const auto repliesCount = item->repliesCount(); + const auto withReplies = IsServerMsgId(item->id) + && (repliesCount > 0 || item->replyToTop()); + const auto noBubbleButton = !withComments + || (item->mainView() && !item->mainView()->drawBubble()); + if (withReplies && noBubbleButton) { + const auto rootId = repliesCount ? item->id : item->replyToTop(); + const auto phrase = (item->repliesCount() > 0) + ? (withComments + ? tr::lng_comments_view + : tr::lng_replies_view)( + tr::now, + lt_count, + item->repliesCount()) + : (withComments + ? tr::lng_comments_view_thread + : tr::lng_replies_view_thread)(tr::now); + _menu->addAction(phrase, [=] { + controller->showRepliesForMessage(_history, rootId); }); } if (item->allowsEdit(base::unixtime::now())) { diff --git a/Telegram/SourceFiles/history/history_message.cpp b/Telegram/SourceFiles/history/history_message.cpp index 0abfa0ea4..f7fc38423 100644 --- a/Telegram/SourceFiles/history/history_message.cpp +++ b/Telegram/SourceFiles/history/history_message.cpp @@ -1253,7 +1253,7 @@ void HistoryMessage::destroyHistoryEntry() { history()->eraseFromUnreadMentions(id); } if (const auto reply = Get()) { - decrementReplyToTopCounter(reply); + changeReplyToTopCounter(reply, -1); } } @@ -1556,7 +1556,7 @@ void HistoryMessage::setReplyToTop(MsgId replyToTop) { return; } reply->replyToMsgTop = replyToTop; - incrementReplyToTopCounter(reply); + changeReplyToTopCounter(reply, 1); } void HistoryMessage::setRealId(MsgId newId) { @@ -1568,54 +1568,56 @@ void HistoryMessage::setRealId(MsgId newId) { if (reply->replyToLink()) { reply->setReplyToLinkFrom(this); } - incrementReplyToTopCounter(reply); + changeReplyToTopCounter(reply, 1); } } void HistoryMessage::incrementReplyToTopCounter() { if (const auto reply = Get()) { - incrementReplyToTopCounter(reply); + changeReplyToTopCounter(reply, 1); } } -void HistoryMessage::incrementReplyToTopCounter( - not_null reply) { +void HistoryMessage::changeReplyToTopCounter( + not_null reply, + int delta) { if (!IsServerMsgId(id) || !reply->replyToTop()) { return; } - if (const auto channelId = history()->channelId()) { - const auto top = history()->owner().message( - channelId, - reply->replyToTop()); - if (top) { - if (const auto from = displayFrom()) { - if (const auto user = from->asUser()) { - top->changeRepliesCount(1, user->bareId()); - return; - } + const auto channelId = history()->channelId(); + if (!channelId) { + return; + } + const auto top = history()->owner().message( + channelId, + reply->replyToTop()); + if (!top) { + return; + } + const auto changeFor = [&](not_null item) { + if (const auto from = displayFrom()) { + if (const auto user = from->asUser()) { + item->changeRepliesCount(delta, user->bareId()); + return; } - top->changeRepliesCount(1, UserId()); + } + item->changeRepliesCount(delta, UserId()); + }; + if (const auto views = top->Get()) { + if (views->commentsChannelId) { + // This is a post in channel, we don't track its replies. + return; } } -} - -void HistoryMessage::decrementReplyToTopCounter( - not_null reply) { - if (!IsServerMsgId(id) || !reply->replyToTop()) { - return; - } - if (const auto channelId = history()->channelId()) { - const auto top = history()->owner().message( - channelId, - reply->replyToTop()); - if (top) { - if (const auto from = displayFrom()) { - if (const auto user = from->asUser()) { - top->changeRepliesCount(-1, user->bareId()); - return; - } + changeFor(top); + if (const auto sender = top->discussionPostOriginalSender()) { + if (const auto forwarded = top->Get()) { + const auto id = FullMsgId( + sender->bareId(), + forwarded->savedFromMsgId); + if (const auto original = history()->owner().message(id)) { + changeFor(original); } - top->changeRepliesCount(-1, UserId()); } } } diff --git a/Telegram/SourceFiles/history/history_message.h b/Telegram/SourceFiles/history/history_message.h index f1857b79c..1acc39575 100644 --- a/Telegram/SourceFiles/history/history_message.h +++ b/Telegram/SourceFiles/history/history_message.h @@ -209,8 +209,9 @@ private: void createComponentsHelper(MTPDmessage::Flags flags, MsgId replyTo, UserId viaBotId, const QString &postAuthor, const MTPReplyMarkup &markup); void createComponents(const CreateConfig &config); void setupForwardedComponent(const CreateConfig &config); - void incrementReplyToTopCounter(not_null reply); - void decrementReplyToTopCounter(not_null reply); + void changeReplyToTopCounter( + not_null reply, + int delta); void refreshRepliesText( not_null views, bool forceResize = false);