Skip to content

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

FIX: Allow reverting topic revisions with only tag changes (#34169)

Colby Howell7f40857a8581
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
index 882dc0eda9c..77eb41c6b47 100644
--- a/app/controllers/posts_controller.rb
+++ b/app/controllers/posts_controller.rb
@@ -572,7 +572,8 @@ class PostsController < ApplicationController
guardian.ensure_can_see!(post_revision)
guardian.ensure_can_edit!(post)
if post_revision.modifications["raw"].blank? && post_revision.modifications["title"].blank? &&
- post_revision.modifications["category_id"].blank?
+ post_revision.modifications["category_id"].blank? &&
+ post_revision.modifications["tags"].blank?
return render_json_error(I18n.t("revert_version_same"))
end
@@ -590,6 +591,13 @@ class PostsController < ApplicationController
0
] if post_revision.modifications["category_id"].present? &&
post_revision.modifications["category_id"][0] != topic.category.id
+ if post_revision.modifications["tags"].present?
+ revision_tags = post_revision.modifications["tags"][0]
+ revision_tags = revision_tags.is_a?(Array) ? revision_tags : revision_tags.split(",")
+ current_tags = topic.tags.pluck(:name)
+
+ changes[:tags] = revision_tags if revision_tags.sort != current_tags.sort
+ end
end
return render_json_error(I18n.t("revert_version_same")) if changes.length <= 0
changes[:edit_reason] = I18n.with_locale(SiteSetting.default_locale) do
diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rb
index 1137d650c18..5855b1cdff2 100644
--- a/spec/requests/posts_controller_spec.rb
+++ b/spec/requests/posts_controller_spec.rb
@@ -2411,6 +2411,33 @@ RSpec.describe PostsController do
},
)
end
+ let(:tag_only_revision) do
+ Fabricate(
+ :post_revision,
+ post: post,
+ modifications: {
+ "tags" => [%w[tag1 tag2], %w[tag2 tag3]],
+ },
+ )
+ end
+ let(:text_number_tags) do
+ Fabricate(
+ :post_revision,
+ post: post,
+ modifications: {
+ "tags" => [%w[123 tag1 tag2], %w[456 tag3 tag4]],
+ },
+ )
+ end
+ let(:legacy_string_tag_revision) do
+ Fabricate(
+ :post_revision,
+ post: post,
+ modifications: {
+ "tags" => ["tag1, tag2", "tag2, tag3"],
+ },
+ )
+ end
let(:post_id) { post.id }
let(:revision_id) { post_revision.number }
@@ -2465,6 +2492,36 @@ RSpec.describe PostsController do
put "/posts/#{post_id}/revisions/#{revision_id}/revert.json"
expect(response.status).to eq(200)
end
+
+ it "supports reverting tag-only revisions" do
+ post.topic.tags = Tag.where(name: %w[tag2 tag3])
+
+ put "/posts/#{post_id}/revisions/#{tag_only_revision.number}/revert.json"
+ expect(response.status).to eq(200)
+
+ post.topic.reload
+ expect(post.topic.tags.pluck(:name).sort).to eq(%w[tag1 tag2])
+ end
+
+ it "supports reverting text and number tags" do
+ post.topic.tags = Tag.where(name: %w[456 tag3 tag4])
+
+ put "/posts/#{post_id}/revisions/#{text_number_tags.number}/revert.json"
+ expect(response.status).to eq(200)
+
+ post.topic.reload
+ expect(post.topic.tags.pluck(:name).sort).to eq(%w[123 tag1 tag2])
+ end
+
+ it "supports reverting legacy string-format tags" do
+ post.topic.tags = Tag.where(name: ["tag2, tag3"])
+
+ put "/posts/#{post_id}/revisions/#{legacy_string_tag_revision.number}/revert.json"
+ expect(response.status).to eq(200)
+
+ post.topic.reload
+ expect(post.topic.tags.pluck(:name).sort).to eq(%w[tag1 tag2])
+ end
end
end