From 395ee845aef93a608ccb9f53458c65b58e63e122 Mon Sep 17 00:00:00 2001 From: w-4 <36057469+w-4@users.noreply.github.com> Date: Sat, 20 Jun 2020 21:18:26 +0700 Subject: [PATCH] Fix discussion unreadCount could be higher than commentCount (#2195) * Fix discussion unreadCount being higher than commentCount if posts have been deleted --- framework/core/js/src/common/models/Discussion.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/framework/core/js/src/common/models/Discussion.js b/framework/core/js/src/common/models/Discussion.js index faf0dda86..1e7c8ae09 100644 --- a/framework/core/js/src/common/models/Discussion.js +++ b/framework/core/js/src/common/models/Discussion.js @@ -68,7 +68,10 @@ Object.assign(Discussion.prototype, { const user = app.session.user; if (user && user.markedAllAsReadAt() < this.lastPostedAt()) { - return Math.max(0, this.lastPostNumber() - (this.lastReadPostNumber() || 0)); + const unreadCount = Math.max(0, this.lastPostNumber() - (this.lastReadPostNumber() || 0)); + // If posts have been deleted, it's possible that the unread count could exceed the + // actual post count. As such, we take the min of the two to ensure this isn't an issue. + return Math.min(unreadCount, this.commentCount()); } return 0;