mirror of
https://github.com/discourse/discourse.git
synced 2025-06-01 01:55:46 +08:00
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:
@ -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?
|
||||
|
Reference in New Issue
Block a user