DEV: create bounce alert earlier if email_log detected from bounce_key

DEV: create bounce alert earlier if email_log detected from bounce_key

From bfb3c4d9f94cbc37aaf40a7c4c0824046b2552e1 Mon Sep 17 00:00:00 2001
From: Vinoth Kannan <vinothkannan@vinkas.com>
Date: Wed, 28 Nov 2018 21:13:06 +0530
Subject: [PATCH] DEV: create bounce alert earlier if email_log detected from
 bounce_key


diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb
index 810afa2..589d279 100644
--- a/lib/email/receiver.rb
+++ b/lib/email/receiver.rb
@@ -116,8 +116,6 @@ module Email
       raise FromReplyByAddressError if is_from_reply_by_email_address?
       raise ScreenedEmailError if ScreenedEmail.should_block?(@from_email)
 
-      hidden_reason_id = is_spam? ? Post.hidden_reasons[:email_spam_header_found] : nil
-
       user = @from_user
 
       if user.present?
@@ -188,6 +186,10 @@ module Email
       end
     end
 
+    def hidden_reason_id
+      @hidden_reason_id ||= is_spam? ? Post.hidden_reasons[:email_spam_header_found] : nil
+    end
+
     def log_and_validate_user(user)
       @incoming_email.update_columns(user_id: user.id)
 
@@ -204,6 +206,7 @@ module Email
 
       if email_log.present?
         email_log.update_columns(bounced: true)
+        post = email_log.post
         topic = email_log.topic
       end
 
@@ -213,9 +216,23 @@ module Email
         Email::Receiver.update_bounce_score(@from_email, SiteSetting.hard_bounce_score)
       end
 
-      return if SiteSetting.enable_whispers? &&
-                @from_user&.staged? &&
-                (topic.blank? || topic.archetype == Archetype.private_message)
+      if SiteSetting.enable_whispers? && @from_user&.staged?
+        return if email_log.blank?
+
+        if post.present? && topic.present? && topic.archetype == Archetype.private_message
+          body, elided = select_body
+          body ||= ""
+
+          create_reply(user: @from_user,
+                       raw: body,
+                       elided: elided,
+                       hidden_reason_id: hidden_reason_id,
+                       post: post,
+                       topic: topic,
+                       skip_validations: true,
+                       bounce: true)
+        end
+      end
 
       raise BouncedEmailError
     end
diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb
index a6e0225..50ab064 100644
--- a/spec/components/email/receiver_spec.rb
+++ b/spec/components/email/receiver_spec.rb
@@ -116,6 +116,8 @@ describe Email::Receiver do
       let(:email_address) { "linux-admin@b-s-c.co.jp" }
       let(:user1) { user1 = Fabricate(:user) }
       let(:user2) { user2 = Fabricate(:staged, email: email_address) }
+      let(:topic) { Fabricate(:topic, archetype: 'private_message', category_id: nil, user: user1, allowed_users: [user1, user2]) }
+      let(:post) { create_post(topic: topic, user: user1) }
 
       before do
         SiteSetting.enable_staged_users = true
@@ -123,11 +125,10 @@ describe Email::Receiver do
       end
 
       def create_post_reply_key(value)
-        pm = Fabricate(:topic, archetype: 'private_message', category_id: nil, user: user1, allowed_users: [user1, user2])
         Fabricate(:post_reply_key,
           reply_key: value,
           user: user2,
-          post: create_post(topic: pm, user: user1)
+          post: post
         )
       end
 
@@ -145,7 +146,7 @@ describe Email::Receiver do
         SiteSetting.reply_by_email_address = "foo+%{reply_key}@discourse.org"
         bounce_key = "14b08c855160d67f2e0c2f8ef36e251e"
         create_post_reply_key(bounce_key)
-        Fabricate(:email_log, to_address: email_address, user: user2, bounce_key: bounce_key)
+        Fabricate(:email_log, to_address: email_address, user: user2, bounce_key: bounce_key, post: post)
 
         expect { process(:hard_bounce_via_verp) }.to raise_error(Email::Receiver::BouncedEmailError)
         post = Post.last

GitHub