From 4a17cdc1e49288dff957a46ba516b7bd65be3ae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 20 Jan 2016 23:08:27 +0100 Subject: [PATCH] FIX: don't invite users with emails configured as 'incoming' (reply, group our category) --- lib/email/receiver.rb | 16 +++++++++++++++- spec/components/email/receiver_spec.rb | 14 +++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb index 52bc4b6ce36..65788fe005f 100644 --- a/lib/email/receiver.rb +++ b/lib/email/receiver.rb @@ -204,6 +204,14 @@ module Email .gsub(Regexp.escape("%{reply_key}"), "([[:xdigit:]]{32})") end + def group_incoming_emails_regex + @group_incoming_emails_regex ||= Regexp.union Group.pluck(:incoming_email).select(&:present?).uniq + end + + def category_email_in_regex + @category_email_in_regex ||= Regexp.union Category.pluck(:email_in).select(&:present?).uniq + end + def find_related_post message_ids = [@mail.in_reply_to, extract_references] message_ids.flatten! @@ -329,7 +337,7 @@ module Email @mail[d].address_list.addresses.each do |address_field| begin email = address_field.address.downcase - if email !~ reply_by_email_address_regex + if should_invite?(email) user = find_or_create_user(address_field) if can_invite?(topic, user) topic.topic_allowed_users.create!(user_id: user.id) @@ -344,6 +352,12 @@ module Email end end + def should_invite?(email) + email !~ reply_by_email_address_regex && + email !~ group_incoming_emails_regex && + email !~ category_email_in_regex + end + def can_invite?(topic, user) !topic.topic_allowed_users.where(user_id: user.id).exists? && !topic.topic_allowed_groups.where("group_id IN (SELECT group_id FROM group_users WHERE user_id = ?)", user.id).exists? diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb index 665a46b63d7..1296cb99154 100644 --- a/spec/components/email/receiver_spec.rb +++ b/spec/components/email/receiver_spec.rb @@ -229,11 +229,11 @@ describe Email::Receiver do expect(user.name).to eq("Случайная Имя") end - it "invites everyone in the chain but users whose email matches the 'reply_by_email_address'" do + it "invites everyone in the chain but emails configured as 'incoming' (via reply, group or category)" do expect { process(:cc) }.to change(Topic, :count) emails = Topic.last.allowed_users.pluck(:email) - expect(emails.size).to eq(4) - expect(emails).to include("someone@else.com", "discourse@bar.com", "team@bar.com", "wat@bar.com") + expect(emails.size).to eq(3) + expect(emails).to include("someone@else.com", "discourse@bar.com", "wat@bar.com") end it "associates email replies using both 'In-Reply-To' and 'References' headers" do @@ -244,11 +244,11 @@ describe Email::Receiver do expect { process(:email_reply_2) }.to change { topic.posts.count } expect { process(:email_reply_3) }.to change { topic.posts.count } - # Why 6 when we only processed 3 emails? + # Why 5 when we only processed 3 emails? # - 3 of them are indeed "regular" posts generated from the emails - # - The 3 others are "small action" posts automatically added because - # we invited 3 users (team@bar.com, two@foo.com and three@foo.com) - expect(topic.posts.count).to eq(6) + # - The 2 others are "small action" posts automatically added because + # we invited 2 users (two@foo.com and three@foo.com) + expect(topic.posts.count).to eq(5) # trash all but the 1st post topic.ordered_posts[1..-1].each(&:trash!)