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
This commit is contained in:
Sam Saffron 2019-11-26 18:23:10 +11:00
parent d0246104ee
commit 941162e90e
4 changed files with 21 additions and 6 deletions

View File

@ -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",

View File

@ -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|

View File

@ -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,

View File

@ -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