Skip to content

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

FIX: don't send email when the post was deleted

Régis Hanolf01169d6ffaf
diff --git a/app/models/skipped_email_log.rb b/app/models/skipped_email_log.rb
index dea77b69327..c7e1da07deb 100644
--- a/app/models/skipped_email_log.rb
+++ b/app/models/skipped_email_log.rb
@@ -31,7 +31,8 @@ class SkippedEmailLog < ActiveRecord::Base
sender_message_blank: 16,
sender_message_to_blank: 17,
sender_text_part_body_blank: 18,
- sender_body_blank: 19
+ sender_body_blank: 19,
+ sender_post_deleted: 20
)
end
diff --git a/lib/email/sender.rb b/lib/email/sender.rb
index f85760089eb..46d0ca84e82 100644
--- a/lib/email/sender.rb
+++ b/lib/email/sender.rb
@@ -87,6 +87,10 @@ module Email
if topic_id.present? && post_id.present?
post = Post.find_by(id: post_id, topic_id: topic_id)
+
+ # guards against deleted posts
+ return skip(SkippedEmailLog.reason_types[:sender_post_deleted]) unless post
+
topic = post.topic
first_post = topic.ordered_posts.first
@@ -124,7 +128,7 @@ module Email
end
# https://www.ietf.org/rfc/rfc2919.txt
- if topic && topic.category && !topic.category.uncategorized?
+ if topic&.category && !topic.category.uncategorized?
list_id = "#{SiteSetting.title} | #{topic.category.name} <#{topic.category.name.downcase.tr(' ', '-')}.#{host}>"
# subcategory case
@@ -166,8 +170,8 @@ module Email
email_log.post_id = post_id if post_id.present?
# Remove headers we don't need anymore
- @message.header['X-Discourse-Topic-Id'] = nil if topic_id.present?
- @message.header['X-Discourse-Post-Id'] = nil if post_id.present?
+ @message.header['X-Discourse-Topic-Id'] = nil if topic_id.present?
+ @message.header['X-Discourse-Post-Id'] = nil if post_id.present?
if reply_key.present?
@message.header[Email::MessageBuilder::ALLOW_REPLY_BY_EMAIL_HEADER] = nil
@@ -199,7 +203,6 @@ module Email
return skip(SkippedEmailLog.reason_types[:custom], custom_reason: e.message)
end
- # Save and return the email log
email_log.save!
email_log
end
diff --git a/spec/components/email/sender_spec.rb b/spec/components/email/sender_spec.rb
index 086afb07134..321b2690a17 100644
--- a/spec/components/email/sender_spec.rb
+++ b/spec/components/email/sender_spec.rb
@@ -333,6 +333,25 @@ describe Email::Sender do
end
end
+ context 'with a deleted post' do
+
+ it 'should skip sending the email' do
+ post = Fabricate(:post, deleted_at: 1.day.ago)
+
+ message = Mail::Message.new to: '[email protected]', body: 'some content'
+ message.header['X-Discourse-Post-Id'] = post.id
+ message.header['X-Discourse-Topic-Id'] = post.topic_id
+ message.expects(:deliver_now).never
+
+ email_sender = Email::Sender.new(message, :valid_type)
+ expect { email_sender.send }.to change { SkippedEmailLog.count }
+
+ log = SkippedEmailLog.last
+ expect(log.reason_type).to eq(SkippedEmailLog.reason_types[:sender_post_deleted])
+ end
+
+ end
+
context 'with a user' do
let(:message) do
message = Mail::Message.new to: '[email protected]', body: 'test body'