A platform for community discussion. Free, open, simple.
FIX: Prevent unnecessary revision when editing first post with tagging enabled (#37989)
Isaac Janzen5a1ab2b054fe
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rbindex 162bf4688a0..1829cd9bcb3 100644--- a/app/controllers/topics_controller.rb+++ b/app/controllers/topics_controller.rb@@ -449,21 +449,25 @@ class TopicsController < ApplicationControllerchanges.delete(:title) if topic.title == changes[:title]changes.delete(:category_id) if topic.category_id.to_i == changes[:category_id].to_i- if Tag.include_tags? && changes[:tags].present?- incoming = changes[:tags]-- if incoming.first.is_a?(String)- Discourse.deprecate(- "Passing tag names as strings to the tags param is deprecated, use tag objects ({id, name}) instead",- since: "2026.01",- drop_from: "2026.07",- )- changes.delete(:tags) if incoming.sort == topic.tags.map(&:name).sort- else- has_new = incoming.any? { |t| t[:id].blank? }- if !has_new && incoming.filter_map { |t| t[:id]&.to_i }.sort == topic.tags.pluck(:id).sort- changes.delete(:tags)+ if Tag.include_tags? && changes.has_key?(:tags)+ if changes[:tags].present?+ incoming = changes[:tags]++ if incoming.first.is_a?(String)+ Discourse.deprecate(+ "Passing tag names as strings to the tags param is deprecated, use tag objects ({id, name}) instead",+ since: "2026.01",+ drop_from: "2026.07",+ )+ changes.delete(:tags) if incoming.sort == topic.tags.map(&:name).sort+ else+ has_new = incoming.any? { |t| t[:id].blank? }+ if !has_new && incoming.filter_map { |t| t[:id]&.to_i }.sort == topic.tags.pluck(:id).sort+ changes.delete(:tags)+ endend+ elsif topic.tags.empty?+ changes.delete(:tags)endenddiff --git a/lib/post_revisor.rb b/lib/post_revisor.rbindex 1f4ef6e5b5f..b65bf911962 100644--- a/lib/post_revisor.rb+++ b/lib/post_revisor.rb@@ -239,6 +239,7 @@ class PostRevisor@fields[:raw] = cleanup_whitespaces(@fields[:raw]) if @fields.has_key?(:raw)@fields[:user_id] = @fields[:user_id].to_i if @fields.has_key?(:user_id)@fields[:category_id] = @fields[:category_id].to_i if @fields.has_key?(:category_id)+ @fields.delete(:tags) if @fields.has_key?(:tags) && @fields[:tags].blank? && @topic.tags.empty?# always reset edit_reason unless provided, do not set to nil else# previous reasons are lostdiff --git a/spec/lib/post_revisor_spec.rb b/spec/lib/post_revisor_spec.rbindex 57c78eef9c9..a61e343e5b8 100644--- a/spec/lib/post_revisor_spec.rb+++ b/spec/lib/post_revisor_spec.rb@@ -575,6 +575,17 @@ describe PostRevisor doexpect(post.public_version).to eq(1)expect(post.revisions.size).to eq(0)end++ it "does not create a new version for tag-only topic change within grace period when tags unchanged" do+ SiteSetting.tagging_enabled = true+ SiteSetting.editing_grace_period = 1.minute++ post_revisor.revise!(post.user, { tags: [] }, revised_at: post.updated_at + 10.seconds)+ post.reload++ expect(post.version).to eq(1)+ expect(post.revisions.size).to eq(0)+ endenddescribe "edit reasons" dodiff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 6d30a3fbeb8..1da6ab3a158 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -2259,6 +2259,25 @@ RSpec.describe TopicsController doexpect(response.status).to eq(200)expect(topic.reload.tags).to contain_exactly(tag)end++ it "does not create a revision when tags param is empty and topic has no tags" do+ expect do+ put "/t/#{topic.slug}/#{topic.id}.json", params: { tags: [] }, as: :json+ end.not_to change { topic.reload.first_post.revisions.count }++ expect(response.status).to eq(200)+ end++ it "creates a revision when all tags are removed from a topic" do+ topic.tags << tag++ expect do+ put "/t/#{topic.slug}/#{topic.id}.json", params: { tags: [] }, as: :json+ end.to change { topic.reload.first_post.revisions.count }.by(1)++ expect(response.status).to eq(200)+ expect(topic.reload.tags).to be_empty+ endendcontext "when topic is private" do