FIX: use MD5 of the email_string when there's no 'Message-Id'

This commit is contained in:
Régis Hanol 2016-03-14 18:18:58 +01:00
parent 1ba65765e4
commit 4a3cb4a000
5 changed files with 13 additions and 20 deletions

View file

@ -40,7 +40,6 @@ module Jobs
message_template = case e
when Email::Receiver::EmptyEmailError then :email_reject_empty
when Email::Receiver::NoBodyDetectedError then :email_reject_empty
when Email::Receiver::NoMessageIdError then :email_reject_no_message_id
when Email::Receiver::AutoGeneratedEmailError then :email_reject_auto_generated
when Email::Receiver::InactiveUserError then :email_reject_inactive_user
when Email::Receiver::BlockedUserError then :email_reject_blocked_user

View file

@ -1837,13 +1837,6 @@ en:
Your reply was sent from a different email address than the one we expected, so we're not sure if this is the same person. Try sending from another email address, or contact a staff member.
email_reject_no_message_id:
subject_template: "[%{site_name}] Email issue -- No Message Id"
text_body_template: |
We're sorry, but your email message to %{destination} (titled %{former_title}) didn't work.
We couldn't find a `Message-Id` header in the email. Try sending from a different email address, or contact a staff member.
email_reject_no_account:
subject_template: "[%{site_name}] Email issue -- Unknown Account"
text_body_template: |

View file

@ -1,3 +1,4 @@
require "digest"
require_dependency "new_post_manager"
require_dependency "post_action_creator"
require_dependency "email/html_cleaner"
@ -9,7 +10,6 @@ module Email
class ProcessingError < StandardError; end
class EmptyEmailError < ProcessingError; end
class NoMessageIdError < ProcessingError; end
class AutoGeneratedEmailError < ProcessingError; end
class NoBodyDetectedError < ProcessingError; end
class InactiveUserError < ProcessingError; end
@ -29,7 +29,7 @@ module Email
raise EmptyEmailError if mail_string.blank?
@raw_email = mail_string
@mail = Mail.new(@raw_email)
raise NoMessageIdError if @mail.message_id.blank?
@message_id = @mail.message_id.presence || Digest::MD5.hexdigest(mail_string)
end
def process!
@ -42,7 +42,7 @@ module Email
end
def find_or_create_incoming_email
IncomingEmail.find_or_create_by(message_id: @mail.message_id) do |ie|
IncomingEmail.find_or_create_by(message_id: @message_id) do |ie|
ie.raw = @raw_email
ie.subject = subject
ie.from_address = @from_email

View file

@ -21,14 +21,6 @@ describe Email::Receiver do
expect { Email::Receiver.new("") }.to raise_error(Email::Receiver::EmptyEmailError)
end
it "raises an NoMessageIdError when 'mail_string' is not an email" do
expect { Email::Receiver.new("wat") }.to raise_error(Email::Receiver::NoMessageIdError)
end
it "raises an NoMessageIdError when 'mail_string' is missing the message_id" do
expect { Email::Receiver.new(email(:missing_message_id)) }.to raise_error(Email::Receiver::NoMessageIdError)
end
it "raises an AutoGeneratedEmailError when the mail is auto generated" do
expect { process(:auto_generated_precedence) }.to raise_error(Email::Receiver::AutoGeneratedEmailError)
expect { process(:auto_generated_header) }.to raise_error(Email::Receiver::AutoGeneratedEmailError)
@ -65,6 +57,12 @@ describe Email::Receiver do
let(:post) { create_post(topic: topic, user: user) }
let!(:email_log) { Fabricate(:email_log, reply_key: reply_key, user: user, topic: topic, post: post) }
it "uses MD5 of 'mail_string' there is no message_id" do
mail_string = email(:missing_message_id)
expect { Email::Receiver.new(mail_string).process! }.to change { IncomingEmail.count }
expect(IncomingEmail.last.message_id).to eq(Digest::MD5.hexdigest(mail_string))
end
it "raises a ReplyUserNotMatchingError when the email address isn't matching the one we sent the notification to" do
expect { process(:reply_user_not_matching) }.to raise_error(Email::Receiver::ReplyUserNotMatchingError)
end

View file

@ -1,5 +1,8 @@
From: Foo Bar <foo@bar.com>
From: Foo Bar <discourse@bar.com>
To: reply+4f97315cc828096c9cb34c6f1a0d6fe8@bar.com
Date: Fri, 15 Jan 2016 00:12:43 +0100
Mime-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
Some body