FIX: Conform EmailLog#bounce_error_code to RFC (#16010)

This commit makes sure that the email log's bounce_error_code
conforms to the SMTP error code RFC on save, so that
it is always in the format X.X.X or XXX without any
additional string details. Also included is a migration
to fix this issue for past records.
This commit is contained in:
Martin Brennan
2022-02-21 11:26:39 +10:00
committed by GitHub
parent 189df5ef43
commit 01ef1d08fc
3 changed files with 46 additions and 0 deletions

View File

@ -14,6 +14,9 @@ class EmailLog < ActiveRecord::Base
signup_after_approval
}
# cf. https://www.iana.org/assignments/smtp-enhanced-status-codes/smtp-enhanced-status-codes.xhtml
SMTP_ERROR_CODE_REGEXP = Regexp.new(/\d\.\d\.\d|\d\d\d/).freeze
belongs_to :user
belongs_to :post
belongs_to :smtp_group, class_name: 'Group'
@ -34,6 +37,13 @@ class EmailLog < ActiveRecord::Base
SQL
end
before_save do
if self.bounce_error_code.present?
match = SMTP_ERROR_CODE_REGEXP.match(self.bounce_error_code)
self.bounce_error_code = match.present? ? match[0] : nil
end
end
after_create do
# Update last_emailed_at if the user_id is present and email was sent
User.where(id: user_id).update_all("last_emailed_at = CURRENT_TIMESTAMP") if user_id.present?