Skip to content

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

FIX: Suppress post revision notifications during silent bulk category moves (#37832)

Régis Hanolaf3cca4462d5
diff --git a/app/models/post_revision.rb b/app/models/post_revision.rb
index 921267d3a08..9c9c32363c3 100644
--- a/app/models/post_revision.rb
+++ b/app/models/post_revision.rb
@@ -4,6 +4,8 @@ class PostRevision < ActiveRecord::Base
belongs_to :post
belongs_to :user
+ attr_accessor :silent
+
serialize :modifications, type: Hash, coder: YAML
after_create :create_notification
diff --git a/app/services/post_action_notifier.rb b/app/services/post_action_notifier.rb
index c90fa520349..698d2362935 100644
--- a/app/services/post_action_notifier.rb
+++ b/app/services/post_action_notifier.rb
@@ -105,6 +105,7 @@ class PostActionNotifier
return if post_revision.user.blank?
return if post.topic.blank?
return if post.topic.private_message?
+ return if post_revision.silent
return if notification_is_disabled?(post_revision)
user_ids = []
diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb
index ae85f470b05..1f4ef6e5b5f 100644
--- a/lib/post_revisor.rb
+++ b/lib/post_revisor.rb
@@ -625,13 +625,15 @@ class PostRevisor
end
@post_revision =
- PostRevision.create!(
+ PostRevision.new(
user_id: @post.last_editor_id,
post_id: @post.id,
number: @post.version,
modifications:,
hidden: @hidden_revision,
)
+ @post_revision.silent = @silent
+ @post_revision.save!
end
def update_revision
diff --git a/spec/lib/topics_bulk_action_spec.rb b/spec/lib/topics_bulk_action_spec.rb
index 9787eef2dc8..43d91464372 100644
--- a/spec/lib/topics_bulk_action_spec.rb
+++ b/spec/lib/topics_bulk_action_spec.rb
@@ -160,84 +160,45 @@ RSpec.describe TopicsBulkAction do
fab!(:first_post) { Fabricate(:post, topic: topic) }
describe "silent option" do
- fab!(:topic_watcher, :user)
- fab!(:category_watcher, :user)
fab!(:admin)
before do
Jobs.run_immediately!
+ PostActionNotifier.enable
+ SiteSetting.create_revision_on_bulk_topic_moves = true
TopicUser.change(
- topic_watcher,
+ Fabricate(:user),
topic.id,
notification_level: TopicUser.notification_levels[:watching],
)
CategoryUser.set_notification_level_for_category(
- category_watcher,
+ Fabricate(:user),
CategoryUser.notification_levels[:watching_first_post],
category.id,
)
end
- shared_examples "silent option suppresses notifications" do
- it "notifies topic watchers when silent is false" do
- expect do
- TopicsBulkAction.new(
- admin,
- [topic.id],
- type: "change_category",
- category_id: category.id,
- ).perform!
- end.to change { Notification.where(user: topic_watcher).count }
- end
-
- it "does not notify topic watchers when silent is true" do
- expect do
- TopicsBulkAction.new(
- admin,
- [topic.id],
- type: "change_category",
- category_id: category.id,
- silent: true,
- ).perform!
- end.to not_change { Notification.where(user: topic_watcher).count }
- end
-
- it "notifies category watchers when silent is false" do
- expect do
- TopicsBulkAction.new(
- admin,
- [topic.id],
- type: "change_category",
- category_id: category.id,
- ).perform!
- end.to change { Notification.where(user: category_watcher).count }.by(1)
-
- expect(Notification.where(user: category_watcher).last.notification_type).to eq(
- Notification.types[:watching_first_post],
- )
- end
-
- it "does not notify category watchers when silent is true" do
- expect do
- TopicsBulkAction.new(
- admin,
- [topic.id],
- type: "change_category",
- category_id: category.id,
- silent: true,
- ).perform!
- end.to not_change { Notification.where(user: category_watcher).count }
- end
- end
-
- context "when create_revision_on_bulk_topic_moves is enabled" do
- before { SiteSetting.create_revision_on_bulk_topic_moves = true }
- include_examples "silent option suppresses notifications"
+ it "does not create any notifications when silent is true" do
+ expect do
+ TopicsBulkAction.new(
+ admin,
+ [topic.id],
+ type: "change_category",
+ category_id: category.id,
+ silent: true,
+ ).perform!
+ end.to not_change { Notification.count }
end
- context "when create_revision_on_bulk_topic_moves is disabled" do
- before { SiteSetting.create_revision_on_bulk_topic_moves = false }
- include_examples "silent option suppresses notifications"
+ it "creates notifications when silent is false" do
+ expect do
+ TopicsBulkAction.new(
+ admin,
+ [topic.id],
+ type: "change_category",
+ category_id: category.id,
+ ).perform!
+ end.to change { Notification.count }
end
end