-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16764 from lichess-org/plus-normalization-emails
Apply +-normalization for all email domains
- Loading branch information
Showing
4 changed files
with
61 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,48 @@ | ||
function gmailNormalize(email) { | ||
const dry = false; | ||
const gmailOrProton = [ | ||
'protonmail.com', | ||
'protonmail.ch', | ||
'pm.me', | ||
'proton.me', | ||
'gmail.com', | ||
'googlemail.com', | ||
]; | ||
|
||
function normalize(email) { | ||
let [name, domain] = email.toLowerCase().split('@'); | ||
[name] = name.split('+'); | ||
return name.replace(/\./g, '') + '@' + domain; | ||
|
||
if (gmailOrProton.includes(domain)) name = name.replace(/\./g, ''); | ||
|
||
return name + '@' + domain; | ||
} | ||
|
||
db.user4 | ||
.find({ email: /[^+.]+[+.].*@(protonmail\.com|protonmail\.ch|pm\.me|gmail\.com|googlemail\.com)$/i }) | ||
.forEach(user => { | ||
const normalized = gmailNormalize(user.email); | ||
const verbatim = user.verbatimEmail || user.email; | ||
print(user.username, ': ', verbatim, '->', normalized); | ||
|
||
db.user4.update( | ||
{ | ||
_id: user._id, | ||
}, | ||
{ | ||
$set: { | ||
email: normalized, | ||
verbatimEmail: verbatim, | ||
}, | ||
}, | ||
); | ||
}); | ||
let nbUpdates = 0; | ||
let nbDups = 0; | ||
|
||
db.user4.find({ email: /^[^+]+\+.*@.+$/ }, { email: 1, verbatimEmail: 1, username: 1 }).forEach(user => { | ||
const normalized = normalize(user.email); | ||
const verbatim = user.verbatimEmail || user.email; | ||
print(user.username, ': ', verbatim, '->', normalized); | ||
|
||
const updates = {}; | ||
if (normalized != user.email) updates.email = normalized; | ||
if (verbatim != user.email) updates.verbatimEmail = verbatim; | ||
|
||
if (!dry && Object.keys(updates).length) { | ||
try { | ||
db.user4.updateOne({ _id: user._id }, { $set: updates }); | ||
db.user_email_backup.update( | ||
{ _id: user._id }, | ||
{ $set: { email: user.email, verbatimEmail: user.verbatimEmail } }, | ||
{ upsert: true }, | ||
); | ||
nbUpdates++; | ||
} catch (e) { | ||
if (e.code == 11000) nbDups++; | ||
} | ||
} | ||
}); | ||
|
||
print('updated:', nbUpdates); | ||
print('skiped duplicates:', nbDups); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,16 @@ class EmailTest extends munit.FunSuite: | |
NormalizedEmailAddress("[email protected]") | ||
) | ||
|
||
test("normalize other"): | ||
assertEquals( | ||
EmailAddress("[email protected]").normalize, | ||
NormalizedEmailAddress("[email protected]") | ||
) | ||
assertEquals( | ||
EmailAddress("[email protected]").normalize, | ||
NormalizedEmailAddress("[email protected]") | ||
) | ||
|
||
test("not similar emails"): | ||
assert(!EmailAddress("[email protected]").similarTo(EmailAddress("[email protected]"))) | ||
assert(!EmailAddress("[email protected]").similarTo(EmailAddress("[email protected]"))) | ||
|