FIX: Move check if user is suspended later (#14566)

FIX: Move check if user is suspended later (#14566)

Calling create_notification_alert could still send a notification to a suspended user. This just moves the check if user is suspended right before sending the notification.

diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb
index 27eafed..081fd70 100644
--- a/app/services/post_alerter.rb
+++ b/app/services/post_alerter.rb
@@ -14,6 +14,8 @@ class PostAlerter
   end
 
   def self.create_notification_alert(user:, post:, notification_type:, excerpt: nil, username: nil)
+    return if user.suspended?
+
     if post_url = post.url
       payload = {
        notification_type: notification_type,
@@ -502,7 +504,7 @@ class PostAlerter
       skip_send_email: skip_send_email
     )
 
-    if created.id && existing_notifications.empty? && NOTIFIABLE_TYPES.include?(type) && !user.suspended?
+    if created.id && existing_notifications.empty? && NOTIFIABLE_TYPES.include?(type)
       create_notification_alert(user: user, post: original_post, notification_type: type, username: original_username)
     end
 
diff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rb
index a0969fa..c9da511 100644
--- a/spec/services/post_alerter_spec.rb
+++ b/spec/services/post_alerter_spec.rb
@@ -848,6 +848,30 @@ describe PostAlerter do
     end
   end
 
+  describe "create_notification_alert" do
+    it "does not nothing for suspended users" do
+      evil_trout.update_columns(suspended_till: 1.year.from_now)
+      post = Fabricate(:post)
+
+      events = nil
+      messages = MessageBus.track_publish do
+        events = DiscourseEvent.track_events do
+          PostAlerter.create_notification_alert(
+            user: evil_trout,
+            post: post,
+            notification_type: Notification.types[:custom],
+            excerpt: "excerpt",
+            username: "username"
+          )
+        end
+      end
+
+      expect(events.size).to eq(0)
+      expect(messages.size).to eq(0)
+      expect(Jobs::PushNotification.jobs.size).to eq(0)
+    end
+  end
+
   describe "watching_first_post" do
     fab!(:group) { Fabricate(:group) }
     fab!(:user) { Fabricate(:user) }

GitHub sha: 8b99a7f73d942adeb8e4f39a1ffb62f059e68e6f

This commit appears in #14566 which was approved by CvX. It was merged by nbianca.