From cb9fb2acfabb3653ed62f3749ca71efc0aa7c787 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 3 Dec 2013 16:25:26 -0500 Subject: [PATCH] FIX: A post via the message bus wasn't updating `highest_post_number` properly. --- .../javascripts/discourse/controllers/topic_controller.js | 3 ++- app/assets/javascripts/discourse/models/post_stream.js | 6 ++++++ test/javascripts/models/post_stream_test.js | 8 +++++--- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/topic_controller.js b/app/assets/javascripts/discourse/controllers/topic_controller.js index 89ef1bca19c..751c15564ea 100644 --- a/app/assets/javascripts/discourse/controllers/topic_controller.js +++ b/app/assets/javascripts/discourse/controllers/topic_controller.js @@ -271,7 +271,8 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected streamPercentage: function() { if (!this.get('postStream.loaded')) { return 0; } if (this.get('postStream.highest_post_number') === 0) { return 0; } - return this.get('progressPosition') / this.get('highest_post_number'); + var perc = this.get('progressPosition') / this.get('highest_post_number'); + return (perc > 1.0) ? 1.0 : perc; }.property('postStream.loaded', 'progressPosition', 'highest_post_number'), multiSelectChanged: function() { diff --git a/app/assets/javascripts/discourse/models/post_stream.js b/app/assets/javascripts/discourse/models/post_stream.js index 8b61983ae7e..37b8370c28d 100644 --- a/app/assets/javascripts/discourse/models/post_stream.js +++ b/app/assets/javascripts/discourse/models/post_stream.js @@ -563,6 +563,12 @@ Discourse.PostStream = Em.Object.extend({ post.set('topic', this.get('topic')); postIdentityMap.set(post.get('id'), post); + + // Update the `highest_post_number` if this post is higher. + var postNumber = post.get('post_number'); + if (postNumber && postNumber > (this.get('topic.highest_post_number') || 0)) { + this.set('topic.highest_post_number', postNumber); + } } return post; }, diff --git a/test/javascripts/models/post_stream_test.js b/test/javascripts/models/post_stream_test.js index 6e4e74faa77..a2b2145722f 100644 --- a/test/javascripts/models/post_stream_test.js +++ b/test/javascripts/models/post_stream_test.js @@ -206,14 +206,16 @@ test("previousWindow", function() { }); test("storePost", function() { - var postStream = buildStream(1234); + var postStream = buildStream(1234), + post = Discourse.Post.create({id: 1, post_number: 100, raw: 'initial value'}); - var post = Discourse.Post.create({id: 1, post_number: 1, raw: 'initial value'}); + blank(postStream.get('topic.highest_post_number'), "it has no highest post number yet"); var stored = postStream.storePost(post); equal(post, stored, "it returns the post it stored"); equal(post.get('topic'), postStream.get('topic'), "it creates the topic reference properly"); + equal(postStream.get('topic.highest_post_number'), 100, "it set the highest post number"); - var dupePost = Discourse.Post.create({id: 1, post_number: 1, raw: 'updated value'}); + var dupePost = Discourse.Post.create({id: 1, post_number: 100, raw: 'updated value'}); var storedDupe = postStream.storePost(dupePost); equal(storedDupe, post, "it returns the previously stored post instead to avoid dupes"); equal(storedDupe.get('raw'), 'updated value', 'it updates the previously stored post');