From 7d4b089cd129cf730d52868bc9998a9bbd4cbb8c Mon Sep 17 00:00:00 2001
From: Robin Ward <robin.ward@gmail.com>
Date: Thu, 18 Jul 2013 16:09:03 -0400
Subject: [PATCH] FIX: Suggested Topics were sometimes disappearing

---
 .../discourse/models/post_stream.js           |  2 +-
 test/javascripts/models/post_stream_test.js   | 20 ++++++++++++++++++-
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/app/assets/javascripts/discourse/models/post_stream.js b/app/assets/javascripts/discourse/models/post_stream.js
index 922998b4a93..9a018f7096c 100644
--- a/app/assets/javascripts/discourse/models/post_stream.js
+++ b/app/assets/javascripts/discourse/models/post_stream.js
@@ -85,7 +85,7 @@ Discourse.PostStream = Em.Object.extend({
   lastPostLoaded: function() {
     if (!this.get('hasLoadedData')) { return false; }
     return !!this.get('posts').findProperty('id', this.get('lastPostId'));
-  }.property('hasLoadedData', 'posts.[]', 'lastPostId'),
+  }.property('hasLoadedData', 'posts.@each.id', 'lastPostId'),
 
   lastPostNotLoaded: Em.computed.not('lastPostLoaded'),
 
diff --git a/test/javascripts/models/post_stream_test.js b/test/javascripts/models/post_stream_test.js
index cdb7d85ad31..f7eb0e1d76d 100644
--- a/test/javascripts/models/post_stream_test.js
+++ b/test/javascripts/models/post_stream_test.js
@@ -293,6 +293,7 @@ test("staging and committing a post", function() {
   // Stage the new post in the stream
   var result = postStream.stagePost(stagedPost, user);
   equal(result, true, "it returns true");
+
   ok(postStream.get('loading'), "it is loading while the post is being staged");
   stagedPost.setProperties({ id: 1234, raw: "different raw value" });
   equal(postStream.get('filteredPostsCount'), 1, "it retains the filteredPostsCount");
@@ -341,6 +342,22 @@ test('triggerNewPostInStream', function() {
 });
 
 
+test("lastPostLoaded when the id changes", function() {
+
+  // This can happen in a race condition between staging a post and it coming through on the
+  // message bus. If the id of a post changes we should reconsider the lastPostLoaded property.
+  var postStream = buildStream(10101, [1, 2]);
+  var postWithoutId = Discourse.Post.create({ raw: 'hello world this is my new post' });
+
+  postStream.appendPost(Discourse.Post.create({id: 1, post_number: 1}));
+  postStream.appendPost(postWithoutId);
+  ok(!postStream.get('lastPostLoaded'), 'the last post is not loaded');
+
+  postWithoutId.set('id', 2);
+  ok(postStream.get('lastPostLoaded'), 'the last post is loaded now that the post has an id');
+
+});
+
 test("comitting and triggerNewPostInStream race condition", function() {
   var postStream = buildStream(4964);
 
@@ -358,4 +375,5 @@ test("comitting and triggerNewPostInStream race condition", function() {
 
   postStream.commitPost(stagedPost);
   equal(postStream.get('filteredPostsCount'), 1, "it does not add the same post twice");
-});
\ No newline at end of file
+});
+