Skip to content

Commit 001a679

Browse files
committed
Add support for additional replacement characers
1 parent 6ecdea1 commit 001a679

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

models/user/user.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,20 +523,21 @@ func GetUserSalt() (string, error) {
523523
// Note: The set of characters here can safely expand without a breaking change,
524524
// but characters removed from this set can cause user account linking to break
525525
var (
526-
removeCharsRE = regexp.MustCompile(`['\x60]`)
526+
customCharsReplacement = strings.NewReplacer("Æ", "AE")
527+
removeCharsRE = regexp.MustCompile(`['´\x60]`)
527528
removeDiacriticsTransform = transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
528-
replaceCharsRE = regexp.MustCompile(`[\s,]`)
529+
replaceCharsHyphenRE = regexp.MustCompile(`[\s~+]`)
529530
)
530531

531532
// normalizeUserName returns a string with single-quotes and diacritics
532533
// removed, and any other non-supported username characters replaced with
533534
// a `-` character
534535
func NormalizeUserName(s string) (string, error) {
535-
strDiacriticsRemoved, n, err := transform.String(removeDiacriticsTransform, s)
536+
strDiacriticsRemoved, n, err := transform.String(removeDiacriticsTransform, customCharsReplacement.Replace(s))
536537
if err != nil {
537538
return "", fmt.Errorf("Failed to normalize character `%v` in provided username `%v`", s[n], s)
538539
}
539-
return replaceCharsRE.ReplaceAllLiteralString(removeCharsRE.ReplaceAllLiteralString(strDiacriticsRemoved, ""), "-"), nil
540+
return replaceCharsHyphenRE.ReplaceAllLiteralString(removeCharsRE.ReplaceAllLiteralString(strDiacriticsRemoved, ""), "-"), nil
540541
}
541542

542543
var (

models/user/user_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,13 +553,13 @@ func Test_NormalizeUserFromEmail(t *testing.T) {
553553
}{
554554
{"test", "test", true},
555555
{"Sinéad.O'Connor", "Sinead.OConnor", true},
556-
{"Æsir", "Æsir", false}, // Currently unsupported
556+
{"Æsir", "AEsir", true},
557557
// \u00e9\u0065\u0301
558558
{"éé", "ee", true},
559559
{"Awareness Hub", "Awareness-Hub", true},
560560
{"double__underscore", "double__underscore", false}, // We should consider squashing double non-alpha characters
561561
{".bad.", ".bad.", false},
562-
{"new😀user", "new😀user", false},
562+
{"new😀user", "new😀user", false}, // No plans to support
563563
}
564564
for _, testCase := range testCases {
565565
normalizedName, err := user_model.NormalizeUserName(testCase.Input)

0 commit comments

Comments
 (0)