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.rbindex 698d2362935..e35f8b10d43 100644--- a/app/services/post_action_notifier.rb+++ b/app/services/post_action_notifier.rb@@ -161,14 +161,21 @@ class PostActionNotifierprivatedef 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++ falseendenddiff --git a/lib/post_revisor.rb b/lib/post_revisor.rbindex b65bf911962..c94d162014a 100644--- a/lib/post_revisor.rb+++ b/lib/post_revisor.rb@@ -68,7 +68,7 @@ class PostRevisorremoved_tags = prev_tags - persisted_tag_namesdiff_tags = added_tags | removed_tags- if diff_tags.present?+ if diff_tags.present? && !self.silentJobs.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.rbindex fdc050a6dbf..3264283495e 100644--- a/lib/topics_bulk_action.rb+++ b/lib/topics_bulk_action.rb@@ -251,75 +251,72 @@ class TopicsBulkActionenddef 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 }endendenddef 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 }endendend- 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_namesif @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]endend- 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,+ }enddef 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_datenew_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.rbindex a61e343e5b8..b619ad5b179 100644--- a/spec/lib/post_revisor_spec.rb+++ b/spec/lib/post_revisor_spec.rb@@ -1066,15 +1066,15 @@ describe PostRevisor doexpect(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)enddiff --git a/spec/lib/topics_bulk_action_spec.rb b/spec/lib/topics_bulk_action_spec.rbindex 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 doenddescribe "change_tags" do+ fab!(:first_post) { Fabricate(:post, topic:) }fab!(:tag1, :tag)fab!(:tag2, :tag)@@ -577,9 +578,30 @@ RSpec.describe TopicsBulkAction doexpect(topic.reload.tags).to contain_exactly(tag1, tag2)endend++ 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 }+ endenddescribe "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 doexpect(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" dotopic_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)endend@@ -651,17 +673,36 @@ RSpec.describe TopicsBulkAction doexpect(topic.reload.tags).to contain_exactly(tag1, tag2)endend++ 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 }+ endenddescribe "remove_tags" do+ fab!(:first_post) { Fabricate(:post, topic:) }fab!(:tag1, :tag)fab!(:tag2, :tag)before doSiteSetting.tagging_enabled = trueSiteSetting.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]endcontext "when the user can edit the topic" do@@ -683,8 +724,22 @@ RSpec.describe TopicsBulkAction dotopic_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)endend++ 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 }+ endendenddiff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 1da6ab3a158..350f0e1e0f4 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -4206,6 +4206,7 @@ RSpec.describe TopicsController doSiteSetting.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 doSiteSetting.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: {