More permissive username search

This commit is contained in:
Fedor Indutny 2024-02-14 10:18:49 -08:00 committed by GitHub
parent 89525d3e16
commit 4a41e87173
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 121 additions and 31 deletions

View file

@ -1,7 +1,10 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { usernames } from '@signalapp/libsignal-client';
import * as RemoteConfig from '../RemoteConfig';
import { getNickname } from '../types/Username';
import { parseIntWithFallback } from './parseIntWithFallback';
export function getMaxNickname(): number {
@ -13,3 +16,26 @@ export function getMaxNickname(): number {
export function getMinNickname(): number {
return parseIntWithFallback(RemoteConfig.getValue('global.nicknames.min'), 3);
}
export function getUsernameFromSearch(searchTerm: string): string | undefined {
const nickname = getNickname(searchTerm);
if (nickname == null || nickname.length < getMinNickname()) {
return undefined;
}
let modifiedTerm = searchTerm;
if (searchTerm.endsWith('.')) {
// Allow nicknames without full discriminator
modifiedTerm = `${searchTerm}01`;
} else if (!/\.\d*$/.test(searchTerm)) {
// Allow nicknames without discriminator
modifiedTerm = `${searchTerm}.01`;
}
try {
usernames.hash(modifiedTerm);
return searchTerm;
} catch {
return undefined;
}
}