Skip to content

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

DEV: route bulk tag actions through PostRevisor (#37848)

Régis Hanold818ef3c7303
diff --git a/app/services/post_action_notifier.rb b/app/services/post_action_notifier.rb
index 698d2362935..e35f8b10d43 100644
--- a/app/services/post_action_notifier.rb
+++ b/app/services/post_action_notifier.rb
@@ -161,14 +161,21 @@ class PostActionNotifier
private
def self.notification_is_disabled?(post_revision)
+ if SiteSetting.disable_system_edit_notifications &&
+ post_revision.user_id == Discourse::SYSTEM_USER_ID
+ return true
+ end
+
modifications = post_revision.modifications
- (
- SiteSetting.disable_system_edit_notifications &&
- post_revision.user_id == Discourse::SYSTEM_USER_ID
- ) ||
- (
- SiteSetting.disable_category_edit_notifications &&
- modifications&.dig("category_id").present?
- ) || (SiteSetting.disable_tags_edit_notifications && modifications&.dig("tags").present?)
+
+ if SiteSetting.disable_category_edit_notifications && modifications&.dig("category_id").present?
+ return true
+ end
+
+ if SiteSetting.disable_tags_edit_notifications && modifications&.dig("tags").present?
+ return true
+ end
+
+ false
end
end
diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb
index b65bf911962..c94d162014a 100644
--- a/lib/post_revisor.rb
+++ b/lib/post_revisor.rb
@@ -68,7 +68,7 @@ class PostRevisor
removed_tags = prev_tags - persisted_tag_names
diff_tags = added_tags | removed_tags
- if diff_tags.present?
+ if diff_tags.present? && !self.silent
Jobs.enqueue(:notify_tag_change, post_id: post.id, notified_user_ids:, diff_tags:)
PostRevisor.create_small_action_for_tag_changes(
diff --git a/lib/topics_bulk_action.rb b/lib/topics_bulk_action.rb
index fdc050a6dbf..3264283495e 100644
--- a/lib/topics_bulk_action.rb
+++ b/lib/topics_bulk_action.rb
@@ -251,75 +251,72 @@ class TopicsBulkAction
end
def change_tags
- tag_ids, tag_names = extract_tag_params
+ tags = resolve_tag_names || []
- topics.each do |t|
- if guardian.can_edit?(t)
- success =
- if tag_ids.present?
- DiscourseTagging.tag_topic_by_ids(t, guardian, tag_ids)
- elsif tag_names.present?
- DiscourseTagging.tag_topic_by_names(t, guardian, tag_names)
- else
- t.tags = []
- true
- end
-
- if success
- @changed_ids << t.id
- else
- t.errors.full_messages.each { |msg| @errors[msg] += 1 }
- end
+ topics_with_tags.each do |t|
+ next unless guardian.can_edit?(t)
+ next unless t.first_post
+
+ if t.first_post.revise(@user, { tags: tags }, bulk_tag_opts)
+ @changed_ids << t.id
+ else
+ t.errors.full_messages.each { |msg| @errors[msg] += 1 }
end
end
end
def append_tags
- tag_ids, tag_names = extract_tag_params
+ tags = resolve_tag_names || []
- topics.each do |t|
- if guardian.can_edit?(t)
- success =
- if tag_ids.present?
- DiscourseTagging.tag_topic_by_ids(t, guardian, tag_ids, append: true)
- elsif tag_names.present?
- DiscourseTagging.tag_topic_by_names(t, guardian, tag_names, append: true)
- else
- true
- end
-
- if success
- @changed_ids << t.id
- else
- t.errors.full_messages.each { |msg| @errors[msg] += 1 }
- end
+ return if tags.blank?
+
+ topics_with_tags.each do |t|
+ next unless guardian.can_edit?(t)
+ next unless t.first_post
+
+ merged = t.tags.map(&:name) | tags
+ if t.first_post.revise(@user, { tags: merged }, bulk_tag_opts)
+ @changed_ids << t.id
+ else
+ t.errors.full_messages.each { |msg| @errors[msg] += 1 }
end
end
end
- def extract_tag_params
+ def remove_tags
+ topics_with_tags.each do |t|
+ next unless guardian.can_edit?(t)
+ next unless t.first_post
+
+ if t.first_post.revise(@user, { tags: [] }, bulk_tag_opts)
+ @changed_ids << t.id
+ else
+ t.errors.full_messages.each { |msg| @errors[msg] += 1 }
+ end
+ end
+ end
+
+ def resolve_tag_names
if @operation[:tag_ids].present?
- [@operation[:tag_ids], nil]
+ Tag.where(id: @operation[:tag_ids]).pluck(:name)
elsif @operation[:tags].present?
Discourse.deprecate(
"the tags param for bulk actions is deprecated, use tag_ids instead",
since: "2026.01",
drop_from: "2026.07",
)
- tag_names = @operation[:tags]
- [nil, tag_names]
- else
- [nil, nil]
+ @operation[:tags]
end
end
- def remove_tags
- topics.each do |t|
- if guardian.can_edit?(t)
- TopicTag.where(topic_id: t.id).in_batches.destroy_all
- @changed_ids << t.id
- end
- end
+ def bulk_tag_opts
+ {
+ bypass_bump: true,
+ validate_post: false,
+ bypass_rate_limiter: true,
+ skip_revision: true,
+ silent: true,
+ }
end
def guardian
@@ -330,6 +327,10 @@ class TopicsBulkAction
@topics ||= Topic.where(id: @topic_ids)
end
+ def topics_with_tags
+ @topics_with_tags ||= topics.includes(:first_post, :tags)
+ end
+
def dismiss_topics_since_date
new_topic_duration_minutes =
@user.user_option&.new_topic_duration_minutes ||
diff --git a/spec/lib/post_revisor_spec.rb b/spec/lib/post_revisor_spec.rb
index a61e343e5b8..b619ad5b179 100644
--- a/spec/lib/post_revisor_spec.rb
+++ b/spec/lib/post_revisor_spec.rb
@@ -1066,15 +1066,15 @@ describe PostRevisor do
expect(post_revisor.raw_changed?).to eq(false)
post_revisor.revise!(admin, tags: %w[new-tag new-tag-2])
- expect(post.post_revisions.last.modifications).to eq(
- "tags" => [["new-tag"], %w[new-tag new-tag-2]],
- )
+ before, after = post.post_revisions.last.modifications["tags"]
+ expect(before).to contain_exactly("new-tag")
+ expect(after).to contain_exactly("new-tag", "new-tag-2")
expect(post_revisor.raw_changed?).to eq(false)
post_revisor.revise!(admin, tags: ["new-tag-3"])
- expect(post.post_revisions.last.modifications).to eq(
- "tags" => [%w[new-tag new-tag-2], ["new-tag-3"]],
- )
+ before, after = post.post_revisions.last.modifications["tags"]
+ expect(before).to contain_exactly("new-tag", "new-tag-2")
+ expect(after).to contain_exactly("new-tag-3")
expect(post_revisor.raw_changed?).to eq(false)
end
diff --git a/spec/lib/topics_bulk_action_spec.rb b/spec/lib/topics_bulk_action_spec.rb
index 43d91464372..43ea3e418e5 100644
--- a/spec/lib/topics_bulk_action_spec.rb
+++ b/spec/lib/topics_bulk_action_spec.rb
@@ -504,6 +504,7 @@ RSpec.describe TopicsBulkAction do
end
describe "change_tags" do
+ fab!(:first_post) { Fabricate(:post, topic:) }
fab!(:tag1, :tag)
fab!(:tag2, :tag)
@@ -577,9 +578,30 @@ RSpec.describe TopicsBulkAction do
expect(topic.reload.tags).to contain_exactly(tag1, tag2)
end
end
+
+ it "does not send notifications" do
+ fab_tag3 = Fabricate(:tag)
+ topic_watcher = Fabricate(:user)
+ Jobs.run_immediately!
+ TopicUser.change(
+ topic_watcher,
+ topic.id,
+ notification_level: TopicUser.notification_levels[:watching],
+ )
+
+ expect do
+ TopicsBulkAction.new(
+ Fabricate(:admin),
+ [topic.id],
+ type: "change_tags",
+ tag_ids: [fab_tag3.id],
+ ).perform!
+ end.to not_change { Notification.where(user: topic_watcher).count }
+ end
end
describe "append_tags" do
+ fab!(:first_post) { Fabricate(:post, topic:) }
fab!(:tag1, :tag)
fab!(:tag2, :tag)
fab!(:tag3, :tag)
@@ -604,11 +626,11 @@ RSpec.describe TopicsBulkAction do
expect(topic.reload.tags).to contain_exactly(tag1, tag2, tag3)
end
- it "keeps existing tags when appending empty array" do
+ it "is a no-op when appending empty array" do
topic_ids =
TopicsBulkAction.new(topic.user, [topic.id], type: "append_tags", tag_ids: []).perform!
- expect(topic_ids).to eq([topic.id])
+ expect(topic_ids).to eq([])
expect(topic.reload.tags).to contain_exactly(tag1, tag2)
end
end
@@ -651,17 +673,36 @@ RSpec.describe TopicsBulkAction do
expect(topic.reload.tags).to contain_exactly(tag1, tag2)
end
end
+
+ it "does not send notifications" do
+ topic_watcher = Fabricate(:user)
+ Jobs.run_immediately!
+ TopicUser.change(
+ topic_watcher,
+ topic.id,
+ notification_level: TopicUser.notification_levels[:watching],
+ )
+
+ expect do
+ TopicsBulkAction.new(
+ Fabricate(:admin),
+ [topic.id],
+ type: "append_tags",
+ tag_ids: [tag3.id],
+ ).perform!
+ end.to not_change { Notification.where(user: topic_watcher).count }
+ end
end
describe "remove_tags" do
+ fab!(:first_post) { Fabricate(:post, topic:) }
fab!(:tag1, :tag)
fab!(:tag2, :tag)
before do
SiteSetting.tagging_enabled = true
SiteSetting.tag_topic_allowed_groups = Group::AUTO_GROUPS[:trust_level_0]
- TopicTag.create!(topic: topic, tag: tag1)
- TopicTag.create!(topic: topic, tag: tag2)
+ topic.tags = [tag1, tag2]
end
context "when the user can edit the topic" do
@@ -683,8 +724,22 @@ RSpec.describe TopicsBulkAction do
topic_ids = TopicsBulkAction.new(topic.user, [topic.id], type: "remove_tags").perform!
expect(topic_ids).to eq([])
- expect(topic.reload.tags.map(&:name)).to contain_exactly(tag1.name, tag2.name)
+ expect(topic.reload.tags).to contain_exactly(tag1, tag2)
end
end
+
+ it "does not send notifications" do
+ topic_watcher = Fabricate(:user)
+ Jobs.run_immediately!
+ TopicUser.change(
+ topic_watcher,
+ topic.id,
+ notification_level: TopicUser.notification_levels[:watching],
+ )
+
+ expect do
+ TopicsBulkAction.new(Fabricate(:admin), [topic.id], type: "remove_tags").perform!
+ end.to not_change { Notification.where(user: topic_watcher).count }
+ end
end
end
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 1da6ab3a158..350f0e1e0f4 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -4206,6 +4206,7 @@ RSpec.describe TopicsController do
SiteSetting.tag_topic_allowed_groups = Group::AUTO_GROUPS[:trust_level_0]
tag1 = Fabricate(:tag)
topic.update!(user:)
+ _first_post = Fabricate(:post, topic:, user:)
put "/topics/bulk.json",
params: {
@@ -4225,6 +4226,7 @@ RSpec.describe TopicsController do
SiteSetting.tag_topic_allowed_groups = Group::AUTO_GROUPS[:trust_level_0]
tag1 = Fabricate(:tag)
topic.update!(user:)
+ _first_post = Fabricate(:post, topic:, user:)
put "/topics/bulk.json",
params: {