Skip to content

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

FIX: In-memory state desynchronization leads to corrupt post revisions (#41515)

Isaac Janzen93960f038b8e
diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb
index 4a77e6f4048..9a3f4fbb2b0 100644
--- a/lib/post_revisor.rb
+++ b/lib/post_revisor.rb
@@ -350,6 +350,8 @@ class PostRevisor
old_raw = @post.raw
+ @should_bump_topic = false
+
Post.transaction do
revise_post
@@ -362,13 +364,18 @@ class PostRevisor
# false positive.
plugin_callbacks
+ @should_bump_topic = @version_changed && successfully_saved_post_and_topic && should_bump?
revise_topic
advance_draft_sequence if !opts[:keep_existing_draft]
+
+ raise ActiveRecord::Rollback if !successfully_saved_post_and_topic
end
# bail out if the post or topic failed to save
return false if !successfully_saved_post_and_topic
+ bump_topic if @should_bump_topic
+
# Lock the post by default if the appropriate setting is true
if SiteSetting.staff_edit_locks_post? && [email protected]? && @fields.has_key?("raw") &&
@editor.staff? && @editor != Discourse.system_user && [email protected]&.staff?
@@ -546,8 +553,7 @@ class PostRevisor
@post.last_version_at = @revised_at
revise
- perform_edit
- bump_topic
+ perform_edit if successfully_saved_post_and_topic
end
def revise
@@ -595,6 +601,7 @@ class PostRevisor
previous_reply_to_post_number = @post.reply_to_post_number_was
@post_successfully_saved = @post.save(validate: @validate_post)
+ @post_changes = @post.previous_changes.slice(*POST_TRACKED_FIELDS) if @post_successfully_saved
@post.link_post_uploads
if @post_successfully_saved
@@ -753,7 +760,7 @@ class PostRevisor
end
def post_changes
- @post.previous_changes.slice(*POST_TRACKED_FIELDS)
+ @post_changes || @post.previous_changes.slice(*POST_TRACKED_FIELDS)
end
def topic_diff
@@ -770,7 +777,6 @@ class PostRevisor
end
def bump_topic
- return if !should_bump?
@topic.update_column(:bumped_at, Time.now)
TopicTrackingState.publish_muted(@topic)
TopicTrackingState.publish_unmuted(@topic)
diff --git a/spec/lib/post_revisor_spec.rb b/spec/lib/post_revisor_spec.rb
index d75d4730366..6cd64da32c3 100644
--- a/spec/lib/post_revisor_spec.rb
+++ b/spec/lib/post_revisor_spec.rb
@@ -1954,7 +1954,73 @@ describe PostRevisor do
{ raw: "updated body" },
revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.second,
)
- }.to change { post.topic.bumped_at }
+ }.to change { post.topic.reload.bumped_at }
+ end
+
+ it "bumps the persisted topic when editing raw and title" do
+ post.topic.update!(bumped_at: 1.day.ago)
+
+ expect {
+ post_revisor.revise!(
+ post.user,
+ { raw: "updated body", title: "This is an updated topic title" },
+ revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.second,
+ )
+ }.to change { post.topic.reload.bumped_at }
+ end
+
+ it "keeps post changes available to the modifier before advancing the draft" do
+ DiscoursePluginRegistry.unregister_modifier(
+ plugin_instance,
+ :should_bump_topic,
+ &modifier_block
+ )
+
+ inspecting_modifier =
+ Proc.new do |value, modifier_post, modifier_post_changes, modifier_topic_changes, editor|
+ modifier_post.is_first_post? && modifier_post_changes.any?
+ end
+
+ plugin_instance.register_modifier(:should_bump_topic, &inspecting_modifier)
+
+ expect {
+ post_revisor.revise!(
+ post.user,
+ { raw: "updated body", title: "Updated topic title" },
+ revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.second,
+ )
+ }.to change { post.topic.reload.bumped_at }
+ ensure
+ if defined?(inspecting_modifier)
+ DiscoursePluginRegistry.unregister_modifier(
+ plugin_instance,
+ :should_bump_topic,
+ &inspecting_modifier
+ )
+ end
+ plugin_instance.register_modifier(:should_bump_topic, &modifier_block)
+ end
+
+ it "doesn't bump the topic when the title edit is invalid" do
+ original_raw = post.raw
+ post.topic.update!(bumped_at: 1.day.ago)
+
+ messages =
+ MessageBus.track_publish(TopicTrackingState::LATEST_MESSAGE_BUS_CHANNEL) do
+ expect {
+ result =
+ post_revisor.revise!(
+ post.user,
+ { raw: "updated body", title: "New Title" },
+ revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.second,
+ )
+ expect(result).to eq(false)
+ }.not_to change { post.topic.reload.bumped_at }
+ end
+
+ expect(messages).to be_empty
+ expect(post.reload.raw).to eq(original_raw)
+ expect(post.version).to eq(1)
end
end
end
diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rb
index 2d8f6728057..4f5afb53b08 100644
--- a/spec/requests/posts_controller_spec.rb
+++ b/spec/requests/posts_controller_spec.rb
@@ -696,6 +696,48 @@ RSpec.describe PostsController do
expect(post.raw).to eq("edited body")
end
+ it "rolls back post changes when a first-post title edit is invalid" do
+ Fabricate(:watched_word, action: WatchedWord.actions[:censor], word: "blockedword")
+
+ first_post = create_post(user:, title: "Original topic title", raw: "Original topic body")
+ original_title = first_post.topic.title
+ original_raw = first_post.raw
+
+ put "/posts/#{first_post.id}.json",
+ params: {
+ title: "A blockedword topic title",
+ post: {
+ raw: "Body from failed title edit",
+ edit_reason: "moderator edit",
+ },
+ }
+
+ expect(response.status).to eq(422)
+ expect(response.parsed_body["errors"].join).to include("blockedword")
+ expect(first_post.reload.raw).to eq(original_raw)
+ expect(first_post.version).to eq(1)
+ expect(PostRevision.where(post_id: first_post.id).pluck(:number)).to eq([])
+
+ put "/posts/#{first_post.id}.json",
+ params: {
+ title: original_title,
+ post: {
+ raw: "Body from valid title edit",
+ edit_reason: "moderator edit",
+ },
+ }
+
+ expect(response.status).to eq(200)
+ expect(first_post.reload.raw).to eq("Body from valid title edit")
+ expect(first_post.version).to eq(2)
+ expect(PostRevision.where(post_id: first_post.id).pluck(:number)).to eq([2])
+
+ get "/posts/#{first_post.id}/revisions/2.json"
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["current_revision"]).to eq(2)
+ end
+
it "won't update bump date if post is a whisper" do
created_at = freeze_time 1.day.ago
post = Fabricate(:post, post_type: Post.types[:whisper], user: user)