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

This commit is contained in:
Régis Hanol 2018-08-22 13:13:58 +02:00
parent 774e6bc795
commit f01169d6ff
3 changed files with 28 additions and 5 deletions

View File

@ -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

View File

@ -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

View File

@ -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: 'disc@ourse.org', 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: 'eviltrout@test.domain', body: 'test body'