From 8e3d45b0b1e0872fb2523b9ecad885854fcea0b0 Mon Sep 17 00:00:00 2001 From: mfo Date: Tue, 11 Jun 2024 10:17:27 +0200 Subject: [PATCH] review(pr): some enhancement, tx @colinux Co-Authored-By: Colin Darie --- app/controllers/email_checker_controller.rb | 2 +- .../controllers/email_input_controller.ts | 6 +++++- app/lib/email_checker.rb | 15 +++++++++------ spec/lib/email_checker_spec.rb | 2 +- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/app/controllers/email_checker_controller.rb b/app/controllers/email_checker_controller.rb index b794b4d7a..19cd0493b 100644 --- a/app/controllers/email_checker_controller.rb +++ b/app/controllers/email_checker_controller.rb @@ -1,5 +1,5 @@ class EmailCheckerController < ApplicationController def show - render json: EmailChecker.check(email: params[:email]) + render json: EmailChecker.new.check(email: params[:email]) end end diff --git a/app/javascript/controllers/email_input_controller.ts b/app/javascript/controllers/email_input_controller.ts index 8b64a7e92..f8442e1d3 100644 --- a/app/javascript/controllers/email_input_controller.ts +++ b/app/javascript/controllers/email_input_controller.ts @@ -21,7 +21,11 @@ export class EmailInputController extends ApplicationController { declare readonly inputTarget: HTMLInputElement; async checkEmail() { - if (!this.inputTarget.value) { + if ( + !this.inputTarget.value || + this.inputTarget.value.length < 5 || + !this.inputTarget.value.includes('@') + ) { return; } diff --git a/app/lib/email_checker.rb b/app/lib/email_checker.rb index 97fa9d803..c2cbe3536 100644 --- a/app/lib/email_checker.rb +++ b/app/lib/email_checker.rb @@ -1,4 +1,7 @@ class EmailChecker + # Extracted 100 most used domain on our users table [june 2024] + # + all .gouv.fr domain on our users table + # + all .ac-xxx on our users table KNOWN_DOMAINS = [ 'gmail.com', 'hotmail.fr', @@ -612,10 +615,10 @@ class EmailChecker 'ac-toulous.fr' ].freeze - def self.check(email:) + def check(email:) return { success: false } if email.blank? - parsed_email = Mail::Address.new(email) + parsed_email = Mail::Address.new(EmailSanitizableConcern::EmailSanitizer.sanitize(email)) return { success: false } if parsed_email.domain.blank? return { success: true } if KNOWN_DOMAINS.any? { _1 == parsed_email.domain } @@ -628,22 +631,22 @@ class EmailChecker private - def self.closest_domains(domain:) + def closest_domains(domain:) KNOWN_DOMAINS.filter do |known_domain| close_by_distance_of(domain, known_domain, distance: 1) || with_same_chars_and_close_by_distance_of(domain, known_domain, distance: 2) end end - def self.close_by_distance_of(a, b, distance:) + def close_by_distance_of(a, b, distance:) String::Similarity.levenshtein_distance(a, b) == distance end - def self.with_same_chars_and_close_by_distance_of(a, b, distance:) + def with_same_chars_and_close_by_distance_of(a, b, distance:) close_by_distance_of(a, b, distance: 2) && a.chars.sort == b.chars.sort end - def self.email_suggestions(parsed_email:, similar_domains:) + def email_suggestions(parsed_email:, similar_domains:) similar_domains.map { Mail::Address.new("#{parsed_email.local}@#{_1}").to_s } end end diff --git a/spec/lib/email_checker_spec.rb b/spec/lib/email_checker_spec.rb index cfcf73bfa..f9c35ea91 100644 --- a/spec/lib/email_checker_spec.rb +++ b/spec/lib/email_checker_spec.rb @@ -1,6 +1,6 @@ describe EmailChecker do describe 'check' do - subject { described_class } + subject { described_class.new } it 'works with identified use cases' do expect(subject.check(email: nil)).to eq({ success: false })