FIX: Use correct group out of multiple for SMTP sender (#14957)

When there are multiple groups on a topic, we were selecting
the first from the topic allowed groups to act as the sender
email address when sending group SMTP replies via PostAlerter.
However, this was not ordered, and since there is no created_at
column on TopicAllowedGroup we cannot order this nicely, which
caused just a random group to be used (based on whatever postgres
decided it felt like that morning).

This commit changes the group used for SMTP sending to be the
group using the email_username of the to address of the first
incoming email for the topic, if there are more than one allowed
groups on the topic. Otherwise it just uses the only SMTP enabled
group.
This commit is contained in:
Martin Brennan 2021-11-16 10:21:49 +10:00 committed by GitHub
parent f4d1fe18f8
commit 31035010af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 1 deletions

View File

@ -1777,6 +1777,10 @@ class Topic < ActiveRecord::Base
end
end
def first_smtp_enabled_group
self.allowed_groups.where(smtp_enabled: true).first
end
private
def invite_to_private_message(invited_by, target_user, guardian)

View File

@ -640,7 +640,15 @@ class PostAlerter
def group_notifying_via_smtp(post)
return nil if !SiteSetting.enable_smtp || post.post_type != Post.types[:regular]
post.topic.allowed_groups.where(smtp_enabled: true).first
if post.topic.allowed_groups.count == 1
return post.topic.first_smtp_enabled_group
end
group = Group.find_by_email(post.topic.incoming_email.first.to_addresses)
if !group&.smtp_enabled
return post.topic.first_smtp_enabled_group
end
group
end
def email_using_group_smtp_if_configured(post)

View File

@ -1374,6 +1374,23 @@ describe PostAlerter do
expect(email.subject).to eq("Re: #{topic.title}")
end
it "sends a group smtp email when the original group has had SMTP disabled and there is an additional topic allowed group" do
incoming_email_post = create_post_with_incoming
topic = incoming_email_post.topic
other_allowed_group = Fabricate(:smtp_group)
TopicAllowedGroup.create(group: other_allowed_group, topic: topic)
post = Fabricate(:post, topic: topic)
group.update!(smtp_enabled: false)
expect { PostAlerter.new.after_save_post(post, true) }.to change { ActionMailer::Base.deliveries.size }.by(1)
email = ActionMailer::Base.deliveries.last
expect(email.from).to include(other_allowed_group.email_username)
expect(email.to).to contain_exactly(topic.reload.topic_allowed_users.order(:created_at).first.user.email)
expect(email.cc).to match_array(["bar@discourse.org", "jim@othersite.com"])
expect(email.subject).to eq("Re: #{topic.title}")
end
it "does not send a group smtp email if smtp is not enabled for the group" do
group.update!(smtp_enabled: false)
incoming_email_post = create_post_with_incoming