Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply +-normalization for all email domains #16764

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions bin/mongodb/fix-normalized-emails.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
function gmailNormalize(email) {
const gmailOrProton = ['protonmail.com', 'protonmail.ch', 'pm.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)) {
return name.replace(/\./g, '') + '@' + domain;
} else {
return name + '@' + domain;
}
}

db.user4
.find({ email: /[^+.]+[+.].*@(protonmail\.com|protonmail\.ch|pm\.me|gmail\.com|googlemail\.com)$/i })
.find({
email:
/([^+.]+[+.].*@(protonmail\.com|protonmail\.ch|pm\.me|gmail\.com|googlemail\.com)|^[^+]+\+.*@.+)$/i,
})
.forEach(user => {
const normalized = gmailNormalize(user.email);
const normalized = normalize(user.email);
const verbatim = user.verbatimEmail || user.email;
print(user.username, ': ', verbatim, '->', normalized);

Expand Down
9 changes: 5 additions & 4 deletions modules/core/src/main/email.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ object email:
def normalize = NormalizedEmailAddress: // changing normalization requires database migration!
val lower = e.toLowerCase
lower.split('@') match
case Array(name, domain) if EmailAddress.gmailLikeNormalizedDomains(domain) =>
val normalizedName = name
.replace(".", "") // remove all dots
.takeWhile('+' != _) // skip everything after the first '+'
case Array(name, domain) =>
val skipAfterPlus = name.takeWhile('+' != _)
val normalizedName =
if EmailAddress.gmailLikeNormalizedDomains(domain) then skipAfterPlus.replace(".", "")
else skipAfterPlus
if normalizedName.isEmpty then lower else s"$normalizedName@$domain"
case _ => lower

Expand Down
10 changes: 10 additions & 0 deletions modules/core/src/test/EmailTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]")))
Expand Down
Loading