FIX: Use correct group out of multiple for SMTP sender (PR #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.


May I suggest some whitespace to slightly improve the code soup? :wink:

      expect {, true) }.to change { ActionMailer::Base.deliveries.size }.by(1)

      email = ActionMailer::Base.deliveries.last
      expect(email.from).to include(other_allowed_group.email_username)

    group = Group.find_by_email(post.topic.incoming_email.first.to_addresses)
    if !group&.smtp_enabled

Thanks @CvX , thinned out the soup now :wink:

@martin-brennan Looking at the error logs: post.topic.incoming_email.first can be nil

Sigh x1000, of course it can. Will make a fix.