Track comments count correctly.

This commit is contained in:
John Preston 2020-09-03 12:42:25 +04:00
parent fb20be3e6c
commit 60002555c3
3 changed files with 60 additions and 50 deletions

View file

@ -1540,19 +1540,26 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) {
_widget->replyToMessage(itemId); _widget->replyToMessage(itemId);
}); });
} }
if (IsServerMsgId(item->id) && item->repliesCount() > 0) { const auto withComments = item->repliesAreComments();
const auto &phrase = item->repliesAreComments() const auto repliesCount = item->repliesCount();
? tr::lng_comments_view const auto withReplies = IsServerMsgId(item->id)
: tr::lng_replies_view; && (repliesCount > 0 || item->replyToTop());
_menu->addAction(phrase(tr::now, lt_count, item->repliesCount()), [=] { const auto noBubbleButton = !withComments
controller->showRepliesForMessage(_history, itemId.msg); || (item->mainView() && !item->mainView()->drawBubble());
}); if (withReplies && noBubbleButton) {
} else if (const auto replyToTop = item->replyToTop()) { const auto rootId = repliesCount ? item->id : item->replyToTop();
const auto &phrase = item->repliesAreComments() const auto phrase = (item->repliesCount() > 0)
? tr::lng_comments_view_thread ? (withComments
: tr::lng_replies_view_thread; ? tr::lng_comments_view
_menu->addAction(phrase(tr::now), [=] { : tr::lng_replies_view)(
controller->showRepliesForMessage(_history, replyToTop); 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())) { if (item->allowsEdit(base::unixtime::now())) {

View file

@ -1253,7 +1253,7 @@ void HistoryMessage::destroyHistoryEntry() {
history()->eraseFromUnreadMentions(id); history()->eraseFromUnreadMentions(id);
} }
if (const auto reply = Get<HistoryMessageReply>()) { if (const auto reply = Get<HistoryMessageReply>()) {
decrementReplyToTopCounter(reply); changeReplyToTopCounter(reply, -1);
} }
} }
@ -1556,7 +1556,7 @@ void HistoryMessage::setReplyToTop(MsgId replyToTop) {
return; return;
} }
reply->replyToMsgTop = replyToTop; reply->replyToMsgTop = replyToTop;
incrementReplyToTopCounter(reply); changeReplyToTopCounter(reply, 1);
} }
void HistoryMessage::setRealId(MsgId newId) { void HistoryMessage::setRealId(MsgId newId) {
@ -1568,54 +1568,56 @@ void HistoryMessage::setRealId(MsgId newId) {
if (reply->replyToLink()) { if (reply->replyToLink()) {
reply->setReplyToLinkFrom(this); reply->setReplyToLinkFrom(this);
} }
incrementReplyToTopCounter(reply); changeReplyToTopCounter(reply, 1);
} }
} }
void HistoryMessage::incrementReplyToTopCounter() { void HistoryMessage::incrementReplyToTopCounter() {
if (const auto reply = Get<HistoryMessageReply>()) { if (const auto reply = Get<HistoryMessageReply>()) {
incrementReplyToTopCounter(reply); changeReplyToTopCounter(reply, 1);
} }
} }
void HistoryMessage::incrementReplyToTopCounter( void HistoryMessage::changeReplyToTopCounter(
not_null<HistoryMessageReply*> reply) { not_null<HistoryMessageReply*> reply,
int delta) {
if (!IsServerMsgId(id) || !reply->replyToTop()) { if (!IsServerMsgId(id) || !reply->replyToTop()) {
return; return;
} }
if (const auto channelId = history()->channelId()) { const auto channelId = history()->channelId();
const auto top = history()->owner().message( if (!channelId) {
channelId, return;
reply->replyToTop()); }
if (top) { const auto top = history()->owner().message(
if (const auto from = displayFrom()) { channelId,
if (const auto user = from->asUser()) { reply->replyToTop());
top->changeRepliesCount(1, user->bareId()); if (!top) {
return; return;
} }
const auto changeFor = [&](not_null<HistoryItem*> 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<HistoryMessageViews>()) {
if (views->commentsChannelId) {
// This is a post in channel, we don't track its replies.
return;
} }
} }
} changeFor(top);
if (const auto sender = top->discussionPostOriginalSender()) {
void HistoryMessage::decrementReplyToTopCounter( if (const auto forwarded = top->Get<HistoryMessageForwarded>()) {
not_null<HistoryMessageReply*> reply) { const auto id = FullMsgId(
if (!IsServerMsgId(id) || !reply->replyToTop()) { sender->bareId(),
return; forwarded->savedFromMsgId);
} if (const auto original = history()->owner().message(id)) {
if (const auto channelId = history()->channelId()) { changeFor(original);
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;
}
} }
top->changeRepliesCount(-1, UserId());
} }
} }
} }

View file

@ -209,8 +209,9 @@ private:
void createComponentsHelper(MTPDmessage::Flags flags, MsgId replyTo, UserId viaBotId, const QString &postAuthor, const MTPReplyMarkup &markup); void createComponentsHelper(MTPDmessage::Flags flags, MsgId replyTo, UserId viaBotId, const QString &postAuthor, const MTPReplyMarkup &markup);
void createComponents(const CreateConfig &config); void createComponents(const CreateConfig &config);
void setupForwardedComponent(const CreateConfig &config); void setupForwardedComponent(const CreateConfig &config);
void incrementReplyToTopCounter(not_null<HistoryMessageReply*> reply); void changeReplyToTopCounter(
void decrementReplyToTopCounter(not_null<HistoryMessageReply*> reply); not_null<HistoryMessageReply*> reply,
int delta);
void refreshRepliesText( void refreshRepliesText(
not_null<HistoryMessageViews*> views, not_null<HistoryMessageViews*> views,
bool forceResize = false); bool forceResize = false);