From 1a3ab7c02e812a89afbd941a68848dba0b604678 Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Fri, 17 Nov 2017 14:49:10 +0100 Subject: [PATCH] ignore some site settings for emails sent to mailinglist mirror category --- lib/email/receiver.rb | 22 ++++++++++++++++++---- spec/components/email/receiver_spec.rb | 19 +++++++++++++++++++ spec/fixtures/emails/mailinglist.eml | 12 ++++++++++++ spec/fixtures/emails/mailinglist_reply.eml | 16 ++++++++++++++++ 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 spec/fixtures/emails/mailinglist.eml create mode 100644 spec/fixtures/emails/mailinglist_reply.eml diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb index fb3eacabb52..82712eaefbf 100644 --- a/lib/email/receiver.rb +++ b/lib/email/receiver.rb @@ -105,7 +105,10 @@ module Email if is_auto_generated? @incoming_email.update_columns(is_auto_generated: true) - raise AutoGeneratedEmailError if SiteSetting.block_auto_generated_emails? + + if SiteSetting.block_auto_generated_emails? && !sent_to_mailinglist_mirror? + raise AutoGeneratedEmailError + end end if action = subscription_action_for(body, subject) @@ -374,9 +377,20 @@ module Email end def destinations - all_destinations + @destinations ||= all_destinations .map { |d| Email::Receiver.check_address(d) } - .drop_while(&:blank?) + .reject(&:blank?) + end + + def sent_to_mailinglist_mirror? + destinations.each do |destination| + next unless destination[:type] == :category + + category = destination[:obj] + return true if category.mailinglist_mirror? + end + + false end def self.check_address(address) @@ -573,7 +587,7 @@ module Email end def find_related_post - return if SiteSetting.find_related_post_with_key + return if SiteSetting.find_related_post_with_key && !sent_to_mailinglist_mirror? message_ids = [@mail.in_reply_to, Email::Receiver.extract_references(@mail.references)] message_ids.flatten! diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb index cf36f2d8e78..8b701a3a776 100644 --- a/spec/components/email/receiver_spec.rb +++ b/spec/components/email/receiver_spec.rb @@ -795,4 +795,23 @@ describe Email::Receiver do end end + context "mailinglist mirror" do + before do + SiteSetting.block_auto_generated_emails = true + SiteSetting.find_related_post_with_key = true + + Fabricate(:mailinglist_mirror_category) + end + + it "should allow creating topic even when email is autogenerated" do + expect { process(:mailinglist) }.to change { Topic.count } + end + + it "should allow replying without reply key" do + process(:mailinglist) + topic = Topic.last + + expect { process(:mailinglist_reply) }.to change { topic.posts.count } + end + end end diff --git a/spec/fixtures/emails/mailinglist.eml b/spec/fixtures/emails/mailinglist.eml new file mode 100644 index 00000000000..a9e7deba92e --- /dev/null +++ b/spec/fixtures/emails/mailinglist.eml @@ -0,0 +1,12 @@ +From: alice@foo.com +To: list@example.com +Subject: mailinglist mirror +Date: Mon, 16 Oct 2017 09:18:32 +0100 +Message-ID: +Precedence: list +List-Id: Example List +Content-Transfer-Encoding: 7bit +Content-Type: text/plain; charset="us-ascii" +Sender: list-bounces@example.com + +I love Discourse's mailinglist mirror feature. diff --git a/spec/fixtures/emails/mailinglist_reply.eml b/spec/fixtures/emails/mailinglist_reply.eml new file mode 100644 index 00000000000..9dd714b9665 --- /dev/null +++ b/spec/fixtures/emails/mailinglist_reply.eml @@ -0,0 +1,16 @@ +In-Reply-To: +References: +From: bob@bar.com +To: list@example.com +Subject: Re: mailinglist mirror +Date: Mon, 16 Oct 2017 09:41:26 +0100 +Message-ID: +Precedence: list +List-Id: Example List +Content-Transfer-Encoding: 7bit +Content-Type: text/plain; charset="us-ascii" +Sender: list-bounces@example.com + +> I love Discourse's mailinglist mirror feature. + +Me too!