FIX: create whisper post in PMs when bounces with verp and user is staged

This commit is contained in:
Vinoth Kannan 2018-11-28 08:24:23 +05:30
parent 654d7996ae
commit 7dbf709467
2 changed files with 49 additions and 31 deletions

View File

@ -196,13 +196,13 @@ module Email
end end
def is_bounce? def is_bounce?
@mail.bounced? || verp @mail.bounced? || bounce_key
end end
def handle_bounce def handle_bounce
@incoming_email.update_columns(is_bounce: true) @incoming_email.update_columns(is_bounce: true)
if verp && (bounce_key = verp[/\+verp-(\h{32})@/, 1]) && (email_log = EmailLog.find_by(bounce_key: bounce_key)) if bounce_key && (email_log = EmailLog.find_by(bounce_key: bounce_key))
email_log.update_columns(bounced: true) email_log.update_columns(bounced: true)
user = email_log.user user = email_log.user
email = user&.email.presence email = user&.email.presence
@ -229,8 +229,11 @@ module Email
Email::Receiver.reply_by_email_address_regex.match(@from_email) Email::Receiver.reply_by_email_address_regex.match(@from_email)
end end
def verp def bounce_key
@verp ||= all_destinations.select { |to| to[/\+verp-\h{32}@/] }.first @bounce_key ||= begin
verp = all_destinations.select { |to| to[/\+verp-\h{32}@/] }.first
verp && verp[/\+verp-(\h{32})@/, 1]
end
end end
def self.update_bounce_score(email, score) def self.update_bounce_score(email, score)
@ -555,7 +558,7 @@ module Email
def destinations def destinations
@destinations ||= all_destinations @destinations ||= all_destinations
.map { |d| Email::Receiver.check_address(d) } .map { |d| Email::Receiver.check_address(d, is_bounce?) }
.reject(&:blank?) .reject(&:blank?)
end end
@ -572,7 +575,7 @@ module Email
end end
end end
def self.check_address(address) def self.check_address(address, include_verp = false)
# only check for a group/category when 'email_in' is enabled # only check for a group/category when 'email_in' is enabled
if SiteSetting.email_in if SiteSetting.email_in
group = Group.find_by_email(address) group = Group.find_by_email(address)
@ -583,7 +586,7 @@ module Email
end end
# reply # reply
match = Email::Receiver.reply_by_email_address_regex.match(address) match = Email::Receiver.reply_by_email_address_regex(true, include_verp).match(address)
if match && match.captures if match && match.captures
match.captures.each do |c| match.captures.each do |c|
next if c.blank? next if c.blank?
@ -785,10 +788,14 @@ module Email
true true
end end
def self.reply_by_email_address_regex(extract_reply_key = true) def self.reply_by_email_address_regex(extract_reply_key = true, include_verp = false)
reply_addresses = [SiteSetting.reply_by_email_address] reply_addresses = [SiteSetting.reply_by_email_address]
reply_addresses << (SiteSetting.alternative_reply_by_email_addresses.presence || "").split("|") reply_addresses << (SiteSetting.alternative_reply_by_email_addresses.presence || "").split("|")
if include_verp && SiteSetting.reply_by_email_address.present? && SiteSetting.reply_by_email_address["+"]
reply_addresses << SiteSetting.reply_by_email_address.sub("%{reply_key}", "verp-%{reply_key}")
end
reply_addresses.flatten! reply_addresses.flatten!
reply_addresses.select!(&:present?) reply_addresses.select!(&:present?)
reply_addresses.map! { |a| Regexp.escape(a) } reply_addresses.map! { |a| Regexp.escape(a) }
@ -1028,7 +1035,7 @@ module Email
if result.post if result.post
@incoming_email.update_columns(topic_id: result.post.topic_id, post_id: result.post.id) @incoming_email.update_columns(topic_id: result.post.topic_id, post_id: result.post.id)
if result.post.topic && result.post.topic.private_message? if result.post.topic&.private_message? && !is_bounce?
add_other_addresses(result.post, user) add_other_addresses(result.post, user)
end end
end end

View File

@ -112,33 +112,44 @@ describe Email::Receiver do
expect(IncomingEmail.last.is_bounce).to eq(true) expect(IncomingEmail.last.is_bounce).to eq(true)
end end
it "creates a whisper post in PM if user is staged" do describe "creating whisper post in PMs for staged users" do
SiteSetting.enable_staged_users = true let(:email_address) { "linux-admin@b-s-c.co.jp" }
SiteSetting.enable_whispers = true
email = "linux-admin@b-s-c.co.jp" before do
user = Fabricate(:staged, email: email) SiteSetting.enable_staged_users = true
SiteSetting.enable_whispers = true
end
private_message = Fabricate(:topic, def create_post_reply_key(value)
archetype: 'private_message', user = Fabricate(:staged, email: email_address)
category_id: nil, pm = Fabricate(:topic, archetype: 'private_message', category_id: nil, user: user, allowed_users: [user])
user: user, Fabricate(:post_reply_key,
allowed_users: [user] reply_key: value,
) user: user,
post: create_post(topic: pm, user: user)
)
end
post = create_post(topic: private_message, user: user) it "when bounce without verp" do
create_post_reply_key("4f97315cc828096c9cb34c6f1a0d6fe8")
post_reply_key = Fabricate(:post_reply_key, expect { process(:bounced_email) }.to raise_error(Email::Receiver::BouncedEmailError)
reply_key: "4f97315cc828096c9cb34c6f1a0d6fe8", post = Post.last
user: user, expect(post.whisper?).to eq(true)
post: post expect(post.raw).to eq(I18n.t("system_messages.email_bounced", email: email_address, raw: "Your email bounced").strip)
) expect(IncomingEmail.last.is_bounce).to eq(true)
end
expect { process(:bounced_email) }.to raise_error(Email::Receiver::BouncedEmailError) it "when bounce without verp" do
post = Post.last SiteSetting.reply_by_email_address = "foo+%{reply_key}@discourse.org"
expect(post.whisper?).to eq(true) create_post_reply_key("14b08c855160d67f2e0c2f8ef36e251e")
expect(post.raw).to eq(I18n.t("system_messages.email_bounced", email: email, raw: "Your email bounced").strip)
expect(IncomingEmail.last.is_bounce).to eq(true) expect { process(:hard_bounce_via_verp) }.to raise_error(Email::Receiver::BouncedEmailError)
post = Post.last
expect(post.whisper?).to eq(true)
expect(post.raw).to eq(I18n.t("system_messages.email_bounced", email: email_address, raw: "Your email bounced").strip)
expect(IncomingEmail.last.is_bounce).to eq(true)
end
end end
end end