From 941162e90e997a078553d21af6895e7755fe52e1 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Tue, 26 Nov 2019 18:23:10 +1100 Subject: [PATCH] FIX: draft not clearing when replying to new topic This amends our API so we provide it with the draft key when saving a post this means post creator can clean up the draft consistently even if we are doing fancy stuff like replying to a new topic or new pm or whatever. There will be some followup work to clean it up so client never calls destroy on draft during normal operation and the #create/#update endpoints takes care of it every time --- .../javascripts/discourse/models/composer.js.es6 | 3 ++- app/controllers/posts_controller.rb | 3 ++- lib/post_creator.rb | 12 +++++++++--- spec/requests/posts_controller_spec.rb | 9 ++++++++- 4 files changed, 21 insertions(+), 6 deletions(-) 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