From 85e885fcfab356cc3f6cec11029d77ed632092e3 Mon Sep 17 00:00:00 2001
From: Sam <sam.saffron@gmail.com>
Date: Mon, 29 Sep 2014 13:49:38 +1000
Subject: [PATCH] FIX: sync up all counts when visiting new and unread

---
 .../discourse/models/topic_tracking_state.js  | 50 +++++++++++++++----
 1 file changed, 39 insertions(+), 11 deletions(-)

diff --git a/app/assets/javascripts/discourse/models/topic_tracking_state.js b/app/assets/javascripts/discourse/models/topic_tracking_state.js
index 4ffcabf93ab..b0b1dab17fb 100644
--- a/app/assets/javascripts/discourse/models/topic_tracking_state.js
+++ b/app/assets/javascripts/discourse/models/topic_tracking_state.js
@@ -1,3 +1,15 @@
+function isNew(topic){
+  return topic.last_read_post_number === null &&
+        ((topic.notification_level !== 0 && !topic.notification_level) ||
+        topic.notification_level >= Discourse.Topic.NotificationLevel.TRACKING);
+}
+
+function isUnread(topic){
+  return topic.last_read_post_number !== null &&
+         topic.last_read_post_number < topic.highest_post_number &&
+         topic.notification_level >= Discourse.Topic.NotificationLevel.TRACKING;
+}
+
 Discourse.TopicTrackingState = Discourse.Model.extend({
   messageCount: 0,
 
@@ -184,6 +196,31 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
       tracker.states["t" + topic.id] = row;
     });
 
+    // Correct missing states, safeguard in case message bus is corrupt
+    if((filter === "new" || filter === "unread") && !list.more_topics_url){
+
+      var ids = {};
+      list.topics.forEach(function(r){
+        ids["t" + r.id] = true;
+      });
+
+      _.each(tracker.states, function(v, k){
+
+        // we are good if we are on the list
+        if (ids[k]) { return; }
+
+        if (filter === "unread" && isUnread(v)) {
+          // pretend read
+          v.last_read_post_number = v.highest_post_number;
+        }
+
+        if (filter === "new" && isNew(v)) {
+          // pretend not new
+          v.last_read_post_number = 1;
+        }
+      });
+    }
+
     this.incrementMessageCount();
   },
 
@@ -193,12 +230,7 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
 
   countNew: function(category_id){
     return _.chain(this.states)
-      .where({last_read_post_number: null})
-      .where(function(topic) {
-        // !0 is true
-        return (topic.notification_level !== 0 && !topic.notification_level) ||
-               topic.notification_level >= Discourse.Topic.NotificationLevel.TRACKING;
-      })
+      .where(isNew)
       .where(function(topic){ return topic.category_id === category_id || !category_id;})
       .value()
       .length;
@@ -215,11 +247,7 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
 
   countUnread: function(category_id){
     return _.chain(this.states)
-      .where(function(topic){
-        return topic.last_read_post_number !== null &&
-               topic.last_read_post_number < topic.highest_post_number;
-      })
-      .where(function(topic) { return topic.notification_level >= Discourse.Topic.NotificationLevel.TRACKING})
+      .where(isUnread)
       .where(function(topic){ return topic.category_id === category_id || !category_id;})
       .value()
       .length;