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);
});
}
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())) {

View file

@ -1253,7 +1253,7 @@ void HistoryMessage::destroyHistoryEntry() {
history()->eraseFromUnreadMentions(id);
}
if (const auto reply = Get<HistoryMessageReply>()) {
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<HistoryMessageReply>()) {
incrementReplyToTopCounter(reply);
changeReplyToTopCounter(reply, 1);
}
}
void HistoryMessage::incrementReplyToTopCounter(
not_null<HistoryMessageReply*> reply) {
void HistoryMessage::changeReplyToTopCounter(
not_null<HistoryMessageReply*> 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<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;
}
}
}
void HistoryMessage::decrementReplyToTopCounter(
not_null<HistoryMessageReply*> 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<HistoryMessageForwarded>()) {
const auto id = FullMsgId(
sender->bareId(),
forwarded->savedFromMsgId);
if (const auto original = history()->owner().message(id)) {
changeFor(original);
}
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 createComponents(const CreateConfig &config);
void setupForwardedComponent(const CreateConfig &config);
void incrementReplyToTopCounter(not_null<HistoryMessageReply*> reply);
void decrementReplyToTopCounter(not_null<HistoryMessageReply*> reply);
void changeReplyToTopCounter(
not_null<HistoryMessageReply*> reply,
int delta);
void refreshRepliesText(
not_null<HistoryMessageViews*> views,
bool forceResize = false);