emails: automatically fix typos in email addresses.

Try to automatically fix various kind of typos and common mistakes in
email addresses when a user creates a new account. It's common for users
to signup with addresses like `name@gmai.com`, which leads to bounces
when we try to send the welcome email.
This commit is contained in:
evazion
2022-10-14 18:38:15 -05:00
parent 4dc1a109c5
commit edc7e52353
6 changed files with 187 additions and 19 deletions

View File

@@ -3,8 +3,11 @@
class EmailAddress < ApplicationRecord
belongs_to :user, inverse_of: :email_address
validates :address, presence: true, confirmation: true, format: { with: EmailValidator::EMAIL_REGEX }
validates :normalized_address, uniqueness: true
attribute :address
attribute :normalized_address
validates :address, presence: true, format: { message: "is invalid", with: EmailValidator::EMAIL_REGEX }
validates :normalized_address, presence: true, uniqueness: true
validates :user_id, uniqueness: true
validate :validate_deliverable, on: :deliverable
@@ -17,6 +20,7 @@ class EmailAddress < ApplicationRecord
end
def address=(value)
value = Danbooru::EmailAddress.normalize(value)&.to_s || value
self.normalized_address = EmailValidator.normalize(value) || address
super
end