From efd631296e4fea36ecaa631f37ac611d6db1dd95 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 9 Jul 2013 12:34:28 -0400 Subject: [PATCH] FIX: Don't allow a user to stage a post while another is being staged. --- app/assets/javascripts/discourse/models/composer.js | 7 ++++++- app/assets/javascripts/discourse/models/post_stream.js | 10 ++++++++-- test/javascripts/models/post_stream_test.js | 9 +++++++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/discourse/models/composer.js b/app/assets/javascripts/discourse/models/composer.js index 177767d04..be48bd408 100644 --- a/app/assets/javascripts/discourse/models/composer.js +++ b/app/assets/javascripts/discourse/models/composer.js @@ -461,7 +461,12 @@ Discourse.Composer = Discourse.Model.extend({ if (post) { post.set('reply_count', (post.get('reply_count') || 0) + 1); } - postStream.stagePost(createdPost, currentUser); + if (!postStream.stagePost(createdPost, currentUser)) { + + // If we can't stage the post, return and don't save. We're likely currently + // staging a post. + return; + } } // Save callback diff --git a/app/assets/javascripts/discourse/models/post_stream.js b/app/assets/javascripts/discourse/models/post_stream.js index b46f7da00..51b668516 100644 --- a/app/assets/javascripts/discourse/models/post_stream.js +++ b/app/assets/javascripts/discourse/models/post_stream.js @@ -323,15 +323,19 @@ Discourse.PostStream = Em.Object.extend({ @param {Discourse.User} the user creating the post **/ stagePost: function(post, user) { - var topic = this.get('topic'); + // We can't stage two posts simultaneously + if (this.get('stagingPost')) { return false; } + + this.set('stagingPost', true); + + var topic = this.get('topic'); topic.setProperties({ posts_count: (topic.get('posts_count') || 0) + 1, last_posted_at: new Date(), 'details.last_poster': user, highest_post_number: (topic.get('highest_post_number') || 0) + 1 }); - this.set('stagingPost', true); post.setProperties({ post_number: topic.get('highest_post_number'), @@ -343,6 +347,8 @@ Discourse.PostStream = Em.Object.extend({ if (this.get('lastPostLoaded')) { this.appendPost(post); } + + return true; }, /** diff --git a/test/javascripts/models/post_stream_test.js b/test/javascripts/models/post_stream_test.js index 4fa37cfc1..6f1acc1ef 100644 --- a/test/javascripts/models/post_stream_test.js +++ b/test/javascripts/models/post_stream_test.js @@ -255,7 +255,8 @@ test("staging and undoing a new post", function() { }); // Stage the new post in the stream - postStream.stagePost(stagedPost, user); + var result = postStream.stagePost(stagedPost, user); + equal(result, true, "it returns true"); equal(topic.get('highest_post_number'), 2, "it updates the highest_post_number"); ok(postStream.get('loading'), "it is loading while the post is being staged"); @@ -290,11 +291,15 @@ test("staging and committing a post", function() { topic.set('posts_count', 1); // Stage the new post in the stream - postStream.stagePost(stagedPost, user); + 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"); + result = postStream.stagePost(stagedPost, user); + equal(result, false, "you can't stage a post while it is currently staging"); + postStream.commitPost(stagedPost); ok(postStream.get('posts').contains(stagedPost), "the post is still in the stream"); ok(!postStream.get('loading'), "it is no longer loading");