diff --git a/app/assets/javascripts/discourse/controllers/topic_controller.js b/app/assets/javascripts/discourse/controllers/topic_controller.js index 2ed6ccb6bb3..b629c37727f 100644 --- a/app/assets/javascripts/discourse/controllers/topic_controller.js +++ b/app/assets/javascripts/discourse/controllers/topic_controller.js @@ -550,20 +550,22 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected } }.observes('currentPost'), - sawObjects: function(posts) { - if (posts) { - var self = this, - lastReadPostNumber = this.get('last_read_post_number'); + readPosts: function(topicId, postNumbers) { + var postStream = this.get('postStream'); - posts.forEach(function(post) { - var postNumber = post.get('post_number'); - if (postNumber > lastReadPostNumber) { - lastReadPostNumber = postNumber; + if(this.get('postStream.topic.id') === topicId){ + _.each(postStream.get('posts'), function(post){ + // optimise heavy loop + // TODO identity map for postNumber + if(_.include(postNumbers,post.post_number) && !post.read){ + post.set("read", true); } - post.set('read', true); }); - self.set('last_read_post_number', lastReadPostNumber); + var max = _.max(postNumbers); + if(max > this.get('last_read_post_number')){ + this.set('last_read_post_number', max); + } } }, diff --git a/app/assets/javascripts/discourse/lib/screen_track.js b/app/assets/javascripts/discourse/lib/screen_track.js index 91a6111f7b6..5cadfd76423 100644 --- a/app/assets/javascripts/discourse/lib/screen_track.js +++ b/app/assets/javascripts/discourse/lib/screen_track.js @@ -16,7 +16,7 @@ Discourse.ScreenTrack = Ember.Object.extend({ this.reset(); }, - start: function(topicId) { + start: function(topicId, topicController) { var currentTopicId = this.get('topicId'); if (currentTopicId && (currentTopicId !== topicId)) { this.tick(); @@ -34,6 +34,7 @@ Discourse.ScreenTrack = Ember.Object.extend({ } this.set('topicId', topicId); + this.set('topicController', topicController); }, stop: function() { @@ -46,6 +47,7 @@ Discourse.ScreenTrack = Ember.Object.extend({ this.flush(); this.reset(); this.set('topicId', null); + this.set('topicController', null); if (this.get('interval')) { clearInterval(this.get('interval')); this.set('interval', null); @@ -87,7 +89,8 @@ Discourse.ScreenTrack = Ember.Object.extend({ if (!Discourse.User.current()) return; var newTimings = {}, - totalTimings = this.get('totalTimings'); + totalTimings = this.get('totalTimings'), + self = this; _.each(this.get('timings'), function(timing) { if (!totalTimings[timing.postNumber]) @@ -126,6 +129,14 @@ Discourse.ScreenTrack = Ember.Object.extend({ headers: { 'X-SILENCE-LOGGER': 'true' } + }).then(function(){ + var controller = self.get('topicController'); + if(controller){ + var postNumbers = Object.keys(newTimings).map(function(v){ + return parseInt(v,10); + }); + controller.readPosts(topicId, postNumbers); + } }); this.set('topicTime', 0); @@ -145,7 +156,16 @@ Discourse.ScreenTrack = Ember.Object.extend({ var diff = new Date().getTime() - this.get('lastTick'); this.set('lastFlush', this.get('lastFlush') + diff); this.set('lastTick', new Date().getTime()); - if (this.get('lastFlush') > (Discourse.SiteSettings.flush_timings_secs * 1000)) { + + var totalTimings = this.get('totalTimings'), timings = this.get('timings'); + var nextFlush = Discourse.SiteSettings.flush_timings_secs * 1000; + + // rush new post numbers + var rush = _.any(_.filter(timings, function(t){return t.time>0;}), function(t){ + return !totalTimings[t.postNumber]; + }); + + if (this.get('lastFlush') > nextFlush || rush) { this.flush(); } diff --git a/app/assets/javascripts/discourse/routes/topic_route.js b/app/assets/javascripts/discourse/routes/topic_route.js index 4a8336bddb1..5820c382f8e 100644 --- a/app/assets/javascripts/discourse/routes/topic_route.js +++ b/app/assets/javascripts/discourse/routes/topic_route.js @@ -180,7 +180,7 @@ Discourse.TopicRoute = Discourse.Route.extend({ controller.subscribe(); // We reset screen tracking every time a topic is entered - Discourse.ScreenTrack.current().start(model.get('id')); + Discourse.ScreenTrack.current().start(model.get('id'), controller); } }); diff --git a/app/assets/javascripts/discourse/templates/post.js.handlebars b/app/assets/javascripts/discourse/templates/post.js.handlebars index 1a9c0f985ad..faf30fd5e30 100644 --- a/app/assets/javascripts/discourse/templates/post.js.handlebars +++ b/app/assets/javascripts/discourse/templates/post.js.handlebars @@ -57,6 +57,7 @@ {{/if}} {{/if}} +