Skip to content

A platform for community discussion. Free, open, simple.

FIX: draft not clearing when replying to new topic

Sam Saffron941162e90e99
diff --git a/app/assets/javascripts/discourse/models/composer.js.es6 b/app/assets/javascripts/discourse/models/composer.js.es6
index 89041903c91..a630f521c41 100644
--- a/app/assets/javascripts/discourse/models/composer.js.es6
+++ b/app/assets/javascripts/discourse/models/composer.js.es6
@@ -57,7 +57,8 @@ const CLOSED = "closed",
tags: "tags",
featured_link: "featuredLink",
shared_draft: "sharedDraft",
- no_bump: "noBump"
+ no_bump: "noBump",
+ draft_key: "draftKey"
},
_edit_topic_serializer = {
title: "topic.title",
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
index c1ba656186f..dd4b78e7561 100644
--- a/app/controllers/posts_controller.rb
+++ b/app/controllers/posts_controller.rb
@@ -671,7 +671,8 @@ class PostsController < ApplicationController
:auto_track,
:typing_duration_msecs,
:composer_open_duration_msecs,
- :visible
+ :visible,
+ :draft_key
]
Post.plugin_permitted_create_params.each do |key, plugin|
diff --git a/lib/post_creator.rb b/lib/post_creator.rb
index dd745d7c49f..d914a735831 100644
--- a/lib/post_creator.rb
+++ b/lib/post_creator.rb
@@ -35,6 +35,7 @@ class PostCreator
# call `enqueue_jobs` after the transaction is comitted.
# hidden_reason_id - Reason for hiding the post (optional)
# skip_validations - Do not validate any of the content in the post
+ # draft_key - the key of the draft we are creating (will be deleted on success)
#
# When replying to a topic:
# topic_id - topic we're replying to
@@ -180,7 +181,9 @@ class PostCreator
update_uploads_secure_status
ensure_in_allowed_users if guardian.is_staff?
unarchive_message
- @post.advance_draft_sequence unless @opts[:import_mode]
+ if !@opts[:import_mode]
+ DraftSequence.next!(@user, draft_key)
+ end
@post.save_reply_relationships
end
end
@@ -292,10 +295,13 @@ class PostCreator
protected
+ def draft_key
+ @draft_key ||= @opts[:draft_key]
+ @draft_key ||= @topic ? "topic_#{@topic.id}" : "new_topic"
+ end
+
def build_post_stats
if PostCreator.track_post_stats
- draft_key = @topic ? "topic_#{@topic.id}" : "new_topic"
-
sequence = DraftSequence.current(@user, draft_key)
revisions = Draft.where(sequence: sequence,
user_id: @user.id,
diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rb
index 32a581b16b2..d9d4270d0c6 100644
--- a/spec/requests/posts_controller_spec.rb
+++ b/spec/requests/posts_controller_spec.rb
@@ -862,18 +862,25 @@ describe PostsController do
it "doesn't enqueue posts when user first creates a topic" do
user.user_stat.update_column(:topic_count, 1)
+ Draft.set(user, "should_clear", 0, "{'a' : 'b'}")
+
post "/posts.json", params: {
raw: 'this is the test content',
title: 'this is the test title for the topic',
composer_open_duration_msecs: 204,
typing_duration_msecs: 100,
- topic_id: topic.id
+ topic_id: topic.id,
+ draft_key: "should_clear"
}
expect(response.status).to eq(200)
parsed = ::JSON.parse(response.body)
expect(parsed["action"]).not_to be_present
+
+ expect {
+ Draft.get(user, "should_clear", 0)
+ }.to raise_error(Draft::OutOfSequence)
end
it "doesn't enqueue replies when the topic is closed" do