Skip to content

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

FIX: Do not trigger post alerts for empty posts. (#7138)

Bianca Nenciu5114ef958a68
diff --git a/app/jobs/regular/post_alert.rb b/app/jobs/regular/post_alert.rb
index 6f5fc2523c9..4b09bb3152e 100644
--- a/app/jobs/regular/post_alert.rb
+++ b/app/jobs/regular/post_alert.rb
@@ -5,7 +5,7 @@ module Jobs
def execute(args)
post = Post.find_by(id: args[:post_id])
- if post&.topic
+ if post&.topic && post.raw.present?
opts = args[:options] || {}
new_record = true == args[:new_record]
PostAlerter.new(opts).after_save_post(post, new_record)
diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb
index c82aae8d14e..df2ace3ef1c 100644
--- a/spec/components/post_creator_spec.rb
+++ b/spec/components/post_creator_spec.rb
@@ -1141,6 +1141,31 @@ describe PostCreator do
post_creator = PostCreator.new(user, title: '', raw: '')
expect { post_creator.create! }.to raise_error(ActiveRecord::RecordNotSaved)
end
+
+ it "does not generate an alert for empty posts" do
+ Jobs.run_immediately!
+
+ user2 = Fabricate(:user)
+ topic = Fabricate(:private_message_topic,
+ topic_allowed_users: [
+ Fabricate.build(:topic_allowed_user, user: user),
+ Fabricate.build(:topic_allowed_user, user: user2)
+ ],
+ )
+ Fabricate(:topic_user,
+ topic: topic,
+ user: user2,
+ notification_level: TopicUser.notification_levels[:watching]
+ )
+
+ expect {
+ PostCreator.create!(user, raw: "", topic_id: topic.id, skip_validations: true)
+ }.to change { user2.notifications.count }.by(0)
+
+ expect {
+ PostCreator.create!(user, raw: "hello world", topic_id: topic.id, skip_validations: true)
+ }.to change { user2.notifications.count }.by(1)
+ end
end
context 'private message to a user that has disabled private messages' do