FIX: don't invite users with emails configured as 'incoming' (reply, group our category)

This commit is contained in:
Régis Hanol 2016-01-20 23:08:27 +01:00
parent f145310cd5
commit 4a17cdc1e4
2 changed files with 22 additions and 8 deletions

View file

@ -204,6 +204,14 @@ module Email
.gsub(Regexp.escape("%{reply_key}"), "([[:xdigit:]]{32})") .gsub(Regexp.escape("%{reply_key}"), "([[:xdigit:]]{32})")
end 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 def find_related_post
message_ids = [@mail.in_reply_to, extract_references] message_ids = [@mail.in_reply_to, extract_references]
message_ids.flatten! message_ids.flatten!
@ -329,7 +337,7 @@ module Email
@mail[d].address_list.addresses.each do |address_field| @mail[d].address_list.addresses.each do |address_field|
begin begin
email = address_field.address.downcase email = address_field.address.downcase
if email !~ reply_by_email_address_regex if should_invite?(email)
user = find_or_create_user(address_field) user = find_or_create_user(address_field)
if can_invite?(topic, user) if can_invite?(topic, user)
topic.topic_allowed_users.create!(user_id: user.id) topic.topic_allowed_users.create!(user_id: user.id)
@ -344,6 +352,12 @@ module Email
end end
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) def can_invite?(topic, user)
!topic.topic_allowed_users.where(user_id: user.id).exists? && !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? !topic.topic_allowed_groups.where("group_id IN (SELECT group_id FROM group_users WHERE user_id = ?)", user.id).exists?

View file

@ -229,11 +229,11 @@ describe Email::Receiver do
expect(user.name).to eq("Случайная Имя") expect(user.name).to eq("Случайная Имя")
end 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) expect { process(:cc) }.to change(Topic, :count)
emails = Topic.last.allowed_users.pluck(:email) emails = Topic.last.allowed_users.pluck(:email)
expect(emails.size).to eq(4) expect(emails.size).to eq(3)
expect(emails).to include("someone@else.com", "discourse@bar.com", "team@bar.com", "wat@bar.com") expect(emails).to include("someone@else.com", "discourse@bar.com", "wat@bar.com")
end end
it "associates email replies using both 'In-Reply-To' and 'References' headers" do 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_2) }.to change { topic.posts.count }
expect { process(:email_reply_3) }.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 # - 3 of them are indeed "regular" posts generated from the emails
# - The 3 others are "small action" posts automatically added because # - The 2 others are "small action" posts automatically added because
# we invited 3 users (team@bar.com, two@foo.com and three@foo.com) # we invited 2 users (two@foo.com and three@foo.com)
expect(topic.posts.count).to eq(6) expect(topic.posts.count).to eq(5)
# trash all but the 1st post # trash all but the 1st post
topic.ordered_posts[1..-1].each(&:trash!) topic.ordered_posts[1..-1].each(&:trash!)